public async Task NullableInt() { var em = await TestFns.NewEm(_serviceName); var emp = new Employee() { FirstName = "Test", LastName = "Test" }; em.AddEntity(emp); Assert.IsTrue(emp.ReportsToEmployeeID == null, "fk should be null"); var sr = await em.SaveChanges(); Assert.IsTrue(sr.Entities.Count == 1); Assert.IsTrue(emp.ReportsToEmployeeID == null, "fk should still be null"); emp.ReportsToEmployeeID = 0; Assert.IsTrue(emp.ReportsToEmployeeID == 0, "fk should be 0"); try { var sr2 = await em.SaveChanges(); Assert.Fail("should not get here"); } catch (SaveException e) { Assert.IsTrue(e.Message.Contains("constraint"), "error should mention fk constraint violation"); } emp.EntityAspect.Delete(); var sr3 = await em.SaveChanges(); Assert.IsTrue(sr3.Entities.Count == 1); }
public async Task INotifyDataErrorInfo() { var em1 = await TestFns.NewEm(_serviceName); var emp = new Employee(); var inde = (INotifyDataErrorInfo) emp; Assert.IsTrue(!inde.HasErrors); var eventArgsList = new List<DataErrorsChangedEventArgs>(); inde.ErrorsChanged += (s, e) => { eventArgsList.Add(e); }; em1.AttachEntity(emp); Assert.IsTrue(eventArgsList.Count == 2); // firstName, lastName // magicString var fnErrors = inde.GetErrors(EntityAspect.AllErrors).Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 2); Assert.IsTrue( fnErrors.All(err => err.Context.PropertyPath == "LastName" || err.Context.PropertyPath == "FirstName")); fnErrors = inde.GetErrors("FirstName").Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 1); Assert.IsTrue(inde.HasErrors); emp.FirstName = "test"; Assert.IsTrue(eventArgsList.Count == 3); fnErrors = inde.GetErrors(EntityAspect.AllErrors).Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 1); fnErrors = inde.GetErrors("FirstName").Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 0); Assert.IsTrue(inde.HasErrors); emp.FirstName = "a very long name that exceeds the valid length of the field" + ".".PadRight(40); Assert.IsTrue(eventArgsList.Count == 4); fnErrors = inde.GetErrors(EntityAspect.AllErrors).Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 2); fnErrors = inde.GetErrors("FirstName").Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 1); Assert.IsTrue(inde.HasErrors); emp.FirstName = "xxx"; emp.LastName = "yyy"; Assert.IsTrue(eventArgsList.Count == 6); fnErrors = inde.GetErrors(EntityAspect.AllErrors).Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 0); fnErrors = inde.GetErrors("FirstName").Cast<ValidationError>(); Assert.IsTrue(fnErrors.Count() == 0); Assert.IsTrue(!inde.HasErrors); }
public async Task HasChangesChangedAfterSave() { await _emTask; var hccArgs = new List<EntityManagerHasChangesChangedEventArgs>(); _em1.HasChangesChanged += (s, e) => { hccArgs.Add(e); }; var emp = new Employee() { FirstName = "Test_Fn", LastName = "Test_Ln" }; _em1.AddEntity(emp); Assert.IsTrue(hccArgs.Count == 1); Assert.IsTrue(hccArgs.Last().HasChanges == true); Assert.IsTrue(_em1.HasChanges()); var sr = await _em1.SaveChanges(); Assert.IsTrue(sr.Entities.Count == 1); Assert.IsTrue(hccArgs.Count == 2); Assert.IsTrue(hccArgs.Last().HasChanges == false); Assert.IsTrue(_em1.HasChanges() == false); }
public async Task RejectChangesFkFixup() { var em1 = await TestFns.NewEm(_serviceName); var emp1 = new Employee(); em1.AttachEntity(emp1); var emp2 = em1.CreateEntity<Employee>(); var order1 = new Order(); emp1.Orders.Add(order1); Assert.IsTrue(order1.EntityAspect.EntityState.IsAdded()); Assert.IsTrue(order1.Employee == emp1); order1.EntityAspect.AcceptChanges(); // move the order; emp2.Orders.Add(order1); Assert.IsTrue(emp1.Orders.Count == 0); Assert.IsTrue(emp2.Orders.Count == 1); Assert.IsTrue(order1.Employee == emp2); Assert.IsTrue(order1.EntityAspect.EntityState.IsModified()); order1.EntityAspect.RejectChanges(); Assert.IsTrue(order1.Employee == emp1); Assert.IsTrue(emp1.Orders.Count == 1); Assert.IsTrue(emp2.Orders.Count == 0); }
public async Task ExpImpTempKeyFixup2() { var em1 = await TestFns.NewEm(_serviceName); var q = new EntityQuery<Foo.Supplier>("Suppliers").Where(s => s.CompanyName.StartsWith("P")); var suppliers = await q.Execute(em1); Assert.IsTrue(suppliers.Count() > 0, "should be some suppliers"); var orderIdProp = MetadataStore.Instance.GetEntityType("Order").KeyProperties[0]; em1.KeyGenerator.GetNextTempId(orderIdProp); var order1 = new Order(); var emp1 = new Employee(); em1.AddEntity(order1); em1.AddEntity(emp1); emp1.LastName = "bar"; var cust1 = new Customer() { CompanyName = "Foo" }; order1.Employee = emp1; order1.Customer = cust1; var exportedEm = em1.ExportEntities(null, false); var em2 = new EntityManager(em1); em2.ImportEntities(exportedEm); var suppliers2 = em2.GetEntities<Supplier>().ToList(); Assert.IsTrue(suppliers.Count() == suppliers2.Count, "should be the same number of suppliers"); var addedOrders = em2.GetEntities<Order>(EntityState.Added); Assert.IsTrue(addedOrders.Count() == 1, "should be 1 added order"); var order1x = addedOrders.First(); var cust1x = order1x.Customer; Assert.IsTrue(cust1x.CompanyName == "Foo", "customer company name should be 'Foo'"); var emp1x = order1x.Employee; Assert.IsTrue(emp1x.LastName == "bar", "lastName should be 'bar'"); }
public async Task ExpImpTempKeyFixup1() { var em1 = await TestFns.NewEm(_serviceName); var q = new EntityQuery<Foo.Employee>("Employees").Take(3); var results = await q.Execute(em1); Assert.IsTrue(results.Count() > 0); var emp1 = new Employee(); var order1 = new Order(); var order2 = new Order(); em1.AddEntity(emp1); emp1.Orders.Add(order1); emp1.Orders.Add(order2); var exportedEntities = em1.ExportEntities(null, false); // custs1 and 2 shouldn't be imported because of default preserveChanges // emps1 and 2 should cause the creation of NEW emps with new temp ids; // tempKeys should cause creation of new entities; var impResult = em1.ImportEntities(exportedEntities); var allEntities = em1.GetEntities(); Assert.IsTrue(allEntities.Count() == 9, "should have 9 (3 orig, 3 added, 3 imported (new) entities in the cache"); Assert.IsTrue(allEntities.OfType<Order>().Count() == 4, "should be 4 orders (2 + 2)"); Assert.IsTrue(allEntities.OfType<Employee>().Count() == 5, "should be 5 emps (3 + 1 + 1) "); Assert.IsTrue(allEntities.OfType<Employee>().Count(c => c.EntityAspect.EntityState.IsAdded()) == 2, "should only be 2 added emps"); Assert.IsTrue(allEntities.OfType<Order>().All(c => c.EntityAspect.EntityState.IsAdded())); Assert.IsTrue(impResult.ImportedEntities.Count == 6, "should have imported 6 entities - 3 orig + 3 new"); Assert.IsTrue(impResult.ImportedEntities.OfType<Order>().Count() == 2, "should have imported 2 orders"); Assert.IsTrue(impResult.ImportedEntities.OfType<Employee>().Count(e => e.EntityAspect.EntityState.IsAdded()) == 1, "should have imported 1 added emp"); Assert.IsTrue(impResult.ImportedEntities.OfType<Employee>().Count(e => e.EntityAspect.EntityState.IsUnchanged()) == 3, "should have imported 3 unchanged emps"); Assert.IsTrue(impResult.TempKeyMap.Count == 3, "tempKeyMap should be of length 3"); Assert.IsTrue(impResult.TempKeyMap.All(kvp => kvp.Key != kvp.Value), "imported entities should not have same key values"); var newOrders = impResult.ImportedEntities.OfType<Order>(); var newEmp = impResult.ImportedEntities.OfType<Employee>().First(e => e.EntityAspect.EntityState.IsAdded()); Assert.IsTrue(newOrders.All(no => no.EmployeeID == newEmp.EmployeeID), "should have updated order empId refs"); }
public async Task AttachRecursive() { await _emTask; var emp1 = new Employee(); var emp2 = new Employee(); var emp3 = new Employee(); emp2.Manager = emp1; emp3.Manager = emp2; _em1.AttachEntity(emp3); Assert.IsTrue(emp3.EntityAspect.IsAttached); Assert.IsTrue(emp2.EntityAspect.IsAttached); Assert.IsTrue(emp1.EntityAspect.IsAttached); Assert.IsTrue(emp1.DirectReports.Contains(emp2), "emp1 manages emp2"); Assert.IsTrue(emp2.DirectReports.Contains(emp3), "emp2 manages emp3"); Assert.IsTrue(emp2.Manager == emp1, "emp2 manager is emp1"); Assert.IsTrue(emp3.Manager == emp2, "emp3 mamager is emp2"); }
public async Task ChangeMessageBaseAndAssembly() { var em1 = await TestFns.NewEm(_serviceName); var emp = new Employee(); var vr = new RequiredValidator().WithMessage("Model.NorthwindIB.CustomMessages2", typeof (Employee).Assembly); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var vc = new ValidationContext(emp, dp, null); var ve = vr.Validate(vc); Assert.IsTrue(ve != null); Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("required") && ve.Message.Contains("CUSTOM 2")); Assert.IsTrue(ve.Context.Entity == emp); Assert.IsTrue(ve.Validator == vr); Assert.IsTrue(ve.Key != null); }
public async Task OnAttach() { var em1 = await TestFns.NewEm(_serviceName); var emp = new Employee(); Assert.IsTrue(!emp.EntityAspect.GetValidationErrors().Any(), "should not be any validation errors"); em1.AddEntity(emp); var ves = emp.EntityAspect.GetValidationErrors(); Assert.IsTrue(ves.Any(), "should be some now"); Assert.IsTrue(ves.Any(ve => ve.Message.Contains("LastName") && ve.Message.Contains("required"))); Assert.IsTrue(ves.All(ve => ve.Context.Entity == emp)); Assert.IsTrue(ves.Any(ve => ve.Validator.Equals(new RequiredValidator().Intern())), "validator should be a requiredValdator. "); Assert.IsTrue(ves.All(ve => ve.Key != null)); }
public async Task RequiredProperty2() { var em1 = await TestFns.NewEm(_serviceName); using (TestFns.ShareWithDetached(em1.MetadataStore)) { var emp = new Employee(); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var ves = emp.EntityAspect.ValidateProperty(dp); Assert.IsTrue(ves.Count() > 0); var ve1 = ves.First(); Assert.IsTrue(ve1.Message.Contains("LastName") && ve1.Message.Contains("required")); Assert.IsTrue(ve1.Context.Entity == emp); Assert.IsTrue(ves.Any(ve => ve.Validator.Equals(new RequiredValidator().Intern())), "validator should be a requiredValdator. "); Assert.IsTrue(ve1.Key != null); } }
public async Task DeleteFromNavCollection2() { var em1 = await TestFns.NewEm(_serviceName); var emp1 = new Employee(); var et1 = new EmployeeTerritory(); var et2 = new EmployeeTerritory(); var ter1 = new Territory() { TerritoryID = 11 }; var ter2 = new Territory() { TerritoryID = 22 }; em1.AddEntity(emp1); //em1.AddEntity(ter1); //em1.AddEntity(ter2); //em1.AddEntity(et1); //em1.AddEntity(et2); et1.Territory = ter1; et2.Territory = ter2; var empterrs = emp1.EmployeeTerritories; empterrs.Add(et1); empterrs.Add(et2); em1.AcceptChanges(); Assert.IsTrue(et1.Employee == emp1, "Employee is attached to EmployeeTerritory"); Assert.IsTrue(emp1.EmployeeTerritories.Count == 2, "Employee has EmployeeTerritories"); Assert.IsTrue(et1.Territory == ter1, "Territory is attached to EmployeeTerritory"); Assert.IsTrue(ter1.EmployeeTerritories.Count == 1, "Territory has EmployeeTerritories"); var collectionChangedList = new List<NotifyCollectionChangedEventArgs>(); empterrs.CollectionChanged += (s, e) => { collectionChangedList.Add(e); }; var propChangedList = new List<PropertyChangedEventArgs>(); ((INotifyPropertyChanged)et1).PropertyChanged += (s, e) => { propChangedList.Add(e); }; var entityChangedList = new List<EntityChangedEventArgs>(); em1.EntityChanged += (s, e) => { entityChangedList.Add(e); }; et1.EntityAspect.Delete(); Assert.IsTrue(collectionChangedList.Last().Action == NotifyCollectionChangedAction.Remove, "should have removed from collection"); Assert.IsTrue(collectionChangedList.Last().OldItems.Contains(et1), "change event should contain et1"); //Assert.IsTrue(propChangedList.Count == 0, "Deleting an entity will not create a propertyChange event"); Assert.IsTrue(!empterrs.Contains(et1), "et1 should have been removed"); Assert.IsTrue(et1.Employee == null, "Employee should be null"); Assert.IsTrue(et1.Territory == null, "Territory should be null"); Assert.IsTrue(et1.EmployeeID == emp1.EmployeeID, "EmployeeID should NOT be cleared when detached - just the Employee"); Assert.IsTrue(et1.TerritoryID == ter1.TerritoryID, "TerritoryID should NOT be cleared when detached - just the Territory"); Assert.IsTrue(et1.EntityAspect.EntityState.IsDeleted(), "state should be deleted"); Assert.IsTrue(empterrs.Count == 1, "count should be 1"); collectionChangedList.Clear(); propChangedList.Clear(); et1.EntityAspect.RejectChanges(); Assert.IsTrue(collectionChangedList.Last().Action == NotifyCollectionChangedAction.Add, "should have added to collection"); Assert.IsTrue(collectionChangedList.Last().NewItems.Contains(et1), "change event should contain employeeTerritory"); Assert.IsTrue(propChangedList.Any(args => args.PropertyName == "Employee"), "propChange should mention Employee"); Assert.IsTrue(propChangedList.Any(args => args.PropertyName == "Territory"), "propChange should mention Territory"); // Not needed because CustomerID is not cleared. // Assert.IsTrue(propChangedList.Any(args => args.PropertyName == "CustomerID"), "propChange should mention CustomerID"); Assert.IsTrue(empterrs.Contains(et1), "et1 should be back"); Assert.IsTrue(et1.Employee == emp1, "Employee should be back"); Assert.IsTrue(et1.Territory == ter1, "Territory should be back"); Assert.IsTrue(et1.EmployeeID == emp1.EmployeeID, "EmployeeID should not have changed"); // null because not required. Assert.IsTrue(et1.EntityAspect.EntityState.IsUnchanged(), "State should be unchanged but is " + et1.EntityAspect.EntityState); Assert.IsTrue(empterrs.Count == 2, "count should be 2"); }
public async Task AttachRecursive() { var em1 = await TestFns.NewEm(_serviceName); using (TestFns.ShareWithDetached(em1.MetadataStore)) { var emp1 = new Employee(); var emp2 = new Employee(); var emp3 = new Employee(); emp2.Manager = emp1; emp3.Manager = emp2; em1.AddEntity(emp3); Assert.IsTrue(emp3.EntityAspect.IsAttached); Assert.IsTrue(emp2.EntityAspect.IsAttached); Assert.IsTrue(emp1.EntityAspect.IsAttached); Assert.IsTrue(emp1.DirectReports.Contains(emp2), "emp1 manages emp2"); Assert.IsTrue(emp2.DirectReports.Contains(emp3), "emp2 manages emp3"); Assert.IsTrue(emp2.Manager == emp1, "emp2 manager is emp1"); Assert.IsTrue(emp3.Manager == emp2, "emp3 manager is emp2"); } }
public async Task ChangeMessageBaseAndAssembly() { await _emTask; var emp = new Employee(); var vr = new RequiredValidator().WithMessage("Model_Northwind_NetClient.CustomMessages2", typeof(Employee).Assembly); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var vc = new ValidationContext(emp, null, dp); var ve = vr.Validate(vc); Assert.IsTrue(ve != null); Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("required") && ve.Message.Contains("CUSTOM 2")); Assert.IsTrue(ve.Context.Instance == emp); Assert.IsTrue(ve.Validator == vr); Assert.IsTrue(ve.Key != null); }
public async Task OnAttach() { await _emTask; var emp = new Employee(); Assert.IsTrue(!emp.EntityAspect.GetValidationErrors().Any(), "should not be any validation errors"); _em1.AddEntity(emp); var ves = emp.EntityAspect.GetValidationErrors(); Assert.IsTrue(ves.Any(), "should be some now"); Assert.IsTrue(ves.Any(ve => ve.Message.Contains("LastName") && ve.Message.Contains("required"))); Assert.IsTrue(ves.All(ve => ve.Context.Instance == emp)); Assert.IsTrue(ves.Any(ve => ve.Validator == new RequiredValidator().Intern()), "validator should a requiredValdator"); Assert.IsTrue(ves.All(ve => ve.Key != null)); }
public async Task NullableProperty() { var em1 = await TestFns.NewEm(_serviceName); var emp = new Employee() { FirstName = "Test Joe", LastName = "Test Smith" , BirthDate = DateTime.Now }; Assert.IsTrue(emp.BirthDate != null); em1.AddEntity(emp); Assert.IsTrue(emp.BirthDate != null); emp.BirthDate = null; var sr = await em1.SaveChanges(new IEntity[] { emp }); Assert.IsTrue(sr.Entities.Count == 1); Assert.IsTrue(sr.Entities.First() == emp); Assert.IsTrue(emp.BirthDate == null); emp.EntityAspect.Delete(); var sr2 = await em1.SaveChanges(); Assert.IsTrue(sr.Entities.Count == 1); Assert.IsTrue(em1.GetEntities().Count() == 0); }
public async Task DeleteWithoutQuery() { var em1 = await TestFns.NewEm(_serviceName); var emp = new Employee(); emp.FirstName = "Test Fn"; emp.LastName = "Test Fn"; em1.AddEntity(emp); var sr = await em1.SaveChanges(); Assert.IsTrue(sr.Entities.Count == 1, "should have saved 1 entity"); Assert.IsTrue(sr.Entities.First() == emp, "should be the same emp"); var em2 = new EntityManager(em1); em2.ImportEntities(em1.ExportEntities()); var sameEmp = (Employee) em2.GetEntities().First(); Assert.IsTrue(emp.EntityAspect.EntityKey == sameEmp.EntityAspect.EntityKey, "should have the same key"); sameEmp.EntityAspect.Delete(); Assert.IsTrue(sameEmp.EntityAspect.EntityState.IsDeleted()); var sr2 = await em2.SaveChanges(); Assert.IsTrue(sr2.Entities.First() == sameEmp, "should still be the same emp"); Assert.IsTrue(sameEmp.EntityAspect.EntityState.IsDetached()); Assert.IsTrue(em2.GetEntities().Count() == 0, "should be no entities"); }
public async Task RequiredProperty2() { await _emTask; var emp = new Employee(); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var ves = emp.EntityAspect.ValidateProperty(dp); Assert.IsTrue(ves.Count() > 0); var ve = ves.First(); Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("required")); Assert.IsTrue(ve.Context.Entity == emp); Assert.IsTrue(ve.Validator == new RequiredValidator().Intern(), "validator should a requiredValdator"); Assert.IsTrue(ve.Key != null); }
public async Task EntireEntity() { var em1 = await TestFns.NewEm(_serviceName); using (TestFns.ShareWithDetached(em1.MetadataStore)) { var emp = new Employee(); var ves = emp.EntityAspect.Validate(); Assert.IsTrue(ves.Count() == 2, "should only be 2 emp validation errors here"); Assert.IsTrue(ves.Any(ve => ve.Message.Contains("LastName") && ve.Message.Contains("required"))); Assert.IsTrue(ves.All(ve => ve.Context.Entity == emp)); var msg = ves.Select(ve => ve.Validator.GetType() + " " + ve.Key + " " + (ve.Validator.Equals(new RequiredValidator().Intern())).ToString() ).ToAggregateString(" ,"); Assert.IsTrue(ves.Any(ve => ve.Validator.Equals(new RequiredValidator().Intern())), "validator should be a requiredValdator. " + msg); Assert.IsTrue(ves.All(ve => ve.Key != null)); } }
public async Task EntireEntity() { await _emTask; var emp = new Employee(); var ves = emp.EntityAspect.Validate(); Assert.IsTrue(ves.Count() > 0); Assert.IsTrue(ves.Any(ve => ve.Message.Contains("LastName") && ve.Message.Contains("required"))); Assert.IsTrue(ves.All(ve => ve.Context.Entity == emp)); Assert.IsTrue(ves.Any(ve => ve.Validator == new RequiredValidator().Intern()), "validator should a requiredValdator"); Assert.IsTrue(ves.All(ve => ve.Key != null)); }
public async Task ChangeMessageString() { var em1 = await TestFns.NewEm(_serviceName); var emp = new Employee(); var vr = new RequiredValidator().WithMessage("{0} is bad"); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var vc = new ValidationContext(emp, dp, null); var ve = vr.Validate(vc); Assert.IsTrue(ve != null); Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("bad")); Assert.IsTrue(ve.Context.Entity == emp); Assert.IsTrue(ve.Validator == vr); Assert.IsTrue(ve.Key != null); }
public async Task ChangeMessageResourceType() { await _emTask; var emp = new Employee(); var vr = new RequiredValidator().WithMessage(typeof(Model_Northwind_NetClient.CustomMessages1)); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var vc = new ValidationContext(emp, dp, null); var ve = vr.Validate(vc); Assert.IsTrue(ve != null); Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("required") && ve.Message.Contains("CUSTOM 1")); Assert.IsTrue(ve.Context.Entity == emp); Assert.IsTrue(ve.Validator == vr); Assert.IsTrue(ve.Key != null); }
public async Task RequiredProperty() { await _emTask; var emp = new Employee(); var vr = new RequiredValidator(); var dp = emp.EntityAspect.EntityType.GetDataProperty("LastName"); var vc = new ValidationContext(emp, dp, null); var ve = vr.Validate(vc); Assert.IsTrue(ve != null); Assert.IsTrue(ve.Message.Contains("LastName") && ve.Message.Contains("required")); Assert.IsTrue(ve.Context.Entity == emp); Assert.IsTrue(ve.Validator == vr); Assert.IsTrue(ve.Key != null); }
public async Task RemovingNavigationProperty() { var entityManager = await TestFns.NewEm(_serviceName); var employee = new Employee() { FirstName = "First", LastName = "Employee" }; entityManager.AddEntity(employee); var manager = new Employee() { FirstName = "First", LastName = "Manager" }; entityManager.AddEntity(manager); employee.Manager = manager; try { var saveResult = await entityManager.SaveChanges(); // Now reverse everything manager.EntityAspect.Delete(); employee.Manager = null; employee.EntityAspect.Delete(); saveResult = await entityManager.SaveChanges(); } catch (Exception e) { var message = string.Format("Save should have succeeded; Received {0}: {1}", e.GetType().Name, e.Message); Assert.Fail(message); } }