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_GetEntitiesByFilterGroup_EmptyGroup() { using (LogGroup logGroup = LogGroup.Start("Testing a simple query with the PropertyFilter.", NLog.LogLevel.Debug)) { TestUser user = new TestUser(); user.ID = Guid.NewGuid(); user.FirstName = "Test"; user.LastName = "User"; user.Username = "******"; TestRole role = new TestRole(); role.ID = Guid.NewGuid(); role.Name = "Test role"; user.Roles = new TestRole[] { role }; DataAccess.Data.Saver.Save(role); DataAccess.Data.Saver.Save(user); ReferenceFilter filter = (ReferenceFilter)DataAccess.Data.CreateReferenceFilter(); filter.PropertyName = "Roles"; filter.ReferencedEntityID = role.ID; filter.ReferenceType = role.GetType(); filter.AddType(typeof(TestUser)); PropertyFilter filter2 = (PropertyFilter)DataAccess.Data.CreatePropertyFilter(); filter2.PropertyName = "Username"; filter2.PropertyValue = user.Username; filter2.AddType(typeof(TestUser)); FilterGroup filterGroup = new FilterGroup(); // Leave empty by not adding filters //filterGroup.Add(filter); //filterGroup.Add(filter2); IEntity[] found = (IEntity[])DataAccess.Data.Indexer.GetEntities(filterGroup); Assert.IsNotNull(found, "Null value returned"); if (found != null) { Assert.IsTrue(found.Length > 0, "No results found."); if (found.Length > 0) { TestUser foundUser = (TestUser)found[0]; Assert.IsNotNull(foundUser, "foundUser == null"); Assert.AreEqual(user.ID, foundUser.ID, "The IDs don't match."); } } } }
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."); } }
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_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."); } }