Esempio n. 1
0
        public async Task TestMethod_WithFactory()
        {
            using (var factory = new SampleDbContextFactory())
            {
                // Get a context
                using (var context = factory.CreateContext())
                {
                    var user = new User()
                    {
                        Email = "*****@*****.**"
                    };
                    context.Users.Add(user);
                    await context.SaveChangesAsync();
                }

                // Get another context using the same connection
                using (var context = factory.CreateContext())
                {
                    var count = await context.Users.CountAsync();

                    Assert.AreEqual(1, count);

                    var u = await context.Users.FirstOrDefaultAsync(user => user.Email == "*****@*****.**");

                    Assert.IsNotNull(u);
                }
            }
        }
Esempio n. 2
0
        public void Dado_AssociadoMaiorIdade_NaoDeve_AdicionarNotificacaoDeErro()
        {
            using var scope   = StartupTest().CreateScope();
            using var factory = new SampleDbContextFactory();
            using var context = factory.CreateContext();
            context.Database.OpenConnection();
            context.Database.Migrate();
            var _database = scope.ServiceProvider.GetRequiredService <ArchContext>();

            _database.Database.MigrateAsync().GetAwaiter().GetResult();
            var _associadoService    = scope.ServiceProvider.GetService <IAssociadoService>();
            var _notificationContext = scope.ServiceProvider.GetService <INotificationContext>();

            _associadoService.CadastrarAssociado(
                new Associado("Thiago Coppi", 21, "07052429942", new Endereco("Rua tapajos", 74, ""))).GetAwaiter().GetResult();
            Assert.IsFalse(_notificationContext.HaveNotification());
            context.Database.CloseConnection();
        }
Esempio n. 3
0
        public async Task ConcurrencyCheckIsSupported(Provider provider)
        {
            // Arrange
            var contextFactory = new SampleDbContextFactory(provider.Action);

            var id = Guid.NewGuid();

            using (var dbContext = contextFactory.CreateContext())
            {
                await dbContext.Database.EnsureDeletedAsync();

                await dbContext.Database.EnsureCreatedAsync();

                dbContext.Data.Add(new Datum
                {
                    Id        = id,
                    Counter   = 0,
                    CommitTag = Guid.NewGuid()
                });

                await dbContext.SaveChangesAsync();
            }

            async Task ModifyDataAsync()
            {
                while (true)
                {
                    using var dbContext = contextFactory.CreateContext();
                    var entity = await dbContext.Data.SingleAsync(datum => datum.Id == id);

                    entity.Counter++;
                    entity.CommitTag = Guid.NewGuid();

                    try
                    {
                        await dbContext.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        continue;
                    }

                    break;
                }
            }

            // Act
            var t1 = Task.Run(async() => await ModifyDataAsync());
            var t2 = Task.Run(async() => await ModifyDataAsync());
            var t3 = Task.Run(async() => await ModifyDataAsync());
            var t4 = Task.Run(async() => await ModifyDataAsync());

            await Task.WhenAll(t1, t2, t3, t4);

            Datum result;

            using (var dbContext = contextFactory.CreateContext())
            {
                result = await dbContext.Data.SingleAsync(datum => datum.Id == id);
            }

            // Assert

            Assert.Equal(4, result.Counter);
        }