Files
raven/test/raven-integration/Search/SearchOps.cs
2018-09-21 18:51:11 +00:00

96 lines
3.5 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
/// </summary>
[Fact]
public async void PhraseOnlySearchShouldWork()
{
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 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 that widget 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<long> MatchingIdList = new List<long>();
foreach (JObject j in v)
{
MatchingIdList.Add(j["id"].Value<long>());
}
MatchingIdList.Should().Contain(MatchFirstWidgetId, "ShouldContainFirstWidget");
MatchingIdList.Should().Contain(MatchSecondWidgetId, "ShouldContainSecondWidget");
MatchingIdList.Should().NotContain(NoMatchThirdWidgetId, "ShouldNotContainThirdWidget");
}//eot
//==================================================
}//eoc
}//eons