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.");
            }
        }
Exemplo n.º 2
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);
            }
        }
        public void Initialize()
        {
            var dependencyConfig = new DependencyConfiguration();
            var mapper           = dependencyConfig.GetInstance <IMapper>();
            var sliQ             = new ShoppingListItem[]
            {
                new ShoppingListItem()
                {
                    Id = Guid.NewGuid(), Amount = 4, Item = new Item()
                    {
                        Id = Guid.NewGuid(), Name = "Aquarius", Description = "Sport energy drink"
                    }
                },
                new ShoppingListItem()
                {
                    Id = Guid.NewGuid(), Amount = 2, Item = new Item()
                    {
                        Id = Guid.NewGuid(), Name = "Watermelon", Description = "Big Fruit"
                    }
                }
            }.AsQueryable();

            sliList = sliQ.Select(sli => mapper.Map <ShoppingListItem>(sli)).ToList();
            var context = new Mock <FMDbContext>();
            var dbSet   = new Mock <DbSet <ShoppingListItem> >();

            dbSet.As <IQueryable <ShoppingListItem> >().Setup(m => m.Provider).Returns(sliQ.Provider);
            dbSet.As <IQueryable <ShoppingListItem> >().Setup(m => m.Expression).Returns(sliQ.Expression);
            dbSet.As <IQueryable <ShoppingListItem> >().Setup(m => m.ElementType).Returns(sliQ.ElementType);
            dbSet.As <IQueryable <ShoppingListItem> >().Setup(m => m.GetEnumerator()).Returns(sliQ.GetEnumerator());
            //dbSet.Setup(d => d.Add(It.IsAny<ShoppinglistItem>())).Callback((ShoppinglistItem sli) => sliList.Add(sli));
            context.Setup(c => c.Set <ShoppingListItem>()).Returns(dbSet.Object);
            _rep = new ShoppingListRepository(context.Object);
        }
        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 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 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);
            }
        }
        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);
            }
        }
        // Initialize

        public override Task <Unit> InitializeAsync() =>
        ShoppingListRepository.GetWithItems(CurrentAccount)
        .Map(newShoppingList => UpdateShoppingList(newShoppingList))
        .Bind(newShoppingList => RecipeRepository.GetDetails(newShoppingList.RecipeItems.Select(i => i.RecipeId)))
        .Map(details => recipeDetails = details.ToImmutableDictionary(d => d.Recipe.Id))
        .Map(_ => Unit.Value)
        .Execute(environment);
        public void should_insert_and_return_shopping_list_item()
        {
            var repository = new ShoppingListRepository();
            var item       = new ShoppingListItemCreate {
                ItemName = "Item1", Quantity = 1
            };
            ShoppingListItem createdItem = repository.Insert(item);

            Assert.AreEqual(createdItem.ItemName, "Item1");
            Assert.AreEqual(createdItem.Quantity, 1);
        }
Exemplo n.º 10
0
        public void Get_ShouldRetriveAShoppingListAndPopulateListItems_WhenIncludeListItemsParameterIsTrue()
        {
            var testDataKeys = CreateTestData();

            using (var dbContext = TestUtils.CreateDbContext(_connection, new MockUserContext(testDataKeys.UserId)))
                using (IUnitOfWork unitOfWork = new EfUnitOfWork(dbContext))
                {
                    var shoppingListRepository = new ShoppingListRepository(dbContext);
                    var shoppingList           = shoppingListRepository.Get(testDataKeys.ShoppingListId, includeListItems: true);
                    Assert.AreEqual(2, shoppingList.ListItems.Count);
                }
        }
        public void should_insert_shopping_list_item_to_data_store()
        {
            var repository = new ShoppingListRepository();
            var item       = new ShoppingListItemCreate {
                ItemName = "Item1", Quantity = 1
            };

            repository.Insert(item);

            Assert.AreEqual(_dataStore.Count, 1);
            Assert.AreEqual(_dataStore[0].ItemName, "Item1");
            Assert.AreEqual(_dataStore[0].Quantity, 1);
        }
Exemplo n.º 12
0
        public void FindAllForUser_ShouldFindAShoppingList_ForTheGivenTitleAndCreator()
        {
            var testDataKeys = CreateTestData();

            using (var dbContext = TestUtils.CreateDbContext(_connection, new MockUserContext(testDataKeys.UserId)))
                using (IUnitOfWork unitOfWork = new EfUnitOfWork(dbContext))
                {
                    var shoppingListRepository = new ShoppingListRepository(dbContext);
                    var shoppingList           = shoppingListRepository.FindByTitle("Shopping list to find", testDataKeys.UserId);
                    Assert.AreEqual(testDataKeys.Marker, shoppingList.Title);
                    Assert.AreEqual(testDataKeys.UserId, shoppingList.CreatorId.ToString());
                }
        }
Exemplo n.º 13
0
        public void FindAllForUser_ShouldFindAllShoppingLists_ForTheGivenCreator()
        {
            var testDataKeys = CreateTestData();

            using (var dbContext = TestUtils.CreateDbContext(_connection))
                using (IUnitOfWork unitOfWork = new EfUnitOfWork(dbContext))
                {
                    var shoppingListRepository = new ShoppingListRepository(dbContext);
                    var shoppingLists          = shoppingListRepository.FindAllForUser(testDataKeys.UserId).ToList();
                    Assert.AreEqual(testDataKeys.UserId, shoppingLists[0].CreatorId);
                    Assert.AreEqual(testDataKeys.UserId, shoppingLists[1].CreatorId);
                }
        }
        public void should_delete_shopping_list_item_in_data_store()
        {
            _dataStore.Add(new ShoppingListItem {
                ItemName = "Item1", Quantity = 1
            });

            Assert.AreEqual(_dataStore.Count, 1);

            var repository = new ShoppingListRepository();

            repository.Delete("Item1");

            Assert.AreEqual(_dataStore.Count, 0);
        }
Exemplo n.º 15
0
        public ShoppingListViewModel()
        {
            ShopRep = new ShoppingListRepository();

            SaveShoppingListCommand = new Command(async() =>
            {
                var shoppingList      = new ShoppingList();
                shoppingList.ListName = TheListName;
                shoppingList.Products = "";
                await ShopRep.SaveShoppingListAsync(shoppingList);
                TheListName = string.Empty;
                TheProducts = string.Empty;
            });
        }
Exemplo n.º 16
0
        public GetShoppingListHandler()
        {
            _repo   = new ShoppingListRepository();
            _pubnub = new Pubnub("pub-c-81182831-bf67-42f1-8a0c-1cae468c5d6f", "sub-c-d7bd3004-a74c-11e6-b237-02ee2ddab7fe");
            _pubnub.Subscribe <string>(
                "GetShoppingList",
                DisplaySubscribeReturnMessage,
                x => {},
                y => {}
                );

            _pubnub.Subscribe <string>("AddNewShoppingListItem", AddNewShoppingListItem, x => {}, y => {});
            _pubnub.Subscribe <string>("ShoppingListItemRemove", ShoppingListItemRemove, x => {}, y => {});
            _pubnub.Subscribe <string>("ToggleShoppingListItem", ToggleShoppingListItem, x => {}, y => {});
        }
        public void should_update_shopping_list_item_in_data_store()
        {
            _dataStore.Add(new ShoppingListItem {
                ItemName = "Item1", Quantity = 1
            });

            var repository = new ShoppingListRepository();
            var item       = new ShoppingListItemUpdate {
                ItemName = "Item1", Quantity = 5
            };

            repository.Update(item);

            Assert.AreEqual(_dataStore[0].ItemName, "Item1");
            Assert.AreEqual(_dataStore[0].Quantity, 5);
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            int option = 0;

            Console.WriteLine("ShoppingList List v 0.1");
            Console.WriteLine("Menu:");
            Console.WriteLine("1. Add Item");
            Console.WriteLine("2. Remove Item");
            Console.WriteLine("3. Update Item");
            Console.WriteLine("4. Show All");
            Console.WriteLine("5. exit");

            ShoppingListRepository repository = new ShoppingListRepository();

            while (option != 5)
            {
                Console.Write("Choose option: ");
                option = int.Parse(Console.ReadLine());

                switch (option)
                {
                case 1:
                    Item newItem = new Item {
                        Name = "Pomidor"
                    };
                    repository.AddItem(newItem);
                    break;

                case 2:
                    repository.DeleteItem(0);
                    break;

                case 3:
                    Item updateItem = new Item();
                    repository.UpdateItem(0, updateItem);
                    break;

                case 4:
                    repository.GetItems();
                    break;

                default:
                    break;
                }
            }
        }
        public void should_not_use_paging_if_page_size_is_invalid(int pageSize)
        {
            for (int i = 1; i <= 20; i++)
            {
                _dataStore.Add(new ShoppingListItem {
                    ItemName = $"Item{i}", Quantity = 1
                });
            }

            var getListOptions = new ShoppingListItemGetList {
                PageNumber = 10, PageSize = pageSize
            };

            var repository = new ShoppingListRepository();
            var items      = repository.GetItems(getListOptions);

            Assert.AreEqual(items.Count, 20);
        }
        public void should_get_shopping_list_from_data_store()
        {
            _dataStore.Add(new ShoppingListItem {
                ItemName = "Item1", Quantity = 1
            });
            _dataStore.Add(new ShoppingListItem {
                ItemName = "Item2", Quantity = 2
            });

            var repository = new ShoppingListRepository();
            var items      = repository.GetItems();

            Assert.AreEqual(items.Count, 2);
            Assert.AreEqual(items[0].ItemName, "Item1");
            Assert.AreEqual(items[0].Quantity, 1);
            Assert.AreEqual(items[1].ItemName, "Item2");
            Assert.AreEqual(items[1].Quantity, 2);
        }
        public void Get_All_If_EmptyList_Return_EmptyList()
        {
            //Arrange
            DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions();

            using (var context = new ShoppingListContext(options))
            {
                IShoppingListRepository mockRepo = new ShoppingListRepository(context);
                var controller     = new ShoppingListController(mockRepo);
                var expectedResult = new OkObjectResult(new List <DrinkOrder>());

                //Act
                var result = controller.Get() as OkObjectResult;

                //Assert
                Assert.IsNotNull(result);
                Assert.AreEqual(200, result.StatusCode);
                CollectionAssert.Equals(expectedResult.Value, result.Value);
            }
        }
Exemplo n.º 22
0
        public ShoppingListViewModel(ShoppingList shoppingList)
        {
            ShopRep = new ShoppingListRepository();

            TheListName = shoppingList.ListName;
            TheProducts = shoppingList.Products;

            SaveShoppingListCommand = new Command(async() =>
            {
                shoppingList.ListName = TheListName;
                shoppingList.Products = TheProducts;
                await ShopRep.SaveShoppingListAsync(shoppingList);
                TheListName = string.Empty;
                TheProducts = string.Empty;
            });

            RemoveShoppingList = new Command(async() =>
            {
                await ShopRep.DeleteShoppingListAsync(shoppingList);
            });
        }
        public void Put_To_Existing_Item_Updates_Quantity()
        {
            //Arrange
            DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions();

            var mockData = MockData.LargeShoppingList();

            using (var context = new ShoppingListContext(options))
            {
                context.AddRange(mockData);
                context.SaveChanges();
                context.Dispose();
            };

            var expectedObject = new DrinkOrder
            {
                Name     = "Pepsi",
                Quantity = 4
            };

            var expectedResult = new OkObjectResult(expectedObject);

            using (var context = new ShoppingListContext(options))
            {
                IShoppingListRepository mockRepo = new ShoppingListRepository(context);

                var controller = new ShoppingListController(mockRepo);

                //Act
                var result = controller.Put(expectedObject) as OkObjectResult;

                //Assert
                Assert.IsNotNull(result);
                Assert.AreEqual(200, result.StatusCode);
                Assert.AreEqual(result.Value, expectedResult.Value);
                //Check context is updated
                Assert.AreEqual(expectedObject.Quantity, context.shoppingList.FirstOrDefault(x => x.Name == expectedObject.Name).Quantity);
            }
        }
        public void should_get_shopping_list_from_data_store_with_paging_when_not_first_page()
        {
            var pageSize = 10;

            for (int i = 1; i <= 50; i++)
            {
                _dataStore.Add(new ShoppingListItem {
                    ItemName = $"Item{i}", Quantity = 1
                });
            }

            var getListOptions = new ShoppingListItemGetList {
                PageNumber = 3, PageSize = pageSize
            };

            var repository = new ShoppingListRepository();
            var items      = repository.GetItems(getListOptions);

            Assert.AreEqual(items.Count, 10);
            Assert.AreEqual(items[0].ItemName, "Item21");
            Assert.AreEqual(items[0].Quantity, 1);
            Assert.AreEqual(items[pageSize - 1].ItemName, "Item30");
            Assert.AreEqual(items[pageSize - 1].Quantity, 1);
        }
        public void Put_To_Non_Existing_Item_Returns_Not_Found()
        {
            DbContextOptions <ShoppingListContext> options = new TestHelper().GetShoppingListContextOptions();

            var mockData = MockData.LargeShoppingList();

            using (var context = new ShoppingListContext(options))
            {
                context.AddRange(mockData);
                context.SaveChanges();
                context.Dispose();
            };

            var expectedObject = new DrinkOrder
            {
                Name     = "Does Not Exist",
                Quantity = 4
            };

            var expectedResult = new OkObjectResult(expectedObject);

            using (var context = new ShoppingListContext(options))
            {
                IShoppingListRepository mockRepo = new ShoppingListRepository(context);

                var controller = new ShoppingListController(mockRepo);

                //Act
                var result = controller.Put(expectedObject) as NotFoundObjectResult;

                //Assert
                Assert.IsNotNull(result);
                Assert.AreEqual(404, result.StatusCode);
                Assert.AreEqual(result.Value, "Drink: Does Not Exist not found on the shopping list.");
            }
        }
        public void DeleteShoppingListAsync_NullParameterThrows()
        {
            repository = new ShoppingListRepository(context);

            Assert.ThrowsAsync <ArgumentNullException>(() => repository.DeleteShoppingListAsync(null));
        }
Exemplo n.º 27
0
 public ShoppingListController(ShoppingListRepository repository, IMapper mapper)
 {
     shoppingListRepository = repository;
     dtoMapper = mapper;
 }
 public ShoppingListListViewModel()
 {
     ShopRep = new ShoppingListRepository();
 }
 public ShoppingListService(ShoppingListRepository repository)
 {
     _repository = repository;
 }
Exemplo n.º 30
0
 public ShoppingListService() : base(new ShoppingListRepository())
 {
     _listRepository = (ShoppingListRepository)_repository;
 }