diff --git a/server/AyaNova/biz/Search.cs b/server/AyaNova/biz/Search.cs index b3613f6d..ca5613b8 100644 --- a/server/AyaNova/biz/Search.cs +++ b/server/AyaNova/biz/Search.cs @@ -157,11 +157,11 @@ namespace AyaNova.Biz //Contains? if (WildCardSearchTerm.StartsWith("%") && WildCardSearchTerm.EndsWith("%")) { - DictionaryMatches.AddRange(await ct.SearchDictionary.Where(m => m.Word.EndsWith(WildCardSearchTerm.Replace("%", ""))).Select(m => m.Id).ToListAsync()); + DictionaryMatches.AddRange(await ct.SearchDictionary.Where(m => m.Word.Contains(WildCardSearchTerm.Replace("%", ""))).Select(m => m.Id).ToListAsync()); } else if (WildCardSearchTerm.EndsWith("%")) //STARTS WITH? { - DictionaryMatches.AddRange(await ct.SearchDictionary.Where(m => m.Word.EndsWith(WildCardSearchTerm.Replace("%", ""))).Select(m => m.Id).ToListAsync()); + DictionaryMatches.AddRange(await ct.SearchDictionary.Where(m => m.Word.StartsWith(WildCardSearchTerm.Replace("%", ""))).Select(m => m.Id).ToListAsync()); } else if (WildCardSearchTerm.StartsWith("%"))//ENDS WITH? { diff --git a/test/raven-integration/Search/SearchOps.cs b/test/raven-integration/Search/SearchOps.cs index 212f5dc9..f365c486 100644 --- a/test/raven-integration/Search/SearchOps.cs +++ b/test/raven-integration/Search/SearchOps.cs @@ -10,9 +10,7 @@ namespace raven_integration public class SearchOps { - /// - /// Test simple phrase only search is fundamentally working - /// + [Fact] public async void PhraseOnlySearchShouldReturnCorrectResultsInOrder() { @@ -129,7 +127,6 @@ namespace raven_integration //Only BizAdmin* roles can read a full user record but anyone should be able to see names //This search should return zero items - a = await Util.PostAsync("Search", await Util.GetTokenAsync("SubContractorLimited"), SearchParameters.ToString()); Util.ValidateDataReturnResponseOk(a); ((JArray)a.ObjectResponse["result"]).Count.Should().Be(0, "User with no rights should not see any results in body search"); @@ -157,52 +154,136 @@ namespace raven_integration MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId"); MatchingIdList.Should().NotContain(MatchNothingWidgetId, "ShouldNotContainThirdWidget"); + }//eot + + + + //TODO: WILDCARD SEARCH TEST + //TODO: WILDCARD PLUS NON WILDCARD SEARCH TEST + + [Fact] + public async void WildCardStartsWithSearchShouldWork() + { + const string TEST_SEARCH_PHRASE = "hap* goose"; + + //CREATE A WIDGET + dynamic D = new JObject(); + D.name = Util.Uniquify("Wildcard startswith search test WIDGET"); + D.dollarAmount = 1.11m; + D.active = true; + D.roles = 0; + D.notes = "This record will match in notes: The quick brown and hapless goose"; + + ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); + Util.ValidateDataReturnResponseOk(a); + long MatchWidgetInNotesId = a.ObjectResponse["result"]["id"].Value(); + + //CREATE FIRST TEST USER WITH PHRASE IN NAME + D = new JObject(); + D.name = Util.Uniquify("Wildcard startswith search NAME happy goose Test User"); + D.notes = "This user has the match in it's name"; + D.ownerId = 1L; + D.active = true; + D.login = Util.Uniquify("LOGIN"); + D.password = Util.Uniquify("PASSWORD"); + D.roles = 0;//norole + D.localeId = 1;//random locale + D.userType = 3;//non scheduleable + + a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); + Util.ValidateDataReturnResponseOk(a); + long MatchUserInNameId = a.ObjectResponse["result"]["id"].Value(); + + + //Now see if can find those objects with a phrase search + dynamic SearchParameters = new JObject(); + + SearchParameters.phrase = TEST_SEARCH_PHRASE; + SearchParameters.nameOnly = false; + SearchParameters.typeOnly = 0;//no type + a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString()); + Util.ValidateDataReturnResponseOk(a); + + //Now validate the return list + ((JArray)a.ObjectResponse["result"]).Count.Should().BeGreaterOrEqualTo(2); + + //Turn the list into an array of id's + var v = ((JArray)a.ObjectResponse["result"]); + List MatchingIdList = new List(); + foreach (JObject j in v) + { + MatchingIdList.Add(j["id"].Value()); + } + + //Ensure the expected items are returned + MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId"); + MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId"); }//eot - /* - {{Initial search results: borked! - {{ - "result": [ - { - "name": "Search NAME simple as in dogs Test WIDGET1537911374", - "type": 2, - "id": 202 - }, - { - "name": "Search NOTES Test WIDGET1537911373", - "type": 2, - "id": 201 - }, - { - "name": "Search NOTES Test User1537911374", - "type": 3, - "id": 22 - }, - { - "name": "Search NAME DOGS simple Test User1537911374", - "type": 3, - "id": 21 - } - ] -}} - */ + [Fact] + public async void WildCardEndsWithSearchShouldWork() + { + const string TEST_SEARCH_PHRASE = "goose *act"; + + //CREATE A WIDGET + dynamic D = new JObject(); + D.name = Util.Uniquify("Wildcard endswith search test WIDGET"); + D.dollarAmount = 1.11m; + D.active = true; + D.roles = 0; + D.notes = "This record will match in notes: react to the goose"; + + ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); + Util.ValidateDataReturnResponseOk(a); + long MatchWidgetInNotesId = a.ObjectResponse["result"]["id"].Value(); + + //CREATE FIRST TEST USER WITH PHRASE IN NAME + D = new JObject(); + D.name = Util.Uniquify("Wildcard endswith search NAME goose exact Test User"); + D.notes = "This user has the match in it's name"; + D.ownerId = 1L; + D.active = true; + D.login = Util.Uniquify("LOGIN"); + D.password = Util.Uniquify("PASSWORD"); + D.roles = 0;//norole + D.localeId = 1;//random locale + D.userType = 3;//non scheduleable + + a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString()); + Util.ValidateDataReturnResponseOk(a); + long MatchUserInNameId = a.ObjectResponse["result"]["id"].Value(); + //Now see if can find those objects with a phrase search + dynamic SearchParameters = new JObject(); + + SearchParameters.phrase = TEST_SEARCH_PHRASE; + SearchParameters.nameOnly = false; + SearchParameters.typeOnly = 0;//no type + a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString()); + Util.ValidateDataReturnResponseOk(a); + + //Now validate the return list + ((JArray)a.ObjectResponse["result"]).Count.Should().BeGreaterOrEqualTo(2); + + //Turn the list into an array of id's + var v = ((JArray)a.ObjectResponse["result"]); + List MatchingIdList = new List(); + foreach (JObject j in v) + { + MatchingIdList.Add(j["id"].Value()); + } + + //Ensure the expected items are returned + MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId"); + MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId"); + + }//eot - - - //TODO: TEST THAT NAME ONLY WORKS AS EXPECTED - - //TODO: TEST THAT NAME ONLY WORKS EVEN WITH NON-READ RIGHTS TO FULL OBJECT - - //TODO: WILDCARD SEARCH TEST - - //TODO: WILDCARD PLUS NON WILDCARD SEARCH TEST - //TODO: TAG SEARCH ALONE TEST //TODO: TAG PLUS SEARCH PHRASE TEST