예제 #1
0
        public void Should_Write_History_For_Tracked_Entities_Create_To_Database()
        {
            // Forward calls from substitute to implementation
            var entityHistoryStore = Resolve <EntityHistoryStore>();

            _entityHistoryStore.When(x => x.SaveAsync(Arg.Any <EntityChangeSet>()))
            .Do(callback => AsyncHelper.RunSync(() =>
                                                entityHistoryStore.SaveAsync(callback.Arg <EntityChangeSet>()))
                );
            _entityHistoryStore.When(x => x.Save(Arg.Any <EntityChangeSet>()))
            .Do(callback => entityHistoryStore.Save(callback.Arg <EntityChangeSet>()));

            UsingDbContext((context) =>
            {
                context.EntityChanges.Count(e => e.TenantId == 1).ShouldBe(0);
                context.EntityChangeSets.Count(e => e.TenantId == 1).ShouldBe(0);
                context.EntityPropertyChanges.Count(e => e.TenantId == 1).ShouldBe(0);
            });

            var justNow = Clock.Now;
            var blog2Id = CreateBlogAndGetId();

            UsingDbContext((context) =>
            {
                context.EntityChanges.Count(e => e.TenantId == 1).ShouldBe(1);
                context.EntityChangeSets.Count(e => e.TenantId == 1).ShouldBe(1);
                context.EntityChangeSets.Single().CreationTime.ShouldBeGreaterThan(justNow);
                context.EntityPropertyChanges.Count(e => e.TenantId == 1).ShouldBe(3);
            });
        }
        public void Should_Write_History_For_Tracked_Entities_Create_To_Database()
        {
            // Forward calls from substitute to implementation
            var entityHistoryStore = Resolve <EntityHistoryStore>();

            _entityHistoryStore.When(x => x.SaveAsync(Arg.Any <EntityChangeSet>()))
            .Do(callback => AsyncHelper.RunSync(() =>
                                                entityHistoryStore.SaveAsync(callback.Arg <EntityChangeSet>()))
                );
            _entityHistoryStore.When(x => x.Save(Arg.Any <EntityChangeSet>()))
            .Do(callback => entityHistoryStore.Save(callback.Arg <EntityChangeSet>()));

            UsingDbContext((context) =>
            {
                context.EntityChanges.Count(e => e.TenantId == 1).ShouldBe(0);
                context.EntityChangeSets.Count(e => e.TenantId == 1).ShouldBe(0);
                context.EntityPropertyChanges.Count(e => e.TenantId == 1).ShouldBe(0);
            });

            /* Advertisement does not have Audited attribute. */
            Resolve <IEntityHistoryConfiguration>().Selectors.Add("Selected", typeof(Advertisement));

            var justNow = Clock.Now;

            Thread.Sleep(1);

            WithUnitOfWork(() =>
            {
                _advertisementRepository.InsertAndGetId(new Advertisement {
                    Banner = "tracked-advertisement"
                });
            });

            UsingDbContext((context) =>
            {
                context.EntityChanges.Count(e => e.TenantId == 1).ShouldBe(1);
                context.EntityChangeSets.Count(e => e.TenantId == 1).ShouldBe(1);
                context.EntityChangeSets.Single().CreationTime.ShouldBeGreaterThan(justNow);
                context.EntityPropertyChanges.Count(e => e.TenantId == 1).ShouldBe(1);
            });
        }