Commits

Anonymous committed 827f881

test test test

Comments (0)

Files changed (2)

Usually/Resolve.cs

         {
             if (this.refList.Count <= 0) return;
             for (int i = 0; i < actualKey.Length; i++)
-                dict[this.refList[i]] = actualKey;
+                dict[this.refList[i]] = actualKey[i];
         }
         #endregion
     }

UsuallyTests/TestResolve.cs

 
     public class TestResolve
     {
+        public static void ForMaster<M>(List<M> list, M master, Action<M> act)
+        {
+            list.Add(master);
+            act(master);
+        }
+        public static void AssertInsert(TransactionRecord t, string tableName, Action<Dictionary<string, object>> build)
+        {
+            Assert.IsInstanceOf(typeof(InsertRecord), t);
+            var ins = t as InsertRecord;
+            Assert.AreEqual(tableName, ins.TableName);
+            var d = new Dictionary<string, object>();
+            build(d);
+            Assert.AreEqual(d.Count, ins.Values.Count);
+            foreach (var i in d)
+                Assert.AreEqual(i.Value, ins.Values[i.Key]);
+        }
+        public static void AssertUpdate(TransactionRecord t, string tableName, string keyName, object keyValue, Action<Dictionary<string, object>> build)
+        {
+            Assert.IsInstanceOf(typeof(UpdateRecord), t);
+            var upd = t as UpdateRecord;
+            Assert.AreEqual(tableName, upd.TableName);
+            Assert.AreEqual(1, upd.Keys.Count);
+            Assert.AreEqual(keyValue, upd.Keys[keyName]);
+            var d = new Dictionary<string, object>();
+            build(d);
+            Assert.AreEqual(d.Count, upd.Values.Count);
+            foreach (var i in d)
+                Assert.AreEqual(i.Value, upd.Values[i.Key]);
+        }
+        public static void AssertDelete(TransactionRecord t, string tableName, string keyName, object keyValue)
+        {
+            Assert.IsInstanceOf(typeof(DeleteRecord), t);
+            var del = t as DeleteRecord;
+            Assert.AreEqual(tableName, del.TableName);
+            var d = new Dictionary<string, object>();
+            Assert.AreEqual(1, del.Keys.Count);
+            Assert.AreEqual(keyValue, del.Keys[keyName]);
+        }
+        public static void AssertNextInsert(IEnumerator<TransactionRecord> records, string tableName, Action<Dictionary<string, object>> build)
+        {
+            Assert.True(records.MoveNext());
+            AssertInsert(records.Current, tableName, build);
+        }
+        public static void AssertNextUpdate(IEnumerator<TransactionRecord> records, string tableName, string keyName, object keyValue, Action<Dictionary<string, object>> build)
+        {
+            Assert.True(records.MoveNext());
+            AssertUpdate(records.Current, tableName, keyName, keyValue, build);
+        }
+        public static void AssertNextDelete(IEnumerator<TransactionRecord> records, string tableName, string keyName, object keyValue)
+        {
+            Assert.True(records.MoveNext());
+            AssertDelete(records.Current, tableName, keyName, keyValue);
+        }
         private List<OrderLine> olds = null;
         private List<OrderLine> news = null;
         [SetUp]
         {
             this.Twist(this.news);
             Assert.AreEqual(8, this.news.Count);
-            var changes = Resolver.Resolve(OrderLine.Meta, olds, news).ToArray();
-            Assert.AreEqual(8, changes.Length);
-            var e = changes.GetEnumerator();
+            var changes = Resolver.Resolve(OrderLine.Meta, olds, news).ToList();
+            Assert.AreEqual(8, changes.Count);
+            var e = changes.GetEnumerator() as IEnumerator<TransactionRecord>;            
             Assert.True(e.MoveNext());
             Assert.IsInstanceOf(typeof(InsertRecord), e.Current);
             {
             r.Add(OrderLine.Meta, this.olds, this.news);
             this.Twist(this.news);
             Assert.AreEqual(8, this.news.Count);
-            var changes = r.Resolve().ToArray();
-            Assert.AreEqual(8, changes.Length);
-            var e = changes.GetEnumerator();
+            var changes = r.Resolve().ToList();
+            Assert.AreEqual(8, changes.Count);
+            var e = changes.GetEnumerator() as IEnumerator<TransactionRecord>;
             Assert.True(e.MoveNext());
             Assert.IsInstanceOf(typeof(InsertRecord), e.Current);
             {
         private List<Order> newOrders = null;
         private List<OrderLine> newLines = null;
         private List<Shipment> newShips = null;
-        private void ForMaster<M>(List<M> list, M master, Action<M> act)
-        {
-            list.Add(master);
-            act(master);
-        }
         [SetUp]
         public void SetUp()
         {
             this.oldOrders = new List<Order>();
             this.oldLines = new List<OrderLine>();
             this.oldShips = new List<Shipment>();
-            this.ForMaster(this.oldOrders,
+            TestResolve.ForMaster(this.oldOrders,
                 new Order { OrderId = 1, OrderNumber = "N-001", OrderDate = new DateTime(2012, 2, 14), Status = "FINISHED", CustomerId = 1 },
                 (o) => 
                 {
-                    this.ForMaster(this.oldLines,
+                    TestResolve.ForMaster(this.oldLines,
                         new OrderLine { OrderId = o.OrderId, LineId = 101, UniqueId = 1101, ItemId = 900001, ItemName = "One", Quantity = 10, Unit = "U1" },
                         (ln) => 
                         {
                             });
                         }
                     );
-                    this.ForMaster(this.oldLines,
+                    TestResolve.ForMaster(this.oldLines,
                         new OrderLine { OrderId = o.OrderId, LineId = 102, UniqueId = 1102, ItemId = 900002, ItemName = "Two", Quantity = 2, Unit = "U2" },
                         (ln) =>
                         {
                             });
                         }
                     );
-                    this.ForMaster(this.oldLines,
+                    TestResolve.ForMaster(this.oldLines,
                         new OrderLine { OrderId = o.OrderId, LineId = 103, UniqueId = 1103, ItemId = 900003, ItemName = "Three", Quantity = 2, Unit = "U3" },
                         (ln) =>
                         {
                     );
                 }
             );
-            this.ForMaster(this.oldOrders,
+            TestResolve.ForMaster(this.oldOrders,
                 new Order { OrderId = 2, OrderNumber = "N-002", OrderDate = new DateTime(2012, 2, 19), Status = "ACTIVE", CustomerId = 2 },
                 (o) =>
                 {
-                    this.ForMaster(this.oldLines,
+                    TestResolve.ForMaster(this.oldLines,
                         new OrderLine { OrderId = o.OrderId, LineId = 201, UniqueId = 2201, ItemId = 900001, ItemName = "One", Quantity = 1, Unit = "U1" },
                         (ln) =>
                         {
                             });
                         }
                     );
-                    this.ForMaster(this.oldLines,
+                    TestResolve.ForMaster(this.oldLines,
                         new OrderLine { OrderId = o.OrderId, LineId = 202, UniqueId = 2202, ItemId = 900002, ItemName = "Two", Quantity = 2, Unit = "U2" },
                         (ln) =>
                         {
                     );
                 }
             );
-            this.ForMaster(this.oldOrders,
+            TestResolve.ForMaster(this.oldOrders,
                 new Order { OrderId = 3, OrderNumber = "N-003", OrderDate = new DateTime(2012, 3, 2), Status = "INCOMPLETED", CustomerId = 3 },
                 (o) => 
                 {
-                    this.ForMaster(this.oldLines,
+                    TestResolve.ForMaster(this.oldLines,
                         new OrderLine { OrderId = o.OrderId, LineId = 301, UniqueId = 3301, ItemId = 900001, ItemName = "One", Quantity = 1, Unit = "U1" },
                         (ln) => 
                         {
             this.newShips.RemoveAll((x) => x.ShipmentId == 20102);   // 1 deleted
             this.newLines.RemoveAll((x) => x.LineId == 202);         // 3 deleted
             this.newOrders.RemoveAll((x) => x.OrderId == 3);         // 2 deleted
-            this.ForMaster(                                          // 9 inserted
+            TestResolve.ForMaster(                                               // 9 inserted
                 this.newOrders,
                 new Order() { OrderId = -4, OrderNumber = "N-004", OrderDate = new DateTime(2012, 3, 5), Status = "ACTIVE", CustomerId = 4 },
                 (o) =>
                 {
-                    this.ForMaster(this.newLines,
-                        new OrderLine { OrderId = o.OrderId, LineId = -401, UniqueId = -4401, ItemId = 900001, ItemName = "One", Quantity = 1, Unit = "U1" },
+                    TestResolve.ForMaster(this.newLines,
+                        new OrderLine { OrderId = o.OrderId, LineId = -401, UniqueId = -4401, ItemId = 900001, ItemName = "One", Quantity = 10, Unit = "U1" },
                         (ln) =>
                         {
                             this.newShips.AddRange(new Shipment[] 
                             });
                         }
                     );
-                    this.ForMaster(this.newLines,
+                    TestResolve.ForMaster(this.newLines,
                         new OrderLine { OrderId = o.OrderId, LineId = -402, UniqueId = -4402, ItemId = 900002, ItemName = "Two", Quantity = 2, Unit = "U2" },
                         (ln) =>
                         {
                     );
                 }
             );
-            this.newLines.Single((o) => o.LineId == 102).OrderId = 3;                             // 2 deleted
+            this.newLines.Single((o) => o.LineId == 102).OrderId = 3;                             // 2 deleted (shipment 10201 already deleted)
             this.newOrders.Single((o) => o.OrderId == 1).Status = "ARCHIVED";                     // 1 updated
             foreach (var ln in (from ln in this.newLines where ln.OrderId == 2 select ln))        // 1 updated
                 ln.Quantity = 12345;
             foreach (var sh in (from sh in this.newShips where sh.OrderLineId == 103 select sh))  // 3 updated
                 sh.Quantity = 54321;
-            var changes = this.CreateResolver().Resolve().ToArray();
-            Assert.AreEqual(24, changes.Length);
-            var e = changes.GetEnumerator();
+            var changes = this.CreateResolver().Resolve().ToList();
+            Assert.AreEqual(24, changes.Count);
+            var e = changes.GetEnumerator() as IEnumerator<TransactionRecord>;
+            TestResolve.AssertNextUpdate(e, "ORDERS", "ORDER_ID", 1, (d) => 
+            { 
+                d["STATUS"] = "ARCHIVED"; 
+            });
+            TestResolve.AssertNextUpdate(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 10301, (d) =>
+            {
+                d["QUANTITY"] = 54321;
+            });
+            TestResolve.AssertNextUpdate(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 10302, (d) =>
+            {
+                d["QUANTITY"] = 54321;
+            });
+            TestResolve.AssertNextUpdate(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 10304, (d) =>
+            {
+                d["QUANTITY"] = 54321;
+            });
+            TestResolve.AssertNextDelete(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 10303);
+            TestResolve.AssertNextUpdate(e, "ORDER_LINES", "LINE_ID", 201, (d) =>
+            {
+                d["QUANTITY"] = 12345;
+            });
+            TestResolve.AssertNextDelete(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 20102);
+            TestResolve.AssertNextDelete(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 20201);
+            TestResolve.AssertNextDelete(e, "ORDER_SHIPMENTS", "SHIPMENT_ID", 20202);
+            TestResolve.AssertNextDelete(e, "ORDER_LINES", "LINE_ID", 202);
+            TestResolve.AssertNextInsert(e, "ORDERS", (d) =>
+            {
+                d["ORDER_ID"] = 1000001;
+                d["ORDER_NUMBER"] = "N-004";
+                d["ORDER_DATE"] = new DateTime(2012, 3, 5);
+                d["STATUS"] = "ACTIVE";
+                d["CUSTOMER_ID"] = 4;
+            });
             Assert.True(e.MoveNext());
-            Assert.IsInstanceOf(typeof(UpdateRecord), e.Current);
+            Assert.IsInstanceOf(typeof(InsertRecord), e.Current);
             {
-                var t = e.Current as UpdateRecord;
+                var t = e.Current as InsertRecord;
+                Assert.AreEqual(t.TableName, "ORDER_LINES");
+                Assert.AreEqual(7, t.Values.Count);
+                Assert.AreEqual(1500001, t.Values["LINE_ID"]);
             }
             Assert.True(e.MoveNext());
             Assert.True(e.MoveNext());
             Assert.True(e.MoveNext());
             Assert.True(e.MoveNext());
             Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
-            Assert.True(e.MoveNext());
+            TestResolve.AssertInsert(e.Current, "ORDER_LINES", (d) =>
+            {
+                d["LINE_ID"] = 1500002;
+                d["ORDER_ID"] = 1000001;
+                d["LINE_UNIQUE_ID"] = 1900002;
+                d["ITEM_ID"] = 900002;
+                d["ITEM_NAME"] = "Two";
+                d["QUANTITY"] = 2;
+                d["UNIT"] = "U2";
+            });
+            Assert.IsInstanceOf(typeof(InsertRecord), e.Current);
+            {
+                var t = e.Current as InsertRecord;
+                Assert.AreEqual(t.TableName, "ORDER_LINES");
+                Assert.AreEqual(7, t.Values.Count);
+                Assert.AreEqual(1500002, t.Values["LINE_ID"]);
+            }
             Assert.True(e.MoveNext());
             Assert.True(e.MoveNext());
             Assert.True(e.MoveNext());