* src/**/*: Flush; syncs to DbLinq r1053. Adds
[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 !MONO_STRICT\r
151         [Test, ExpectedException(typeof(NotImplementedException))]\r
152         public void Ctor_FileOrServerOrConnectionIsFilename()\r
153         {\r
154             MappingSource mapping = new AttributeMappingSource();\r
155             string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;\r
156             new DataContext(fileOrServerOrConnection, mapping);\r
157         }\r
158 \r
159         [Test, ExpectedException(typeof(NotImplementedException))]\r
160         public void Ctor_FileOrServerOrConnectionIsServer()\r
161         {\r
162             MappingSource mapping = new AttributeMappingSource();\r
163             string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";\r
164             new DataContext(fileOrServerOrConnection, mapping);\r
165         }\r
166 #endif\r
167 \r
168         [Test]\r
169         public void Connection()\r
170         {\r
171             IDbConnection connection = new NullConnection() { ConnectionString = "" };\r
172             DataContext dc = new DataContext(connection);\r
173             Assert.AreEqual(connection, dc.Connection);\r
174 \r
175 #if !MONO_STRICT\r
176             dc = new DataContext (new DummyConnection());\r
177             Assert.AreEqual(null, dc.Connection);\r
178 #endif\r
179         }\r
180 \r
181         [Test, ExpectedException(typeof(ArgumentNullException))]\r
182         public void ExecuteQuery_ElementTypeNull()\r
183         {\r
184             Type elementType = null;\r
185             context.ExecuteQuery(elementType, "command");\r
186         }\r
187 \r
188         [Test, ExpectedException(typeof(ArgumentNullException))]\r
189         public void ExecuteQuery_QueryNull()\r
190         {\r
191             Type elementType = typeof(Person);\r
192             context.ExecuteQuery(elementType, null);\r
193         }\r
194 \r
195         [Test, ExpectedException(typeof(ArgumentNullException))]\r
196         public void ExecuteQueryTResult_QueryNull()\r
197         {\r
198             context.ExecuteQuery<Person>(null);\r
199         }\r
200 \r
201         [Test, ExpectedException(typeof(ArgumentNullException))]\r
202         public void GetCommand_QueryNull()\r
203         {\r
204             IQueryable query = null;\r
205             context.GetCommand(query);\r
206         }\r
207 \r
208         [Test, ExpectedException(typeof(ArgumentNullException))]\r
209         public void GetTable_TypeNull()\r
210         {\r
211             context.GetTable(null);\r
212         }\r
213 \r
214         [Test, ExpectedException(typeof(InvalidOperationException))]\r
215         public void GetTable_NotSupportedType()\r
216         {\r
217             context.GetTable(typeof(object));\r
218         }\r
219 \r
220         [Test, ExpectedException(typeof(InvalidOperationException))]\r
221         public void GetTableTEntity_NotSupportedType()\r
222         {\r
223             context.GetTable<object>();\r
224         }\r
225 \r
226         [Test]\r
227         public void GetTableTEntity()\r
228         {\r
229             Table<Person> table = context.GetTable<Person>();\r
230         }\r
231 \r
232         [Test, ExpectedException(typeof(ArgumentNullException))]\r
233         public void Translate_ReaderNull()\r
234         {\r
235             context.Translate(typeof(Person), null);\r
236         }\r
237 \r
238         [Test, ExpectedException(typeof(ArgumentNullException))]\r
239         public void Translate_ElementTypeNull()\r
240         {\r
241             DbDataReader reader = new NullDataReader();\r
242             context.Translate(null, reader);\r
243         }\r
244 \r
245         [Test, ExpectedException(typeof(ArgumentNullException))]\r
246         public void TranslateTResult_ReaderNull()\r
247         {\r
248             context.Translate<Person>(null);\r
249         }\r
250     }\r
251 }\r
252 \r