public void Test_Update_InvalidEntityMustNotUpdate() { // Create the mock entity MockRequiredEntity entity = new MockRequiredEntity(); entity.TestProperty = "Test1"; entity.Validator = new ValidateStrategy(); // Save the entity SaveStrategy.New(entity, false).Save(entity); // Set the required property to empty entity.TestProperty = ""; Assert.IsFalse(entity.IsValid, "The validator returned true when it should return false."); // Update the invalid entity bool isValid = UpdateStrategy.New(entity, false).Update(entity); Assert.IsFalse(isValid, "The update strategy didn't recognise the entity as invalid."); MockRequiredEntity foundEntity = RetrieveStrategy.New <MockRequiredEntity>(false).Retrieve <MockRequiredEntity>("ID", entity.ID); Assert.IsNotNull(foundEntity); Assert.AreNotEqual(foundEntity.TestProperty, entity.TestProperty, "The entity was updated despite being invalid."); }
public void Test_Save_RemovesUnauthorisedReferences() { MockEntity entity = new MockEntity(); entity.ID = Guid.NewGuid(); MockRestrictedEntity restrictedEntity = new MockRestrictedEntity(); restrictedEntity.ID = Guid.NewGuid(); entity.RestrictedEntities = new MockRestrictedEntity[] { restrictedEntity }; SaveStrategy.New(restrictedEntity, false).Save(restrictedEntity); SaveStrategy.New(entity).Save(entity); MockEntity foundEntity = RetrieveStrategy.New <MockEntity>(false).Retrieve <MockEntity>("ID", entity.ID); Assert.IsNotNull(foundEntity); foundEntity.Activate(); Assert.AreEqual(0, foundEntity.RestrictedEntities.Length, "Restricted entity wasn't removed from reference property."); }
public void Test_Save() { TestArticle article = CreateStrategy.New <TestArticle>(false).Create <TestArticle>(); article.ID = Guid.NewGuid(); StrategyInfo info = StrategyState.Strategies["Save", "IEntity"]; ISaveStrategy strategy = SaveStrategy.New <TestArticle>(false); strategy.Save(article); TestArticle foundArticle = Data.DataAccess.Data.Reader.GetEntity <TestArticle>("ID", article.ID); Assert.IsNotNull(foundArticle); }
public void Test_Delete_Reorder() { using (LogGroup logGroup = LogGroup.StartDebug("Testing deletion of sub entities to ensure their position is updated.")) { TestArticle article = CreateStrategy.New <TestArticle>(false).Create <TestArticle>(); article.ID = Guid.NewGuid(); article.Title = "Test Article"; SaveStrategy.New(article, false).Save(article); Collection <TestArticlePage> pages = new Collection <TestArticlePage>(); // Create and save 3 article pages associated with the article for (int i = 0; i < 3; i++) { TestArticlePage page = CreateStrategy.New <TestArticlePage>(false).Create <TestArticlePage>(); page.Article = article; page.Title = "Page " + (i + 1); page.ID = Guid.NewGuid(); page.PageNumber = i + 1; pages.Add(page); SaveStrategy.New(page, false).Save(page); } // Delete the second page (0 based position = 1) DeleteStrategy.New(pages[1], false).Delete(pages[1]); // Load the article from the store TestArticle foundArticle = RetrieveStrategy.New <TestArticle>(false).Retrieve <TestArticle>("ID", article.ID); // Activate the pages on the article ActivateStrategy.New(foundArticle, false).Activate(foundArticle, "Pages"); Assert.IsNotNull(foundArticle.Pages, "Pages property isn't set."); Assert.AreEqual(2, foundArticle.Pages.Length, "Invalid number of pages."); foundArticle.Pages = Collection <TestArticlePage> .Sort(foundArticle.Pages, "PageNumberAscending"); Assert.AreEqual(1, foundArticle.Pages[0].PageNumber, "First page has wrong number."); Assert.AreEqual("Page 1", foundArticle.Pages[0].Title, "First page has wrong title."); Assert.AreEqual(2, foundArticle.Pages[1].PageNumber, "Third page has wrong number (should now be 2 as it's moved up one spot)."); Assert.AreEqual("Page 3", foundArticle.Pages[1].Title, "Third page has wrong title."); // Page 3 should now be at position 1 (ie. second place) } }
public void Test_Create_Parent() { MockProjection projection = new MockProjection(); projection.RequireAuthorisation = false; projection.Command = new CreateComandInfo(typeof(TestArticlePage).Name); TestArticle article = CreateStrategy.New <TestArticle>(false).Create <TestArticle>(); article.ID = Guid.NewGuid(); article.Title = "Test title"; SaveStrategy.New <TestArticle>(false).Save(article); CreateController controller = CreateController.New(projection); Assert.IsTrue(controller is CreateSubEntityController, "Invalid controller type: " + controller.GetType().ToString()); CreateSubEntityController createSubEntityController = (CreateSubEntityController)controller; ISubEntity subEntity = (ISubEntity)createSubEntityController.Create(article.ID, String.Empty); Assert.IsNotNull(subEntity, "No sub entity returned."); Assert.IsTrue((subEntity is TestArticlePage), "Wrong type returned: " + subEntity.GetType().ToString()); Assert.IsNotNull(subEntity.Parent, "No parent assigned to sub entity."); Assert.AreEqual(article.ID.ToString(), subEntity.Parent.ID.ToString(), "Parent ID doesn't match expected."); Assert.AreEqual(1, subEntity.Number, "Sub entity has wrong number."); SaveStrategy.New(subEntity, false).Save(subEntity); ISubEntity subEntity2 = (ISubEntity)createSubEntityController.Create(Guid.Empty, article.UniqueKey); Assert.IsNotNull(subEntity2, "No sub entity returned."); Assert.IsTrue((subEntity2 is TestArticlePage), "Wrong type returned: " + subEntity.GetType().ToString()); Assert.IsNotNull(subEntity2.Parent, "No parent assigned to sub entity."); Assert.AreEqual(article.ID.ToString(), subEntity2.Parent.ID.ToString(), "Parent ID doesn't match expected."); Assert.AreEqual(2, subEntity2.Number, "Sub entity has wrong number."); }
public void Test_Update_AutoActivateReferences() { // Create the mock entities TestUser user = CreateStrategy.New <TestUser>(false).Create <TestUser>(); user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; TestRole role = CreateStrategy.New <TestRole>(false).Create <TestRole>(); role.ID = Guid.NewGuid(); role.Name = "Test Role"; // Assign the user to the role user.Roles = new TestRole[] { role }; // Save the entities SaveStrategy.New(role, false).Save(role); SaveStrategy.New(user, false).Save(user); // Retrieve the mock user from the data store TestUser foundUser = RetrieveStrategy.New <TestUser>(false).Retrieve <TestUser>("ID", user.ID); // Change a standard property value foundUser.FirstName = "Test2"; // Update WITHOUT having activated the user manually // This update should automatically activate the user entity before updating and // should therefore persist the references UpdateStrategy.New(foundUser, false).Update(foundUser); // Retrieve the mock user again TestUser foundUser2 = RetrieveStrategy.New <TestUser>(false).Retrieve <TestUser>("ID", user.ID); // Manually activate the user foundUser2.Activate(); // Assert that the referenced roles are found on the user which indicates // that the update strategy did automatically activate the entity and persist // the references Assert.IsNotNull(foundUser2.Roles, "Roles property is null."); Assert.AreEqual(1, foundUser2.Roles.Length, "Invalid number of roles assigned to user."); }
public void Test_Create_FromParentID() { TestArticle article = CreateStrategy.New <TestArticle>(false).Create <TestArticle>(); article.ID = Guid.NewGuid(); article.Title = "Test Title"; SaveStrategy.New <TestArticle>(false).Save(article); CreateSubEntityStrategy strategy = (CreateSubEntityStrategy)CreateStrategy.New("TestArticlePage", false); TestArticlePage page = strategy.Create <TestArticlePage>(article.ID, String.Empty); Assert.IsNotNull(page.Article, "The article wasn't assigned to the page."); Assert.AreEqual(article.Title, page.Article.Title, "Article titles don't match."); Assert.AreEqual(1, page.PageNumber, "Invalid page number"); }
public void Test_Save_InvalidEntityMustNotSave() { // Create the mock entity MockInvalidEntity entity = new MockInvalidEntity(); entity.Validator = new ValidateStrategy(); Assert.IsFalse(entity.IsValid, "The validator returned true when it should return false."); // Try to save the entity bool isValid = SaveStrategy.New(entity, false).Save(entity); // Ensure that the save was rejected Assert.IsFalse(isValid, "The save strategy didn't recognise the entity as invalid."); // Try loading the entity from the data store to see if it's found MockInvalidEntity foundEntity = RetrieveStrategy.New <MockInvalidEntity>(false).Retrieve <MockInvalidEntity>("ID", entity.ID); // Ensure the entity wasn't found and therefore wasn't saved Assert.IsNull(foundEntity, "The entity was found in the store even though it shouldn't have saved."); }
public void Test_GetPostedEntities_AutoLoadPostEntitiesFalse() { TestRecord record = new TestRecord(); record.ID = Guid.NewGuid(); EntitySelect <TestRecord> ctrl = new EntitySelect <TestRecord>(); ctrl.AutoLoadPostEntities = false; ctrl.EntityType = record.GetType().FullName + ", " + typeof(TestRecord).Assembly.GetName().Name; ctrl.RequireAuthorisation = false; // Only false during testing Guid[] ids = new Guid[] { record.ID }; SaveStrategy.New(record, false).Save(record); IEntity[] entities = ctrl.GetPostedEntities(ids); Assert.IsNotNull(entities, "Returned null"); // No entities should be returned because they weren't found on DataSource Assert.AreEqual(0, entities.Length, "Invalid number of entities returned."); }
public void Test_GetPostedEntities_AutoLoadPostEntitiesTrue() { TestRecord record = new TestRecord(); record.ID = Guid.NewGuid(); EntitySelect <TestRecord> ctrl = new EntitySelect <TestRecord>(); ctrl.AutoLoadPostEntities = true; ctrl.EntityType = record.GetType().FullName + ", " + typeof(TestRecord).Assembly.GetName().Name; ctrl.RequireAuthorisation = false; // Only false during testing Guid[] ids = new Guid[] { record.ID }; SaveStrategy.New(record, false).Save(record); IEntity[] entities = ctrl.GetPostedEntities(ids); Assert.IsNotNull(entities, "Returned null"); // The entity should have been loaded automatically Assert.AreEqual(1, entities.Length, "Invalid number of entities returned."); }
public void Test_Update_InactiveEntityCausesException() { // Create the mock entity TestUser user = CreateStrategy.New <TestUser>(false).Create <TestUser>(); user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; // Save the entity SaveStrategy.New(user, false).Save(user); // Change a standard property value user.FirstName = "Test2"; // Set AutoActivate to false otherwise it'll auto activate and won't be tested properly user.AutoActivate = false; // Set IsActivated to false user.IsActivated = false; // Update the inactive entity UpdateStrategy.New(user, false).Update(user); }