public void ShouldCorrectlySetEntityStatesForAddedObjectGraph() { //arrange DataFormSettingsDescriptor formDescriptor = Descriptors.DepartmentForm; DepartmentModel departmentModel = null; ObservableCollection <IValidatable> modifiedProperties = CreateValidatablesFormSettings(formDescriptor, typeof(DepartmentModel)); IDictionary <string, IValidatable> propertiesDictionary = modifiedProperties.ToDictionary(property => property.Name); propertiesDictionary["DepartmentID"].Value = 1; propertiesDictionary["Name"].Value = "Mathematics"; propertiesDictionary["Budget"].Value = 100000m; propertiesDictionary["StartDate"].Value = new DateTime(2021, 5, 20); propertiesDictionary["InstructorID"].Value = 1; propertiesDictionary["Courses"].Value = new ObservableCollection <CourseModel> ( new List <CourseModel> { new CourseModel { CourseID = 1, Credits = 3, Title = "Trigonometry" }, new CourseModel { CourseID = 2, Credits = 4, Title = "Physics" }, new CourseModel { CourseID = 4, Credits = 5, Title = "Algebra" } } ); DepartmentModel currentDepartment = serviceProvider.GetRequiredService <IEntityStateUpdater>().GetUpdatedModel ( departmentModel, departmentModel.EntityToObjectDictionary ( serviceProvider.GetRequiredService <IMapper>(), formDescriptor.FieldSettings ), modifiedProperties, formDescriptor.FieldSettings ); Assert.Equal(LogicBuilder.Domain.EntityStateType.Added, currentDepartment.EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Added, currentDepartment.Courses.Single(c => c.CourseID == 1).EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Added, currentDepartment.Courses.Single(c => c.CourseID == 2).EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Added, currentDepartment.Courses.Single(c => c.CourseID == 4).EntityState); Assert.Equal(3, currentDepartment.Courses.Count); }
public void ShouldCorrectlySetEntityStatesForChileFormGroupArray() { //arrange EditFormSettingsDescriptor formDescriptor = Descriptors.DepartmentForm; DepartmentModel departmentModel = new DepartmentModel { DepartmentID = 1, Name = "Mathematics", Budget = 100000m, StartDate = new DateTime(2021, 5, 20), InstructorID = 1, Courses = new List <CourseModel> { new CourseModel { CourseID = 1, Credits = 3, Title = "Trigonometry" }, new CourseModel { CourseID = 2, Credits = 4, Title = "Physics" }, new CourseModel { CourseID = 3, Credits = 5, Title = "Calculus" } } }; ObservableCollection <IValidatable> modifiedProperties = CreateValidatablesFromSettings(formDescriptor); IDictionary <string, IValidatable> propertiesDictionary = modifiedProperties.ToDictionary(property => property.Name); propertiesDictionary["DepartmentID"].Value = 1; propertiesDictionary["Name"].Value = "Mathematics"; propertiesDictionary["Budget"].Value = 100000m; propertiesDictionary["StartDate"].Value = new DateTime(2021, 5, 20); propertiesDictionary["InstructorID"].Value = 1; propertiesDictionary["Courses"].Value = new ObservableCollection <CourseModel> ( new List <CourseModel> { new CourseModel { CourseID = 1, Credits = 3, Title = "Trigonometry" }, new CourseModel { CourseID = 2, Credits = 4, Title = "Physics" }, new CourseModel { CourseID = 4, Credits = 5, Title = "Algebra" } } ); IMapper mapper = serviceProvider.GetRequiredService <IMapper>(); Dictionary <string, object> existing = departmentModel.EntityToObjectDictionary ( mapper, formDescriptor.FieldSettings ); Dictionary <string, object> current = modifiedProperties.ValidatableListToObjectDictionary ( mapper, formDescriptor.FieldSettings ); EntityMapper.UpdateEntityStates ( existing, current, formDescriptor.FieldSettings ); DepartmentModel currentDepartment = mapper.Map <DepartmentModel>(current); Assert.Equal(LogicBuilder.Domain.EntityStateType.Modified, currentDepartment.EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Unchanged, currentDepartment.Courses.Single(c => c.CourseID == 1).EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Unchanged, currentDepartment.Courses.Single(c => c.CourseID == 2).EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Deleted, currentDepartment.Courses.Single(c => c.CourseID == 3).EntityState); Assert.Equal(LogicBuilder.Domain.EntityStateType.Added, currentDepartment.Courses.Single(c => c.CourseID == 4).EntityState); Assert.Equal(4, currentDepartment.Courses.Count); }