//Uses a decorator pattern to construct and return a filtered IQueryable. private IQueryable <Item> Search(string searchString, string getItemsBy) { searchString = searchString.ToLower(); ItemSearchToDecorate toDecorate = new ItemSearchToDecorate(_context); Filter decorated = null; switch (getItemsBy) { case "Name": var nameContains = new NameContains(searchString); nameContains.setToBeDecorated(toDecorate); decorated = nameContains; break; case "Description": var descriptionContains = new DescriptionContains(searchString); descriptionContains.setToBeDecorated(toDecorate); decorated = descriptionContains; break; default: var defaulted = new NameContains(searchString); defaulted.setToBeDecorated(toDecorate); decorated = defaulted; break; } return(decorated.GetItems()); }
public void SpellsSearch_NameContainsFIlter_NameContainsTower() { //Arrange List <Spell> spells = CreateTestData.GetListOfSpells(); var mockSet = new Mock <DbSet <Spell> >() .SetupData(spells, o => { return(spells.Single(x => x.Spell_id.CompareTo(o.First()) == 0)); }); List <Spell> expected = new List <Spell>(); Spell Tower = new Spell { Spell_id = Guid.Parse("46d10bb8-84d2-408d-a928-5847ff99461f"), Name = "Widogast's Nascent Nine-sided Tower", Description = "A flavored Magnificent Mansion", Level = 7, School_id = Guid.Parse("361bd911-0702-437f-ab59-a29da0f9fba4"), CastingTime = "1 minute", Range = "100 feet", Duration = "24 hours", RequiresVerbal = true, RequiresSomantic = true, RequiresMaterial = false, RequiresConcentration = true }; expected.Add(Tower); using (var mockContext = AutoMock.GetLoose()) { mockContext.Mock <SpellsContext>() .Setup(x => x.Spells).Returns(mockSet.Object); mockContext.Mock <SpellsContext>() .Setup(x => x.Set <Spell>()).Returns(mockSet.Object); SpellsContext context = mockContext.Create <SpellsContext>(); //Act SpellSearchToDecorate baseObject = new SpellSearchToDecorate(context); NameContains toTest = new NameContains("Tower"); toTest.setToBeDecorated(baseObject); var actual = toTest.GetSpells().ToList(); //Assert actual.Should().BeEquivalentTo(expected); } }
public void ItemSearch_NameContainsFilter_NameContainsKnuckles() { //Arrange List <Item> items = CreateTestData.GetListOfItems(); var mockSet = new Mock <DbSet <Item> >() .SetupData(items, o => { return(items.Single(x => x.Item_id.CompareTo(o.First()) == 0)); }); List <Item> expected = new List <Item>(); Item TitanstoneKnuckles = new Item { Item_id = Guid.Parse("026a7dff-5e85-4e6d-94c6-6613828e5df6"), Name = "Titanstone Knuckles", Description = "Gauntlets fashioned from the Titan of Stone, enhancing your strength to rival that of the gods.", isEquippable = true, isConsumable = false, requiresAttunement = true, Value = 999 }; expected.Add(TitanstoneKnuckles); using (var mockContext = AutoMock.GetLoose()) { mockContext.Mock <ItemsContext>() .Setup(x => x.Items).Returns(mockSet.Object); mockContext.Mock <ItemsContext>() .Setup(x => x.Set <Item>()).Returns(mockSet.Object); ItemsContext context = mockContext.Create <ItemsContext>(); //Act ItemSearchToDecorate baseObject = new ItemSearchToDecorate(context); NameContains toTest = new NameContains("Knuckles"); toTest.setToBeDecorated(baseObject); var actual = toTest.GetItems().ToList(); //Assert actual.Should().BeEquivalentTo(expected); } }