PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/UnitTests/Linq/UserTests/Issue369.cs

http://github.com/igor-tkachev/bltoolkit
C# | 411 lines | 360 code | 51 blank | 0 comment | 4 complexity | e528bf3b72f3f6a5fb1eef39198f3da5 MD5 | raw file
  1using System;
  2using System.Collections;
  3using System.Collections.Generic;
  4using System.Data.Linq;
  5using System.Linq;
  6using System.Xml.Linq;
  7using BLToolkit;
  8using BLToolkit.Data;
  9using BLToolkit.Data.DataProvider;
 10using BLToolkit.Data.Linq;
 11using BLToolkit.DataAccess;
 12using BLToolkit.Mapping;
 13using Data.Linq.Model;
 14using NUnit.Framework;
 15
 16namespace Data.Linq.UserTests
 17{
 18	[TestFixture]
 19	public class Issue369 : TestBase
 20	{
 21		[Test]
 22		public void Test1([IncludeDataContextsAttribute("Sql2012")] string config)
 23		{
 24			TestMethod(
 25				(db, data) =>
 26					db.SetCommand("insert into person (LastName, FirstName, Gender) values ('Issue369', @FirstName, @Gender)")
 27						.ExecuteForEach((ICollection) data),
 28				config);
 29		}
 30
 31		[Test]
 32		public void Test2([IncludeDataContextsAttribute("Sql2012")] string config)
 33		{
 34			TestMethod(
 35				(db, data) => db.InsertBatch(data),
 36				config);
 37		}
 38
 39		[Test]
 40		public void Test3([IncludeDataContextsAttribute("Sql2012")] string config)
 41		{
 42			TestMethod(
 43				(db, data) =>
 44					db.SetSpCommand("Person_Insert")
 45						.ExecuteForEach((ICollection)data),
 46				config);
 47		}
 48
 49		public void TestMethod(Action<DbManager, Person[]> insert, string config)
 50		{
 51			using (var db = new TestDbManager(config))
 52			{
 53				db.Person.Where(_ => _.LastName == "Issue369").Delete();
 54
 55				var data = new []
 56				{
 57					new Person() {LastName = "Issue369", FirstName = ""},
 58					new Person() {LastName = "Issue369", FirstName = "my"},
 59					new Person() {LastName = "Issue369", FirstName = "My first name"},
 60
 61				};
 62
 63				insert(db, data);
 64				var data2 = db.Person.Where(_ => _.LastName == "Issue369").ToArray();
 65
 66				for (var i = 0; i < data.Length; i ++)
 67				{
 68					Assert.AreEqual(data[i].FirstName, data2[i].FirstName);
 69				}
 70
 71				db.Person.Where(_ => _.LastName == "Issue369").Delete();
 72			}
 73
 74		}
 75
 76		[TableName("DataTypeTest")]
 77		public class SmallDataTypeTest
 78		{
 79			[Identity]
 80			public int DataTypeID;
 81			public byte? Byte_;
 82			public DateTime? DateTime_;
 83			public decimal? Decimal_;
 84			public double? Double_;
 85			public Guid? Guid_;
 86			public short? Int16_;
 87			public int? Int32_;
 88			public long? Int64_;
 89			public decimal? Money_;
 90			public byte? SByte_;
 91			public float? Single_;
 92			public string String_;
 93			public short? UInt16_;
 94			public int? UInt32_;
 95			public long? UInt64_;
 96		}
 97
 98		[TableName("DataTypeTest")]
 99		public class SmallDataTypeTest2
100		{
101			[Identity]
102			public int DataTypeID;
103			[Nullable]public byte Byte_;
104			[Nullable]public DateTime DateTime_;
105			[Nullable]public decimal Decimal_;
106			[Nullable]public double Double_;
107			[Nullable]public Guid Guid_;
108			[Nullable]public short Int16_;
109			[Nullable]public int Int32_;
110			[Nullable]public long Int64_;
111			[Nullable]public decimal Money_;
112			[Nullable]public byte SByte_;
113			[Nullable]public float Single_;
114			[Nullable]public string String_;
115			[Nullable]public short UInt16_;
116			[Nullable]public int UInt32_;
117			[Nullable]public long UInt64_;
118		}
119
120		[Test]
121		public void NullableTest1([DataContextsAttribute(ProviderName.DB2, ProviderName.Firebird,
122			ProviderName.Informix, ProviderName.MySql, ProviderName.Sybase, ProviderName.PostgreSQL,
123			ExcludeLinqService = true)] string config)
124		{
125			NullableTest(
126				(db, data) =>
127				{
128					var res = Convert.ToInt32(db.InsertWithIdentity(data));
129
130					db.InsertBatch(new[] {data, data});
131
132					return res;
133				},
134				config
135				);
136		}
137
138		[Test]
139		public void NullableTest2([DataContextsAttribute(ProviderName.DB2, ProviderName.Firebird,
140			ProviderName.Informix, ProviderName.MySql, ProviderName.Sybase, ProviderName.PostgreSQL,
141			"Oracle",  ProviderName.OracleManaged,
142			ExcludeLinqService = true)]string config)
143		{
144			NullableTest4(
145				(db, data) =>
146				{
147					new SqlQuery<SmallDataTypeTest2>().Insert(db, data);
148					return db.GetTable<SmallDataTypeTest2>().Max(_ => _.DataTypeID);
149				},
150				config);
151		}
152
153		[Test]
154		public void NullableTest1_2([DataContextsAttribute(ProviderName.DB2, ProviderName.Firebird,
155			ProviderName.Informix, ProviderName.MySql, ProviderName.Sybase, ProviderName.PostgreSQL, ProviderName.Access,
156			ExcludeLinqService = true)] string config)
157		{
158			NullableTest4(
159				(db, data) =>
160				{
161					var res = Convert.ToInt32(db.InsertWithIdentity(data));
162
163					db.InsertBatch(new[] { data, data });
164
165					return res;
166				},
167				config
168				);
169		}
170
171		[Test]
172		public void NullableTest2_2([DataContextsAttribute(ProviderName.DB2, ProviderName.Firebird,
173			ProviderName.Informix, ProviderName.MySql, ProviderName.Sybase, ProviderName.PostgreSQL,
174			"Oracle",  ProviderName.OracleManaged,
175			ExcludeLinqService = true)] string config)
176		{
177			NullableTest(
178				(db, data) =>
179				{
180					new SqlQuery<SmallDataTypeTest>().Insert(db, data);
181					return db.GetTable<SmallDataTypeTest>().Max(_ => _.DataTypeID);
182				},
183				config);
184		}
185
186
187		[Test]
188		public void NullableTest3([IncludeDataContexts("Sql2012")] string config)
189		{
190			NullableTest2(
191				(db, data) =>
192				{
193					db.SetSpCommand("DataTypeTest_Insert", db.CreateParameters(data))
194						.ExecuteNonQuery();
195					var id = db.GetTable<SmallDataTypeTest>().Max(_ => _.DataTypeID);
196
197					db.SetSpCommand("DataTypeTest_Insert")
198						.ExecuteForEach((ICollection) new[] {data, data});
199
200					return id;
201				},
202				config);
203		}
204
205		[Test]
206		public void NullableTest3_2([IncludeDataContexts("Sql2012")] string config)
207		{
208			NullableTest3(
209				(db, data) =>
210				{
211					db.SetSpCommand("DataTypeTest_Insert", db.CreateParameters(data))
212						.ExecuteNonQuery();
213					var id = db.GetTable<SmallDataTypeTest>().Max(_ => _.DataTypeID);
214
215					db.SetSpCommand("DataTypeTest_Insert")
216						.ExecuteForEach((ICollection) new[] {data, data});
217
218					return id;
219				},
220				config);
221		}
222
223
224		public void NullableTest2(Func<DbManager, DataTypeTest, int> insert, string config)
225		{
226			using (var db = new TestDbManager(config))
227			{
228
229				var data = new DataTypeTest()
230				{
231					Byte_ = 0,
232					Int16_ = 0,
233					Int32_ = 0,
234					Int64_ = 0,
235					UInt16_ = 0,
236					UInt32_ = 0,
237					UInt64_ = 0,
238					Decimal_ = 0,
239					Double_ = 0,
240					Money_ = 0,
241					Single_ = 0,
242					SByte_ = 0,
243				};
244
245				var id = insert(db, data);
246
247				var list = db.GetTable<DataTypeTest>().Where(_ => _.DataTypeID >= id).ToList();
248				var i = 0;
249				foreach (var d2 in list)
250				{
251					Console.WriteLine("Iteration: " + i++);
252					Assert.IsNotNull(d2.Byte_);
253					Assert.IsNotNull(d2.Decimal_);
254					Assert.IsNotNull(d2.Double_);
255					Assert.IsNotNull(d2.Int16_);
256					Assert.IsNotNull(d2.Int32_);
257					Assert.IsNotNull(d2.Int64_);
258					Assert.IsNotNull(d2.Money_);
259					Assert.IsNotNull(d2.Single_);
260
261					Assert.IsNotNull(d2.SByte_);
262					Assert.IsNotNull(d2.UInt16_);
263					Assert.IsNotNull(d2.UInt32_);
264					Assert.IsNotNull(d2.UInt64_);
265
266				}
267				db.GetTable<SmallDataTypeTest>().Delete(_ => _.DataTypeID > 2);
268			}
269		}
270
271		public void NullableTest3(Func<DbManager, DataTypeTest2, int> insert, string config)
272		{
273			using (var db = new TestDbManager(config))
274			{
275
276				var data = new DataTypeTest2()
277				{
278					Byte_ = 0,
279					Int16_ = 0,
280					Int32_ = 0,
281					Int64_ = 0,
282					UInt16_ = 0,
283					UInt32_ = 0,
284					UInt64_ = 0,
285					Decimal_ = 0,
286					Double_ = 0,
287					Money_ = 0,
288					Single_ = 0,
289					SByte_ = 0,
290				};
291
292				var id = insert(db, data);
293
294				var list = db.GetTable<DataTypeTest>().Where(_ => _.DataTypeID >= id).ToList();
295				var i = 0;
296				foreach (var d2 in list)
297				{
298					Console.WriteLine("Iteration: " + i++);
299					Assert.IsNull(d2.Byte_);
300					Assert.IsNull(d2.Decimal_);
301					Assert.IsNull(d2.Double_);
302					Assert.IsNull(d2.Int16_);
303					Assert.IsNull(d2.Int32_);
304					Assert.IsNull(d2.Int64_);
305					Assert.IsNull(d2.Money_);
306					Assert.IsNull(d2.Single_);
307
308					Assert.IsNull(d2.SByte_);
309					Assert.IsNull(d2.UInt16_);
310					Assert.IsNull(d2.UInt32_);
311					Assert.IsNull(d2.UInt64_);
312
313				}
314				db.GetTable<SmallDataTypeTest>().Delete(_ => _.DataTypeID > 2);
315			}
316		}
317
318		public void NullableTest(Func<DbManager, SmallDataTypeTest, int> insert, string config)
319		{
320			using (var db = new TestDbManager(config))
321			{
322
323				var data = new SmallDataTypeTest()
324				{
325					Byte_ = 0,
326					Int16_ = 0,
327					Int32_ = 0,
328					Int64_ = 0,
329					UInt16_ = 0,
330					UInt32_ = 0,
331					UInt64_ = 0,
332					Decimal_ = 0,
333					Double_ = 0,
334					Money_ = 0,
335					Single_ = 0,
336					SByte_ = 0,
337				};
338
339				var id = insert(db, data);
340
341				var list = db.GetTable<SmallDataTypeTest>().Where(_ => _.DataTypeID >= id).ToList();
342				var i = 0;
343				foreach (var d2 in list)
344				{
345					Console.WriteLine("Iteration: " + i++);
346					Assert.IsNotNull(d2.Byte_);
347					Assert.IsNotNull(d2.Decimal_);
348					Assert.IsNotNull(d2.Double_);
349					Assert.IsNotNull(d2.Int16_);
350					Assert.IsNotNull(d2.Int32_);
351					Assert.IsNotNull(d2.Int64_);
352					Assert.IsNotNull(d2.Money_);
353					Assert.IsNotNull(d2.Single_);
354
355					Assert.IsNotNull(d2.SByte_);
356					Assert.IsNotNull(d2.UInt16_);
357					Assert.IsNotNull(d2.UInt32_);
358					Assert.IsNotNull(d2.UInt64_);
359
360				}
361				db.GetTable<SmallDataTypeTest>().Delete(_ => _.DataTypeID > 2);
362			}
363		}
364		public void NullableTest4(Func<DbManager, SmallDataTypeTest2, int> insert, string config)
365		{
366			using (var db = new TestDbManager(config))
367			{
368
369				var data = new SmallDataTypeTest2()
370				{
371					Byte_ = 0,
372					Int16_ = 0,
373					Int32_ = 0,
374					Int64_ = 0,
375					UInt16_ = 0,
376					UInt32_ = 0,
377					UInt64_ = 0,
378					Decimal_ = 0,
379					Double_ = 0,
380					Money_ = 0,
381					Single_ = 0,
382					SByte_ = 0,
383				};
384
385				var id = insert(db, data);
386
387				var list = db.GetTable<SmallDataTypeTest>().Where(_ => _.DataTypeID >= id).ToList();
388				var i = 0;
389				foreach (var d2 in list)
390				{
391					Console.WriteLine("Iteration: " + i++);
392					Assert.IsNull(d2.Byte_);
393					Assert.IsNull(d2.Decimal_);
394					Assert.IsNull(d2.Double_);
395					Assert.IsNull(d2.Int16_);
396					Assert.IsNull(d2.Int32_);
397					Assert.IsNull(d2.Int64_);
398					Assert.IsNull(d2.Money_);
399					Assert.IsNull(d2.Single_);
400
401					Assert.IsNull(d2.SByte_);
402					Assert.IsNull(d2.UInt16_);
403					Assert.IsNull(d2.UInt32_);
404					Assert.IsNull(d2.UInt64_);
405
406				}
407				db.GetTable<SmallDataTypeTest>().Delete(_ => _.DataTypeID > 2);
408			}
409		}
410	}
411}