5 // Copyright (c) 2009 Novell, Inc.
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
29 using System.Data.Common;
\r
30 using System.Data.Linq.Mapping;
\r
35 using System.Data.Linq;
\r
37 using DbLinq.Data.Linq;
\r
40 using NUnit.Framework;
\r
44 namespace DbLinqTest {
\r
46 class DummyConnection : IDbConnection
\r
48 public DummyConnection()
\r
50 ConnectionString = "";
\r
53 public IDbTransaction BeginTransaction() {return null;}
\r
54 public IDbTransaction BeginTransaction(IsolationLevel il) {return null;}
\r
55 public void ChangeDatabase(string databaseName) {}
\r
56 public void Close() {}
\r
57 public IDbCommand CreateCommand() {return null;}
\r
58 public string ConnectionString{get; set;}
\r
59 public int ConnectionTimeout{get {return 0;}}
\r
60 public string Database{get {return null;}}
\r
61 public void Dispose() {}
\r
62 public void Open() {}
\r
63 public ConnectionState State{get {return ConnectionState.Closed;}}
\r
67 public class DataContextTest
\r
69 DataContext context;
\r
74 context = new DataContext(new NullConnection() { ConnectionString = "" });
\r
78 public void TearDown()
\r
83 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
84 public void Ctor_ConnectionStringNull()
\r
86 string connectionString = null;
\r
87 new DataContext(connectionString);
\r
90 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
91 public void Ctor_ConnectionNull()
\r
93 IDbConnection connection = null;
\r
94 new DataContext(connection);
\r
97 [Test, ExpectedException(typeof(NullReferenceException))]
\r
98 public void Ctor_ConnectionStringOfConnectionIsNull()
\r
100 IDbConnection connection = new NullConnection() { ConnectionString = null };
\r
101 new DataContext(connection);
\r
104 [Test, ExpectedException(typeof(ArgumentException))]
\r
105 public void Ctor_ConnectionString_DbLinqConnectionType_Empty()
\r
107 new DataContext("DbLinqConnectionType=");
\r
110 [Test, ExpectedException(typeof(ArgumentException))]
\r
111 public void Ctor_ConnectionString_DbLinqConnectionType_Empty2()
\r
113 new DataContext("DbLinqConnectionType=;");
\r
116 [Test, ExpectedException(typeof(ArgumentException))]
\r
117 public void Ctor_ConnectionString_DbLinqConnectionType_Invalid()
\r
119 new DataContext("DbLinqConnectionType=InvalidType, DoesNotExist");
\r
122 [Test, ExpectedException(typeof(ArgumentException))]
\r
123 public void Ctor_ConnectionString_DbLinqProvider_InvalidVendor()
\r
125 new DataContext("DbLinqProvider=ThisVendorDoesNotExist");
\r
128 [Test, ExpectedException(typeof(ArgumentException))]
\r
129 public void Ctor_ConnectionString_DbLinqProvider_InvalidVendorWithDots()
\r
131 new DataContext("DbLinqProvider=DbLinq.Sqlite.dll");
\r
134 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
135 public void Ctor_FileOrServerOrConnectionIsNull()
\r
137 MappingSource mapping = new AttributeMappingSource();
\r
138 string fileOrServerOrConnection = null;
\r
139 new DataContext(fileOrServerOrConnection, mapping);
\r
142 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
143 public void Ctor_MappingIsNull()
\r
145 MappingSource mapping = null;
\r
146 string fileOrServerOrConnection = null;
\r
147 new DataContext("", mapping);
\r
151 // DbLinqProvider/etc. obviously aren't removed under L2SQL
\r
152 [ExpectedException(typeof(ArgumentException))]
\r
155 public void Ctor_ConnectionString_ExtraParameters_Munging()
\r
157 DataContext ctx = new DataContext("Server=localhost;User id=test;Database=test;DbLinqProvider=Sqlite;DbLinqConnectionType=Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite");
\r
158 Assert.AreEqual(-1, ctx.Connection.ConnectionString.IndexOf("DbLinqProvider"));
\r
159 Assert.AreEqual(-1, ctx.Connection.ConnectionString.IndexOf("DbLinqConnectionType"));
\r
163 [Test, ExpectedException(typeof(NotImplementedException))]
\r
164 public void Ctor_FileOrServerOrConnectionIsFilename()
\r
166 MappingSource mapping = new AttributeMappingSource();
\r
167 string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;
\r
168 new DataContext(fileOrServerOrConnection, mapping);
\r
171 [Test, ExpectedException(typeof(NotImplementedException))]
\r
172 public void Ctor_FileOrServerOrConnectionIsServer()
\r
174 MappingSource mapping = new AttributeMappingSource();
\r
175 string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";
\r
176 new DataContext(fileOrServerOrConnection, mapping);
\r
181 public void Connection()
\r
183 IDbConnection connection = new NullConnection() { ConnectionString = "" };
\r
184 DataContext dc = new DataContext(connection);
\r
185 Assert.AreEqual(connection, dc.Connection);
\r
188 dc = new DataContext (new DummyConnection());
\r
189 Assert.AreEqual(null, dc.Connection);
\r
193 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
194 public void ExecuteQuery_ElementTypeNull()
\r
196 Type elementType = null;
\r
197 context.ExecuteQuery(elementType, "command");
\r
200 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
201 public void ExecuteQuery_QueryNull()
\r
203 Type elementType = typeof(Person);
\r
204 context.ExecuteQuery(elementType, null);
\r
207 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
208 public void ExecuteQueryTResult_QueryNull()
\r
210 context.ExecuteQuery<Person>(null);
\r
213 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
214 public void GetCommand_QueryNull()
\r
216 IQueryable query = null;
\r
217 context.GetCommand(query);
\r
220 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
221 public void GetTable_TypeNull()
\r
223 context.GetTable(null);
\r
226 [Test, ExpectedException(typeof(InvalidOperationException))]
\r
227 public void GetTable_NotSupportedType()
\r
229 context.GetTable(typeof(object));
\r
232 [Test, ExpectedException(typeof(InvalidOperationException))]
\r
233 public void GetTableTEntity_NotSupportedType()
\r
235 context.GetTable<object>();
\r
239 public void GetTableTEntity()
\r
241 Table<Person> table = context.GetTable<Person>();
\r
244 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
245 public void Translate_ReaderNull()
\r
247 context.Translate(typeof(Person), null);
\r
250 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
251 public void Translate_ElementTypeNull()
\r
253 DbDataReader reader = new NullDataReader();
\r
254 context.Translate(null, reader);
\r
257 [Test, ExpectedException(typeof(ArgumentNullException))]
\r
258 public void TranslateTResult_ReaderNull()
\r
260 context.Translate<Person>(null);
\r