public async void Should_ReturnMoreThanOneShoppingList() { var options = ReturnDbContextOptions("Should_ReturnMoreThanOneShoppingList"); // Arrange using (var context = new ShoppingListContext(options)) { await context.ShoppingList.AddRangeAsync( new ShoppingList { Name = "Test1", BudgetSum = 10 }, new ShoppingList { Name = "Test2", BudgetSum = 15 }); await context.SaveChangesAsync(); } using (var context = new ShoppingListContext(options)) { var sut = new ShoppingListController(new Repository(context)); // Act var result = await sut.GetAllShoppingListsAsync(); // Assert Assert.True(result.Value.Count > 1); Assert.Equal(2, result.Value.Count); } }
public List <FileStorage> ReadAll() { using (var context = new ShoppingListContext()) { return(context.FileStorage.ToList()); } }
public override async Task Execute(Message message, TelegramBotClient botClient) { string listName = GetListName(message.Text); ShoppingListContext context = new ShoppingListContext(); ShopList shopList = context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id).FirstOrDefault(s => s.NameOfList == listName); if (shopList == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There's no such list."); return; } foreach (string item in GetItems(message.Text)) { BuyItem buyItem = context.BuyItems.Where(b => b.ShopListId == shopList.ShopListId).FirstOrDefault(b => b.Item == item); if (buyItem == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There is no element '" + item + "' in list '" + listName + "'"); continue; } context.BuyItems.Remove(buyItem); } context.SaveChanges(); await botClient.SendTextMessageAsync(message.Chat.Id, "Items was succesfully removed"); }
public void Delete_Removes_Existing_Drink_From_Shopping_List() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var expectedResult = mockData.FirstOrDefault(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get(expectedResult.Name); //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); Assert.AreEqual(result.Value, expectedResult); } }
public void Post_Existing_Item_Returns_Conflict() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var testObject = mockData.FirstOrDefault(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); //Act var result = controller.Post(testObject); //Assert Assert.IsNotNull(result); Assert.AreEqual(409, result.StatusCode); Assert.AreEqual(result.Value, $"Drink {testObject.Name} already exists in the shopping list."); } }
public void Post_To_Empty_List_Successfully_Adds_Item() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var expectedObject = new DrinkOrder { Name = "Pepsi", Quantity = 1 }; var expectedResult = new CreatedResult("", expectedObject); using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); //Act var result = controller.Post(expectedObject) as CreatedResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(201, result.StatusCode); //Can't effectively mock HttpRequest, and don't really want to go down that rabbit hole so just check //it's not null. Assert.IsNotNull(result.Location); Assert.AreEqual(result.Value, expectedResult.Value); } }
public override async Task Execute(Message message, TelegramBotClient botClient) { ShoppingListContext context = new ShoppingListContext(); string listName = GetListName(message.Text); ShopList shopList = context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id).FirstOrDefault(s => s.NameOfList == listName); if (shopList == null) { await botClient.SendTextMessageAsync(message.Chat.Id, "There's no such list"); return; } foreach (string i in GetItems(message.Text)) { if (context.BuyItems.Where(b => b.ShopListId == shopList.ShopListId).FirstOrDefault(b => b.Item == i) != null) { await botClient.SendTextMessageAsync(message.Chat.Id, i + " already in list."); continue; } BuyItem item = new BuyItem { Item = i, ShopList = shopList, ShopListId = shopList.ShopListId }; context.BuyItems.Add(item); } context.SaveChanges(); await botClient.SendTextMessageAsync(message.Chat.Id, "Items was succesfully added"); }
public List <Group> GetGroups() { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { return(context.Groups.ToList()); } }
private Manager() { if (_ctx == null) { _ctx = new ShoppingListContext(); } }
public void Get_All_Return_Expected_List() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var expectedResult = new OkObjectResult(mockData); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get(); //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); CollectionAssert.Equals(expectedResult.Value, result.Value); } }
private static IEnumerable <PropertyInfo> GetPrimaryKeyFieldsFor <TBusinessEntity>() { Type entityType = typeof(TBusinessEntity); if (primaryKeys.ContainsKey(entityType)) { return(primaryKeys[entityType]); } ShoppingListContext dbContext = new ShoppingListContext(); var _objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; var metadata = _objectContext.MetadataWorkspace .GetItems <EntityType>(DataSpace.OSpace) .SingleOrDefault(p => p.FullName == entityType.FullName); if (metadata == null) { throw new InvalidOperationException($"The type {entityType.FullName} is not known to the DbContext."); } var results = metadata.KeyMembers .Select(k => entityType.GetProperty(k.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) .ToList(); primaryKeys[entityType] = results; return(results); }
public List <Category> ReadAll() { using (var context = new ShoppingListContext()) { return(context.Category.OrderBy(c => c.Name).ToList()); } }
public void Get_Non_Existant_Item_Returns_Not_Found() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get("Does Not Exist"); //Assert Assert.IsNotNull(result); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual(result.Value, "Drink: Does Not Exist not found on the shopping list."); } }
public async void Should_ReturnOneShoppingList() { var options = ReturnDbContextOptions("Should_ReturnOneShoppingList"); // Arrange using (var context = new ShoppingListContext(options)) { await context.ShoppingList.AddAsync(new ShoppingList { Name = "Test", BudgetSum = 20 }); await context.SaveChangesAsync(); } using (var context = new ShoppingListContext(options)) { var sut = new ShoppingListController(new Repository(context)); // Act var result = await sut.GetAllShoppingListsAsync(); // Assert Assert.Single(result.Value); } }
public void DeleteListItem(string listName, long itemId, int quantity) { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { List targetList = context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .FirstOrDefault(); if (targetList != null) { ListItem itemToDelete = targetList.ListItems.Where(item => item.Id == itemId).FirstOrDefault(); if (itemToDelete != null) { if (itemToDelete.Quantity <= quantity) { context.ListItems.Remove(itemToDelete); } else { itemToDelete.Quantity -= quantity; } context.SaveChanges(); } } } }
public void AddItemToList(string listName, long itemId, int quantity) { // Add a ListItem entry for the specified ID to the specified list using (ShoppingListContext context = ShoppingListContextFactory.Create()) { List targetList = context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .FirstOrDefault(); if (targetList != null) { // If there is already an item with the same identity in the list then simply update its quantity ListItem itemToUpdate = targetList.ListItems.Where(item => item.ItemId == itemId).FirstOrDefault(); if (itemToUpdate != null) { itemToUpdate.Quantity += quantity; } else { context.ListItems.Add(new ListItem { ItemId = itemId, ListId = targetList.Id, Quantity = quantity }); } context.SaveChanges(); } } }
public void Get_Item_Returns_Expected_Item() { //Arrange DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions(); var mockData = MockData.LargeShoppingList(); var expectedObject = mockData.FirstOrDefault(); var expectedResult = new OkObjectResult(expectedObject); using (var context = new ShoppingListContext(options)) { context.AddRange(mockData); context.SaveChanges(); }; using (var context = new ShoppingListContext(options)) { IShoppingListRepository mockRepo = new ShoppingListRepository(context); var controller = new ShoppingListController(mockRepo); //Act var result = controller.Get(expectedObject.Name) as OkObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); Assert.IsTrue(expectedResult.Value.Equals(result.Value)); } }
public void SetUp() { var optionsBuilder = new DbContextOptionsBuilder <ShoppingListContext>(); optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()); context = new ShoppingListContext(optionsBuilder.Options); drinkRepository = new Repository <Drink, string>(context); }
public ActionResult DeleteConfirmed(int id) { ShoppingListContext shoppingListContext = db.ShoppingList.Find(id); db.ShoppingList.Remove(shoppingListContext); db.SaveChanges(); return(RedirectToAction("Index")); }
public ShoppingListRepository(ShoppingListContext dbContext, IToDomainConverter <Entities.ShoppingList, IShoppingList> toModelConverter, IToEntityConverter <IShoppingList, Entities.ShoppingList> toEntityConverter) { this.dbContext = dbContext; this.toModelConverter = toModelConverter; this.toEntityConverter = toEntityConverter; }
public Cart Create(Cart cart) { using (var _context = new ShoppingListContext()) { _context.Cart.Add(cart); DbContextHelper.HandleUniqueKeyViolation(() => _context.SaveChanges()); return(cart); } }
public List <Product> ReadAll() { using (var _context = new ShoppingListContext()) { return(_context.Product .Include(c => c.Category) .Include(f => f.FileStorage) .ToList()); } }
public Product ReadSingle(ProductFindRequest request) { using (var _context = new ShoppingListContext()) { return(_context.Product .Include(c => c.Category) .Include(f => f.FileStorage) .FirstOrDefault(p => p.Id == request.ProductId)); } }
public ShoppingListController(ShoppingListContext context) { _context = context; //if (_context.ShoppingList.Count() == 0) //{ // _context.ShoppingList.Add(new ShoppingItem { Title = "Google Pixel 3", Description = "A Google Product" }); // _context.SaveChanges(); //} }
public User ReadById(UserFindRequest request) { using (var _context = new ShoppingListContext()) { return(_context.User .Where(u => u.Id == request.Id) .Include(au => au.Addresses.Select(u => u.User_Id == request.Id)) .FirstOrDefault()); } }
public ActionResult Edit([Bind(Include = "Id,UserId,Name,Color,CreatedUtc,ModifiedUtc")] ShoppingListContext shoppingListContext) { if (ModelState.IsValid) { db.Entry(shoppingListContext).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(shoppingListContext)); }
public override async Task Execute(Message message, TelegramBotClient botClient) { ShoppingListContext context = new ShoppingListContext(); string answer = ""; foreach (ShopList shopList in context.ShopLists.Where(s => s.User.UserTelegramId == message.From.Id)) { answer += shopList.NameOfList + "\n"; } await botClient.SendTextMessageAsync(message.Chat.Id, answer); }
public List GetList(string listName) { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { return(context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .ThenInclude(itemInList => itemInList.Item) .ThenInclude(item => item.Group) .FirstOrDefault()); } }
public ShoppingListController(ShoppingListContext context) { _context = context; if (_context.ShoppingListItems.Count() == 0) { _context.ShoppingListItems.Add(new ShoppingListItem { Name = "Bananas" }); _context.SaveChanges(); } }
public static ShoppingListContext Create() { // Pass the SQLite connection options to the context ShoppingListContext context = new ShoppingListContext( new DbContextOptionsBuilder <ShoppingListContext>(). UseSqlite($"Filename={ConfigurationService.FullDatabasePath}").Options); // Ensure that the SQLite database and sechema is created! context.Database.EnsureCreated(); return(context); }