Exemple #1
0
        public async Task UpdateSucceedsIfExpectedChangeTokenMatches()
        {
            DbContextDataProvider <DefaultDataProvider, TestContext> provider = CreateProvider();
            string          changeToken = Guid.NewGuid().ToString("N");
            ITokenGenerator generator   = CreateTokenGenerator(changeToken);

            TestContext testContext = provider.GetContext();
            var         dbEntity    = new DbEntityWithChangeToken()
            {
                ChangeToken = changeToken
            };

            testContext.ChangeTokenEntities.Add(dbEntity);
            await testContext.SaveChangesAsync();

            TestContextChangeTokenExRepository repository = CreateChangeTokenExRepository(provider, generator);
            await repository.UpdateAsync(
                new EntityWithChangeTokenEx { Id = dbEntity.Id, ChangeToken = "abc", ExpectedChangeToken = changeToken },
                CancellationToken.None);
        }
Exemple #2
0
        public async Task CreateUsesExplicitChangeToken()
        {
            DbContextDataProvider <DefaultDataProvider, TestContext> provider = CreateProvider();
            string changeToken = Guid.NewGuid().ToString("N");

            TestContextChangeTokenExRepository repository = CreateChangeTokenExRepository(provider, Substitute.For <ITokenGenerator>());

            EntityWithChangeTokenEx result = await repository.CreateAsync(
                new EntityWithChangeTokenEx { ChangeToken = changeToken },
                CancellationToken.None);

            result.ChangeToken.Should()
            .Be(changeToken);

            DbEntityWithChangeToken dbEntity =
                await provider.GetContext()
                .ChangeTokenEntities.FirstAsync(e => e.Id == result.Id);

            dbEntity.ChangeToken.Should()
            .Be(changeToken);
        }
Exemple #3
0
        public async Task CreateInitializesChangeTokenEx()
        {
            DbContextDataProvider <DefaultDataProvider, TestContext> provider = CreateProvider();
            string          changeToken = Guid.NewGuid().ToString("N");
            ITokenGenerator generator   = CreateTokenGenerator(changeToken);

            TestContextChangeTokenExRepository repository = CreateChangeTokenExRepository(provider, generator);

            EntityWithChangeTokenEx result = await repository.CreateAsync(
                new EntityWithChangeTokenEx(),
                CancellationToken.None);

            result.ChangeToken.Should()
            .Be(changeToken);

            DbEntityWithChangeToken dbEntity =
                await provider.GetContext()
                .ChangeTokenEntities.FirstAsync(e => e.Id == result.Id);

            dbEntity.ChangeToken.Should()
            .Be(changeToken);
        }
Exemple #4
0
        public async Task UpdateThrowsIfExpectedChangeTokenDoesNotMatch()
        {
            DbContextDataProvider <DefaultDataProvider, TestContext> provider = CreateProvider();
            string          changeToken = Guid.NewGuid().ToString("N");
            ITokenGenerator generator   = CreateTokenGenerator(changeToken);

            TestContext testContext = provider.GetContext();
            var         dbEntity    = new DbEntityWithChangeToken()
            {
                ChangeToken = changeToken
            };

            testContext.ChangeTokenEntities.Add(dbEntity);
            await testContext.SaveChangesAsync();

            TestContextChangeTokenExRepository repository = CreateChangeTokenExRepository(provider, generator);

            await Assert.ThrowsAsync <EntityConcurrencyException>(
                () => repository.UpdateAsync(
                    new EntityWithChangeTokenEx {
                Id = dbEntity.Id, ExpectedChangeToken = "abc"
            },
                    CancellationToken.None));
        }