public void TestTableRelation() { // Prepare and Cleanup Database.RegisterDataObject(typeof(TestTableRelation)); Database.RegisterDataObject(typeof(TestTableRelationEntry)); var all = Database.SelectAllObjects<TestTableRelationEntry>(); foreach(var obj in all) Database.DeleteObject(obj); var none = Database.SelectAllObjects<TestTableRelationEntry>(); Assert.IsEmpty(none, "Database shouldn't have any record For TestTableRelationEntry."); var allrel = Database.SelectAllObjects<TestTableRelation>(); foreach(var obj in allrel) Database.DeleteObject(obj); var nonerel = Database.SelectAllObjects<TestTableRelation>(); Assert.IsEmpty(nonerel, "Database shouldn't have any record For TestTableRelation."); // Try Add with no Relation var noRelObj = new TestTableRelation() { TestField = "RelationTestValue" }; var inserted = Database.AddObject(noRelObj); Assert.IsTrue(inserted, "Test Table Relation could not insert object with no relation."); Assert.IsNull(noRelObj.Entry, "Test Table Relation object with no relation should have null Entry."); // Try Select var selectInserted = Database.SelectAllObjects<TestTableRelation>(); CollectionAssert.IsNotEmpty(selectInserted, "Test Table Relation could not retrieve objects without relation."); // Try Adding Relation var relObj = new TestTableRelationEntry() { TestField = "RelationEntryTestValue", ObjectId = noRelObj.ObjectId }; var relInserted = Database.AddObject(relObj); Assert.IsTrue(relInserted, "Test Table Relation Entry could not be inserted."); noRelObj.Entry = relObj; var saved = Database.SaveObject(noRelObj); Assert.IsTrue(saved, "Test Table Relation could not save Object with a new relation Added."); // Try Retrieving Relation var retrieve = Database.FindObjectByKey<TestTableRelation>(noRelObj.ObjectId); Assert.IsNotNull(retrieve, "Test Table Relation could not retrieve relation object by ObjectId."); Assert.IsNotNull(retrieve.Entry, "Test Table Relation retrieved object have no entry object."); Assert.AreEqual(relObj.TestField, retrieve.Entry.TestField, "Test Table Relation retrieved object Entry Relation is different from created object."); // Try Deleting Relation var deleted = Database.DeleteObject(noRelObj); Assert.IsTrue(deleted, "Test Table Relation could not delete object with relation."); Assert.IsTrue(noRelObj.IsDeleted, "Test Table Relation deleted object should have deleted flag set."); Assert.IsTrue(noRelObj.Entry.IsDeleted, "Test Table Relation deleted object Entry should have deleted flag set."); // Check that Relation was deleted var relRetrieve = Database.FindObjectByKey<TestTableRelationEntry>(relObj.ObjectId); Assert.IsNull(relRetrieve, "Test Table Relation Entry was not auto deleted with relation object."); Assert.IsTrue(relObj.IsDeleted, "Test Table Relation Entry should have deleted flag set after auto delete."); // Check Null Relation var nullObj = new TestTableRelation() { TestField = "RelationNullTestValue" }; var nullAdded = Database.AddObject(nullObj); Assert.IsTrue(nullAdded, "Test Table Relation need an object for the Null Relation Test."); nullObj.ObjectId = null; nullObj.Entry = new TestTableRelationEntry() { TestField = "RelationEntryNullTestValue" }; Database.FillObjectRelations(nullObj); Assert.IsNull(nullObj.Entry, "Test Table Relation should not have entry with null local value."); // Check Allow Add Restriction var allowAddRestriction = new TestTableRelation() { TestField = "RelationTestValueAllowAddRestriction" }; allowAddRestriction.Entry = new TestTableRelationEntry() { TestField = "RelationEntryTestValueAllowAddRestriction", ObjectId = allowAddRestriction.ObjectId, AllowAdd = false }; var restrictionAdded = Database.AddObject(allowAddRestriction); Assert.IsFalse(restrictionAdded, "Test Table Relation should return error when relation failed saving."); Assert.IsTrue(allowAddRestriction.IsPersisted, "Test Table Relation should be persisted in database."); Assert.IsFalse(allowAddRestriction.Entry.IsPersisted, "Test Table Relation Entry should not be persisted if AllowAdd Restriction is in place."); // Check Dirty Restriction allowAddRestriction.Entry.AllowAdd = true; var restrictionSaved = Database.SaveObject(allowAddRestriction); Assert.IsTrue(restrictionSaved, "Test Table Relation restriction test should have the restricted object seved to database."); Assert.IsTrue(allowAddRestriction.Entry.IsPersisted, "Test Table Relation Entry restriction test should be persisted."); Assert.IsFalse(allowAddRestriction.Dirty, "Test Table Relation restriction object should not have Dirty flag after saving."); var modifiedText = "RelationEntryTestValueAllowAddRestrictionModified"; allowAddRestriction.Entry.TestField = modifiedText; allowAddRestriction.Entry.Dirty = false; Assert.IsFalse(allowAddRestriction.Entry.Dirty, "Test Table Relation Entry need to have its Dirty flag off for this Test."); var modifySaved = Database.SaveObject(allowAddRestriction); Assert.IsTrue(modifySaved, "Test Table Relation should be saved correctly with Dirty flag off."); Database.FillObjectRelations(allowAddRestriction); Assert.AreNotEqual(modifiedText, allowAddRestriction.Entry.TestField, "Test Table Relation Entry should not have been saved with modified text."); // Check Allow Delete Restriction var restrictionEntry = allowAddRestriction.Entry; allowAddRestriction.Entry.AllowDelete = false; var restrictionDeleted = Database.DeleteObject(allowAddRestriction); Assert.IsFalse(restrictionDeleted, "Test Table Relation should not be deleted correctly."); Assert.IsTrue(restrictionEntry.IsPersisted, "Test Table Relation Entry should still be Persisted."); Assert.IsFalse(restrictionEntry.IsDeleted, "Test Table Relation Entry should not be deleted."); Assert.IsFalse(allowAddRestriction.IsPersisted, "Test Table Relation object should not be persisted."); }
public void TestTableRelation() { // Prepare and Cleanup DatabaseSetUp.Database.RegisterDataObject(typeof(TestTableRelation)); DatabaseSetUp.Database.RegisterDataObject(typeof(TestTableRelationEntry)); var all = DatabaseSetUp.Database.SelectAllObjects<TestTableRelationEntry>(); foreach(var obj in all) DatabaseSetUp.Database.DeleteObject(obj); var none = DatabaseSetUp.Database.SelectAllObjects<TestTableRelationEntry>(); Assert.IsEmpty(none, "Database shouldn't have any record For TestTableRelationEntry."); var allrel = DatabaseSetUp.Database.SelectAllObjects<TestTableRelation>(); foreach(var obj in allrel) DatabaseSetUp.Database.DeleteObject(obj); var nonerel = DatabaseSetUp.Database.SelectAllObjects<TestTableRelation>(); Assert.IsEmpty(nonerel, "Database shouldn't have any record For TestTableRelation."); // Try Add with no Relation var noRelObj = new TestTableRelation() { TestField = "RelationTestValue" }; var inserted = DatabaseSetUp.Database.AddObject(noRelObj); Assert.IsTrue(inserted, "Test Table Relation could not insert object with no relation."); Assert.IsNull(noRelObj.Entry, "Test Table Relation object with no relation should have null Entry."); // Try Adding Relation var relObj = new TestTableRelationEntry() { TestField = "RelationEntryTestValue", ObjectId = noRelObj.ObjectId }; var relInserted = DatabaseSetUp.Database.AddObject(relObj); Assert.IsTrue(relInserted, "Test Table Relation Entry could not be inserted."); noRelObj.Entry = relObj; var saved = DatabaseSetUp.Database.SaveObject(noRelObj); Assert.IsTrue(saved, "Test Table Relation could not save Object with a new relation Added."); // Try Retrieving Relation var retrieve = DatabaseSetUp.Database.FindObjectByKey<TestTableRelation>(noRelObj.ObjectId); Assert.IsNotNull(retrieve, "Test Table Relation could not retrieve relation object by ObjectId."); Assert.IsNotNull(retrieve.Entry, "Test Table Relation retrieved object have no entry object."); Assert.AreEqual(relObj.TestField, retrieve.Entry.TestField, "Test Table Relation retrieved object Entry Relation is different from created object."); // Try Deleting Relation var deleted = DatabaseSetUp.Database.DeleteObject(noRelObj); Assert.IsTrue(deleted, "Test Table Relation could not delete object with relation."); Assert.IsTrue(noRelObj.IsDeleted, "Test Table Relation deleted object should have deleted flag set."); // Check that Relation was deleted var relRetrieve = DatabaseSetUp.Database.FindObjectByKey<TestTableRelationEntry>(relObj.ObjectId); Assert.IsNull(relRetrieve, "Test Table Relation Entry was not auto deleted with relation object."); Assert.IsTrue(relObj.IsDeleted, "Test Table Relation Entry should have deleted flag set after auto delete."); }