Files
raven/test/raven-integration/Search/SearchOps.cs
2018-09-25 18:28:56 +00:00

177 lines
6.6 KiB
C#

using System;
using Xunit;
using Newtonsoft.Json.Linq;
using System.Linq;
using FluentAssertions;
using System.Collections.Generic;
namespace raven_integration
{
public class SearchOps
{
/// <summary>
/// Test simple phrase only search is fundamentally working
/// </summary>
[Fact]
public async void PhraseOnlySearchShouldReturnCorrectResultsInOrder()
{
const string TEST_SEARCH_PHRASE = "simple dogs";
//CREATE A WIDGET
dynamic D = new JObject();
D.name = Util.Uniquify("Search Simple Test WIDGET");
D.dollarAmount = 1.11m;
D.active = true;
D.roles = 0;
D.notes = "The quick brown and simple fox jumped over the six lazy dogs!";
ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
Util.ValidateDataReturnResponseOk(a);
long MatchFirstWidgetId = a.ObjectResponse["result"]["id"].Value<long>();
//CREATE A USER
D = new JObject();
D.name = Util.Uniquify("Search simple Test User");
D.notes = "This user has the word dogs in its notes";
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 MatchUserId = a.ObjectResponse["result"]["id"].Value<long>();
//CREATE A SECOND WIDGET
D = new JObject();
D.name = Util.Uniquify("Search simple as in dogs SECOND Test WIDGET");
D.dollarAmount = 1.11m;
D.active = true;
D.roles = 0;
D.notes = "This Widget should be returned in the search as it contains both keywords in the name";
a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
Util.ValidateDataReturnResponseOk(a);
long MatchSecondWidgetId = a.ObjectResponse["result"]["id"].Value<long>();
//CREATE A THIRD WIDGET
D = new JObject();
D.name = Util.Uniquify("Search Simple THIRD Test WIDGET");
D.dollarAmount = 1.11m;
D.active = true;
D.roles = 0;
D.notes = "This Widget should not be returned in the search as it only contains a single keyword in the name not both";
a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
Util.ValidateDataReturnResponseOk(a);
long NoMatchThirdWidgetId = a.ObjectResponse["result"]["id"].Value<long>();
//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(3);
//Turn the list into an array of id's
var v = ((JArray)a.ObjectResponse["result"]);
List<long> MatchingIdList = new List<long>();
foreach (JObject j in v)
{
MatchingIdList.Add(j["id"].Value<long>());
}
//Ensure the expected items are returned
MatchingIdList.Should().Contain(MatchFirstWidgetId, "ShouldContainFirstWidget");
MatchingIdList.Should().Contain(MatchSecondWidgetId, "ShouldContainSecondWidget");
MatchingIdList.Should().Contain(MatchUserId, "ShouldContainUser");
MatchingIdList.Should().NotContain(NoMatchThirdWidgetId, "ShouldNotContainThirdWidget");
//Assert the order (roughly, this is kind of a waste of time, either the code is sorting or not, it's not going to change)
//first item must be a widget
a.ObjectResponse["result"][0]["type"].Value<int>().Should().Be(2);
//final item must be a user
a.ObjectResponse["result"][MatchingIdList.Count - 1]["type"].Value<int>().Should().Be(3);
}//eot
/*
{{
"result": [
{
"name": "Search simple as in dogs SECOND Test WIDGET1537899693",
"type": 2,
"id": 205
},
{
"name": "Search Simple Test WIDGET1537899693",
"type": 2,
"id": 204
},
{
"name": "Search simple as in dogs SECOND Test WIDGET1537899490",
"type": 2,
"id": 202
},
{
"name": "Search Simple Test WIDGET1537899489",
"type": 2,
"id": 201
},
{
"name": "Search simple Test User1537899693",
"type": 3,
"id": 22
},
{
"name": "Search simple Test User1537899490",
"type": 3,
"id": 21
}
]
}}
*/
// TODO: TEST RESULTS ARE RETURNED GROUPED (together) BY OBJECT TYPE THEN OBJECT ID DESCENDING
//TODO: TEST THAT RIGHTS ARE WORKING PROPERLY WITH RETURNED RESULTS
//NAME ONLY SEARCH SHOULD WORK WITH NO RIGHTS TO READ FULL RECORD
//IF NO RIGHTS TO FULL READ AND SEARCH IS ON FULL OBJECT AND NOT NAME THEN IT SHOULD NOT RETURN THE RESULTS
//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
//TODO: TAG PLUS REGULAR SEARCH PHRASE PLUS WILDCARD PHRASE TEST
//TODO: TAG PLUS USER TEST OR MODIFY ALL OF THE ABOVE TO INCLUDE A USER INSTEAD OF A SECOND TEST WIDGET
//==================================================
}//eoc
}//eons