PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/UnitTests/Linq/BatchTest.cs

http://github.com/igor-tkachev/bltoolkit
C# | 300 lines | 259 code | 41 blank | 0 comment | 10 complexity | 9e23c0ad8eb284d670364117b6bd408a MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Data.Linq;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using BLToolkit.Data;
  7. using BLToolkit.Data.Linq;
  8. using BLToolkit.DataAccess;
  9. using BLToolkit.Mapping;
  10. using BLToolkit.Mapping.MemberMappers;
  11. using Data.Linq;
  12. using Data.Linq.Model;
  13. using NUnit.Framework;
  14. namespace Update
  15. {
  16. [TestFixture]
  17. public class BatchTest : TestBase
  18. {
  19. [Test]
  20. public void Transaction([DataContexts(ExcludeLinqService = true)] string context, [Values(Int32.MaxValue, 1)]int batchSize)
  21. {
  22. using (var db = new TestDbManager(context))
  23. {
  24. var list = new[]
  25. {
  26. new Parent { ParentID = 1111, Value1 = 1111 },
  27. new Parent { ParentID = 2111, Value1 = 2111 },
  28. new Parent { ParentID = 3111, Value1 = 3111 },
  29. new Parent { ParentID = 4111, Value1 = 4111 },
  30. };
  31. foreach (var parent in list)
  32. db.Parent.Delete(p => p.ParentID == parent.ParentID);
  33. db.BeginTransaction();
  34. db.InsertBatch(batchSize, list);
  35. db.CommitTransaction();
  36. foreach (var parent in list)
  37. db.Parent.Delete(p => p.ParentID == parent.ParentID);
  38. }
  39. }
  40. [Test]
  41. public void NoTransaction([DataContexts(ExcludeLinqService = true)] string context, [Values(Int32.MaxValue, 1)]int batchSize)
  42. {
  43. using (var db = new TestDbManager(context))
  44. {
  45. var list = new[]
  46. {
  47. new Parent { ParentID = 1111, Value1 = 1111 },
  48. new Parent { ParentID = 2111, Value1 = 2111 },
  49. new Parent { ParentID = 3111, Value1 = 3111 },
  50. new Parent { ParentID = 4111, Value1 = 4111 },
  51. };
  52. foreach (var parent in list)
  53. db.Parent.Delete(p => p.ParentID == parent.ParentID);
  54. db.InsertBatch(batchSize, list);
  55. foreach (var parent in list)
  56. db.Parent.Delete(p => p.ParentID == parent.ParentID);
  57. }
  58. }
  59. [TableName("TestIdentity")]
  60. public class Table
  61. {
  62. [Identity, MapField("ID")]
  63. public int Id;
  64. public int? IntValue;
  65. public string StringValue;
  66. }
  67. public class TestObject
  68. {
  69. public int Value { get; set; }
  70. }
  71. [TableName("TestIdentity")]
  72. public class Table2
  73. {
  74. [Identity, MapField("ID")]
  75. public int Id;
  76. public int? IntValue;
  77. [MemberMapper(typeof(JSONSerialisationMapper))]
  78. [MapField("StringValue"), DbType(DbType.String)]
  79. public TestObject Object;
  80. }
  81. [Test]
  82. public void TransactionWithIdentity1([DataContexts(ExcludeLinqService = true)] string context)
  83. {
  84. using (var db = new TestDbManager(context))
  85. {
  86. try
  87. {
  88. var list = new[]
  89. {
  90. new Table {IntValue = 1111, StringValue = "1111"},
  91. new Table {IntValue = 2111, StringValue = "2111"},
  92. new Table {IntValue = 3111, StringValue = "3111"},
  93. new Table {IntValue = 4111, StringValue = "4111"},
  94. };
  95. db.GetTable<Table>().Delete(_ => _.Id > 2);
  96. var c1 = db.GetTable<Table>().Count();
  97. db.BeginTransaction();
  98. db.InsertBatch(list);
  99. db.CommitTransaction();
  100. var c2 = db.GetTable<Table>().Count();
  101. Assert.AreEqual(c1+4, c2);
  102. }
  103. finally
  104. {
  105. db.GetTable<Table>().Delete(_ => _.Id > 2);
  106. }
  107. }
  108. }
  109. [Test]
  110. public void NoTransactionWithIdentity1([DataContexts(ExcludeLinqService = true)] string context)
  111. {
  112. using (var db = new TestDbManager(context))
  113. {
  114. try
  115. {
  116. var list = new[]
  117. {
  118. new Table {IntValue = 1111, StringValue = "1111"},
  119. new Table {IntValue = 2111, StringValue = "2111"},
  120. new Table {IntValue = 3111, StringValue = "3111"},
  121. new Table {IntValue = 4111, StringValue = "4111"},
  122. };
  123. db.GetTable<Table>().Delete(_ => _.Id > 2);
  124. var c1 = db.GetTable<Table>().Count();
  125. db.InsertBatch(list);
  126. var c2 = db.GetTable<Table>().Count();
  127. Assert.AreEqual(c1+4, c2);
  128. }
  129. finally
  130. {
  131. db.GetTable<Table>().Delete(_ => _.Id > 2);
  132. }
  133. }
  134. }
  135. [Test]
  136. public void TransactionWithIdentity2([DataContexts(ExcludeLinqService = true)] string context)
  137. {
  138. using (var db = new TestDbManager(context))
  139. {
  140. try
  141. {
  142. var list = new[]
  143. {
  144. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  145. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  146. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  147. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  148. };
  149. db.GetTable<Table2>().Delete(_ => _.IntValue == 1111);
  150. var c1 = db.GetTable<Table2>().Count();
  151. db.BeginTransaction();
  152. db.InsertBatch(list);
  153. db.CommitTransaction();
  154. var c2 = db.GetTable<Table2>().Count();
  155. Assert.AreEqual(c1+4, c2);
  156. var result = db.GetTable<Table2>().Where(_ => _.IntValue == 1111).ToList();
  157. foreach (var e in result)
  158. {
  159. Assert.AreEqual(1111, e.Object.Value);
  160. }
  161. }
  162. finally
  163. {
  164. db.GetTable<Table2>().Delete(_ => _.IntValue == 1111);
  165. }
  166. }
  167. }
  168. [Test]
  169. public void NoTransactionWithIdentity2([DataContexts(ExcludeLinqService = true)] string context)
  170. {
  171. using (var db = new TestDbManager(context))
  172. {
  173. try
  174. {
  175. var list = new[]
  176. {
  177. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  178. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  179. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  180. new Table2 {IntValue = 1111, Object = new TestObject{Value = 1111}},
  181. };
  182. db.GetTable<Table2>().Delete(_ => _.IntValue == 1111);
  183. var c1 = db.GetTable<Table2>().Count();
  184. db.InsertBatch(list);
  185. var c2 = db.GetTable<Table2>().Count();
  186. Assert.AreEqual(c1+4, c2);
  187. var result = db.GetTable<Table2>().Where(_ => _.IntValue == 1111).ToList();
  188. foreach (var e in result)
  189. {
  190. Assert.AreEqual(1111, e.Object.Value);
  191. }
  192. }
  193. finally
  194. {
  195. db.GetTable<Table2>().Delete(_ => _.IntValue == 1111);
  196. }
  197. }
  198. }
  199. [TableName(Database="KanoonIr", Name="Area")]
  200. public class Area
  201. {
  202. [ PrimaryKey(1)] public int AreaCode { get; set; }
  203. public string AreaName { get; set; }
  204. public int StateCode { get; set; }
  205. [ PrimaryKey(2)] public int CityCode { get; set; }
  206. public string Address { get; set; }
  207. public string Tels { get; set; }
  208. [Nullable ] public string WebSite { get; set; }
  209. public bool IsActive { get; set; }
  210. }
  211. [Test, ExpectedException(typeof(InvalidOperationException)
  212. /*,ExpectedMessage="Cannot access destination table '[KanoonIr]..[Area]'."*/)]
  213. public void Issue260([IncludeDataContexts("Sql2005")] string context)
  214. {
  215. using (var db = GetDataContext(context))
  216. {
  217. ((DbManager)db).InsertBatch(new[] { new Area { AreaCode = 1 } });
  218. }
  219. }
  220. [TableName("LinqDataTypes")]
  221. public class Table3
  222. {
  223. [PrimaryKey(1)] public int ID;
  224. public decimal MoneyValue;
  225. public DateTime DateTimeValue;
  226. [PrimaryKey(2)] public bool BoolValue;
  227. public short SmallIntValue;
  228. }
  229. [Test]
  230. public void BatchWithTwoKeys([DataContexts(ExcludeLinqService = true)] string context)
  231. {
  232. using (var db = (TestDbManager)GetDataContext(context))
  233. {
  234. var list = new[]
  235. {
  236. new Table3 {ID = 1000, BoolValue = true, MoneyValue = 10.1m, DateTimeValue = DateTime.Today},
  237. new Table3 {ID = 1001, BoolValue = false, MoneyValue = 10.1m, DateTimeValue = DateTime.Today},
  238. new Table3 {ID = 1002, BoolValue = true, MoneyValue = 10.1m, DateTimeValue = DateTime.Today},
  239. new Table3 {ID = 1003, BoolValue = false, MoneyValue = 10.1m, DateTimeValue = DateTime.Today},
  240. };
  241. db.GetTable<Table3>().Delete(_ => _.ID >= 1000);
  242. db.InsertBatch(list);
  243. var tomorrow = DateTime.Today.AddDays(1);
  244. var res = db.GetTable<Table3>().Where(_ => _.ID >= 1000).ToList();
  245. res.ForEach(_ => { _.MoneyValue = _.ID; _.DateTimeValue = tomorrow; _.SmallIntValue = 121; });
  246. new SqlQuery<Table3>().Update(db, int.MaxValue, res);
  247. res = db.GetTable<Table3>().Where(_ => _.ID >= 1000).ToList();
  248. res.ForEach(_=> { Assert.AreEqual(_.ID, _.MoneyValue); Assert.AreEqual(tomorrow, _.DateTimeValue); Assert.AreEqual(121, _.SmallIntValue);});
  249. res.ForEach(_=> { _.MoneyValue = _.ID+1; _.DateTimeValue = tomorrow; _.SmallIntValue = 131; });
  250. db.Update<Table3>(res);
  251. res = db.GetTable<Table3>().Where(_ => _.ID >= 1000).ToList();
  252. res.ForEach(_=> { Assert.AreEqual(_.ID+1, _.MoneyValue); Assert.AreEqual(tomorrow, _.DateTimeValue); Assert.AreEqual(131, _.SmallIntValue);});
  253. db.Delete<Table3>(res);
  254. res = db.GetTable<Table3>().Where(_ => _.ID >= 1000).ToList();
  255. Assert.IsEmpty(res);
  256. }
  257. }
  258. }
  259. }