public void ChangeAssessorTests_SubPropertyValuesIdDifferent_SubPropertySetWithAnAddAndARemove() { var source = MockDataHelper.BuildMultiLevelA("123"); var destination = Mapper.Map <MultiLevelA>(source); //Different ids source.B = MockDataHelper.BuildMultiLevelB("1"); destination.B = Mapper.Map <MultiLevelB>(source.B); destination.B.Id = "SOmething different"; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(0, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(2, assessment.Relationships.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); var relationshipDeleted = assessment.Relationships.FirstOrDefault(f => f.Value == destination.B); Assert.IsNotNull(relationshipDeleted, "Destination should be in the list of changed relationships"); Assert.AreEqual(relationshipDeleted.Parent, source, "Proper parent should be assigned in the relationship "); Assert.AreEqual(relationshipDeleted.ChangeType, ChangeType.Deleted, "Change Type is set to Added "); var relationshipAdded = assessment.Relationships.FirstOrDefault(f => f.Value == source.B); Assert.IsNotNull(relationshipAdded, "Destination should be in the list of changed relationships"); Assert.AreEqual(relationshipAdded.Parent, source, "Proper parent should be assigned in the relationship "); Assert.AreEqual(relationshipAdded.ChangeType, ChangeType.Added, "Change Type is set to Added "); }
public void ChangeAssessorTests_SubListPropertyValuesAddDeletedAndUpdated_IsOwner_SubListPropertySetAsMixedStatuses() { var source = MockDataHelper.BuildMultiLevelA("123"); var destination = Mapper.Map <MultiLevelA>(source); source.B = MockDataHelper.BuildMultiLevelB("5523"); destination.B = MockDataHelper.BuildMultiLevelB("5523"); //The root is changed as well destination.StringField = Guid.NewGuid().ToString(); //Note out of order to ensure test covers other scenarios ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note ownership .AddOwnerMapping <MultiLevelA>(a => a.B); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(2, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); Assert.IsTrue(assessment.OwnedEntities.Contains(source), "Root record should be in the list of changes"); Assert.AreEqual(ChangeType.Updated, source.ChangeType, string.Format("Record {0} should have the update change type", 2)); Assert.IsTrue(assessment.OwnedEntities.Contains(source.B), "Sub record should be in the list of changes"); Assert.AreEqual(ChangeType.Updated, source.B.ChangeType, string.Format("Record {0} should have the update change type", 2)); }
public void ChangeAssessorTests_SubPropertyRemoved_IsOwner_SubPropertySetAsRemoved() { var source = MockDataHelper.BuildMultiLevelA("1"); var destination = Mapper.Map <MultiLevelA>(source); //Note source will now have a sub single property //This is removed and not in the source destination.B = MockDataHelper.BuildMultiLevelB("23145"); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note ownership assignment .AddOwnerMapping <MultiLevelA>(a => a.B); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 1, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(destination.B, assessment.OwnedEntities[0], "Destination record returned as Deleted"); Assert.AreEqual(ChangeType.Deleted, destination.B.ChangeType, "Change Type is set to Deleted"); //Relationships Assert.AreEqual(assessment.Relationships.Count, 1, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities.Count)); var relationship = assessment.Relationships.FirstOrDefault(r => r.Value == destination.B); Assert.IsNotNull(relationship, "Record {0} should be in the list of changed relationships"); Assert.AreEqual(relationship.Parent, source, "Proper parent should be assigned in the relationship"); Assert.AreEqual(relationship.ChangeType, ChangeType.Deleted, "Change Type is set to Deleted"); }
public void ChangeAssessorTests_SubListPropertyValuesChanged_IsOwner_SubListPropertySetAsUpdated() { var source = MockDataHelper.BuildMultiLevelA("1"); source.Cs = new System.Collections.Generic.List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("11"), MockDataHelper.BuildMultiLevelC("22") }; var destination = Mapper.Map <MultiLevelA>(source); destination.Cs = source.Cs.Select(c => Mapper.Map <MultiLevelC>(c)).ToList(); destination.Cs[0].StringField = "I am different1"; destination.Cs[1].StringField = "I am different2"; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note ownership .AddOwnerMapping <MultiLevelA>(a => a.Cs); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 2, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities.Count)); Assert.IsTrue(assessment.OwnedEntities.Contains(source.Cs[0]), "Cs 0 returned as changed"); Assert.IsTrue(assessment.OwnedEntities.Contains(source.Cs[1]), "Cs 1 returned as changed"); Assert.AreEqual(source.Cs[0].ChangeType, ChangeType.Updated, "Change Type is set to updated for C 0"); Assert.AreEqual(source.Cs[1].ChangeType, ChangeType.Updated, "Change Type is set to updated for C 1"); }
public void ChangeAssessorTests_ChangeTypeChanged_NoChangesShouldBePresentedAsThisFieldIsIgnored() { //Note I am using sub properties to ensure the name is ignored in both levels of different types var source = MockDataHelper.BuildMultiLevelA("1"); source.B = MockDataHelper.BuildMultiLevelB("23145"); var destination = Mapper.Map <MultiLevelA>(source); destination.B = Mapper.Map <MultiLevelB>(source.B); //Ensure statuses are different source.ChangeType = ChangeType.Updated; destination.ChangeType = ChangeType.Added; source.B.ChangeType = ChangeType.None; destination.B.ChangeType = ChangeType.Deleted; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, "No changes should be found as the changed fields should be ignored"); }
public void ChangeAssessorTests_List_RootValuesAdded_RootSetAsAdded() { var source = new List <OneLevel>() { MockDataHelper.BuildOneLevel("-2124"), MockDataHelper.BuildOneLevel("1"), MockDataHelper.BuildOneLevel("5212"), MockDataHelper.BuildOneLevel("561223") }; //Note out of order to ensure test covers other scenarios var destination = new List <OneLevel>() { source[2] }; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatusList(source, destination); var expectedIndices = new[] { 0, 1, 3 }; Assert.AreEqual(expectedIndices.Length, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); foreach (var expectedIndex in expectedIndices) { Assert.IsTrue(assessment.OwnedEntities.Contains(source[expectedIndex]), string.Format("Record {0} should be in the list of changes", expectedIndex)); Assert.AreEqual(ChangeType.Added, source[expectedIndex].ChangeType, string.Format("Record {0} should have the added change type", expectedIndex)); } }
public void ChangeAssessorTests_IgnoreFieldNameSet_ChangesAreNotDetectedForFieldsSet() { //Note I am using sub properties to ensure the name is ignored in both levels of different types var source = MockDataHelper.BuildMultiLevelA("1"); source.B = MockDataHelper.BuildMultiLevelB("23145"); var destination = Mapper.Map <MultiLevelA>(source); destination.B = Mapper.Map <MultiLevelB>(source.B); //Change StringField, and Int32 source.StringField = Guid.NewGuid().ToString(); source.Int32Field += 2000; source.B.StringField = Guid.NewGuid().ToString(); source.B.Int32Field += 1000; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Ignoring both fields .IgnoreFields("StringField", "Int32Field"); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, "No changes should be found as the changed fields should be ignored"); }
public void ChangeAssessorTests_List_RootValuesNoChangeAndOutOfOrder_RootSetAsAdded() { var source = new List <OneLevel>() { MockDataHelper.BuildOneLevel("-2124"), MockDataHelper.BuildOneLevel("1"), MockDataHelper.BuildOneLevel("5212"), MockDataHelper.BuildOneLevel("561223") }; //Note out of order to ensure test covers other scenarios var destination = new List <OneLevel>() { Mapper.Map <OneLevel>(source[2]), Mapper.Map <OneLevel>(source[1]), Mapper.Map <OneLevel>(source[0]), Mapper.Map <OneLevel>(source[3]) }; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatusList(source, destination); Assert.AreEqual(0, assessment.OwnedEntities.Count, string.Format("No changes expected, received {0}", assessment.OwnedEntities.Count)); foreach (var sourceItem in source) { Assert.AreEqual(ChangeType.None, sourceItem.ChangeType, string.Format("Record {0} should have the none change type", sourceItem.Id)); } }
public void ChangeAssessorTests_SubPropertyAdded_IsOwner_SubPropertySetAsAdded() { var source = MockDataHelper.BuildMultiLevelA("1"); var destination = Mapper.Map <MultiLevelA>(source); //Note source will now have a sub single property //This is new and not in the destiation source.B = MockDataHelper.BuildMultiLevelB("23145"); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note owner addition .AddOwnerMapping <MultiLevelA>(a => a.B) ; var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 1, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(source.B, assessment.OwnedEntities[0], "Source record returned as Added"); Assert.AreEqual(source.B.ChangeType, ChangeType.Added, "Change Type is set to Added"); //relationships Assert.AreEqual(assessment.Relationships.Count, 1, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(source.B, assessment.Relationships[0].Value, "Source record returned as in relationship changes"); Assert.AreEqual(assessment.Relationships[0].Parent, source, "Proper parent should be assigned in the relationship"); Assert.AreEqual(assessment.Relationships[0].ChangeType, ChangeType.Added, "Change Type is set to Added"); }
public void ChangeAssessorTests_ListDestinationNull_RootValuesAdded_RootSetAsAdded() { var source = new List <OneLevel>() { MockDataHelper.BuildOneLevel("-2124"), MockDataHelper.BuildOneLevel("1"), MockDataHelper.BuildOneLevel("5212"), MockDataHelper.BuildOneLevel("561223") }; //note null List <OneLevel> destination = null; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatusList(source, destination); var expectedIndices = Enumerable.Range(0, source.Count); Assert.AreEqual(expectedIndices.Count(), assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); foreach (var expectedIndex in expectedIndices) { Assert.IsTrue(assessment.OwnedEntities.Contains(source[expectedIndex]), string.Format("Record {0} should be in the list of changes", expectedIndex)); Assert.AreEqual(ChangeType.Added, source[expectedIndex].ChangeType, string.Format("Record {0} should have the added change type", expectedIndex)); } }
public void ChangeAssessorTests_List_NullAndNullPassed_NoChangesReturned() { ChangeAssessor <int> changeAssessor = new ChangeAssessor <int>(); var assessment = changeAssessor.SetChangeStatusList((List <IChangeTrackable <int> >)null, (List <IChangeTrackable <int> >)null); Assert.IsFalse(assessment.OwnedEntities.Any(), "There should be no assessments"); }
public void ChangeAssessorTests_List_SubPropertyValuesAddDeletedAndUpdatedAndRootUpdated_SubPropertySetAsMixedStatuses() { var source = MockDataHelper.BuildMultiLevelA("123"); var destination = Mapper.Map <MultiLevelA>(source); source.Cs = new List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("-2124"), MockDataHelper.BuildMultiLevelC("1"), MockDataHelper.BuildMultiLevelC("5212") }; destination.Cs = new List <MultiLevelC>() { //Existing, unchanged Mapper.Map <MultiLevelC>(source.Cs[0]), //To Be updated MockDataHelper.BuildMultiLevelC("1"), //Note index 2 is not here and therefore added //Does not exist in source and therefore will be deleted MockDataHelper.BuildMultiLevelC("14wdwedqweqe") }; //The root is changed as well destination.StringField = Guid.NewGuid().ToString(); //Note out of order to ensure test covers other scenarios ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(1, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(2, assessment.Relationships.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); Assert.IsTrue(assessment.OwnedEntities.Contains(source), "Root record should be in the list of changes"); Assert.AreEqual(ChangeType.Updated, source.ChangeType, string.Format("Record {0} should have the update change type", 2)); Assert.AreEqual(ChangeType.None, source.Cs[0].ChangeType, string.Format("Record {0} should have the none change type", 0)); Assert.AreEqual(ChangeType.None, source.Cs[1].ChangeType, string.Format("Record {0} should have the none change type as it is not owned", 1)); var relationshipS2 = assessment.Relationships.FirstOrDefault(r => r.Value == source.Cs[2]); Assert.IsNotNull(relationshipS2, "Record 2 should be in the list of changed relationships S2"); Assert.AreEqual(relationshipS2.Parent, source, "Proper parent should be assigned in the relationship S2"); Assert.AreEqual(relationshipS2.ChangeType, ChangeType.Added, "Change Type is set to Added S2"); Assert.AreEqual(source.Cs[2].ChangeType, ChangeType.None, "Actual Entity type not changed due to lack of ownership S2"); var relationshipD2 = assessment.Relationships.FirstOrDefault(r => r.Value == destination.Cs[2]); Assert.IsNotNull(relationshipD2, "Record 2 should be in the list of changed relationships D2"); Assert.AreEqual(relationshipD2.Parent, source, "Proper parent should be assigned in the relationship D2"); Assert.AreEqual(relationshipD2.ChangeType, ChangeType.Deleted, "Change Type is set to Deleted D2"); Assert.AreEqual(destination.Cs[2].ChangeType, ChangeType.None, "Actual Entity type not changed due to lack of ownership D2"); }
private void InitializeAssessor() { _changeAssessor = new ChangeAssessor <int>() //These are all sub properties that are only related to one movie .AddOwnerMapping <Movie>(m => m.BoxOfficeDetails) .AddOwnerMapping <Movie>(m => m.CastRoles) //Role a can create an actor but not delete them .AddOwnerMapping <Role>(m => m.Actor, update: false, delete: false) .AddOwnerMapping <Movie>(m => m.Reviews) //You can add a director through a movie but not delete or update them .AddOwnerMapping <Movie>(m => m.Director, update: false, delete: false) ; }
public void ChangeAssessorTests_RootValuesChanged_RootSetAsNoChange() { //Note both should be randomly generated but have the same ID var source = MockDataHelper.BuildOneLevel("1"); var destination = Mapper.Map <OneLevel>(source); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, string.Format("No changes expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(source.ChangeType, ChangeType.None, "Change Type is set to NoChange"); }
public void ChangeAssessorTests_RootValuesChanged_RootSetAsUpdated() { //Note both should be randomly generated but have the same ID var source = MockDataHelper.BuildOneLevel("1"); var destination = MockDataHelper.BuildOneLevel("1"); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 1, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities)); Assert.AreEqual(source, assessment.OwnedEntities[0], "Source record returned as changed"); Assert.AreEqual(source.ChangeType, ChangeType.Updated, "Change Type is set to updated"); }
public void ChangeAssessorTests_List_SubPropertyValuesAddDeletedAndUpdatedAndRootUpdated_IsOwner_SubPropertySetAsMixedStatuses() { var source = MockDataHelper.BuildMultiLevelA("123"); var destination = Mapper.Map <MultiLevelA>(source); source.Cs = new List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("-2124"), MockDataHelper.BuildMultiLevelC("1"), MockDataHelper.BuildMultiLevelC("5212") }; destination.Cs = new List <MultiLevelC>() { //Existing, unchanged Mapper.Map <MultiLevelC>(source.Cs[0]), //To Be updated MockDataHelper.BuildMultiLevelC("1"), //Note index 2 is not here and therefore added //Does not exist in source and therefore will be deleted MockDataHelper.BuildMultiLevelC("14wdwedqweqe") }; //The root is changed as well destination.StringField = Guid.NewGuid().ToString(); //Note out of order to ensure test covers other scenarios ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Adding Ownershio .AddOwnerMapping <MultiLevelA>(c => c.Cs); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(4, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); Assert.IsTrue(assessment.OwnedEntities.Contains(source), "Root record should be in the list of changes"); Assert.AreEqual(ChangeType.Updated, source.ChangeType, string.Format("Record {0} should have the update change type", 2)); Assert.AreEqual(ChangeType.None, source.Cs[0].ChangeType, string.Format("Record {0} should have the none change type", 0)); Assert.IsTrue(assessment.OwnedEntities.Contains(source.Cs[1]), string.Format("Record {0} should be in the list of changes", 1)); Assert.AreEqual(ChangeType.Updated, source.Cs[1].ChangeType, string.Format("Record {0} should have the updated change type", 1)); Assert.IsTrue(assessment.OwnedEntities.Contains(source.Cs[2]), string.Format("Record {0} should be in the list of changes", 2)); Assert.AreEqual(ChangeType.Added, source.Cs[2].ChangeType, string.Format("Record {0} should have the added change type", 2)); Assert.IsTrue(assessment.OwnedEntities.Contains(destination.Cs[2]), string.Format("Record {0} should be in the list of changes", 2)); Assert.AreEqual(ChangeType.Deleted, destination.Cs[2].ChangeType, string.Format("Record {0} should have the removed change type", 2)); }
public void ChangeAssessorTests_SubListPropertyValuessAdded_IsOwner_SubListPropertySetAsAdded() { var source = MockDataHelper.BuildMultiLevelA("1"); source.Cs = new System.Collections.Generic.List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("00"), MockDataHelper.BuildMultiLevelC("11"), MockDataHelper.BuildMultiLevelC("22") }; var destination = Mapper.Map <MultiLevelA>(source); //Note only one exists therefore 2 should be added destination.Cs = new List <MultiLevelC>() { Mapper.Map <MultiLevelC>(source.Cs[1]) }; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note ownership .AddOwnerMapping <MultiLevelA>(a => a.Cs); var assessment = changeAssessor.SetChangeStatus(source, destination); var expectedIndices = new[] { 0, 2 }; Assert.AreEqual(expectedIndices.Length, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); foreach (var expectedIndex in expectedIndices) { Assert.IsTrue(assessment.OwnedEntities.Contains(source.Cs[expectedIndex]), string.Format("Record {0} should be in the list of changes", expectedIndex)); Assert.AreEqual(ChangeType.Added, source.Cs[expectedIndex].ChangeType, string.Format("Record {0} should have the added change type", expectedIndex)); } //Relationships Assert.AreEqual(expectedIndices.Length, assessment.Relationships.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); foreach (var expectedIndex in expectedIndices) { var relationship = assessment.Relationships.FirstOrDefault(r => r.Value == source.Cs[expectedIndex]); Assert.IsNotNull(relationship, string.Format("Record {0} should be in the list of changed relationships", expectedIndex)); Assert.AreEqual(relationship.Parent, source, "Proper parent should be assigned in the relationship " + expectedIndex); Assert.AreEqual(relationship.ChangeType, ChangeType.Added, "Change Type is set to Added " + expectedIndex); } }
public void ChangeAssessorTests_IgnoreBaseClassSet_ChangesAreNotDetectedForBaseClassFields() { var source = MockDataHelper.BuildMultiLevelC("1"); var destination = Mapper.Map <MultiLevelC>(source); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note ignoring the whole class .IgnoreFields <BaseClassToIgnore>() ; //Only change fields from ignored class cast for test safety var sourceCastToBaseClass = (BaseClassToIgnore)source; sourceCastToBaseClass.StringField = Guid.NewGuid().ToString(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, "No changes should be found as the changed fields should be ignored"); }
public void ChangeAssessorTests_SubPropertyValuesChanged_SubPropertySetAsNoChange() { var source = MockDataHelper.BuildMultiLevelA("1"); source.B = MockDataHelper.BuildMultiLevelB("2"); var destination = Mapper.Map <MultiLevelA>(source); destination.B = Mapper.Map <MultiLevelB>(source.B); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, string.Format("No changes expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(source.B.ChangeType, ChangeType.None, "Change Type is set to None"); }
public void ChangeAssessorTests_CirclerReferenceSubListProperty_NoStackOverflow() { var source = MockDataHelper.BuildMultiLevelA("5213123"); source.Cs = new List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("1"), MockDataHelper.BuildMultiLevelC("2"), MockDataHelper.BuildMultiLevelC("3"), MockDataHelper.BuildMultiLevelC("6") }; foreach (var sourceC in source.Cs) { sourceC.A = source; } ; var destination = Mapper.Map <MultiLevelA>(source); destination.Cs = new List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("1"), MockDataHelper.BuildMultiLevelC("2"), MockDataHelper.BuildMultiLevelC("3"), MockDataHelper.BuildMultiLevelC("4") }; foreach (var destinationC in destination.Cs) { destinationC.A = destination; } ; //Note change results dont matter for this test, just ensure the results return. Amount checked just for //Some kind of assertion ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Circular ownership .AddOwnerMapping <MultiLevelA>(c => c.Cs) .AddOwnerMapping <MultiLevelC>(c => c.A); var assessment = changeAssessor.SetChangeStatus(source, destination); //3 updates, 1 add, 1 remove Assert.AreEqual(5, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); }
public void ChangeAssessorTests_IgnoreInterfaceSet_ChangesAreNotDetectedForInterfaceFields() { var source = MockDataHelper.BuildOneLevel("1"); var destination = Mapper.Map <OneLevel>(source); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note ignoring the whole interface .IgnoreFields <InterfaceToIgnore>(); //Only change fields from interface cast for test safety var sourceCastToInterface = (InterfaceToIgnore)source; sourceCastToInterface.BooleanField = !sourceCastToInterface.BooleanField; sourceCastToInterface.Int32Field += 100; var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, "No changes should be found as the changed fields should be ignored"); }
public void ChangeAssessorTests_SubPropertyValuesChanged_IsOwner_SubPropertySetAsUpdated() { var source = MockDataHelper.BuildMultiLevelA("1"); source.B = MockDataHelper.BuildMultiLevelB("2"); var destination = Mapper.Map <MultiLevelA>(source); destination.B = Mapper.Map <MultiLevelB>(source.B); source.B.StringField = "I am changed"; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Note Ownership .AddOwnerMapping <MultiLevelA>(a => a.B); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 1, string.Format("One and only one change is expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(source.B, assessment.OwnedEntities[0], "Source record returned as changed"); Assert.AreEqual(source.B.ChangeType, ChangeType.Updated, "Change Type is set to updated"); }
public void ChangeAssessorTests_List_RootValuesAddDeletedAndUpdated_RootSetAsMixedStatuses() { var source = new List <OneLevel>() { MockDataHelper.BuildOneLevel("-2124"), MockDataHelper.BuildOneLevel("1"), MockDataHelper.BuildOneLevel("5212") }; //Note out of order to ensure test covers other scenarios var destination = new List <OneLevel>() { //Existing, unchanged Mapper.Map <OneLevel>(source[0]), //To Be updated MockDataHelper.BuildOneLevel("1"), //Note index 2 is not here and therefore added //Does not exist in source and therefore will be deleted MockDataHelper.BuildOneLevel("14wdwedqweqe") }; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatusList(source, destination); Assert.AreEqual(3, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(ChangeType.None, source[0].ChangeType, string.Format("Record {0} should have the none change type", 0)); Assert.IsTrue(assessment.OwnedEntities.Contains(source[1]), string.Format("Record {0} should be in the list of changes", 1)); Assert.AreEqual(ChangeType.Updated, source[1].ChangeType, string.Format("Record {0} should have the updated change type", 1)); Assert.IsTrue(assessment.OwnedEntities.Contains(source[2]), string.Format("Record {0} should be in the list of changes", 2)); Assert.AreEqual(ChangeType.Added, source[2].ChangeType, string.Format("Record {0} should have the added change type", 2)); Assert.IsTrue(assessment.OwnedEntities.Contains(destination[2]), string.Format("Record {0} should be in the list of changes", 2)); Assert.AreEqual(ChangeType.Deleted, destination[2].ChangeType, string.Format("Record {0} should have the removed change type", 2)); }
public void ChangeAssessorTests_SubListPropertyValuesRemoved_SubListPropertySetAsRemoved() { var destination = MockDataHelper.BuildMultiLevelA("1"); destination.Cs = new System.Collections.Generic.List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("00"), MockDataHelper.BuildMultiLevelC("11"), MockDataHelper.BuildMultiLevelC("22") }; var source = Mapper.Map <MultiLevelA>(destination); //Note only one exists therefore 2 should be added source.Cs = new List <MultiLevelC>() { Mapper.Map <MultiLevelC>(source.Cs[1]) }; ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); var expectedIndices = new[] { 0, 2 }; Assert.AreEqual(expectedIndices.Length, assessment.Relationships.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); foreach (var expectedIndex in expectedIndices) { var relationship = assessment.Relationships.FirstOrDefault(r => r.Value == destination.Cs[expectedIndex]); Assert.IsNotNull(relationship, string.Format("Record {0} should be in the list of changed relationships", expectedIndex)); Assert.AreEqual(relationship.Parent, source, "Proper parent should be assigned in the relationship " + expectedIndex); Assert.AreEqual(relationship.ChangeType, ChangeType.Deleted, "Change Type is set to Deleted " + expectedIndex); Assert.AreEqual(destination.Cs[expectedIndex].ChangeType, ChangeType.None, "Actual Entity type not changed due to lack of ownership " + expectedIndex); } }
public void ChangeAssessorTests_SubListPropertyValuesChanged_SubListPropertySetAsNoChange() { var source = MockDataHelper.BuildMultiLevelA("1"); source.Cs = new System.Collections.Generic.List <MultiLevelC>() { MockDataHelper.BuildMultiLevelC("11"), MockDataHelper.BuildMultiLevelC("22") }; var destination = Mapper.Map <MultiLevelA>(source); destination.Cs = source.Cs.Select(c => Mapper.Map <MultiLevelC>(c)).ToList(); ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>(); var assessment = changeAssessor.SetChangeStatus(source, destination); Assert.AreEqual(assessment.OwnedEntities.Count, 0, string.Format("No changes expected, received {0}", assessment.OwnedEntities.Count)); Assert.AreEqual(source.Cs[0].ChangeType, ChangeType.None, "Change Type is set to None for C 0"); Assert.AreEqual(source.Cs[1].ChangeType, ChangeType.None, "Change Type is set to None for C 1"); }
public void ChangeAssessorTests_CirclerReferenceSubProperty_NoStackOverflow() { var source = MockDataHelper.BuildMultiLevelA("5213123"); source.B = MockDataHelper.BuildMultiLevelB("1"); source.B.A = source; var destination = Mapper.Map <MultiLevelA>(source); destination.B = MockDataHelper.BuildMultiLevelB("1"); destination.B.A = destination; //Note change results dont matter for this test, just ensure the results return. Amount checked just for //Some kind of assertion ChangeAssessor <string> changeAssessor = new ChangeAssessor <string>() //Circular ownership .AddOwnerMapping <MultiLevelA>(c => c.B) .AddOwnerMapping <MultiLevelB>(c => c.A); var assessment = changeAssessor.SetChangeStatus(source, destination); //1 updates Assert.AreEqual(1, assessment.OwnedEntities.Count, string.Format("Invalid change count, received {0}", assessment.OwnedEntities.Count)); }