コード例 #1
0
        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);
            }
        }
コード例 #2
0
 public List <FileStorage> ReadAll()
 {
     using (var context = new ShoppingListContext())
     {
         return(context.FileStorage.ToList());
     }
 }
コード例 #3
0
        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");
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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.");
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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");
        }
コード例 #8
0
 public List <Group> GetGroups()
 {
     using (ShoppingListContext context = ShoppingListContextFactory.Create())
     {
         return(context.Groups.ToList());
     }
 }
コード例 #9
0
ファイル: Manager.cs プロジェクト: Barbedx/ShoppingList
 private Manager()
 {
     if (_ctx == null)
     {
         _ctx = new ShoppingListContext();
     }
 }
コード例 #10
0
        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);
            }
        }
コード例 #11
0
        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);
        }
コード例 #12
0
 public List <Category> ReadAll()
 {
     using (var context = new ShoppingListContext())
     {
         return(context.Category.OrderBy(c => c.Name).ToList());
     }
 }
コード例 #13
0
        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.");
            }
        }
コード例 #14
0
        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);
            }
        }
コード例 #15
0
        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();
                    }
                }
            }
        }
コード例 #16
0
        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();
                }
            }
        }
コード例 #17
0
        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));
            }
        }
コード例 #18
0
        public void SetUp()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ShoppingListContext>();

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());
            context         = new ShoppingListContext(optionsBuilder.Options);
            drinkRepository = new Repository <Drink, string>(context);
        }
コード例 #19
0
        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;
 }
コード例 #21
0
 public Cart Create(Cart cart)
 {
     using (var _context = new ShoppingListContext())
     {
         _context.Cart.Add(cart);
         DbContextHelper.HandleUniqueKeyViolation(() => _context.SaveChanges());
         return(cart);
     }
 }
コード例 #22
0
 public List <Product> ReadAll()
 {
     using (var _context = new ShoppingListContext())
     {
         return(_context.Product
                .Include(c => c.Category)
                .Include(f => f.FileStorage)
                .ToList());
     }
 }
コード例 #23
0
 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));
     }
 }
コード例 #24
0
        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();
            //}
        }
コード例 #25
0
 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());
     }
 }
コード例 #26
0
 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));
 }
コード例 #27
0
        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);
        }
コード例 #28
0
 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());
     }
 }
コード例 #29
0
        public ShoppingListController(ShoppingListContext context)
        {
            _context = context;

            if (_context.ShoppingListItems.Count() == 0)
            {
                _context.ShoppingListItems.Add(new ShoppingListItem {
                    Name = "Bananas"
                });
                _context.SaveChanges();
            }
        }
コード例 #30
0
        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);
        }