public void Setup() { inMemoryConnection = DbConnectionFactory.CreateTransient(); using var context = new MarketDbContext(inMemoryConnection); context.Init(); filterStoreRank = new FilterByStoreRank(4); filterByItemRank = new FilterByItemRank(3); filterByPrice = new FilterByPrice(5, 20.66); RegisteredUser owner = new RegisteredUser("OWNER", new byte[] { }); context.Users.Add(owner); Guid Owner = owner.ID; Guid storeID = Guid.NewGuid(); store = new Store(storeID, DataForTests.CreateTestContactDetails(), new PurchasePolicy(storeID), new DiscountPolicy(storeID), Owner, context); context.Stores.Add(store); HashSet <Category> categories = new HashSet <Category>() { new Category("cat1"), new Category("cat2") }; context.Categories.AddRange(categories); item = new Item(Guid.NewGuid(), store.Id, "name ", 2, categories, 3.55); context.Items.Add(item); context.SaveChanges(); }
public void SearchItems_ByCategoryAndKeywords_WithItemRankFilterAndPriceFilter_ShouldPass() { using var context = new MarketDbContext(inMemoryConnection); List <Item> items = SetUpInventoryForSearchTests(context); items[4].Rank = 3.5; items[5].Rank = 8; SearchFilter rankFilter = new FilterByItemRank(2); SearchFilter priceFilter = new FilterByPrice(13, null); List <SearchFilter> filters = new List <SearchFilter>() { rankFilter, priceFilter }; List <string> keywordsForSearch = new List <string>() { "word1" }; ReadOnlyCollection <Item> results = storeInventory.SearchItems(context: context, category: "cat1", keywords: keywordsForSearch, itemFilters: filters); Assert.AreEqual(1, results.Count); Assert.IsTrue(results.Contains(items[5])); }
/// <summary> /// Search for items in store by the "And" of following params: /// name (if provided) & /// category (if provided) & /// keywords (if provided) & /// standing in filters (if provided) /// </summary> /// <param name="name"></param> /// <param name="category"></param> /// <param name="keywords"></param> /// <param name="filters"></param> /// <returns>dictionary of <storeID, reaonly collection of items></storeID> if there are, otherwise empty dictionary</returns> public Dictionary <Guid, ReadOnlyCollection <Item> > SearchItems( MarketDbContext context, double?filterItemRank, double?filterMinPrice, double?filterMaxPrice, double?filterStoreRank, string name = null, string category = null, List <string> keywords = null ) { List <SearchFilter> filters = new List <SearchFilter>(); if (filterItemRank is double itemRank) { Logger.writeEvent(string.Format("SearchFacade: Filtering by Item Rank- {0}", itemRank)); FilterByItemRank fItemRank = new FilterByItemRank(itemRank); filters.Add(fItemRank); } if (filterMinPrice.HasValue || filterMaxPrice.HasValue) // if at least one has a value there is a filter price { Logger.writeEvent(string.Format("SearchFacade: Filtering by price, Max- {0} Min- {1}", filterMaxPrice.HasValue ? string.Format("N2", filterMaxPrice) : "None", filterMinPrice.HasValue ? string.Format("N2", filterMinPrice) : "None")); FilterByPrice fPrice = new FilterByPrice(filterMinPrice, filterMaxPrice); filters.Add(fPrice); } if (filterStoreRank is double storeRank) { Logger.writeEvent(string.Format("SearchFacade: Filtering by Store Rank- {0}", storeRank)); FilterByStoreRank fStoreRank = new FilterByStoreRank(storeRank); filters.Add(fStoreRank); } if (filters.Count == 0) { filters = null; } Dictionary <Guid, ReadOnlyCollection <Item> > results = storeHandler.SearchItems(context, name: name, category: category, keywords: keywords, filters: filters); if (name != null && results.Keys.Count == 0) { Logger.writeEvent(string.Format("SearchFacade: No results found, trying to fix spelling of \'{0}\'", name)); string tryRepairName = TryCorrectNameBySpellChecking(name); if (!tryRepairName.Equals(name)) { results = storeHandler.SearchItems(context, name: tryRepairName, category: category, keywords: keywords, filters: filters); } } return(results); }
public void SearchItems_ByCategory_StoreRankFilterItemRankFilter_ShouldPass() { using var context = new MarketDbContext(inMemoryConnection); SearchFilter storeRankFilter = new FilterByStoreRank(4.5); SearchFilter itemRankFilter = new FilterByItemRank(6.5); List <SearchFilter> filters = new List <SearchFilter>() { storeRankFilter, itemRankFilter }; Store[] stores = DataForTests.CreateStoresForSearchTests(storeHandler, context); storeHandler.SetStoreRankById(stores[0].Id, 4.5, context); storeHandler.SetStoreRankById(stores[1].Id, 3, context); //update rank of item2 of "store" ReadOnlyCollection <Item> items_Store = stores[0].GetStoreItems(); Guid itemId2 = items_Store[1].Id; Dictionary <StoresUtils.ItemEditDetails, object> newDetails_item2 = new Dictionary <StoresUtils.ItemEditDetails, object>() { { StoresUtils.ItemEditDetails.rank, 6.5 } }; IStoreInventoryManager storeInventoryManager = storeHandler.GetStoreInventoryManager(stores[0].Id, context); storeInventoryManager.EditItem(itemId2, newDetails_item2, context); //update rank of item5 of "store1" ReadOnlyCollection <Item> items_Store1 = stores[1].GetStoreItems(); Guid itemId5 = items_Store1[3].Id; Dictionary <StoresUtils.ItemEditDetails, object> newDetails_item5 = new Dictionary <StoresUtils.ItemEditDetails, object>() { { StoresUtils.ItemEditDetails.rank, 6.9 } }; IStoreInventoryManager storeInventoryManager1 = storeHandler.GetStoreInventoryManager(stores[1].Id, context); storeInventoryManager1.EditItem(itemId5, newDetails_item5, context); //start search test Dictionary <Guid, ReadOnlyCollection <Item> > results = storeHandler.SearchItems(context: context, category: "cat2", filters: filters); Assert.AreEqual(1, results.Keys.Count); ReadOnlyCollection <Item> resultStore = results[stores[0].Id]; Assert.AreEqual("item two", resultStore[0].Name); Assert.AreEqual(1, resultStore.Count); }