2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Test / DataContextTest.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2009 Novell, Inc.\r
6 // \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
13 // \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
16 // \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
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Data;\r
29 using System.Data.Common;\r
30 using System.Data.Linq.Mapping;\r
31 using System.Linq;\r
32 using System.IO;\r
33 \r
34 #if MONO_STRICT\r
35 using System.Data.Linq;\r
36 #else\r
37 using DbLinq.Data.Linq;\r
38 #endif\r
39 \r
40 using NUnit.Framework;\r
41 \r
42 using DbLinq.Null;\r
43 \r
44 namespace DbLinqTest {\r
45 \r
46     class DummyConnection : IDbConnection\r
47     {\r
48         public DummyConnection()\r
49         {\r
50             ConnectionString = "";\r
51         }\r
52 \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
64     }\r
65 \r
66     [TestFixture]\r
67     public class DataContextTest\r
68     {\r
69         DataContext context;\r
70 \r
71         [SetUp]\r
72         public void SetUp()\r
73         {\r
74             context = new DataContext(new NullConnection() { ConnectionString = "" });\r
75         }\r
76 \r
77         [TearDown]\r
78         public void TearDown()\r
79         {\r
80             context = null;\r
81         }\r
82 \r
83         [Test, ExpectedException(typeof(ArgumentNullException))]\r
84         public void Ctor_ConnectionStringNull()\r
85         {\r
86             string connectionString = null;\r
87             new DataContext(connectionString);\r
88         }\r
89 \r
90         [Test, ExpectedException(typeof(ArgumentNullException))]\r
91         public void Ctor_ConnectionNull()\r
92         {\r
93             IDbConnection connection = null;\r
94             new DataContext(connection);\r
95         }\r
96 \r
97         [Test, ExpectedException(typeof(NullReferenceException))]\r
98         public void Ctor_ConnectionStringOfConnectionIsNull()\r
99         {\r
100             IDbConnection connection = new NullConnection() { ConnectionString = null };\r
101             new DataContext(connection);\r
102         }\r
103 \r
104         [Test, ExpectedException(typeof(ArgumentException))]\r
105         public void Ctor_ConnectionString_DbLinqConnectionType_Empty()\r
106         {\r
107             new DataContext("DbLinqConnectionType=");\r
108         }\r
109 \r
110         [Test, ExpectedException(typeof(ArgumentException))]\r
111         public void Ctor_ConnectionString_DbLinqConnectionType_Empty2()\r
112         {\r
113             new DataContext("DbLinqConnectionType=;");\r
114         }\r
115 \r
116         [Test, ExpectedException(typeof(ArgumentException))]\r
117         public void Ctor_ConnectionString_DbLinqConnectionType_Invalid()\r
118         {\r
119             new DataContext("DbLinqConnectionType=InvalidType, DoesNotExist");\r
120         }\r
121 \r
122         [Test, ExpectedException(typeof(ArgumentException))]\r
123         public void Ctor_ConnectionString_DbLinqProvider_InvalidVendor()\r
124         {\r
125             new DataContext("DbLinqProvider=ThisVendorDoesNotExist");\r
126         }\r
127 \r
128         [Test, ExpectedException(typeof(ArgumentException))]\r
129         public void Ctor_ConnectionString_DbLinqProvider_InvalidVendorWithDots()\r
130         {\r
131             new DataContext("DbLinqProvider=DbLinq.Sqlite.dll");\r
132         }\r
133 \r
134         [Test, ExpectedException(typeof(ArgumentNullException))]\r
135         public void Ctor_FileOrServerOrConnectionIsNull()\r
136         {\r
137             MappingSource mapping = new AttributeMappingSource();\r
138             string fileOrServerOrConnection = null;\r
139             new DataContext(fileOrServerOrConnection, mapping);\r
140         }\r
141 \r
142         [Test, ExpectedException(typeof(ArgumentNullException))]\r
143         public void Ctor_MappingIsNull()\r
144         {\r
145             MappingSource mapping = null;\r
146             string fileOrServerOrConnection = null;\r
147             new DataContext("", mapping);\r
148         }\r
149 \r
150 #if L2SQL\r
151         // DbLinqProvider/etc. obviously aren't removed under L2SQL\r
152         [ExpectedException(typeof(ArgumentException))]\r
153 #endif\r
154         [Test]\r
155         public void Ctor_ConnectionString_ExtraParameters_Munging()\r
156         {\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
160         }\r
161         \r
162 #if !L2SQL\r
163         [Test, ExpectedException(typeof(NotImplementedException))]\r
164         public void Ctor_FileOrServerOrConnectionIsFilename()\r
165         {\r
166             MappingSource mapping = new AttributeMappingSource();\r
167             string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;\r
168             new DataContext(fileOrServerOrConnection, mapping);\r
169         }\r
170 \r
171         [Test, ExpectedException(typeof(NotImplementedException))]\r
172         public void Ctor_FileOrServerOrConnectionIsServer()\r
173         {\r
174             MappingSource mapping = new AttributeMappingSource();\r
175             string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";\r
176             new DataContext(fileOrServerOrConnection, mapping);\r
177         }\r
178 #endif\r
179 \r
180         [Test]\r
181         public void Connection()\r
182         {\r
183             IDbConnection connection = new NullConnection() { ConnectionString = "" };\r
184             DataContext dc = new DataContext(connection);\r
185             Assert.AreEqual(connection, dc.Connection);\r
186 \r
187 #if !L2SQL\r
188             dc = new DataContext (new DummyConnection());\r
189             Assert.AreEqual(null, dc.Connection);\r
190 #endif\r
191         }\r
192 \r
193         [Test, ExpectedException(typeof(ArgumentNullException))]\r
194         public void ExecuteQuery_ElementTypeNull()\r
195         {\r
196             Type elementType = null;\r
197             context.ExecuteQuery(elementType, "command");\r
198         }\r
199 \r
200         [Test, ExpectedException(typeof(ArgumentNullException))]\r
201         public void ExecuteQuery_QueryNull()\r
202         {\r
203             Type elementType = typeof(Person);\r
204             context.ExecuteQuery(elementType, null);\r
205         }\r
206 \r
207         [Test, ExpectedException(typeof(ArgumentNullException))]\r
208         public void ExecuteQueryTResult_QueryNull()\r
209         {\r
210             context.ExecuteQuery<Person>(null);\r
211         }\r
212 \r
213         [Test, ExpectedException(typeof(ArgumentNullException))]\r
214         public void GetCommand_QueryNull()\r
215         {\r
216             IQueryable query = null;\r
217             context.GetCommand(query);\r
218         }\r
219 \r
220         [Test, ExpectedException(typeof(ArgumentNullException))]\r
221         public void GetTable_TypeNull()\r
222         {\r
223             context.GetTable(null);\r
224         }\r
225 \r
226         [Test, ExpectedException(typeof(InvalidOperationException))]\r
227         public void GetTable_NotSupportedType()\r
228         {\r
229             context.GetTable(typeof(object));\r
230         }\r
231 \r
232         [Test, ExpectedException(typeof(InvalidOperationException))]\r
233         public void GetTableTEntity_NotSupportedType()\r
234         {\r
235             context.GetTable<object>();\r
236         }\r
237 \r
238         [Test]\r
239         public void GetTableTEntity()\r
240         {\r
241             Table<Person> table = context.GetTable<Person>();\r
242         }\r
243 \r
244         [Test, ExpectedException(typeof(ArgumentNullException))]\r
245         public void Translate_ReaderNull()\r
246         {\r
247             context.Translate(typeof(Person), null);\r
248         }\r
249 \r
250         [Test, ExpectedException(typeof(ArgumentNullException))]\r
251         public void Translate_ElementTypeNull()\r
252         {\r
253             DbDataReader reader = new NullDataReader();\r
254             context.Translate(null, reader);\r
255         }\r
256 \r
257         [Test, ExpectedException(typeof(ArgumentNullException))]\r
258         public void TranslateTResult_ReaderNull()\r
259         {\r
260             context.Translate<Person>(null);\r
261         }\r
262     }\r
263 }\r
264 \r