PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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