Exemplo n.º 1
0
        public void AllInOneTest()
        {
            var entity1 = new SimpleEntity { Name = "Repository Test 1" };
            var entity2 = new SimpleEntity { Name = "Repository Test 2" };
            var entity3 = new SimpleEntity { Name = "Repository Test 3" };
            var entity4 = new SimpleEntity { Name = "Repository Test 4" };
            var entity5 = new SimpleEntity { Name = "Repository Test 5" };
            var entity6 = new SimpleEntity { Name = "Repository Test Query 6" };
            var entity7 = new SimpleEntity { Name = "Repository Test Query 7" };

            Repository.Data.Save(entity1);
            Repository.Data.Save(entity2, entity3, entity4);
            Repository.Data.Save(new List<SimpleEntity>{entity5, entity6, entity7});

            var all = new List<SimpleEntity>{entity1, entity2, entity3, entity4, entity5, entity6, entity7};
            foreach (var entity in all)
            {
                var data = Repository.Data.Get<SimpleEntity>(entity.Id);
                Assert.NotNull(data);
                Assert.Equal(entity.Name, data.Name);
            }

            Repository.Data.Delete(entity1);
            Repository.Data.Delete(entity2, entity3);
            Repository.Data.Delete(new List<SimpleEntity> { entity4, entity5 });
            Repository.Data.Delete<SimpleEntity>(x => x.Name.StartsWith("Repository Test Query"));

            foreach (var entity in all)
            {
                var data = Repository.Data.Get<SimpleEntity>(entity.Id);
                Assert.Null(data);
            }
        }
Exemplo n.º 2
0
        public void NestedUnitsCombinatedTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};
            var entity3 = new SimpleEntity
                          	{
                          		Name = "UnitTest3"
                          	};
            var entity4 = new SimpleEntity
                          	{
                          		Name = "UnitTest4"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);

                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    using (var unit3 = new UnitOfWork())
                    {
                        Repository.Data.Save(entity3);
                        unit3.Commit();
                    }
                    using (var unit4 = new UnitOfWork(UnitOfWorkScopeType.New))
                    {
                        Repository.Data.Save(entity4);
                        unit4.Commit();
                    }
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                }

                Assert.Throws<UnitOfWorkException>(() => unit.Commit());
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            entity3 = Repository.Data.Get<SimpleEntity>(entity3.Id);
            entity4 = Repository.Data.Get<SimpleEntity>(entity4.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
            Assert.Null(entity3);
            Assert.NotNull(entity4);
        }
Exemplo n.º 3
0
        public void TryToAutoMergeIfNeeded()
        {
            var item = new SimpleEntity
                       	{
                       		Name = "PageType #1"
                       	};
            Repository.Data.Save(item);
            ConversationHelper.ReOpen();

            item = Repository.Data.Get<SimpleEntity>(item.Id);
            item.Name += ".2";

            Repository.Data.Save(item);

            ConversationHelper.ReOpen();
            Repository.Data.Get<SimpleEntity>(item.Id);

            item.Name += ".3";
            Repository.Data.Save(item);
        }
Exemplo n.º 4
0
        public void NestedUnitsWithInnerScopeRollbackAndCommitActionTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                UnitOfWork.Current.PostCommitActions.Add(() => { });
                UnitOfWork.Current.PostRollbackActions.Add(() => { });
                Repository.Data.Save(entity);
                Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count);
                Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count);

                using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New))
                {
                    UnitOfWork.Current.PostCommitActions.Add(() => { });
                    UnitOfWork.Current.PostRollbackActions.Add(() => { });
                    Repository.Data.Save(entity2);
                    unit2.Rollback();
                }

                Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count);
                Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count);
                unit.Commit();
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.NotNull(entity);
            Assert.Null(entity2);
        }
Exemplo n.º 5
0
        public void ClearCacheAllTest()
        {
            var sessionFactory = IoC.Resolve<ISessionFactory>();

            var item1 = new SimpleCacheEntity
            {
                Name = "Query Cache Test #1"
            };
            var item2 = new SimpleEntity
            {
                Name = "Query Cache Test #2"
            };
            Repository.Data.Save(item1);
            Repository.Data.Save(item2);

            var cachedEntity1 = GetCachedObjectDirectly(item1.Id, item1.GetType());
            Assert.NotNull(cachedEntity1);
            var cachedEntity2 = GetCachedObjectDirectly(item2.Id, item2.GetType());
            Assert.NotNull(cachedEntity2);

            var query = Repository.Data.Get<SimpleCacheEntity>().Where(x => x.Name.Contains("Query Cache Test"));
            var queryResult = query.All();
            Assert.NotEmpty(queryResult);
            sessionFactory.Statistics.Clear();
            var cachedQueryResult = query.All();
            Assert.NotEmpty(cachedQueryResult);
            Assert.Equal(0, sessionFactory.Statistics.EntityLoadCount);

            Repository.Data.Cache.Clear();

            cachedEntity1 = GetCachedObjectDirectly(item1.Id, item1.GetType());
            Assert.Null(cachedEntity1);
            cachedEntity2 = GetCachedObjectDirectly(item2.Id, item2.GetType());
            Assert.Null(cachedEntity2);
            sessionFactory.Statistics.Clear();
            var retrievedQueryResult = query.All();
            Assert.NotEmpty(retrievedQueryResult);
            Assert.True(sessionFactory.Statistics.EntityLoadCount > 0);
        }
Exemplo n.º 6
0
        public void UnitOfWorkRollbackTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                unit.Rollback();
            }

            Repository.Data.Cache.Clear(entity);
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            Assert.Null(entity);
        }
Exemplo n.º 7
0
        public void UnitOfWorkRollBackAfterRollbackTest()
        {
            var entity = new SimpleEntity
            {
                Name = "UnitTest"
            };
            var entity2 = new SimpleEntity
            {
                Name = "UnitTest2"
            };

            using (var unit = new UnitOfWork())
            {
                //Some external method
                Repository.Data.Save(entity);
                UnitOfWork.Current.Rollback();

                Assert.Throws<UnitOfWorkException>(() => Repository.Data.Save(entity2));
                unit.Rollback();
            }

            Repository.Data.Cache.Clear(entity);
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            Assert.Null(entity);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity2);
        }
Exemplo n.º 8
0
        public void ScopeWithoutException()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                unit.Rollback(); // will not throw exception
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();
            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            Assert.Null(entity);
        }
Exemplo n.º 9
0
        public void NestedUnitsWithSameScopeRollbackBothTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                }
                unit.Rollback();
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Exemplo n.º 10
0
        public void NestedUnitsWithSameScopeRollbackAndCommitActionTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                int onCommitActionsCount;
                int onRollBackActionsCount;

                UnitOfWork.Current.PostCommitActions.Add(() => { });
                UnitOfWork.Current.PostRollbackActions.Add(() => { });
                Repository.Data.Save(entity);
                Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count);
                Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count);

                using (var unit2 = new UnitOfWork())
                {
                    UnitOfWork.Current.PostCommitActions.Add(() => { });
                    UnitOfWork.Current.PostRollbackActions.Add(() => { });
                    onCommitActionsCount = UnitOfWork.Current.PostCommitActions.Count;
                    onRollBackActionsCount = UnitOfWork.Current.PostRollbackActions.Count;
                    Repository.Data.Save(entity2);
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                }

                Assert.Equal(2, onCommitActionsCount);
                Assert.Equal(2, onRollBackActionsCount);
                Assert.Throws<UnitOfWorkException>(() => unit.Commit());
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Exemplo n.º 11
0
        public void NestedUnitsWithSameScopeOuterDisposeTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    unit2.Commit();
                }
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Exemplo n.º 12
0
        public void NestedUnitsWithSameScopeInnerException()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork())
                {
                    Repository.Data.Save(entity2);
                    Assert.Throws<UnitOfWorkException>(() => unit2.Rollback());
                        // will throw exception because this is not the owner of the UOW
                }

                // user will not come to this point and try to commit transaction
                // else it should throw exception
                Assert.Throws<UnitOfWorkException>(() => unit.Commit());
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();
            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.Null(entity);
            Assert.Null(entity2);
        }
Exemplo n.º 13
0
        public void NestedUnitsWithNewInnerScopeRollbackAndCommitTest()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New))
                {
                    Repository.Data.Save(entity2);
                    unit2.Rollback();
                }
                unit.Commit();
            }

            Repository.Data.Cache.Clear(typeof (SimpleEntity));
            ConversationHelper.ReOpen();

            entity = Repository.Data.Get<SimpleEntity>(entity.Id);
            entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id);
            Assert.NotNull(entity);
            Assert.Null(entity2);
        }
Exemplo n.º 14
0
        public void NestedUnitsWithInnerScopeWithoutException()
        {
            var entity = new SimpleEntity
                         	{
                         		Name = "UnitTest"
                         	};
            var entity2 = new SimpleEntity
                          	{
                          		Name = "UnitTest2"
                          	};

            using (var unit = new UnitOfWork())
            {
                Repository.Data.Save(entity);
                using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New))
                {
                    Repository.Data.Save(entity2);
                    unit2.Rollback(); // will not throw exception because this is own transaction
                }
                // user can commit the outer UOW
                unit.Commit();
            }
        }
Exemplo n.º 15
0
        public void ClearCacheForEntityTypeTest()
        {
            var item1 = new SimpleCacheEntity
            {
                Name = "Query Cache Test #1"
            };
            var item2 = new SimpleCacheEntity
            {
                Name = "Query Cache Test #2"
            };
            var item3 = new SimpleEntity
            {
                Name = "Query Cache Test #3"
            };
            Repository.Data.Save(item1);
            Repository.Data.Save(item2);
            Repository.Data.Save(item3);

            var cachedEntity1 = GetCachedObjectDirectly(item1.Id, item1.GetType());
            Assert.NotNull(cachedEntity1);
            var cachedEntity2 = GetCachedObjectDirectly(item2.Id, item2.GetType());
            Assert.NotNull(cachedEntity2);
            var cachedEntity3 = GetCachedObjectDirectly(item3.Id, item3.GetType());
            Assert.NotNull(cachedEntity3);

            Repository.Data.Cache.Clear(typeof(SimpleCacheEntity));

            cachedEntity1 = GetCachedObjectDirectly(item1.Id, item1.GetType());
            Assert.Null(cachedEntity1);
            cachedEntity2 = GetCachedObjectDirectly(item2.Id, item2.GetType());
            Assert.Null(cachedEntity2);
            cachedEntity3 = GetCachedObjectDirectly(item3.Id, item3.GetType());
            Assert.NotNull(cachedEntity3);
        }