public void TestTableRelations() { // Prepare and Cleanup Database.RegisterDataObject(typeof(TestTableRelations)); Database.RegisterDataObject(typeof(TestTableRelationsEntries)); var all = Database.SelectAllObjects<TestTableRelationsEntries>(); foreach(var obj in all) Database.DeleteObject(obj); var none = Database.SelectAllObjects<TestTableRelationsEntries>(); Assert.IsEmpty(none, "Database shouldn't have any record For TestTableRelationsEntries."); var allrel = Database.SelectAllObjects<TestTableRelations>(); foreach(var obj in allrel) Database.DeleteObject(obj); var nonerel = Database.SelectAllObjects<TestTableRelations>(); Assert.IsEmpty(nonerel, "Database shouldn't have any record For TestTableRelations."); // Try Add With no Relation var noRelObj = new TestTableRelations() { TestField = "RelationsTestValue" }; var inserted = Database.AddObject(noRelObj); Assert.IsTrue(inserted, "Test Table Relations could not insert object with no relation."); Assert.IsNull(noRelObj.Entries, "Test Table Relations object with no relation should have null Entry."); // Try Selecting var selectInserted = Database.SelectAllObjects<TestTableRelations>(); CollectionAssert.IsNotEmpty(selectInserted, "Test Table Relations could not retrieve objects without relation."); // Try Adding Relation var testValues = new[] { "RelationsEntriesTestValue 1", "RelationsEntriesTestValue 2", "RelationsEntriesTestValue 3" }; var relObjs = testValues.Select(val => new TestTableRelationsEntries() { TestField = val, ForeignTestField = noRelObj.ObjectId }).ToArray(); var relInserted = relObjs.Select(o => Database.AddObject(o)).ToArray(); Assert.IsTrue(relInserted.All(res => res), "Test Table Relations Entries could not be inserted."); noRelObj.Entries = relObjs; var saved = Database.SaveObject(noRelObj); Assert.IsTrue(saved, "Test Table Relations could not save Object with a new relations Added."); // Try Retrieving Relation var retrieve = Database.FindObjectByKey<TestTableRelations>(noRelObj.ObjectId); Assert.IsNotNull(retrieve, "Test Table Relations could not retrieve relations object by ObjectId."); Assert.IsNotNull(retrieve.Entries, "Test Table Relations retrieved object have no entries objects."); CollectionAssert.AreEquivalent(testValues, retrieve.Entries.Select(o => o.TestField), "Test Table Relations retrieved objects Entries Relation are different from created objects."); // Try Deleting Relation var deleted = Database.DeleteObject(noRelObj); Assert.IsTrue(deleted, "Test Table Relations could not delete object with relations."); Assert.IsTrue(noRelObj.IsDeleted, "Test Table Relations deleted object should have deleted flag set."); Assert.IsTrue(noRelObj.Entries.All(ent => ent.IsDeleted), "Test Table Relations deleted object should have all Entries with deleted flag set."); // Check that Relation was deleted var relRetrieve = Database.SelectAllObjects<TestTableRelationsEntries>().Where(o => o.ForeignTestField == noRelObj.ObjectId); Assert.IsEmpty(relRetrieve, "Test Table Relations Entries were not auto deleted with relations object."); Assert.IsTrue(relObjs.All(o => o.IsDeleted), "Test Table Relations Entries should have deleted flags set after auto delete."); // Check Null Relation var nullObj = new TestTableRelations() { TestField = "RelationsNullTestValue" }; var nullAdded = Database.AddObject(nullObj); Assert.IsTrue(nullAdded, "Test Table Relations need an object for the Null Relation Test."); nullObj.ObjectId = null; nullObj.Entries = new TestTableRelationsEntries[0]; Database.FillObjectRelations(nullObj); Assert.IsNull(nullObj.Entries, "Test Table Relations should have null entries with null local value."); // Check Allow Add Restriction var allowAddRestriction = new TestTableRelations() { TestField = "RelationTestValueAllowAddRestriction" }; allowAddRestriction.Entries = new [] { new TestTableRelationsEntries() { TestField = "RelationsEntriesTestValueAllowAddRestriction", ForeignTestField = allowAddRestriction.ObjectId, AllowAdd = false } }; var restrictionAdded = Database.AddObject(allowAddRestriction); Assert.IsFalse(restrictionAdded, "Test Table Relations should return false when missing relation adding."); Assert.IsTrue(allowAddRestriction.IsPersisted, "Test Table Relations should be persisted."); Assert.IsFalse(allowAddRestriction.Entries[0].IsPersisted, "Test Table Relations Entries should not be persisted if AllowAdd Restriction is in place."); // Check Dirty Restriction allowAddRestriction.Entries[0].AllowAdd = true; var restrictionSaved = Database.SaveObject(allowAddRestriction); Assert.IsTrue(restrictionSaved, "Test Table Relations restriction test should have the restricted object seved to database."); Assert.IsTrue(allowAddRestriction.Entries[0].IsPersisted, "Test Table Relations Entries restriction test should be persisted."); Assert.IsFalse(allowAddRestriction.Dirty, "Test Table Relations restriction object should not have Dirty flag after saving."); var modifiedText = "RelationEntryTestValueAllowAddRestrictionModified"; allowAddRestriction.Entries[0].TestField = modifiedText; allowAddRestriction.Entries[0].Dirty = false; Assert.IsFalse(allowAddRestriction.Entries[0].Dirty, "Test Table Relations Entries need to have its Dirty flag off for this Test."); var modifySaved = Database.SaveObject(allowAddRestriction); Assert.IsTrue(modifySaved, "Test Table Relations should be saved correctly with Dirty flag off."); Database.FillObjectRelations(allowAddRestriction); Assert.AreNotEqual(modifiedText, allowAddRestriction.Entries[0].TestField, "Test Table Relations Entries should not have been saved with modified text."); // Check Allow Delete Restriction var restrictionEntry = allowAddRestriction.Entries[0]; allowAddRestriction.Entries[0].AllowDelete = false; var restrictionDeleted = Database.DeleteObject(allowAddRestriction); Assert.IsFalse(restrictionDeleted, "Test Table Relations should not be deleted correctly."); Assert.IsTrue(restrictionEntry.IsPersisted, "Test Table Relations Entries should still be Persisted."); Assert.IsFalse(restrictionEntry.IsDeleted, "Test Table Relations Entries should not be deleted."); Assert.IsFalse(allowAddRestriction.IsPersisted, "Test Table Relations object should not be persisted."); }
public void TestTableRelations() { // Prepare and Cleanup DatabaseSetUp.Database.RegisterDataObject(typeof(TestTableRelations)); DatabaseSetUp.Database.RegisterDataObject(typeof(TestTableRelationsEntries)); var all = DatabaseSetUp.Database.SelectAllObjects<TestTableRelationsEntries>(); foreach(var obj in all) DatabaseSetUp.Database.DeleteObject(obj); var none = DatabaseSetUp.Database.SelectAllObjects<TestTableRelationsEntries>(); Assert.IsEmpty(none, "Database shouldn't have any record For TestTableRelationsEntries."); var allrel = DatabaseSetUp.Database.SelectAllObjects<TestTableRelations>(); foreach(var obj in allrel) DatabaseSetUp.Database.DeleteObject(obj); var nonerel = DatabaseSetUp.Database.SelectAllObjects<TestTableRelations>(); Assert.IsEmpty(nonerel, "Database shouldn't have any record For TestTableRelations."); // Try Add With no Relation var noRelObj = new TestTableRelations() { TestField = "RelationsTestValue" }; var inserted = DatabaseSetUp.Database.AddObject(noRelObj); Assert.IsTrue(inserted, "Test Table Relations could not insert object with no relation."); Assert.IsNull(noRelObj.Entries, "Test Table Relations object with no relation should have null Entry."); // Try Adding Relation var testValues = new[] { "RelationsEntriesTestValue 1", "RelationsEntriesTestValue 2", "RelationsEntriesTestValue 3" }; var relObjs = testValues.Select(val => new TestTableRelationsEntries() { TestField = val, ForeignTestField = noRelObj.ObjectId }).ToArray(); var relInserted = relObjs.Select(o => DatabaseSetUp.Database.AddObject(o)).ToArray(); Assert.IsTrue(relInserted.All(res => res), "Test Table Relations Entries could not be inserted."); noRelObj.Entries = relObjs; var saved = DatabaseSetUp.Database.SaveObject(noRelObj); Assert.IsTrue(saved, "Test Table Relations could not save Object with a new relations Added."); // Try Retrieving Relation var retrieve = DatabaseSetUp.Database.FindObjectByKey<TestTableRelations>(noRelObj.ObjectId); Assert.IsNotNull(retrieve, "Test Table Relations could not retrieve relations object by ObjectId."); Assert.IsNotNull(retrieve.Entries, "Test Table Relations retrieved object have no entries objects."); CollectionAssert.AreEquivalent(testValues, retrieve.Entries.Select(o => o.TestField), "Test Table Relations retrieved objects Entries Relation are different from created objects."); // Try Deleting Relation var deleted = DatabaseSetUp.Database.DeleteObject(noRelObj); Assert.IsTrue(deleted, "Test Table Relations could not delete object with relations."); Assert.IsTrue(noRelObj.IsDeleted, "Test Table Relations deleted object should have deleted flag set."); // Check that Relation was deleted var relRetrieve = DatabaseSetUp.Database.SelectAllObjects<TestTableRelationsEntries>().Where(o => o.ForeignTestField == noRelObj.ObjectId); Assert.IsEmpty(relRetrieve, "Test Table Relations Entries were not auto deleted with relations object."); Assert.IsTrue(relObjs.All(o => o.IsDeleted), "Test Table Relations Entries should have deleted flags set after auto delete."); }