Skip to content

Commit 455a9e7

Browse files
authored
[Search] Add mock tests for search facets (Azure#30068)
1 parent bcb8477 commit 455a9e7

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
using Azure.Core.Pipeline;
8+
using Azure.Core.TestFramework;
9+
using Azure.Search.Documents.Models;
10+
using NUnit.Framework;
11+
12+
namespace Azure.Search.Documents.Tests
13+
{
14+
internal class SearchMockTests : ClientTestBase
15+
{
16+
private static readonly string s_endpoint = "https://search.cognitiveservices.azure.com/";
17+
private static readonly string s_apiKey = "FakeapiKey";
18+
19+
public SearchMockTests(bool isAsync) : base(isAsync)
20+
{
21+
}
22+
23+
private SearchClient CreateTestClient(HttpPipelineTransport transport)
24+
{
25+
var options = new SearchClientOptions(SearchClientOptions.ServiceVersion.V2021_04_30_Preview)
26+
{
27+
Transport = transport
28+
};
29+
30+
var client = InstrumentClient(new SearchClient(new Uri(s_endpoint), "fakeIndex", new AzureKeyCredential(s_apiKey), options));
31+
32+
return client;
33+
}
34+
35+
[Test]
36+
public async Task ValueFacets()
37+
{
38+
var mockResponse = new MockResponse(200);
39+
40+
var content = @"{
41+
""@search.facets"":{
42+
""category"":[
43+
{""count"":2,""value"":""Luxury""},
44+
{""count"":1,""value"":""Boutique""},
45+
{""count"":1,""value"":""Budget""}
46+
]},
47+
""value"":[
48+
{""@search.score"":1.0,""hotelId"":""3"",""hotelName"":""EconoStay"",""description"":""Very popular hotel in town"",""descriptionFr"":""H\u00f4tel le plus populaire en ville"",""category"":""Boutique"",""tags"":[""wifi"",""budget""],""parkingIncluded"":true,""smokingAllowed"":false,""lastRenovationDate"":""1995-07-01T00:00:00Z"",""rating"":4,""location"":{""type"":""Point"",""coordinates"":[-122.131577,46.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""geoLocation"":{""type"":""Point"",""coordinates"":[-122.131577,46.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""address"":null,""rooms"":[]},
49+
{""@search.score"":1.0,""hotelId"":""2"",""hotelName"":""Roach Motel"",""description"":""Cheapest hotel in town. Infact, a motel."",""descriptionFr"":""H\u00f4tel le moins cher en ville. Infact, un motel."",""category"":""Budget"",""tags"":[""motel"",""budget""],""parkingIncluded"":true,""smokingAllowed"":true,""lastRenovationDate"":""1982-04-28T00:00:00Z"",""rating"":1,""location"":{""type"":""Point"",""coordinates"":[-122.131577,49.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""geoLocation"":{""type"":""Point"",""coordinates"":[-122.131577,49.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""address"":null,""rooms"":[]},
50+
{""@search.score"":1.0,""hotelId"":""4"",""hotelName"":""Express Rooms"",""description"":""Pretty good hotel"",""descriptionFr"":""Assez bon h\u00f4tel"",""category"":""Luxury"",""tags"":[""wifi"",""budget""],""parkingIncluded"":true,""smokingAllowed"":false,""lastRenovationDate"":""1995-07-01T00:00:00Z"",""rating"":4,""location"":{""type"":""Point"",""coordinates"":[-122.131577,48.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""geoLocation"":{""type"":""Point"",""coordinates"":[-122.131577,48.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""address"":null,""rooms"":[]},
51+
{""@search.score"":1.0,""hotelId"":""1"",""hotelName"":""Fancy Stay"",""description"":""Best hotel"",""descriptionFr"":null,""category"":""Luxury"",""tags"":[""pool"",""view"",""wifi"",""concierge""],""parkingIncluded"":false,""smokingAllowed"":false,""lastRenovationDate"":""2010-06-27T00:00:00Z"",""rating"":5,""location"":{""type"":""Point"",""coordinates"":[-122.131577,47.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""geoLocation"":{""type"":""Point"",""coordinates"":[-122.131577,47.678581],""crs"":{""type"":""name"",""properties"":{""name"":""EPSG:4326""}}},""address"":null,""rooms"":[]}
52+
]}";
53+
54+
mockResponse.SetContent(content);
55+
56+
var mockTransport = new MockTransport(new[] { mockResponse });
57+
var client = CreateTestClient(mockTransport);
58+
Response<SearchResults<Hotel>> response =
59+
await client.SearchAsync<Hotel>(
60+
"*",
61+
new SearchOptions
62+
{
63+
Facets = new[]
64+
{
65+
"category,sort:count",
66+
}
67+
});
68+
69+
AssertFacetsEqual(
70+
GetFacetsForField(response.Value.Facets, "category", 3),
71+
MakeValueFacet(2, "Luxury"),
72+
MakeValueFacet(1, "Boutique"),
73+
MakeValueFacet(1, "Budget"));
74+
}
75+
76+
public FacetResult MakeValueFacet(int count, object value) => SearchModelFactory.FacetResult(count, new Dictionary<string, object>()
77+
{
78+
["value"] = value
79+
});
80+
81+
private void AssertFacetsEqual(ICollection<FacetResult> actualFacets, params FacetResult[] expectedFacets)
82+
{
83+
Assert.AreEqual(actualFacets.Count, expectedFacets.Length);
84+
int i = 0;
85+
foreach (FacetResult actualFacet in actualFacets)
86+
{
87+
FacetResult expectedFacet = expectedFacets[i++];
88+
Assert.AreEqual(expectedFacet.Count, actualFacet.Count);
89+
CollectionAssert.IsSubsetOf(actualFacet.Keys, expectedFacet.Keys);
90+
foreach (string key in expectedFacet.Keys)
91+
{
92+
Assert.AreEqual(
93+
expectedFacet[key],
94+
actualFacet.TryGetValue(key, out object value) ? value : null);
95+
}
96+
}
97+
}
98+
99+
private ICollection<FacetResult> GetFacetsForField(IDictionary<string, IList<FacetResult>> facets, string expectedField, int expectedCount)
100+
{
101+
Assert.True(facets.ContainsKey(expectedField), $"Expecting facets to contain {expectedField}");
102+
ICollection<FacetResult> fieldFacets = facets[expectedField];
103+
Assert.AreEqual(expectedCount, fieldFacets.Count);
104+
return fieldFacets;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)