PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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        [Test]
151        public void Ctor_ConnectionString_ExtraParameters_Munging()
152        {
153            var ex = Assert.Throws<ArgumentException> ( () => new DataContext("Server=localhost;User id=test;Database=test;DbLinqProvider=Sqlite;DbLinqConnectionType=Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite"));
154            // Keyword not supported: 'dblinqprovider'
155            Assert.Null(ex.InnerException);
156            Assert.NotNull(ex.Message);
157            Assert.True(ex.Message.IndexOf("'dblinqprovider'") != -1);
158            Assert.Null(ex.ParamName);
159        }
160
161        [Test]
162        public void Ctor_FileOrServerOrConnectionIsFilename()
163        {
164            MappingSource mapping = new AttributeMappingSource();
165            string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location;
166            new DataContext(fileOrServerOrConnection, mapping);
167        }
168
169        [Test]
170        public void Ctor_FileOrServerOrConnectionIsServer()
171        {
172            MappingSource mapping = new AttributeMappingSource();
173            string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName";
174            new DataContext(fileOrServerOrConnection, mapping);
175        }
176
177        [Test]
178        public void Connection()
179        {
180            IDbConnection connection = new NullConnection() { ConnectionString = "" };
181            DataContext dc = new DataContext(connection);
182            Assert.AreEqual(connection, dc.Connection);
183
184            var ex = Assert.Throws<Exception> ( () => new DataContext (new DummyConnection()));
185            Assert.Null(ex.InnerException);
186            Assert.NotNull(ex.Message);
187            Assert.True(ex.Message.IndexOf("'connection'") != -1);
188        }
189
190        [Test, ExpectedException(typeof(ArgumentNullException))]
191        public void ExecuteQuery_ElementTypeNull()
192        {
193            Type elementType = null;
194            context.ExecuteQuery(elementType, "command");
195        }
196
197        [Test, ExpectedException(typeof(ArgumentNullException))]
198        public void ExecuteQuery_QueryNull()
199        {
200            Type elementType = typeof(Person);
201            context.ExecuteQuery(elementType, null);
202        }
203
204        [Test, ExpectedException(typeof(ArgumentNullException))]
205        public void ExecuteQueryTResult_QueryNull()
206        {
207            context.ExecuteQuery<Person>(null);
208        }
209
210        [Test, ExpectedException(typeof(ArgumentNullException))]
211        public void GetCommand_QueryNull()
212        {
213            IQueryable query = null;
214            context.GetCommand(query);
215        }
216
217        [Test, ExpectedException(typeof(ArgumentNullException))]
218        public void GetTable_TypeNull()
219        {
220            context.GetTable(null);
221        }
222
223        [Test, ExpectedException(typeof(InvalidOperationException))]
224        public void GetTable_NotSupportedType()
225        {
226            context.GetTable(typeof(object));
227        }
228
229        [Test, ExpectedException(typeof(InvalidOperationException))]
230        public void GetTableTEntity_NotSupportedType()
231        {
232            context.GetTable<object>();
233        }
234
235        [Test]
236        public void GetTableTEntity()
237        {
238            Table<Person> table = context.GetTable<Person>();
239        }
240
241        [Test, ExpectedException(typeof(ArgumentNullException))]
242        public void Translate_ReaderNull()
243        {
244            context.Translate(typeof(Person), null);
245        }
246
247        [Test, ExpectedException(typeof(ArgumentNullException))]
248        public void Translate_ElementTypeNull()
249        {
250            DbDataReader reader = new NullDataReader();
251            context.Translate(null, reader);
252        }
253
254        [Test, ExpectedException(typeof(ArgumentNullException))]
255        public void TranslateTResult_ReaderNull()
256        {
257            context.Translate<Person>(null);
258        }
259    }
260}