public static lib.Order Map(db.Order dbOrder) { lib.Order libOrder = new lib.Order { Id = dbOrder.Id, LocationId = dbOrder.LocationId, UserId = dbOrder.UserId, TimePlaced = dbOrder.TimePlaced }; List <db.OrderJunction> orderJunctions = orderJunctionRepo.GetAllOrderJunctions() .Where(o => o.OrderId == dbOrder.Id).ToList(); foreach (var orderJunction in orderJunctions) { List <db.PizzaJunction> pizzaJunctions = pizzaRepo.GetAllPizzaJunctions() .Where(p => p.PizzaId == orderJunction.PizzaId).ToList(); lib.Pizza libPizza = new lib.Pizza { Id = orderJunction.PizzaId }; foreach (var pizzaJunction in pizzaJunctions) { for (int i = 0; i < pizzaJunction.Count; ++i) { libPizza.AddIngredientsToPizza(lib.Ingredient.GetById(pizzaJunction.IngredientId).Name); } } libOrder.AddPizzaToOrder(libPizza); } return(libOrder); }
public void OrderCreateSucceedsWithValidLocationIdAndUserId() { // Arrange RepoTesting repo = new RepoTesting(); repo.ResetDatabase("Order_Test_2"); dbm.Location dbLocation = new dbm.Location { Name = "a" }; repo.locationRepo.Create(dbLocation); repo.SaveChanges(); dbm.User dbUser = new dbm.User { Id = 2, FirstName = "John", LastName = "Pot", DefaultLocationId = dbLocation.Id }; repo.userRepo.Create(dbUser); repo.SaveChanges(); // Ensure the entities exist Assert.NotNull(repo.locationRepo.GetById(dbLocation.Id)); Assert.NotNull(repo.userRepo.GetById(dbUser.Id)); dbm.Order dbOrder = new dbm.Order { Id = 2, LocationId = dbLocation.Id, UserId = dbUser.Id, TimePlaced = DateTime.Now, TotalPrice = 20.50m }; // Act repo.orderRepo.Create(dbOrder); repo.SaveChanges(); // Assert // New order should be findable Assert.NotNull(repo.orderRepo.GetById(dbOrder.Id)); }
public void OrderCreateFailsWithInvalidLocationId() { // Arrange RepoTesting repo = new RepoTesting(); repo.ResetDatabase("Order_Test_3"); dbm.User dbUser = new dbm.User { Id = 2, FirstName = "John", LastName = "Pot" }; repo.userRepo.Create(dbUser); repo.SaveChanges(); int invalidLocationId = -1; // Ensure the user is valid Assert.NotNull(repo.userRepo.GetById(dbUser.Id)); // Ensure the invalidLocationId is in fact invalid Assert.Throws <e.InvalidIdException>(() => repo.locationRepo.GetById(invalidLocationId)); dbm.Order dbOrder = new dbm.Order { Id = 2, LocationId = invalidLocationId, UserId = dbUser.Id, TimePlaced = DateTime.Now, TotalPrice = 20.50m }; // Act Assert.Throws <e.InvalidIdException>(() => repo.orderRepo.Create(dbOrder)); // Assert // New order should not have been added to the database Assert.Throws <e.InvalidIdException>(() => repo.orderRepo.GetById(dbOrder.Id)); }
public void OrderCreateFailsWithInvalidUserId() { // Arrange RepoTesting repo = new RepoTesting(); repo.ResetDatabase("Order_Test_4"); dbm.Location dbLocation = new dbm.Location { Id = 2, Name = "a" }; repo.locationRepo.Create(dbLocation); repo.SaveChanges(); int invalidUserId = -1; // Ensure the location is valid Assert.NotNull(repo.locationRepo.GetById(dbLocation.Id)); // Ensure the user is invalid Assert.Throws <e.InvalidIdException>(() => repo.userRepo.GetById(invalidUserId)); dbm.Order dbOrder = new dbm.Order { Id = 2, LocationId = dbLocation.Id, UserId = invalidUserId, TimePlaced = DateTime.Now, TotalPrice = 20.50m }; // Act // Create should throw an exception Assert.Throws <e.InvalidIdException>(() => repo.orderRepo.Create(dbOrder)); // Assert // New order should not be searchable Assert.Throws <e.InvalidIdException>(() => repo.orderRepo.GetById(dbOrder.Id)); }
public static db.Order Map(lib.Order libOrder, out List <db.OrderJunction> orderJunctionList, out List <db.PizzaJunction> pizzaJunctionList) { db.Order dbOrder = new db.Order { Id = libOrder.Id, LocationId = libOrder.LocationId, UserId = libOrder.UserId, TimePlaced = libOrder.TimePlaced, TotalPrice = libOrder.TotalPrice }; orderJunctionList = new List <db.OrderJunction>(); pizzaJunctionList = new List <db.PizzaJunction>(); foreach (var pizza in libOrder.Pizzas) { db.OrderJunction orderJunction = new db.OrderJunction { OrderId = libOrder.Id, PizzaId = pizza.Id }; orderJunctionList.Add(orderJunction); foreach (var ingredient in pizza.Ingredients.Distinct()) { db.PizzaJunction pizzaJunction = new db.PizzaJunction { PizzaId = pizza.Id, IngredientId = ingredient.Id, Count = pizza.Ingredients.Where(i => i.Id == ingredient.Id).Count() }; pizzaJunctionList.Add(pizzaJunction); } } return(dbOrder); }
public void ResetDatabase(string dbName) { var options = new DbContextOptionsBuilder <db.PizzaStoreDBContext>() .UseInMemoryDatabase(dbName).Options; database = new db.PizzaStoreDBContext(options); ingredientRepo = new IngredientRepository(database); locationRepo = new LocationRepository(database); userRepo = new UserRepository(database); inventoryRepo = new InventoryJunctionRepository(database); pizzaRepo = new PizzaJunctionRepository(database); orderJunctionRepo = new OrderJunctionRepository(database); orderRepo = new OrderRepository(database); // Going to add one entry to each repo for testing purposes. dbIngredient = new dbm.Ingredient { Id = 9999, Name = "cheese", Price = 1.50m }; ingredientRepo.Create(dbIngredient); dbLocation = new dbm.Location { Id = 9999, Name = "John's Pizzaria" }; locationRepo.Create(dbLocation); // Save should populate the above entities' Ids SaveChanges(); dbUser = new dbm.User { Id = 9999, FirstName = "John", LastName = "Pot", DefaultLocationId = dbLocation.Id }; userRepo.Create(dbUser); dbInventory = new dbm.InventoryJunction { LocationId = dbLocation.Id, IngredientId = dbIngredient.Id, Count = 100 }; inventoryRepo.Create(dbInventory); // Have to manually set the pizza junction id since it is a nested many-to-many relationship Random rand = new Random(DateTime.Now.TimeOfDay.Milliseconds); dbPizza = new dbm.PizzaJunction { PizzaId = dbm.PizzaJunction.GetNewId(), IngredientId = dbIngredient.Id, Count = 2 }; pizzaRepo.Create(dbPizza); // Update user id for order usage SaveChanges(); dbOrder = new dbm.Order { Id = 9999, LocationId = dbLocation.Id, UserId = dbUser.Id, TimePlaced = DateTime.Now, TotalPrice = 20.50m }; orderRepo.Create(dbOrder); // Order junction needs order to have an Id SaveChanges(); dbOrderJunction = new dbm.OrderJunction { OrderId = dbOrder.Id, PizzaId = dbPizza.PizzaId }; orderJunctionRepo.Create(dbOrderJunction); SaveChanges(); // All tables should now have one entry. }