public void Repo_DuplicateCustomer_DbRejects() { // arrange using var connection = new SqliteConnection("Data Source=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <DataModel.Models.TrainingProjectContext>().UseSqlite(connection).Options; var customer = new Customer("John", "Doe", "*****@*****.**"); var customer2 = new Customer("John", "Doe", "*****@*****.**"); using (var context = new DataModel.Models.TrainingProjectContext(options)) { context.Database.EnsureCreated(); var repo = new StoreRepository(context); repo.AddCustomer(customer); repo.Save(); // act repo.AddCustomer(customer2); try { repo.Save(); } catch (DbUpdateException e) { } // assert var transaction = context.ChangeTracker.Entries() .Where(x => x.State == EntityState.Added || x.State == EntityState.Deleted || x.State == EntityState.Modified).ToList(); Assert.Empty(transaction); } using var context2 = new DataModel.Models.TrainingProjectContext(options); Assert.Equal(1, context2.Customers.Count()); }
public void Repo_ValidCustomer_AddsToDb() { // arrange using var connection = new SqliteConnection("Data Source=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <DataModel.Models.TrainingProjectContext>().UseSqlite(connection).Options; var customer = new Customer("John", "Doe", "*****@*****.**"); using (var context = new DataModel.Models.TrainingProjectContext(options)) { context.Database.EnsureCreated(); var repo = new StoreRepository(context); // act repo.AddCustomer(customer); repo.Save(); } // assert using var context2 = new DataModel.Models.TrainingProjectContext(options); DataModel.Models.Customer customerActual = context2.Customers.Single(c => c.Email == "*****@*****.**"); Assert.Equal(customer.FirstName, customerActual.FirstName); }