public void Test_GetMirrorPropertyName_Multiple_Implicit_Sync() { TestUser user = new TestUser(); string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(user, user.GetType().GetProperty("Roles")); Assert.AreEqual("Users", mirrorPropertyName, "The mirror property name wasn't determined correctly."); }
public void Test_CreateFilePath_Entity() { TestUser user = new TestUser(); user.FirstName = "First"; user.ID = Guid.NewGuid(); string basePath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Export"; string path = new EntityFileNamer(user, basePath).CreateFilePath(); string expected = basePath + Path.DirectorySeparatorChar + user.GetType() + Path.DirectorySeparatorChar + user.ID.ToString() + ".xml"; Assert.AreEqual(expected, path, "The path doesn't match what's expected."); }
public virtual void Test_GetEntitiesByType() { using (LogGroup logGroup = LogGroup.Start("Testing a query of all entities of a specific type.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; user.Username = "******"; DataAccess.Data.Saver.Save(user); IEntity[] entities = DataAccess.Data.Indexer.GetEntities(user.GetType()); Assert.AreEqual(1, entities.Length, "Invalid number found."); } }
public void Test_Activate_2References_Async_Converging() { using (LogGroup logGroup = LogGroup.Start("Testing the Activate function with 2 asynchronous, converging references.", NLog.LogLevel.Debug)) { TestArticle article = new TestArticle(); article.ID = Guid.NewGuid(); Guid articleID = article.ID; article.Title = "Test"; TestUser user = new TestUser(); user.ID = Guid.NewGuid(); Guid userID = user.ID; user.FirstName = "First"; user.LastName = "Last"; user.Username ="******"; user.Password = "******"; /*TestArticlePage page = new TestArticlePage(); page.ID = Guid.NewGuid(); Guid pageID = page.ID; page.Title = "Test Page"; TestArticlePage page2 = new TestArticlePage(); page2.ID = Guid.NewGuid(); Guid page2ID = page2.ID; page2.Title = "Test Page 2"; */ //article.Pages = new TestArticlePage[] {page, page2}; //user.Roles = Collection<TestRole>.Add(user.Roles, role); article.Editors = new TestUser[] {user}; //DataAccess.Data.Saver.Save(page); //DataAccess.Data.Saver.Save(page2); DataAccess.Data.Saver.Save(user); DataAccess.Data.Saver.Save(article); // TODO: Check if needed //page = null; //page2 = null; //article = null; EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(article.GetType().Name, user.GetType().Name); Assert.IsNotNull(references, "references == null"); Assert.AreEqual(1, references.Count, "Invalid number of references found."); TestArticle foundArticle = DataAccess.Data.Reader.GetEntity<TestArticle>("ID", articleID); Assert.IsNotNull(foundArticle, "The foundArticle variable is null."); DataAccess.Data.Activator.Activate(foundArticle, "Editors"); DataAccess.Data.Activator.Activate(foundArticle, "Authors"); Assert.IsNotNull(foundArticle.Editors, "The article.Editors property is null."); Assert.AreEqual(1, foundArticle.Editors.Length, "article.Editors.Length != 1."); if (foundArticle.Editors != null && foundArticle.Editors.Length == 1) { TestUser foundUser1 = foundArticle.Editors[0]; // Use an array and IndexOf function so the match will work in any order Guid[] userIDs = new Guid[] {userID}; Assert.AreEqual(userID.ToString(), foundUser1.ID.ToString(), "The IDs of the user in Editors property doesn't match expected."); } Assert.AreEqual(0, foundArticle.Authors.Length, "Authors found when they shouldn't be. The two properties must be getting mixed up."); } }
public void Test_GetEntityType() { using (LogGroup logGroup = LogGroup.Start("Testing the DataUtilities.GetEntityType function.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; TestRole role = new TestRole(); role.ID = Guid.NewGuid(); role.Name = "Test Role"; user.Roles = Collection<TestRole>.Add(user.Roles, role); role.Users = Collection<TestUser>.Add(role.Users, user); PropertyInfo rolesProperty = user.GetType().GetProperty("Roles"); Type entityType = DataUtilities.GetEntityType(user, rolesProperty); Assert.AreEqual(entityType.FullName, role.GetType().FullName, "The types don't match #1."); PropertyInfo usersProperty = role.GetType().GetProperty("Users"); Type entityType2 = DataUtilities.GetEntityType(role, usersProperty); Assert.AreEqual(entityType2.FullName, user.GetType().FullName, "The types don't match #2."); } }
public void Test_GetReference_Sync_Exclude() { using (LogGroup logGroup = LogGroup.Start("Testing the GetReference function with a synchronous reference to ensure it retrieves the correct reference.", NLog.LogLevel.Debug)) { TestUtilities.CreateDummyReferences(100); TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; TestUser user2 = new TestUser(); user2.ID = Guid.NewGuid(); user2.FirstName = "Test"; user2.LastName = "User 2"; TestRole role = new TestRole(); role.ID = Guid.NewGuid(); role.Name = "Test Role"; TestRole role2 = new TestRole(); role2.ID = Guid.NewGuid(); role2.Name = "Test Role 2"; user.Roles = new TestRole[] {role}; user2.Roles = new TestRole[] { role2 }; //role2.Users = new TestUser[] {user, user2}; LogWriter.Debug("User 1 ID: " + user.ID.ToString()); LogWriter.Debug("User 2 ID: " + user2.ID.ToString()); LogWriter.Debug("Role 1 ID: " + role.ID.ToString()); LogWriter.Debug("Role 2 ID: " + role2.ID.ToString()); EntityReferenceCollection userReferences = DataAccess.Data.Referencer.GetActiveReferences(user); EntityReferenceCollection user2References = DataAccess.Data.Referencer.GetActiveReferences(user2); Assert.AreEqual(1, userReferences.Count, "userReferences.Length is incorrect"); Assert.AreEqual(1, user2References.Count, "user2References.Length is incorrect"); EntityReference originalReference1 = userReferences[0]; EntityReference originalReference2 = user2References[0]; LogWriter.Debug("Original reference - Entity 1 ID: " + originalReference1.Entity1ID.ToString()); LogWriter.Debug("Original reference - Entity 2 ID: " + originalReference1.Entity2ID.ToString()); LogWriter.Debug("Original reference - Property 1 name: " + originalReference1.Property1Name); LogWriter.Debug("Original reference - Property 2 name: " + originalReference1.Property2Name); LogWriter.Debug("Original reference - Type 1 name: " + originalReference1.Type1Name); LogWriter.Debug("Original reference - Type 2 name: " + originalReference1.Type2Name); DataAccess.Data.Saver.Save(role2); DataAccess.Data.Saver.Save(role); DataAccess.Data.Saver.Save(user); DataAccess.Data.Saver.Save(user2); string referenceStoreName = DataUtilities.GetDataStoreName("TestUser", "TestRole"); EntityReferenceCollection referenceEntities = DataAccess.Data.Referencer.GetReferences(user.GetType().Name, role.GetType().Name); Assert.AreEqual(2, referenceEntities.Count, "Incorrect number of references found in the store after saving entities."); // Switch the references around if necessary if (referenceEntities[0].Entity1ID == originalReference2.Entity1ID) { EntityReference r1 = referenceEntities[1]; EntityReference r2 = referenceEntities[0]; referenceEntities = new EntityReferenceCollection(); referenceEntities.Add(r1); referenceEntities.Add(r2); } bool firstReferenceMatches = (((referenceEntities[0].Includes(originalReference1.Entity1ID, originalReference1.Property1Name) && referenceEntities[0].Includes(originalReference1.Entity2ID, originalReference1.Property2Name)) || ((referenceEntities[0].Includes(originalReference2.Entity1ID, originalReference2.Property1Name) && referenceEntities[0].Includes(originalReference2.Entity2ID, originalReference2.Property2Name))))); bool secondReferenceMatches = (((referenceEntities[1].Includes(originalReference1.Entity1ID, originalReference1.Property1Name) && referenceEntities[1].Includes(originalReference1.Entity2ID, originalReference1.Property2Name)) || ((referenceEntities[1].Includes(originalReference2.Entity1ID, originalReference2.Property1Name) && referenceEntities[1].Includes(originalReference2.Entity2ID, originalReference2.Property2Name))))); Assert.IsTrue(firstReferenceMatches, "First reference doesn't match original references."); Assert.IsTrue(secondReferenceMatches, "Second reference doesn't match original references."); EntityReference reference = DataAccess.Data.Referencer.GetReference(user.GetType(), user.ID, "Roles", role.GetType(), role2.ID, "Users", false); Assert.IsNull(reference, "The return value should be null."); } }
public void Test_GetReference_EmptyReferencedEntityID_Found() { using (LogGroup logGroup = LogGroup.Start("Testing the retrieval of references for an entity when specifying a Guid.Empty referenced entity ID.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); Guid userID = user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; TestRole role = new TestRole(); Guid roleID = role.ID = Guid.NewGuid(); role.Name = "Test Role"; user.Roles = Collection<TestRole>.Add(user.Roles, role); DataAccess.Data.Saver.Save(role); DataAccess.Data.Saver.Save(user); EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(user.GetType(), user.ID, "Roles", typeof(TestRole), false); Assert.IsNotNull(references, "The references object returned was null."); if (references != null) { Assert.AreEqual(1, references.Count, "Wrong number of references returned."); Assert.IsTrue(references[0].Includes(userID, "Roles"), "The user ID wasn't found on the reference."); } } }
public void Test_GetReferences_2References() { using (LogGroup logGroup = LogGroup.Start("Testing the retrieval of references for an entity.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); Guid userID = user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; TestRole role = new TestRole(); Guid roleID = role.ID = Guid.NewGuid(); role.Name = "Test Role"; TestRole role2 = new TestRole(); Guid role2ID = role2.ID = Guid.NewGuid(); role2.Name = "Test Role 2 "; user.Roles = new TestRole[] {role, role2}; DataAccess.Data.Saver.Save(role); DataAccess.Data.Saver.Save(role2); DataAccess.Data.Saver.Save(user); EntityReferenceCollection referenceEntities = DataAccess.Data.Referencer.GetReferences("TestUser", "TestRole"); Assert.AreEqual(2, referenceEntities.Count, "Incorrect number of references found in the store after saving entities."); EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(user.GetType(), user.ID, "Roles", typeof(TestRole), false); Assert.IsNotNull(references, "The references object returned was null."); if (references != null) { Assert.AreEqual(2, references.Count, "Wrong number of references returned."); EntityReference reference1 = references[0]; EntityReference reference2 = references[1]; // Switch the references around if necessary to match (so they can be found in any order) if (!reference1.Includes(role.ID, "Users")) { EntityReference tmp = reference1; reference1 = reference2; reference2 = tmp; } Assert.IsTrue(reference1.Includes(userID, "Roles"), "The user ID wasn't found on the reference."); Assert.IsTrue(reference1.Includes(roleID, "Users"), "The role ID wasn't found on the reference."); Assert.IsTrue(reference2.Includes(userID, "Roles"), "The user ID wasn't found on the second reference."); Assert.IsTrue(reference2.Includes(role2ID, "Users"), "The role2 ID wasn't found on the second reference."); } } }
public TestUser CreateTestUser(string testingDirectory) { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "First"; user.LastName = "Last"; user.Username = "******"; user.Email = "*****@*****.**"; string filePath = testingDirectory + Path.DirectorySeparatorChar + user.GetType().FullName + Path.DirectorySeparatorChar + user.ID.ToString() + ".xml"; string dirPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath); using (StreamWriter writer = File.CreateText(filePath)) { XmlSerializer serializer = new XmlSerializer(typeof(TestUser)); serializer.Serialize(writer, user); writer.Close(); } return user; }
public void Test_RenewSchema() { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "--FirstName--"; user.LastName = "--LastName--"; user.Email = "--Email--"; user.Username = "******"; XmlDocument document = XmlUtilities.SerializeToDocument(user); RenamePropertyCommand command1 = new RenamePropertyCommand(); command1.PropertyName = "LastName"; command1.NewPropertyName = "Surname"; command1.TypeName = "TestUser"; DataSchemaCommandCollection commands = new DataSchemaCommandCollection(); commands.Add(command1); DataSchema schema = (DataSchema)DataAccess.Data.InitializeDataSchema(); schema.SchemaCommands = commands; schema.RenewSchema(document); TestUser user2 = (TestUser)XmlUtilities.DeserializeFromDocument(document, user.GetType()); Assert.IsNotNull(user2, "user2 == null"); Assert.AreEqual(user.ID.ToString(), user2.ID.ToString(), "The IDs don't match."); Assert.AreEqual(user.LastName, user2.Surname, "The value of the LastName wasn't moved to the Surname property like it should have."); }
public void Test_Save_2ParallelReferences() { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); Guid userID = user.ID; user.FirstName = "Test"; user.LastName = "User"; TestRole role = new TestRole(); role.ID = Guid.NewGuid(); Guid roleID = role.ID; role.Name = "Test Role"; TestUser user2 = new TestUser(); user.ID = Guid.NewGuid(); Guid user2ID = user.ID; user2.FirstName = "Test2"; user2.LastName = "User2"; TestRole role2 = new TestRole(); role2.ID = Guid.NewGuid(); Guid role2ID = role2.ID; role2.Name = "Test Role2"; user.Roles = new TestRole[] {role}; user2.Roles = new TestRole[] {role2}; DataAccess.Data.Saver.Save(role2); DataAccess.Data.Saver.Save(role); DataAccess.Data.Saver.Save(user2); DataAccess.Data.Saver.Save(user); EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(user.GetType().Name, role.GetType().Name); Assert.AreEqual(2, references.Count, "Incorrect number of references found."); EntityReference reference1 = (EntityReference)references[0]; EntityReference reference2 = (EntityReference)references[1]; // Switch the references around if necessary to match (so they can be found in any order) if (!reference1.Includes(user2.ID, "Roles")) { EntityReference tmp = reference1; reference1 = reference2; reference2 = tmp; } Assert.IsTrue(reference1.Includes(user2.ID, "Roles"), "First reference does not include expected user."); Assert.IsTrue(reference1.Includes(role2.ID, "Users"), "First reference does not include expected role."); Assert.IsTrue(reference2.Includes(user.ID, "Roles"), "Second reference does not include expected user."); Assert.IsTrue(reference2.Includes(role.ID, "Users"), "Second reference does not include expected role."); Assert.IsFalse(reference1.Includes(user.ID, "Roles"), "First reference includes unexpected user."); Assert.IsFalse(reference1.Includes(role.ID, "Users"), "First reference includes unexpected role."); Assert.IsFalse(reference2.Includes(user2.ID, "Roles"), "Second reference includes unexpected user."); Assert.IsFalse(reference2.Includes(role2.ID, "Users"), "Second reference includes unexpected role."); //Assert.AreEqual(role2.ID.ToString(), ((EntityReference)references[0]).Entity1ID.ToString(), "First reference has invalid entity 1 ID."); //Assert.AreEqual(user2.ID.ToString(), ((EntityReference)references[0]).Entity2ID.ToString(), "First reference has invalid entity 2 ID."); //Assert.AreEqual(role1.ID.ToString(), ((EntityReference)references[1]).Entity1ID.ToString(), "Second reference has invalid entity 1 ID."); //Assert.AreEqual(user1.ID.ToString(), ((EntityReference)references[1]).Entity2ID.ToString(), "Second reference has invalid entity 2 ID."); }
public void Test_Save_2References_CheckLocationOfReferencedEntities() { using (LogGroup logGroup = LogGroup.Start("Testing the Save function and checking that the references are created successfully.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); Guid userID = user.ID; user.FirstName = "Test"; user.LastName = "User"; TestRole role = new TestRole(); role.ID = Guid.NewGuid(); Guid roleID = role.ID; role.Name = "Test Role"; TestUser user2 = new TestUser(); user.ID = Guid.NewGuid(); Guid user2ID = user.ID; user2.FirstName = "Test2"; user2.LastName = "User2"; TestRole role2 = new TestRole(); role2.ID = Guid.NewGuid(); Guid role2ID = role2.ID; role2.Name = "Test Role2"; user.Roles = new TestRole[] {role}; user2.Roles = new TestRole[] {role2}; DataAccess.Data.Saver.Save(role2); DataAccess.Data.Saver.Save(role); DataAccess.Data.Saver.Save(user2); DataAccess.Data.Saver.Save(user); EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(user.GetType().Name, role.GetType().Name); Assert.AreEqual(2, references.Count, "Incorrect number of references found."); // Load the roles out of the users store (there should be none) IEntity[] rolesInUsersStore = DataAccess.Data.Stores[typeof(TestUser)].Indexer.GetEntities<TestRole>(); Assert.AreEqual(0, rolesInUsersStore.Length, "Role(s) found in users store after save."); IEntity[] rolesInRolesStore = DataAccess.Data.Stores[role.GetType()].Indexer.GetEntities<TestRole>(); Assert.AreEqual(2, rolesInRolesStore.Length, "Role(s) not found in roles store after save."); TestUser foundUser = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID); Assert.IsNotNull(foundUser, "The foundUser variable is null."); foundUser.Username = user.Username + " 2"; DataAccess.Data.Saver.Save(foundUser); // Load the roles out of the users store (there should be none) IEntity[] rolesInUsersStore2 = DataAccess.Data.Stores[typeof(TestUser)].Indexer.GetEntities<TestRole>(); Assert.AreEqual(0, rolesInUsersStore2.Length, "Role(s) found in users store after update."); IEntity[] rolesInRolesStore2 = DataAccess.Data.Stores[typeof(TestRole)].Indexer.GetEntities<TestRole>(); Assert.AreEqual(2, rolesInRolesStore2.Length, "Role(s) not found in roles store after update."); } }