Exemplo n.º 1
0
        public void AddCustomer_Database_test()
        {
            // arrange
            using var connection = new SqliteConnection("Data Source=:memory:");
            connection.Open();
            var options  = new DbContextOptionsBuilder <StoreAppDbContext>().UseSqlite(connection).Options;
            var customer = new Customer
            {
                Id        = 1,
                FirstName = "Jerry",
                LastName  = "Smith",
                Email     = "*****@*****.**"
            };

            // act
            using (var context = new StoreAppDbContext(options))
            {
                context.Database.EnsureCreated();
                var repo = new StoreRepository(context, new NullLogger <StoreRepository>());

                repo.AddCustomer(customer);
            }
            //assert
            using var context2 = new StoreAppDbContext(options);
            CustomerEntity customerActual = context2.Customers
                                            .Single(l => l.FirstName == "Jerry");

            Assert.Equal(customer.FirstName, customerActual.FirstName);
        }
Exemplo n.º 2
0
        public void AddProduct_Database_test()
        {
            // arrange
            using var connection = new SqliteConnection("Data Source=:memory:");
            connection.Open();
            var options = new DbContextOptionsBuilder <StoreAppDbContext>().UseSqlite(connection).Options;
            var product = new Product
            {
                Id    = 1,
                Name  = "Walmart",
                Price = 5
            };

            // act
            using (var context = new StoreAppDbContext(options))
            {
                context.Database.EnsureCreated();
                var repo = new StoreRepository(context, new NullLogger <StoreRepository>());

                repo.AddProduct(product);
            }
            //assert
            using var context2 = new StoreAppDbContext(options);
            ProductEntity productActual = context2.Products
                                          .Single(l => l.Name == "Walmart");

            Assert.Equal(product.Name, productActual.Name);
        }
Exemplo n.º 3
0
        public void AddLocation_Database_Test()
        {
            // arrange
            using var connection = new SqliteConnection("Data Source=:memory:");
            connection.Open();
            var options  = new DbContextOptionsBuilder <StoreAppDbContext>().UseSqlite(connection).Options;
            var location = new Location
            {
                Id   = 1,
                Name = "Walmart"
            };

            // act
            using (var context = new StoreAppDbContext(options))
            {
                context.Database.EnsureCreated();
                var repo = new StoreRepository(context, new NullLogger <StoreRepository>());

                repo.AddLocation(location);
            }
            //assert
            using var context2 = new StoreAppDbContext(options);
            LocationEntity customerActual = context2.Locations
                                            .Single(l => l.Name == "Walmart");

            Assert.Equal(location.Name, customerActual.Name);
        }
Exemplo n.º 4
0
        private ProductRepository GetInMemoryProductRepository()
        {
            DbContextOptions <StoreAppDbContext> options;
            var builder = new DbContextOptionsBuilder <StoreAppDbContext>();

            builder.UseInMemoryDatabase("InMemoryProductTestDB");
            options = builder.Options;
            StoreAppDbContext context = new StoreAppDbContext(options);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            return(new ProductRepository(context));
        }
Exemplo n.º 5
0
 public UserRepository(StoreAppDbContext context)
 {
     _context = context;
 }
Exemplo n.º 6
0
 public StoreRepository(StoreAppDbContext context, ILogger <StoreRepository> logger)
 {
     _context = context ?? throw new ArgumentNullException();
     _logger  = logger ?? throw new ArgumentNullException();
 }