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_ValidOrder_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", "*****@*****.**"); var location = new Location("TestLocation", "", "", "", "", "", ""); Dictionary <int, int> productsList = new Dictionary <int, int>(); var order = new Order(location, customer, DateTime.Now, productsList); using (var context = new DataModel.Models.TrainingProjectContext(options)) { context.Database.EnsureCreated(); var repo = new StoreRepository(context); repo.AddCustomer(customer); repo.AddLocation(location); customer.Id = 1; location.Id = 1; repo.UpdateLocationStock(location); repo.Save(); // act repo.AddOrder(order); repo.Save(); } // assert using var context2 = new DataModel.Models.TrainingProjectContext(options); DataModel.Models.Order orderActual = context2.Orders.Single(o => o.Id == 1); Assert.Equal(order.Time, orderActual.Date); }
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); }
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); }
static void Main(string[] args) { ////For the debugging the code for SQL using var logStream = new StreamWriter("ef-logs.txt"); //// DbContextOptions is how we give the context its connection string (to log in to the sql server), //// tell it to use SQL server var optionsBuilder = new DbContextOptionsBuilder <Project0Context>(); optionsBuilder.UseSqlServer(GetConnectionString()); optionsBuilder.LogTo(logStream.Write, LogLevel.Information); using var context = new Project0Context(optionsBuilder.Options); StoreRepository storeRepo = new StoreRepository(context); int action = chooseAction(); while (action != 0) { var random = new Random(); Console.WriteLine("You chose " + action); switch (action) { //place an order case 2: Console.WriteLine("let's place an order"); int Id = random.Next(1, 1000); int OrderId = random.Next(1, 1000); string ProductId; int PID; string Pr; int Price; string Qua; int Quantity; Console.WriteLine("Enter the Product Id"); ProductId = Console.ReadLine(); Console.WriteLine("Enter the Price"); Pr = Console.ReadLine(); Console.WriteLine("Enter the Quantity"); Qua = Console.ReadLine(); PID = Convert.ToInt32(ProductId); Price = Convert.ToInt32(Pr); Quantity = Convert.ToInt32(Qua); var newSales = new Library.Sale(Id, OrderId, PID, Price, Quantity); storeRepo.AddSales(newSales); break; // add a new customer case 1: Console.WriteLine("Let's add a new customer"); string Name = ""; int CustomerId = random.Next(1, 10000); Console.WriteLine("Enter the name"); Name = Console.ReadLine(); var newCustomer = new Library.Customer(CustomerId, Name); storeRepo.AddCustomer(newCustomer); Console.WriteLine(Name + " was added"); break; // Search Customer by Name case 6: Console.WriteLine("Let's Search for a customer"); string SName = ""; Console.WriteLine("Enter the name"); SName = Console.ReadLine(); var oldCustomer = new Library.Customer(SName); //storeRepo.SearchCustomers(oldCustomer); break; // Display Details of An Order case 3: break; // Display Order History of a Store case 4: break; // Display Order History of a Customer case 5: break; default: break; } action = chooseAction(); } }