PageRenderTime 49ms CodeModel.GetById 21ms app.highlight 20ms RepoModel.GetById 1ms 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
  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
 27using System;
 28using System.Data;
 29using System.Data.Common;
 30using System.Data.Linq.Mapping;
 31using System.Linq;
 32using System.IO;
 33
 34#if MONO_STRICT
 35using System.Data.Linq;
 36#else
 37using DbLinq.Data.Linq;
 38#endif
 39
 40using NUnit.Framework;
 41
 42using DbLinq.Null;
 43
 44namespace DbLinqTest {
 45
 46    class DummyConnection : IDbConnection
 47    {
 48        public DummyConnection()
 49        {
 50            ConnectionString = "";
 51        }
 52
 53        public IDbTransaction BeginTransaction() {return null;}
 54        public IDbTransaction BeginTransaction(IsolationLevel il) {return null;}
 55        public void ChangeDatabase(string databaseName) {}
 56        public void Close() {}
 57        public IDbCommand CreateCommand() {return null;}
 58        public string ConnectionString{get; set;}
 59        public int ConnectionTimeout{get {return 0;}}
 60        public string Database{get {return null;}}
 61        public void Dispose() {}
 62        public void Open() {}
 63        public ConnectionState State{get {return ConnectionState.Closed;}}
 64    }
 65
 66    [TestFixture]
 67    public class DataContextTest
 68    {
 69        DataContext context;
 70
 71        [SetUp]
 72        public void SetUp()
 73        {
 74            context = new DataContext(new NullConnection() { ConnectionString = "" });
 75        }
 76
 77        [TearDown]
 78        public void TearDown()
 79        {
 80            context = null;
 81        }
 82
 83        [Test, ExpectedException(typeof(ArgumentNullException))]
 84        public void Ctor_ConnectionStringNull()
 85        {
 86            string connectionString = null;
 87            new DataContext(connectionString);
 88        }
 89
 90        [Test, ExpectedException(typeof(ArgumentNullException))]
 91        public void Ctor_ConnectionNull()
 92        {
 93            IDbConnection connection = null;
 94            new DataContext(connection);
 95        }
 96
 97        [Test, ExpectedException(typeof(NullReferenceException))]
 98        public void Ctor_ConnectionStringOfConnectionIsNull()
 99        {
100            IDbConnection connection = new NullConnection() { ConnectionString = null };
101            new DataContext(connection);
102        }
103
104        [Test, ExpectedException(typeof(ArgumentException))]
105        public void Ctor_ConnectionString_DbLinqConnectionType_Empty()
106        {
107            new DataContext("DbLinqConnectionType=");
108        }
109
110        [Test, ExpectedException(typeof(ArgumentException))]
111        public void Ctor_ConnectionString_DbLinqConnectionType_Empty2()
112        {
113            new DataContext("DbLinqConnectionType=;");
114        }
115
116        [Test, ExpectedException(typeof(ArgumentException))]
117        public void Ctor_ConnectionString_DbLinqConnectionType_Invalid()
118        {
119            new DataContext("DbLinqConnectionType=InvalidType, DoesNotExist");
120        }
121
122        [Test, ExpectedException(typeof(ArgumentException))]
123        public void Ctor_ConnectionString_DbLinqProvider_InvalidVendor()
124        {
125            new DataContext("DbLinqProvider=ThisVendorDoesNotExist");
126        }
127
128        [Test, ExpectedException(typeof(ArgumentException))]
129        public void Ctor_ConnectionString_DbLinqProvider_InvalidVendorWithDots()
130        {
131            new DataContext("DbLinqProvider=DbLinq.Sqlite.dll");
132        }
133
134        [Test, ExpectedException(typeof(ArgumentNullException))]
135        public void Ctor_FileOrServerOrConnectionIsNull()
136        {
137            MappingSource mapping = new AttributeMappingSource();
138            string fileOrServerOrConnection = null;
139            new DataContext(fileOrServerOrConnection, mapping);
140        }
141
142        [Test, ExpectedException(typeof(ArgumentNullException))]
143        public void Ctor_MappingIsNull()
144        {
145            MappingSource mapping = null;
146            string fileOrServerOrConnection = null;
147            new DataContext("", mapping);
148        }
149
150#if L2SQL
151        // DbLinqProvider/etc. obviously aren't removed under L2SQL
152        [ExpectedException(typeof(ArgumentException))]
153#endif
154        [Test]
155        public void Ctor_ConnectionString_ExtraParameters_Munging()
156        {
157            if (Type.GetType("Mono.Runtime", false) != null)
158                Assert.Ignore("Mono's System.Data.Linq is expected to remove DbLinq parameters.");
159            DataContext ctx = new DataContext("Server=localhost;User id=test;Database=test;DbLinqProvider=Sqlite;DbLinqConnectionType=Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite");
160            Assert.AreEqual(-1, ctx.Connection.ConnectionString.IndexOf("DbLinqProvider"));
161            Assert.AreEqual(-1, ctx.Connection.ConnectionString.IndexOf("DbLinqConnectionType"));
162        }
163        
164#if !L2SQL
165        [Test, ExpectedException(typeof(NotImplementedException))]
166        public void Ctor_FileOrServerOrConnectionIsFilename()
167        {
168            MappingSource mapping = new AttributeMappingSource();
169            string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;
170            new DataContext(fileOrServerOrConnection, mapping);
171        }
172
173        [Test, ExpectedException(typeof(NotImplementedException))]
174        public void Ctor_FileOrServerOrConnectionIsServer()
175        {
176            MappingSource mapping = new AttributeMappingSource();
177            string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";
178            new DataContext(fileOrServerOrConnection, mapping);
179        }
180#endif
181
182        [Test]
183        public void Connection()
184        {
185            IDbConnection connection = new NullConnection() { ConnectionString = "" };
186            DataContext dc = new DataContext(connection);
187            Assert.AreEqual(connection, dc.Connection);
188
189#if !L2SQL
190            dc = new DataContext (new DummyConnection());
191            Assert.AreEqual(null, dc.Connection);
192#endif
193        }
194
195        [Test, ExpectedException(typeof(ArgumentNullException))]
196        public void ExecuteQuery_ElementTypeNull()
197        {
198            Type elementType = null;
199            context.ExecuteQuery(elementType, "command");
200        }
201
202        [Test, ExpectedException(typeof(ArgumentNullException))]
203        public void ExecuteQuery_QueryNull()
204        {
205            Type elementType = typeof(Person);
206            context.ExecuteQuery(elementType, null);
207        }
208
209        [Test, ExpectedException(typeof(ArgumentNullException))]
210        public void ExecuteQueryTResult_QueryNull()
211        {
212            context.ExecuteQuery<Person>(null);
213        }
214
215        [Test, ExpectedException(typeof(ArgumentNullException))]
216        public void GetCommand_QueryNull()
217        {
218            IQueryable query = null;
219            context.GetCommand(query);
220        }
221
222        [Test, ExpectedException(typeof(ArgumentNullException))]
223        public void GetTable_TypeNull()
224        {
225            context.GetTable(null);
226        }
227
228        [Test, ExpectedException(typeof(InvalidOperationException))]
229        public void GetTable_NotSupportedType()
230        {
231            context.GetTable(typeof(object));
232        }
233
234        [Test, ExpectedException(typeof(InvalidOperationException))]
235        public void GetTableTEntity_NotSupportedType()
236        {
237            context.GetTable<object>();
238        }
239
240        [Test]
241        public void GetTableTEntity()
242        {
243            Table<Person> table = context.GetTable<Person>();
244        }
245
246        [Test, ExpectedException(typeof(ArgumentNullException))]
247        public void Translate_ReaderNull()
248        {
249            context.Translate(typeof(Person), null);
250        }
251
252        [Test, ExpectedException(typeof(ArgumentNullException))]
253        public void Translate_ElementTypeNull()
254        {
255            DbDataReader reader = new NullDataReader();
256            context.Translate(null, reader);
257        }
258
259        [Test, ExpectedException(typeof(ArgumentNullException))]
260        public void TranslateTResult_ReaderNull()
261        {
262            context.Translate<Person>(null);
263        }
264    }
265}
266