PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mono/mono
C# | 260 lines | 200 code | 36 blank | 24 comment | 2 complexity | d2ccd1343f025e50482931eaa4b9fb16 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Unlicense, Apache-2.0
  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. [Test]
  130. public void Ctor_ConnectionString_ExtraParameters_Munging()
  131. {
  132. var ex = Assert.Throws<ArgumentException> ( () => new DataContext("Server=localhost;User id=test;Database=test;DbLinqProvider=Sqlite;DbLinqConnectionType=Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite"));
  133. // Keyword not supported: 'dblinqprovider'
  134. Assert.Null(ex.InnerException);
  135. Assert.NotNull(ex.Message);
  136. Assert.True(ex.Message.IndexOf("'dblinqprovider'") != -1);
  137. Assert.Null(ex.ParamName);
  138. }
  139. [Test]
  140. public void Ctor_FileOrServerOrConnectionIsFilename()
  141. {
  142. MappingSource mapping = new AttributeMappingSource();
  143. string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;
  144. new DataContext(fileOrServerOrConnection, mapping);
  145. }
  146. [Test]
  147. public void Ctor_FileOrServerOrConnectionIsServer()
  148. {
  149. MappingSource mapping = new AttributeMappingSource();
  150. string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";
  151. new DataContext(fileOrServerOrConnection, mapping);
  152. }
  153. [Test]
  154. public void Connection()
  155. {
  156. IDbConnection connection = new NullConnection() { ConnectionString = "" };
  157. DataContext dc = new DataContext(connection);
  158. Assert.AreEqual(connection, dc.Connection);
  159. var ex = Assert.Throws<Exception> ( () => new DataContext (new DummyConnection()));
  160. Assert.Null(ex.InnerException);
  161. Assert.NotNull(ex.Message);
  162. Assert.True(ex.Message.IndexOf("'connection'") != -1);
  163. }
  164. [Test, ExpectedException(typeof(ArgumentNullException))]
  165. public void ExecuteQuery_ElementTypeNull()
  166. {
  167. Type elementType = null;
  168. context.ExecuteQuery(elementType, "command");
  169. }
  170. [Test, ExpectedException(typeof(ArgumentNullException))]
  171. public void ExecuteQuery_QueryNull()
  172. {
  173. Type elementType = typeof(Person);
  174. context.ExecuteQuery(elementType, null);
  175. }
  176. [Test, ExpectedException(typeof(ArgumentNullException))]
  177. public void ExecuteQueryTResult_QueryNull()
  178. {
  179. context.ExecuteQuery<Person>(null);
  180. }
  181. [Test, ExpectedException(typeof(ArgumentNullException))]
  182. public void GetCommand_QueryNull()
  183. {
  184. IQueryable query = null;
  185. context.GetCommand(query);
  186. }
  187. [Test, ExpectedException(typeof(ArgumentNullException))]
  188. public void GetTable_TypeNull()
  189. {
  190. context.GetTable(null);
  191. }
  192. [Test, ExpectedException(typeof(InvalidOperationException))]
  193. public void GetTable_NotSupportedType()
  194. {
  195. context.GetTable(typeof(object));
  196. }
  197. [Test, ExpectedException(typeof(InvalidOperationException))]
  198. public void GetTableTEntity_NotSupportedType()
  199. {
  200. context.GetTable<object>();
  201. }
  202. [Test]
  203. public void GetTableTEntity()
  204. {
  205. Table<Person> table = context.GetTable<Person>();
  206. }
  207. [Test, ExpectedException(typeof(ArgumentNullException))]
  208. public void Translate_ReaderNull()
  209. {
  210. context.Translate(typeof(Person), null);
  211. }
  212. [Test, ExpectedException(typeof(ArgumentNullException))]
  213. public void Translate_ElementTypeNull()
  214. {
  215. DbDataReader reader = new NullDataReader();
  216. context.Translate(null, reader);
  217. }
  218. [Test, ExpectedException(typeof(ArgumentNullException))]
  219. public void TranslateTResult_ReaderNull()
  220. {
  221. context.Translate<Person>(null);
  222. }
  223. }
  224. }