public void Remove_persisted_entity2()
        {
            MockEntity entity = new MockEntity();

            MockEntityTwo persistedLocation = new MockEntityTwo()
            {
                id = int.MaxValue
            };

            persistedLocation.MarkPersisted();
            MockEntityOne persistedContract = new MockEntityOne()
            {
                id = int.MinValue
            };

            persistedContract.MarkPersisted();
            MockEntity persistedServiceContractSiteContact = new MockEntity()
            {
                IsDeleted = true
            };

            persistedServiceContractSiteContact.MarkPersisted();
            entity.RelationalEntities.Add(persistedServiceContractSiteContact);

            DualDbEntityAssociativeCollectionManager <MockEntityOne, MockEntityTwo, MockEntity> manager = new DualDbEntityAssociativeCollectionManager <MockEntityOne, MockEntityTwo, MockEntity>(1, entity, () => entity.RelationalEntities, null, null, null, (o, ae) => true, (e1, ae) => true, (e2, ae) => true);

            MockEntity removed = manager.Remove(persistedLocation).Single();

            Assert.AreEqual(1, entity.RelationalEntities.Count);
            Assert.AreEqual(persistedServiceContractSiteContact, removed);
            Assert.IsTrue(removed.IsDeleted);
        }
        public void Add_new_entity2()
        {
            int  userId = 1;
            bool isSetEntity1ForeignEntityActionCalled = false;
            bool isSetEntity2ForeignEntityActionCalled = false;
            Action <IDbEntity, MockEntity, MockEntity> setEntity1ForeignEntity = (o, e1, ae) => { isSetEntity1ForeignEntityActionCalled = true; };
            Action <IDbEntity, MockEntity, MockEntity> setEntity2ForeignEntity = (o, e2, ae) => { isSetEntity2ForeignEntityActionCalled = true; };
            MockEntity entity = new MockEntity();
            DualDbEntityAssociativeCollectionManager <MockEntity, MockEntity, MockEntity> manager = new DualDbEntityAssociativeCollectionManager <MockEntity, MockEntity, MockEntity>(userId, entity, () => entity.RelationalEntities, setEntity1ForeignEntity, setEntity2ForeignEntity, (o, e1, e2, ae) => { }, null, null, null);

            MockEntity newLocation = new MockEntity()
            {
                id = int.MaxValue
            };
            MockEntity persistedContract = new MockEntity()
            {
                id = int.MinValue
            };

            persistedContract.MarkPersisted();
            MockEntity added = manager.Add(persistedContract, newLocation);

            Assert.IsFalse(isSetEntity1ForeignEntityActionCalled);
            Assert.IsTrue(isSetEntity2ForeignEntityActionCalled);
        }
        public void MatchesAssociativeEntity_persisted_entity_false()
        {
            MockEntity existingEntity = new MockEntity()
            {
                id = int.MaxValue
            };

            existingEntity.MarkPersisted();
            MockEntity entityToMatch = new MockEntity()
            {
                id = int.MinValue
            };

            entityToMatch.MarkPersisted();
            Assert.IsFalse(U.MatchAssociativeEntities(existingEntity, existingEntity.id, entityToMatch, entityToMatch.id));
        }
        public void Get_deleted_persisted_entity()
        {
            int        userId        = 1;
            MockEntity deletedEntity = new MockEntity()
            {
                id = int.MaxValue, IsDeleted = true
            };

            deletedEntity.MarkPersisted();
            MockEntity entity = new MockEntity();

            entity.RelationalEntities.Add(deletedEntity);

            ForeignEntityCollectionManager <MockEntity> manager = new ForeignEntityCollectionManager <MockEntity>(userId, entity, () => entity.RelationalEntities, null);

            Assert.IsNull(manager.Get(deletedEntity.PrimaryKeys));
        }
        public void Get_persisted_entity()
        {
            int        userId          = 1;
            MockEntity persistedEntity = new MockEntity()
            {
                id = int.MaxValue
            };

            persistedEntity.MarkPersisted();
            MockEntity entity = new MockEntity();

            entity.RelationalEntities.Add(persistedEntity);

            ForeignEntityCollectionManager <MockEntity> manager = new ForeignEntityCollectionManager <MockEntity>(userId, entity, () => entity.RelationalEntities, null);

            Assert.AreEqual(persistedEntity, manager.Get(persistedEntity.PrimaryKeys));
        }
        public void Contains_persisted_entity_false()
        {
            int        userId          = 1;
            MockEntity persistedEntity = new MockEntity()
            {
                id = int.MaxValue
            };

            persistedEntity.MarkPersisted();
            MockEntity entity = new MockEntity();

            entity.RelationalEntities.Add(persistedEntity);

            ForeignEntityCollectionManager <MockEntity> manager = new ForeignEntityCollectionManager <MockEntity>(userId, entity, () => entity.RelationalEntities, null);

            Assert.IsFalse(manager.Contains(new MockEntity()));
        }
        public void Equals_persisted_new_false()
        {
            MockEntity e1 = new MockEntity()
            {
                id = 1
            };
            MockEntity e2 = new MockEntity()
            {
                id = 1
            };

            e2.MarkPersisted();

            IEqualityComparer <MockEntity> comparer = new DbEntitiesComparer <MockEntity>();

            Assert.IsFalse(comparer.Equals(e2, e1));
        }
        public void GetHashCode_persisted_entities_equal()
        {
            MockEntity e1 = new MockEntity()
            {
                id = 1
            };

            e1.MarkPersisted();
            MockEntity e2 = new MockEntity()
            {
                id = 1
            };

            e2.MarkPersisted();

            IEqualityComparer <MockEntity> comparer = new DbEntitiesComparer <MockEntity>();

            Assert.AreEqual(comparer.GetHashCode(e1), comparer.GetHashCode(e2));
        }
        public void Equals_persisted_entities_true()
        {
            MockEntity e1 = new MockEntity()
            {
                id = 1
            };

            e1.MarkPersisted();
            MockEntity e2 = new MockEntity()
            {
                id = 1
            };

            e2.MarkPersisted();

            IEqualityComparer <MockEntity> comparer = new DbEntitiesComparer <MockEntity>();

            Assert.IsTrue(comparer.Equals(e1, e2));
        }
        public void SerializeEntity_relational_entity_and_foreign_key_change_same_prefix()
        {
            MockEntity            entity  = new MockEntity();
            PropertyChangeTracker tracker = new PropertyChangeTracker(entity);

            tracker.Start();

            MockEntity relationalEntity = new MockEntity()
            {
                id = 1
            };

            relationalEntity.MarkPersisted();
            entity.RelationalEntity1 = relationalEntity;
            entity.IntProperty       = relationalEntity.id;

            string json = DbEntityJsonConvert.SerializeEntity(entity, tracker.DbEntityChanges());

            Assert.AreEqual("{\"$id\":\"" + entity.Guid + "\",\"Guid\":\"" + entity.Guid + "\",\"RelationalEntity1\":{\"$id\":\"" + relationalEntity.Guid + "\",\"id\":" + relationalEntity.id + "},\"IntProperty\":" + relationalEntity.id + "}", json);
        }
        public void Remove_persisted_entity()
        {
            int        userId          = 1;
            MockEntity persistedEntity = new MockEntity()
            {
                id = int.MaxValue
            };

            persistedEntity.MarkPersisted();
            MockEntity entity = new MockEntity();

            entity.RelationalEntities.Add(persistedEntity);

            ForeignEntityCollectionManager <MockEntity> manager = new ForeignEntityCollectionManager <MockEntity>(userId, entity, () => entity.RelationalEntities, null);

            MockEntity removed = manager.Remove(persistedEntity);

            Assert.AreEqual(1, entity.RelationalEntities.Count);
            Assert.AreEqual(persistedEntity, removed);
            Assert.IsTrue(removed.IsDeleted);
        }
        public void ReAdd_previously_deleted_entity()
        {
            bool isSetForeignKeysActionCalled          = false;
            bool isSetEntity1ForeignEntityActionCalled = false;
            bool isSetEntity2ForeignEntityActionCalled = false;
            Action <IDbEntity, MockEntity, MockEntity, MockEntity> setForeignKeys          = (o, e1, e2, ae) => { isSetForeignKeysActionCalled = true; };
            Action <IDbEntity, MockEntity, MockEntity>             setEntity1ForeignEntity = (o, e1, ae) => { isSetEntity1ForeignEntityActionCalled = true; };
            Action <IDbEntity, MockEntity, MockEntity>             setEntity2ForeignEntity = (o, e2, ae) => { isSetEntity2ForeignEntityActionCalled = true; };
            MockEntity entity = new MockEntity();
            DualDbEntityAssociativeCollectionManager <MockEntity, MockEntity, MockEntity> manager = new DualDbEntityAssociativeCollectionManager <MockEntity, MockEntity, MockEntity>(1, entity, () => entity.RelationalEntities, setEntity1ForeignEntity, setEntity2ForeignEntity, setForeignKeys, (o, ae) => true, (e1, ae) => true, (e2, ae) => true);

            MockEntity persistedLocation = new MockEntity()
            {
                id = int.MaxValue
            };

            persistedLocation.MarkPersisted();
            MockEntity persistedContract = new MockEntity()
            {
                id = int.MinValue
            };

            persistedContract.MarkPersisted();
            MockEntity persistedServiceContractSiteContact = new MockEntity()
            {
                IsDeleted = true
            };

            persistedServiceContractSiteContact.MarkPersisted();
            entity.RelationalEntities.Add(persistedServiceContractSiteContact);
            MockEntity added = manager.Add(persistedContract, persistedLocation);

            Assert.AreEqual(1, entity.RelationalEntities.Count);
            Assert.AreEqual(added, persistedServiceContractSiteContact);
            Assert.IsFalse(isSetEntity1ForeignEntityActionCalled);
            Assert.IsFalse(isSetEntity2ForeignEntityActionCalled);
            Assert.IsFalse(isSetForeignKeysActionCalled);
            Assert.IsFalse(added.IsDeleted);
        }
        public void DbEntityPropertyPath_entity_path_no_property_path_3()
        {
            MockEntity entity1 = new MockEntity()
            {
                id = 1
            };

            entity1.MarkPersisted();
            MockEntity entity2 = new MockEntity()
            {
                id = 2
            };

            entity2.MarkPersisted();
            MockAssociativeEntity associativeEntity = new MockAssociativeEntity()
            {
                RelationalEntity1Id = entity1.id, RelationalEntity2Id = entity2.id, RelationalEntity2 = entity2
            };

            associativeEntity.MarkPersisted();
            entity1.AssociativeEntities.Add(associativeEntity);
            MockEntity entity3 = new MockEntity();

            entity2.RelationalEntity1 = entity3;
            MockEntity entity4 = new MockEntity()
            {
                id = 3, StringProperty = "3"
            };

            entity3.RelationalEntities.Add(entity4);
            string         entityPath         = ".AssociativeEntities[Guid=" + associativeEntity.Guid + "].RelationalEntity2.RelationalEntity1.RelationalEntities[Guid=" + entity4.Guid + "]";
            string         expectedEntityPath = ".AssociativeEntities[RelationalEntity1Id=" + entity1.id + ",RelationalEntity2Id=" + entity2.id + "].RelationalEntity2.RelationalEntity1.RelationalEntities[id=" + entity4.id + "]";
            PropertyChange change             = new PropertyChange(entityPath, string.Empty, string.Empty, null, null);

            Assert.AreEqual(expectedEntityPath, change.DbEntityPropertyPath(entity1));
        }
        public void ReAdd_previously_deleted_entity()
        {
            int        userId = 1;
            bool       isSetForeignKeysActionCalled = false;
            MockEntity entity = new MockEntity();

            Action <IDbEntity, MockEntity> setForeignKeys       = (o, e) => { isSetForeignKeysActionCalled = true; };
            ForeignEntityCollectionManager <MockEntity> manager = new ForeignEntityCollectionManager <MockEntity>(userId, entity, () => entity.RelationalEntities, setForeignKeys);

            MockEntity existing = new MockEntity()
            {
                id = 2, IsDeleted = true, CreatedByID = 3
            };

            existing.MarkPersisted();
            entity.RelationalEntities.Add(existing);
            MockEntity added = manager.Add(existing);

            Assert.AreEqual(1, entity.RelationalEntities.Count);
            Assert.AreEqual(added, existing);
            Assert.IsFalse(isSetForeignKeysActionCalled);
            Assert.IsFalse(added.IsDeleted);
            Assert.AreNotEqual(userId, added.CreatedByID);
        }