コード例 #1
0
        public void Should_Update_Entity_Properties_When_Updating()
        {
            var entity = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(entity);
            UnitOfWork.Commit();

            var principalProvider = Container.Resolve <IPrincipalProvider>();

            Assert.IsNotNull(entity.CreatedOn);
            Assert.AreEqual(entity.CreatedByUser, principalProvider.CurrentPrincipalName);
            Assert.IsNotNull(entity.ModifiedOn);
            Assert.AreEqual(entity.ModifiedByUser, principalProvider.CurrentPrincipalName);

            var modified = new DateTime(entity.ModifiedOn.Ticks);

            Thread.Sleep(25);

            var loadedEntity = Repository.FirstOrDefault <TestItemModel>(entity.Id);

            loadedEntity.Name = TestDataProvider.ProvideRandomString(100);
            Repository.Save(loadedEntity);
            UnitOfWork.Commit();

            Assert.IsNotNull(loadedEntity.CreatedOn);
            Assert.AreEqual(loadedEntity.CreatedByUser, principalProvider.CurrentPrincipalName);
            Assert.IsNotNull(loadedEntity.ModifiedOn);
            Assert.AreEqual(loadedEntity.ModifiedByUser, principalProvider.CurrentPrincipalName);

            Assert.AreEqual(loadedEntity.CreatedOn, entity.CreatedOn);
            Assert.AreNotEqual(loadedEntity.ModifiedOn, modified);
        }
コード例 #2
0
        public void Should_Refresh_Entity()
        {
            // Create entity
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(model);
            UnitOfWork.Commit();

            var version = model.Version;

            // Load attached and detached version, touch multiple times
            var detachedModel = Repository.First <TestItemModel>(model.Id);

            // Open another session
            using (var container = ContextScopeProvider.CreateChildContainer())
            {
                var repository2 = container.Resolve <IRepository>();
                var unitOfWork2 = container.Resolve <IUnitOfWork>();

                var attachedModel = repository2.First <TestItemModel>(model.Id);
                attachedModel.Name = TestDataProvider.ProvideRandomString();
                unitOfWork2.Commit();
            }

            Assert.AreEqual(detachedModel.Version, version);

            // Refresh detached entity - version should be updated
            Repository.Refresh(detachedModel);
            Assert.AreNotEqual(detachedModel.Version, version);
        }
コード例 #3
0
        public void Should_Commit_And_Rollback_Transactions_Successfully()
        {
            var model1 = DatabaseTestDataProvider.ProvideRandomTestItemModel();
            var model2 = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            try
            {
                UnitOfWork.BeginTransaction();
                Repository.Save(model1);
                UnitOfWork.Commit();

                UnitOfWork.BeginTransaction();
                Repository.Save(model2);
                UnitOfWork.Rollback();
            }
            catch
            {
                // Do nothing here
            }

            var loadedModel1 = Repository.FirstOrDefault <TestItemModel>(model1.Id);
            var loadedModel2 = Repository.FirstOrDefault <TestItemModel>(model2.Id);

            Assert.IsNotNull(loadedModel1);
            Assert.IsNull(loadedModel2);
        }
コード例 #4
0
        public void Should_Attach_Entity()
        {
            // Create entity
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(model);
            UnitOfWork.Commit();

            var version = model.Version;

            // Load detached version, touch multiple times
            var detachedModel = Repository.First <TestItemModel>(model.Id);

            Repository.Detach(detachedModel);
            detachedModel.Name = TestDataProvider.ProvideRandomString();
            UnitOfWork.Commit();
            detachedModel.Name = TestDataProvider.ProvideRandomString();
            UnitOfWork.Commit();

            Assert.AreEqual(detachedModel.Version, version);

            // Attach and save again
            Repository.Attach(detachedModel);
            detachedModel.Name = TestDataProvider.ProvideRandomString();
            UnitOfWork.Commit();

            Assert.AreNotEqual(detachedModel.Version, version);
        }
コード例 #5
0
        public void Should_Detach_Entity()
        {
            // Create entity
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(model);
            UnitOfWork.Commit();

            // Touch entity - changes should be saved on flush
            var version = model.Version;

            var attachedModel = Repository.First <TestItemModel>(model.Id);

            attachedModel.Name = TestDataProvider.ProvideRandomString();
            UnitOfWork.Commit();

            Assert.Greater(attachedModel.Version, version);
            version = attachedModel.Version;

            // Detach and touch again - changes shouldn't saved on flush
            var detachedModel = Repository.First <TestItemModel>(model.Id);

            Repository.Detach(detachedModel);
            detachedModel.Name = TestDataProvider.ProvideRandomString();
            UnitOfWork.Commit();

            Assert.AreEqual(detachedModel.Version, version);
        }
コード例 #6
0
        public void SetUp()
        {
            if (!isSet)
            {
                isSet = true;

                category1 = DatabaseTestDataProvider.ProvideRandomTestItemCategory();

                model1            = DatabaseTestDataProvider.ProvideRandomTestItemModel(category1);
                model1.Name       = "DRT_01";
                model2            = DatabaseTestDataProvider.ProvideRandomTestItemModel(category1);
                model2.Name       = "DRT_02";
                model3            = DatabaseTestDataProvider.ProvideRandomTestItemModel(category1);
                model3.Name       = "DRT_03";
                deletedModel      = DatabaseTestDataProvider.ProvideRandomTestItemModel(category1);
                deletedModel.Name = "DRT_04";

                Repository.Save(model3);
                Repository.Save(deletedModel);
                Repository.Save(model2);
                Repository.Save(model1);
                UnitOfWork.Commit();

                Repository.Delete(deletedModel);
                UnitOfWork.Commit();
            }
        }
コード例 #7
0
        public void Should_Save_Entity()
        {
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(model);
            UnitOfWork.Commit();

            Assert.IsTrue(model.Id != default(Guid));
        }
コード例 #8
0
        public void Should_Create_Version()
        {
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Assert.AreEqual(model.Version, 0);

            Repository.Save(model);
            UnitOfWork.Commit();

            Assert.AreEqual(model.Version, 1);
        }
コード例 #9
0
        public void Should_Update_Entity_Properties_When_Creating()
        {
            var entity = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(entity);
            UnitOfWork.Commit();

            var principalProvider = Container.Resolve <IPrincipalProvider>();

            Assert.IsNotNull(entity.CreatedOn);
            Assert.AreEqual(entity.CreatedByUser, principalProvider.CurrentPrincipalName);
            Assert.IsNotNull(entity.ModifiedOn);
            Assert.AreEqual(entity.ModifiedByUser, principalProvider.CurrentPrincipalName);
        }
コード例 #10
0
        public void Should_Delete_Entity()
        {
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(model);
            UnitOfWork.Commit();

            Assert.IsTrue(model.Id != default(Guid));

            Repository.Delete(model);
            UnitOfWork.Commit();

            var exists = Repository.Any <TestItemModel>(q => q.Id == model.Id);

            Assert.IsFalse(exists);
        }
コード例 #11
0
        public void Should_Mark_Entity_As_Deleted()
        {
            var entity = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Repository.Save(entity);
            UnitOfWork.Commit();

            Assert.AreNotSame(entity.Id.ToString(), Guid.Empty.ToString());
            Repository.Delete(entity);
            UnitOfWork.Commit();

            var principalProvider = Container.Resolve <IPrincipalProvider>();

            Assert.IsTrue(entity.IsDeleted);
            Assert.IsNotNull(entity.DeletedOn);
            Assert.AreEqual(entity.DeletedByUser, principalProvider.CurrentPrincipalName);
        }
コード例 #12
0
        public void Should_Throw_Concurrent_Data_Exception_Deleting()
        {
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Assert.AreEqual(model.Version, 0);

            Repository.Save(model);
            UnitOfWork.Commit();

            model.Name    = TestDataProvider.ProvideRandomString();
            model.Version = 3;

            Assert.Throws <ConcurrentDataException>(() =>
            {
                Repository.Delete(model);
                UnitOfWork.Commit();
            });
        }
コード例 #13
0
        public void Should_Filter_Null_Or_Whitespace_Column_Correctly()
        {
            var category = DatabaseTestDataProvider.ProvideRandomTestItemCategory();

            category.Name = "   ";

            Repository.Save(category);
            UnitOfWork.Commit();

            TestItemCategory alias = null;
            var loadedCategory     = Repository
                                     .AsQueryOver(() => alias)
                                     .Where(RestrictionsExtensions.IsNullOrWhiteSpace(Projections.Property(() => alias.Name)))
                                     .And(() => alias.Id == category.Id)
                                     .SingleOrDefault <TestItemCategory>();

            Assert.IsNotNull(loadedCategory);
            Assert.AreEqual(category.Id, loadedCategory.Id);
        }
コード例 #14
0
        public void Should_Return_Correct_Items_Future_Count()
        {
            var category1 = DatabaseTestDataProvider.ProvideRandomTestItemCategory();
            var category2 = DatabaseTestDataProvider.ProvideRandomTestItemCategory();
            var category3 = DatabaseTestDataProvider.ProvideRandomTestItemCategory();

            category1.Name = "QEIT_" + category1.Name.Substring(10);
            category2.Name = "QEIT_" + category1.Name.Substring(10);
            category3.Name = "QEIT_" + category1.Name.Substring(10);

            Repository.Save(category1);
            Repository.Save(category2);
            Repository.Save(category3);
            UnitOfWork.Commit();

            var query       = Repository.AsQueryable <TestItemCategory>().Where(c => c.Name.StartsWith("QEIT_"));
            var countFuture = query.ToRowCountFutureValue();
            var future      = query.ToFuture();

            Assert.AreEqual(future.ToList().Count, 3);
            Assert.AreEqual(countFuture.Value, 3);
        }
コード例 #15
0
        public void Should_Increase_Version_If_Dirty()
        {
            var model = DatabaseTestDataProvider.ProvideRandomTestItemModel();

            Assert.AreEqual(model.Version, 0);

            Repository.Save(model);
            UnitOfWork.Commit();

            Assert.AreEqual(model.Version, 1);

            Repository.Save(model);
            UnitOfWork.Commit();

            Assert.AreEqual(model.Version, 1);

            model.Name = TestDataProvider.ProvideRandomString();
            Repository.Save(model);
            UnitOfWork.Commit();

            Assert.AreEqual(model.Version, 2);
        }