PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Test/DataContextTest.cs

https://github.com/ekovalenko-softheme/mono
C# | 266 lines | 205 code | 37 blank | 24 comment | 2 complexity | 075c5bf9f17400ac790c371a14683507 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense
  1. #region MIT license
  2. //
  3. // MIT license
  4. //
  5. // Copyright (c) 2009 Novell, Inc.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #endregion
  26. using System;
  27. using System.Data;
  28. using System.Data.Common;
  29. using System.Data.Linq.Mapping;
  30. using System.Linq;
  31. using System.IO;
  32. #if MONO_STRICT
  33. using System.Data.Linq;
  34. #else
  35. using DbLinq.Data.Linq;
  36. #endif
  37. using NUnit.Framework;
  38. using DbLinq.Null;
  39. namespace DbLinqTest {
  40. class DummyConnection : IDbConnection
  41. {
  42. public DummyConnection()
  43. {
  44. ConnectionString = "";
  45. }
  46. public IDbTransaction BeginTransaction() {return null;}
  47. public IDbTransaction BeginTransaction(IsolationLevel il) {return null;}
  48. public void ChangeDatabase(string databaseName) {}
  49. public void Close() {}
  50. public IDbCommand CreateCommand() {return null;}
  51. public string ConnectionString{get; set;}
  52. public int ConnectionTimeout{get {return 0;}}
  53. public string Database{get {return null;}}
  54. public void Dispose() {}
  55. public void Open() {}
  56. public ConnectionState State{get {return ConnectionState.Closed;}}
  57. }
  58. [TestFixture]
  59. public class DataContextTest
  60. {
  61. DataContext context;
  62. [SetUp]
  63. public void SetUp()
  64. {
  65. context = new DataContext(new NullConnection() { ConnectionString = "" });
  66. }
  67. [TearDown]
  68. public void TearDown()
  69. {
  70. context = null;
  71. }
  72. [Test, ExpectedException(typeof(ArgumentNullException))]
  73. public void Ctor_ConnectionStringNull()
  74. {
  75. string connectionString = null;
  76. new DataContext(connectionString);
  77. }
  78. [Test, ExpectedException(typeof(ArgumentNullException))]
  79. public void Ctor_ConnectionNull()
  80. {
  81. IDbConnection connection = null;
  82. new DataContext(connection);
  83. }
  84. [Test, ExpectedException(typeof(NullReferenceException))]
  85. public void Ctor_ConnectionStringOfConnectionIsNull()
  86. {
  87. IDbConnection connection = new NullConnection() { ConnectionString = null };
  88. new DataContext(connection);
  89. }
  90. [Test, ExpectedException(typeof(ArgumentException))]
  91. public void Ctor_ConnectionString_DbLinqConnectionType_Empty()
  92. {
  93. new DataContext("DbLinqConnectionType=");
  94. }
  95. [Test, ExpectedException(typeof(ArgumentException))]
  96. public void Ctor_ConnectionString_DbLinqConnectionType_Empty2()
  97. {
  98. new DataContext("DbLinqConnectionType=;");
  99. }
  100. [Test, ExpectedException(typeof(ArgumentException))]
  101. public void Ctor_ConnectionString_DbLinqConnectionType_Invalid()
  102. {
  103. new DataContext("DbLinqConnectionType=InvalidType, DoesNotExist");
  104. }
  105. [Test, ExpectedException(typeof(ArgumentException))]
  106. public void Ctor_ConnectionString_DbLinqProvider_InvalidVendor()
  107. {
  108. new DataContext("DbLinqProvider=ThisVendorDoesNotExist");
  109. }
  110. [Test, ExpectedException(typeof(ArgumentException))]
  111. public void Ctor_ConnectionString_DbLinqProvider_InvalidVendorWithDots()
  112. {
  113. new DataContext("DbLinqProvider=DbLinq.Sqlite.dll");
  114. }
  115. [Test, ExpectedException(typeof(ArgumentNullException))]
  116. public void Ctor_FileOrServerOrConnectionIsNull()
  117. {
  118. MappingSource mapping = new AttributeMappingSource();
  119. string fileOrServerOrConnection = null;
  120. new DataContext(fileOrServerOrConnection, mapping);
  121. }
  122. [Test, ExpectedException(typeof(ArgumentNullException))]
  123. public void Ctor_MappingIsNull()
  124. {
  125. MappingSource mapping = null;
  126. string fileOrServerOrConnection = null;
  127. new DataContext("", mapping);
  128. }
  129. #if L2SQL
  130. // DbLinqProvider/etc. obviously aren't removed under L2SQL
  131. [ExpectedException(typeof(ArgumentException))]
  132. #endif
  133. [Test]
  134. public void Ctor_ConnectionString_ExtraParameters_Munging()
  135. {
  136. if (Type.GetType("Mono.Runtime", false) != null)
  137. Assert.Ignore("Mono's System.Data.Linq is expected to remove DbLinq parameters.");
  138. DataContext ctx = new DataContext("Server=localhost;User id=test;Database=test;DbLinqProvider=Sqlite;DbLinqConnectionType=Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite");
  139. Assert.AreEqual(-1, ctx.Connection.ConnectionString.IndexOf("DbLinqProvider"));
  140. Assert.AreEqual(-1, ctx.Connection.ConnectionString.IndexOf("DbLinqConnectionType"));
  141. }
  142. #if !L2SQL
  143. [Test, ExpectedException(typeof(NotImplementedException))]
  144. public void Ctor_FileOrServerOrConnectionIsFilename()
  145. {
  146. MappingSource mapping = new AttributeMappingSource();
  147. string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;
  148. new DataContext(fileOrServerOrConnection, mapping);
  149. }
  150. [Test, ExpectedException(typeof(NotImplementedException))]
  151. public void Ctor_FileOrServerOrConnectionIsServer()
  152. {
  153. MappingSource mapping = new AttributeMappingSource();
  154. string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";
  155. new DataContext(fileOrServerOrConnection, mapping);
  156. }
  157. #endif
  158. [Test]
  159. public void Connection()
  160. {
  161. IDbConnection connection = new NullConnection() { ConnectionString = "" };
  162. DataContext dc = new DataContext(connection);
  163. Assert.AreEqual(connection, dc.Connection);
  164. #if !L2SQL
  165. dc = new DataContext (new DummyConnection());
  166. Assert.AreEqual(null, dc.Connection);
  167. #endif
  168. }
  169. [Test, ExpectedException(typeof(ArgumentNullException))]
  170. public void ExecuteQuery_ElementTypeNull()
  171. {
  172. Type elementType = null;
  173. context.ExecuteQuery(elementType, "command");
  174. }
  175. [Test, ExpectedException(typeof(ArgumentNullException))]
  176. public void ExecuteQuery_QueryNull()
  177. {
  178. Type elementType = typeof(Person);
  179. context.ExecuteQuery(elementType, null);
  180. }
  181. [Test, ExpectedException(typeof(ArgumentNullException))]
  182. public void ExecuteQueryTResult_QueryNull()
  183. {
  184. context.ExecuteQuery<Person>(null);
  185. }
  186. [Test, ExpectedException(typeof(ArgumentNullException))]
  187. public void GetCommand_QueryNull()
  188. {
  189. IQueryable query = null;
  190. context.GetCommand(query);
  191. }
  192. [Test, ExpectedException(typeof(ArgumentNullException))]
  193. public void GetTable_TypeNull()
  194. {
  195. context.GetTable(null);
  196. }
  197. [Test, ExpectedException(typeof(InvalidOperationException))]
  198. public void GetTable_NotSupportedType()
  199. {
  200. context.GetTable(typeof(object));
  201. }
  202. [Test, ExpectedException(typeof(InvalidOperationException))]
  203. public void GetTableTEntity_NotSupportedType()
  204. {
  205. context.GetTable<object>();
  206. }
  207. [Test]
  208. public void GetTableTEntity()
  209. {
  210. Table<Person> table = context.GetTable<Person>();
  211. }
  212. [Test, ExpectedException(typeof(ArgumentNullException))]
  213. public void Translate_ReaderNull()
  214. {
  215. context.Translate(typeof(Person), null);
  216. }
  217. [Test, ExpectedException(typeof(ArgumentNullException))]
  218. public void Translate_ElementTypeNull()
  219. {
  220. DbDataReader reader = new NullDataReader();
  221. context.Translate(null, reader);
  222. }
  223. [Test, ExpectedException(typeof(ArgumentNullException))]
  224. public void TranslateTResult_ReaderNull()
  225. {
  226. context.Translate<Person>(null);
  227. }
  228. }
  229. }