public void TreePersist_Insert_WithExtsNullOneToOneChildren_ShouldEqualWhenLoaded()
        {
            try
            {
                var          con         = SetupTables();
                ITransaction transaction = CreateTransaction(con);

                RegisterForExternal();

                int id = 35;
                ITreeTestRootEntity rootEntity = CreateFullObjectTree(id, TYPE_EXTERNAL);
                rootEntity.One2OneEntity = null;
                rootEntity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(con);
                ITreeTestRootEntity loadedEntity = new TreeTestRootEntityExt();
                LoadEntityWithId(transaction, loadedEntity, id);
                transaction.Commit();
                con.Close();

                bool compareResult = CompareEntities(rootEntity, loadedEntity);
                Assert.IsTrue(compareResult);
            }
            catch (System.Exception e)
            {
                LogManager.GetLogger(typeof(DbGateTreePersistTests)).Fatal("Exception during test", e);
                Assert.Fail(e.Message);
            }
        }
        public void TreePersist_Insert_WithAttributesDifferentTypeOfChildren_ShouldEqualWhenLoaded()
        {
            try
            {
                var          con         = SetupTables();
                ITransaction transaction = CreateTransaction(con);

                int id = 35;
                ITreeTestRootEntity rootEntity = CreateFullObjectTree(id, TYPE_ATTRIBUTE);
                rootEntity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(con);
                ITreeTestRootEntity loadedEntity = new TreeTestRootEntityAttributes();
                LoadEntityWithId(transaction, loadedEntity, id);
                con.Close();

                bool compareResult = CompareEntities(rootEntity, loadedEntity);
                Assert.IsTrue(compareResult);
            }
            catch (System.Exception e)
            {
                LogManager.GetLogger(typeof(DbGateTreePersistTests)).Fatal("Exception during test", e);
                Assert.Fail(e.Message);
            }
        }
        private bool CompareEntities(ITreeTestRootEntity rootA, ITreeTestRootEntity rootB)
        {
            bool result = rootA.IdCol == rootB.IdCol;

            result &= rootA.Name == rootB.Name;

            if (rootA.One2OneEntity != null &&
                rootB.One2OneEntity != null)
            {
                result &= rootA.One2OneEntity.IdCol == rootB.One2OneEntity.IdCol;
                result &= rootA.One2OneEntity.Name.Equals(rootB.One2OneEntity.Name);
            }
            else if (rootA.One2OneEntity == null &&
                     rootB.One2OneEntity == null)
            {
            }
            else
            {
                return(false);
            }
            if (!result)
            {
                return(false);
            }

            if (rootA.One2ManyEntities != null &&
                rootB.One2ManyEntities != null)
            {
                if (rootA.One2ManyEntities.Count != rootB.One2ManyEntities.Count)
                {
                    return(false);
                }
                foreach (ITreeTestOne2ManyEntity one2ManyEntityA in rootA.One2ManyEntities)
                {
                    bool found = false;
                    foreach (ITreeTestOne2ManyEntity one2ManyEntityB in rootB.One2ManyEntities)
                    {
                        found  = one2ManyEntityA.IdCol == one2ManyEntityB.IdCol;
                        found &= one2ManyEntityA.IndexNo == one2ManyEntityB.IndexNo;
                        found &= one2ManyEntityA.Name.Equals(one2ManyEntityB.Name);
                    }
                    if (!found)
                    {
                        return(false);
                    }
                }
            }
            else if (rootA.One2ManyEntities == null &&
                     rootB.One2ManyEntities == null)
            {
            }
            else
            {
                return(false);
            }

            return(result);
        }
        public void TreePersist_Update_WithExtsDifferentTypeOfChildren_ShouldEqualWhenLoaded()
        {
            try
            {
                var          con         = SetupTables();
                ITransaction transaction = CreateTransaction(con);

                RegisterForExternal();

                int id = 35;
                ITreeTestRootEntity rootEntity = CreateFullObjectTree(id, TYPE_EXTERNAL);
                rootEntity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(con);
                ITreeTestRootEntity loadedEntity = new TreeTestRootEntityExt();
                LoadEntityWithId(transaction, loadedEntity, id);

                loadedEntity.Name                 = "changed-name";
                loadedEntity.Status               = EntityStatus.Modified;
                loadedEntity.One2OneEntity.Name   = "changed-one2one";
                loadedEntity.One2OneEntity.Status = EntityStatus.Modified;

                IEnumerator <ITreeTestOne2ManyEntity> enumerator = loadedEntity.One2ManyEntities.GetEnumerator();
                enumerator.MoveNext();

                ITreeTestOne2ManyEntity one2ManyEntity = enumerator.Current;
                one2ManyEntity.Name   = "changed-one2many";
                one2ManyEntity.Status = EntityStatus.Modified;

                loadedEntity.Persist(transaction);

                ITreeTestRootEntity reLoadedEntity = new TreeTestRootEntityExt();
                LoadEntityWithId(transaction, reLoadedEntity, id);
                con.Close();

                bool compareResult = CompareEntities(loadedEntity, reLoadedEntity);
                Assert.IsTrue(compareResult);
            }
            catch (System.Exception e)
            {
                LogManager.GetLogger(typeof(DbGateTreePersistTests)).Fatal("Exception during test", e);
                Assert.Fail(e.Message);
            }
        }
        public void TreePersist_Delete_WithExtsDifferentTypeOfChildren_ShouldEqualWhenLoaded()
        {
            try
            {
                var          con         = SetupTables();
                ITransaction transaction = CreateTransaction(con);

                RegisterForExternal();

                int id = 35;
                ITreeTestRootEntity rootEntity = CreateFullObjectTree(id, TYPE_EXTERNAL);
                rootEntity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(con);
                ITreeTestRootEntity loadedEntity = new TreeTestRootEntityExt();
                LoadEntityWithId(transaction, loadedEntity, id);

                loadedEntity.Name   = "changed-name";
                loadedEntity.Status = EntityStatus.Deleted;

                loadedEntity.Persist(transaction);

                ITreeTestRootEntity reLoadedEntity = new TreeTestRootEntityExt();
                bool loaded         = LoadEntityWithId(transaction, reLoadedEntity, id);
                bool existsOne2one  = ExistsOne2ManyChild(transaction, id);
                bool existsOne2many = ExistsOne2ManyChild(transaction, id);
                transaction.Close();

                Assert.IsFalse(loaded);
                Assert.IsFalse(existsOne2one);
                Assert.IsFalse(existsOne2many);
            }
            catch (System.Exception e)
            {
                LogManager.GetLogger(typeof(DbGateTreePersistTests)).Fatal("Exception during test", e);
                Assert.Fail(e.Message);
            }
        }
        private ITreeTestRootEntity CreateFullObjectTree(int id, int type)
        {
            ITreeTestRootEntity entity = null;

            entity = (type == TYPE_ATTRIBUTE)
                         ? new TreeTestRootEntityAttributes()
                         : (type == TYPE_FIELD)
                               ? (ITreeTestRootEntity) new TreeTestRootEntityFields()
                               : new TreeTestRootEntityExt();
            entity.IdCol = id;
            entity.Name  = "root";

            ITreeTestOne2OneEntity one2OneEntity = null;

            one2OneEntity = (type == TYPE_ATTRIBUTE)
                                ? new TreeTestOne2OneEntityAttributes()
                                : (type == TYPE_FIELD)
                                      ? (ITreeTestOne2OneEntity) new TreeTestOne2OneEntityFields()
                                      : new TreeTestOne2OneEntityExt();
            one2OneEntity.IdCol  = id;
            one2OneEntity.Name   = "one2one";
            entity.One2OneEntity = one2OneEntity;

            ITreeTestOne2ManyEntity one2ManyEntity = null;

            one2ManyEntity = (type == TYPE_ATTRIBUTE)
                                 ? new TreeTestOne2ManyEntityAttributes()
                                 : (type == TYPE_FIELD)
                                       ? (ITreeTestOne2ManyEntity) new TreeTestOne2ManyEntityFields()
                                       : new TreeTestOne2ManyEntityExt();
            one2ManyEntity.IdCol    = id;
            one2ManyEntity.IndexNo  = 0;
            one2ManyEntity.Name     = "one2many";
            entity.One2ManyEntities = new List <ITreeTestOne2ManyEntity>();
            entity.One2ManyEntities.Add(one2ManyEntity);

            return(entity);
        }
        private bool LoadEntityWithId(ITransaction transaction, ITreeTestRootEntity loadEntity, int id)
        {
            bool loaded = false;

            IDbCommand cmd = transaction.CreateCommand();

            cmd.CommandText = "select * from tree_test_root where id_col = ?";

            IDbDataParameter parameter = cmd.CreateParameter();

            cmd.Parameters.Add(parameter);
            parameter.DbType = DbType.Int32;
            parameter.Value  = id;

            IDataReader reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                loadEntity.Retrieve(reader, transaction);
                loaded = true;
            }

            return(loaded);
        }