public void Compare_Object_Against_Null() { var model = new SimpleModel { Id = 1, Check = true, Name = "Name", Value = 1.23m, Date = new DateTime(2015, 01, 01, 12, 50, 30), Time = new TimeSpan(5, 4, 3) }; var nullToModel = SutEngine.Compare(null, model); var modelToNull = SutEngine.Compare(model, null); Assert.IsTrue(nullToModel.All(x => modelToNull.Single(y => x.Name == y.Name && object.Equals(y.OldValue, x.NewValue)) != null)); }
public void Two_Engines_Can_Run_At_The_Same_Time() { var engine1 = SutEngine; engine1.Configure <SimpleModel>() .Ignore(x => x.Date) .Compile.Async(); IComparerEngine engine2 = new Engine(); engine2.Configure <SimpleModel>() .Ignore(x => x.Value) .Compile.Async(); var obj1 = new SimpleModel { Id = 1, Name = "Name", Date = new DateTime(2015, 1, 1), Value = 3.14m }; var obj2 = new SimpleModel { Id = 2, Name = "Name2", Date = new DateTime(2015, 1, 2), Value = 1m }; var diff1 = engine1.Compare(obj1, obj2); var diff2 = engine2.Compare(obj1, obj2); Assert.AreEqual(diff1.Count, diff2.Count); Assert.AreEqual(diff1.First(x => x.Name == "Id"), diff2.First(x => x.Name == "Id")); Assert.AreEqual(diff1.First(x => x.Name == "Name"), diff2.First(x => x.Name == "Name")); Assert.IsNotNull(diff1.FirstOrDefault(x => x.Name == "Value")); Assert.IsNotNull(diff2.FirstOrDefault(x => x.Name == "Date")); Assert.IsNull(diff1.FirstOrDefault(x => x.Name == "Date")); Assert.IsNull(diff2.FirstOrDefault(x => x.Name == "Value")); }
public void Two_Engines_Can_Run_At_The_Same_Time() { var engine1 = SutEngine; engine1.Configure<SimpleModel>() .Ignore(x => x.Date) .Compile.Async(); IComparerEngine engine2 = new Engine(); engine2.Configure<SimpleModel>() .Ignore(x => x.Value) .Compile.Async(); var obj1 = new SimpleModel { Id = 1, Name = "Name", Date = new DateTime(2015, 1, 1), Value = 3.14m }; var obj2 = new SimpleModel { Id = 2, Name = "Name2", Date = new DateTime(2015, 1, 2), Value = 1m }; var diff1 = engine1.Compare(obj1, obj2); var diff2 = engine2.Compare(obj1, obj2); Assert.AreEqual(diff1.Count, diff2.Count); Assert.AreEqual(diff1.First(x => x.Name == "Id"), diff2.First(x => x.Name == "Id")); Assert.AreEqual(diff1.First(x => x.Name == "Name"), diff2.First(x => x.Name == "Name")); Assert.IsNotNull(diff1.FirstOrDefault(x => x.Name == "Value")); Assert.IsNotNull(diff2.FirstOrDefault(x => x.Name == "Date")); Assert.IsNull(diff1.FirstOrDefault(x => x.Name == "Date")); Assert.IsNull(diff2.FirstOrDefault(x => x.Name == "Value")); }
public void Compare_Simple_Model() { var comparer = SutEngine.Get <SimpleModel>(); var oldModel = new SimpleModel { Id = 1, Check = true, Name = "Name", Value = 1.23m, Date = new DateTime(2015, 01, 01, 12, 50, 30), Time = new TimeSpan(5, 4, 3), State = State.Inactive, Nullable = null, }; var newModel = new SimpleModel { Id = 1, Check = false, Name = "Name?", Value = 10.23m, Date = new DateTime(2015, 01, 02, 12, 50, 30), Time = new TimeSpan(5, 4, 1), State = State.Active, Nullable = true, }; var changes = comparer(oldModel, newModel); Assert.AreEqual(changes.Count(), 7); Assert.IsNotNull(changes.Single(x => x.Name == "Check" && (bool)x.OldValue == true && (bool)x.NewValue == false)); Assert.IsNotNull(changes.Single(x => x.Name == "Name" && (string)x.OldValue == "Name" && (string)x.NewValue == "Name?")); Assert.IsNotNull(changes.Single(x => x.Name == "Value" && (decimal)x.OldValue == 1.23m && (decimal)x.NewValue == 10.23m)); Assert.IsNotNull(changes.Single(x => x.Name == "Date" && (DateTime)x.OldValue != (DateTime)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "Time" && (TimeSpan)x.OldValue != (TimeSpan)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "State" && (State)x.OldValue != (State)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "Nullable" && (bool?)x.OldValue == null && (bool?)x.NewValue == true)); }
public void Static_Comparer_Compare_A_Simple_Type() { var oldModel = new SimpleModel { Id = 1, Check = true, Name = "Name", Value = 1.23m, Date = new DateTime(2015, 01, 01, 12, 50, 30), Time = new TimeSpan(5, 4, 3), State = State.Inactive, Nullable = null, }; var newModel = new SimpleModel { Id = 1, Check = false, Name = "Name?", Value = 10.23m, Date = new DateTime(2015, 01, 02, 12, 50, 30), Time = new TimeSpan(5, 4, 1), State = State.Active, Nullable = true, }; var changes = Comparer.Compare(oldModel, newModel); Assert.AreEqual(changes.Count(), 7); Assert.IsNotNull(changes.Single(x => x.Name == "Check" && (bool)x.OldValue == true && (bool)x.NewValue == false)); Assert.IsNotNull(changes.Single(x => x.Name == "Name" && (string)x.OldValue == "Name" && (string)x.NewValue == "Name?")); Assert.IsNotNull(changes.Single(x => x.Name == "Value" && (decimal)x.OldValue == 1.23m && (decimal)x.NewValue == 10.23m)); Assert.IsNotNull(changes.Single(x => x.Name == "Date" && (DateTime)x.OldValue != (DateTime)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "Time" && (TimeSpan)x.OldValue != (TimeSpan)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "State" && (State)x.OldValue != (State)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "Nullable" && (bool?)x.OldValue == null && (bool?)x.NewValue == true)); }
public void When_I_Compare_Two_Objects_I_Get_A_List_Of_Updated_Properties() { var comparer = Comparer.GetComparer<SimpleModel>(); var oldModel = new SimpleModel { Id = 1, Check = true, Name = "Name", Value = 1.23m, Date = new DateTime(2015, 01, 01, 12, 50, 30), Time = new TimeSpan(5, 4, 3), State = State.Inactive, Nullable = null, }; var newModel = new SimpleModel { Id = 1, Check = false, Name = "Name?", Value = 10.23m, Date = new DateTime(2015, 01, 02, 12, 50, 30), Time = new TimeSpan(5, 4, 1), State = State.Active, Nullable = true, }; var changes = comparer(oldModel, newModel); Assert.AreEqual(changes.Count(), 7); Assert.IsNotNull(changes.Single(x => x.Name == "Check" && (bool)x.OldValue == true && (bool)x.NewValue == false)); Assert.IsNotNull(changes.Single(x => x.Name == "Name" && (string)x.OldValue == "Name" && (string)x.NewValue == "Name?")); Assert.IsNotNull(changes.Single(x => x.Name == "Value" && (decimal)x.OldValue == 1.23m && (decimal)x.NewValue == 10.23m)); Assert.IsNotNull(changes.Single(x => x.Name == "Date" && (DateTime)x.OldValue != (DateTime)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "Time" && (TimeSpan)x.OldValue != (TimeSpan)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "State" && (State)x.OldValue != (State)x.NewValue)); Assert.IsNotNull(changes.Single(x => x.Name == "Nullable" && (bool?)x.OldValue == null && (bool?)x.NewValue == true)); }
public void When_I_Compare_Null_Objects_I_Get_All_Properties() { var model = new SimpleModel { Id = 1, Check = true, Name = "Name", Value = 1.23m, Date = new DateTime(2015, 01, 01, 12, 50, 30), Time = new TimeSpan(5, 4, 3) }; var nullToModel = Comparer.Compare(null, model); var modelToNull = Comparer.Compare(model, null); Assert.IsTrue(nullToModel.All(x => modelToNull.Single(y => x.Name == y.Name && object.Equals(y.OldValue, x.NewValue)) != null)); }