Skip to content

Commit

Permalink
Regular expressions tests (Azure#41)
Browse files Browse the repository at this point in the history
* Adding test: canSearchWithRegex()

* Adding test: canSearchWithRegex()

* Fix search() implementations

* Adding Test searchThrowsWhenSpecialCharInRegexIsUnescaped()

* remove unneeded implementations

* remove unused import

* fix playback file
  • Loading branch information
rabee333 authored and navalev committed Aug 22, 2019
1 parent c1efed5 commit ce69fbf
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,42 @@ public void testCanGetResultCountInSearch() {
.verifyComplete();
}

@Override
public void canSearchWithRegex() {
SearchParameters searchParameters = new SearchParameters()
.queryType(QueryType.FULL)
.select(Arrays.asList("HotelName", "Rating"));

PagedFlux<SearchResult> results = client
.search("HotelName:/.*oach.*\\/?/", searchParameters, new SearchRequestOptions());
Assert.assertNotNull(results);

List<SearchResult> searchResultsList = results.log().collectList().block();
Assert.assertEquals(1, searchResultsList.size());

Map<String, Object> result = searchResultsList.get(0).additionalProperties();

Map<String, Object> expectedHotel = new HashMap<>();
expectedHotel.put("HotelName", "Roach Motel");
expectedHotel.put("Rating", 1);

Assert.assertEquals(dropUnnecessaryFields(result), expectedHotel);
}

@Override
public void canSearchWithEscapedSpecialCharsInRegex() {
SearchParameters searchParameters = new SearchParameters().queryType(QueryType.FULL);

PagedFlux<SearchResult> results = client
.search(
"\\+\\-\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\~\\*\\?\\:", searchParameters,
new SearchRequestOptions());
Assert.assertNotNull(results);

List<SearchResult> searchResultsList = results.log().collectList().block();
Assert.assertEquals(0, searchResultsList.size());
}

private void assertResponse(SearchPagedResponse response, List<Map<String, Object>> actualResults) {
Assert.assertNull(response.count());
Assert.assertNull(response.coverage());
Expand All @@ -251,7 +287,7 @@ private void assertResponse(SearchPagedResponse response, List<Map<String, Objec

@Override
protected void search(String searchText, SearchParameters searchParameters, SearchRequestOptions searchRequestOptions) {
PagedFlux<SearchResult> results = client.search("*", searchParameters, new SearchRequestOptions());
PagedFlux<SearchResult> results = client.search(searchText, searchParameters, searchRequestOptions);
results.log().blockFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class SearchSyncTests extends SearchTestBase {
private SearchIndexClient client;

@Override
protected void search(String searchText, SearchParameters searchParameters, SearchRequestOptions searchRequestOptions) {
PagedIterable<SearchResult> results = client.search("*", searchParameters, new SearchRequestOptions());
protected void search(
String searchText, SearchParameters searchParameters, SearchRequestOptions searchRequestOptions) {
PagedIterable<SearchResult> results = client.search(searchText, searchParameters, searchRequestOptions);
results.iterableByPage().iterator().next();
}

Expand Down Expand Up @@ -144,7 +145,8 @@ private void assertKeySequenceEqual(PagedIterable<SearchResult> results, List<St

@Override
public void searchWithoutOrderBySortsByScore() {
Iterator<SearchResult> results = client.search("*", new SearchParameters().filter("Rating lt 4"), new SearchRequestOptions()).iterator();
Iterator<SearchResult> results = client
.search("*", new SearchParameters().filter("Rating lt 4"), new SearchRequestOptions()).iterator();
SearchResult firstResult = results.next();
SearchResult secondResult = results.next();
Assert.assertTrue(firstResult.score() <= secondResult.score());
Expand All @@ -158,7 +160,9 @@ public void orderByProgressivelyBreaksTies() {

String[] expectedResults = new String[]{"1", "9", "3", "4", "5", "10", "2", "6", "7", "8"};

Stream<String> results = client.search("*", new SearchParameters().orderBy(orderByValues), new SearchRequestOptions()).stream().map(res -> res.additionalProperties().get("HotelId").toString());
Stream<String> results = client
.search("*", new SearchParameters().orderBy(orderByValues), new SearchRequestOptions()).stream()
.map(res -> res.additionalProperties().get("HotelId").toString());
Assert.assertArrayEquals(results.toArray(), expectedResults);
}

Expand Down Expand Up @@ -209,31 +213,71 @@ public void canFilterNonNullableType() throws Exception {

@Override
public void testCanSearchWithSearchModeAll() {
List<Map<String, Object>> response = getSearchResults(client.search("Cheapest hotel", new SearchParameters().queryType(SIMPLE).searchMode(ALL), new SearchRequestOptions()));
List<Map<String, Object>> response = getSearchResults(client
.search("Cheapest hotel", new SearchParameters().queryType(SIMPLE).searchMode(ALL),
new SearchRequestOptions()));
Assert.assertEquals(1, response.size());
Assert.assertEquals("2", response.get(0).get("HotelId"));

}

@Override
public void testDefaultSearchModeIsAny() {
List<Map<String, Object>> response = getSearchResults(client.search("Cheapest hotel", new SearchParameters(), new SearchRequestOptions()));
List<Map<String, Object>> response = getSearchResults(
client.search("Cheapest hotel", new SearchParameters(), new SearchRequestOptions()));
Assert.assertEquals(7, response.size());
Assert.assertEquals(Arrays.asList("2", "10", "3", "4", "5", "1", "9"), response.stream().map(res -> res.get("HotelId").toString()).collect(Collectors.toList()));
Assert.assertEquals(
Arrays.asList("2", "10", "3", "4", "5", "1", "9"),
response.stream().map(res -> res.get("HotelId").toString()).collect(Collectors.toList()));

}


@Override
public void testCanGetResultCountInSearch() {
PagedIterable<SearchResult> results = client.search("*", new SearchParameters().includeTotalResultCount(true), new SearchRequestOptions());
PagedIterable<SearchResult> results = client
.search("*", new SearchParameters().includeTotalResultCount(true), new SearchRequestOptions());
Assert.assertNotNull(results);
Iterator<PagedResponse<SearchResult>> resultsIterator = results.iterableByPage().iterator();

Assert.assertEquals(hotels.size(), ((SearchPagedResponse) resultsIterator.next()).count().intValue());
Assert.assertFalse(resultsIterator.hasNext());
}

@Override
public void canSearchWithRegex() {
SearchParameters searchParameters = new SearchParameters()
.queryType(QueryType.FULL)
.select(Arrays.asList("HotelName", "Rating"));

PagedIterable<SearchResult> results = client
.search("HotelName:/.*oach.*\\/?/", searchParameters, new SearchRequestOptions());
Assert.assertNotNull(results);

List<Map<String, Object>> resultsList = getSearchResults(results);

Map<String, Object> expectedHotel = new HashMap<>();
expectedHotel.put("HotelName", "Roach Motel");
expectedHotel.put("Rating", 1);

Assert.assertEquals(1, resultsList.size());
Assert.assertEquals(dropUnnecessaryFields(resultsList.get(0)), expectedHotel);
}

@Override
public void canSearchWithEscapedSpecialCharsInRegex() {
SearchParameters searchParameters = new SearchParameters().queryType(QueryType.FULL);

PagedIterable<SearchResult> results = client
.search(
"\\+\\-\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\~\\*\\?\\:", searchParameters,
new SearchRequestOptions());
Assert.assertNotNull(results);

List<Map<String, Object>> resultsList = getSearchResults(results);
Assert.assertEquals(0, resultsList.size());
}

private List<Map<String, Object>> getSearchResults(PagedIterable<SearchResult> results) {
Iterator<PagedResponse<SearchResult>> iterator = results.iterableByPage().iterator();
List<Map<String, Object>> searchResults = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.azure.search.data.env.SearchIndexService;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
import com.azure.search.data.generated.models.QueryType;
import com.azure.search.data.generated.models.SearchParameters;
import com.azure.search.data.generated.models.SearchRequestOptions;
import com.azure.search.data.generated.models.SearchResult;
Expand Down Expand Up @@ -181,6 +182,23 @@ public void searchThrowsWhenRequestIsMalformed() {
@Test
public abstract void testCanGetResultCountInSearch();

@Test
public abstract void canSearchWithRegex();

@Test
public abstract void canSearchWithEscapedSpecialCharsInRegex();

@Test
public void searchThrowsWhenSpecialCharInRegexIsUnescaped() {
thrown.expect(HttpResponseException.class);
thrown.expectMessage("Failed to parse query string at line 1, column 8.");

SearchParameters invalidSearchParameters = new SearchParameters()
.queryType(QueryType.FULL);

search("/.*/.*/", invalidSearchParameters, new SearchRequestOptions());
}

abstract void search(String searchText, SearchParameters searchParameters, SearchRequestOptions searchRequestOptions);

abstract void indexDocuments(List<IndexAction> indexActions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"networkCallRecords": [
{
"Method": "POST",
"Uri": "https://azs-sdk86b96718fcdb.search.windows.net/indexes('hotels')/docs/search.index?api-version=2019-05-06",
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Response": {
"Pragma": "no-cache",
"retry-after": "0",
"request-id": "75e8fa58-53b3-476f-894d-0bb1c7022c61",
"StatusCode": "200",
"Date": "Thu, 22 Aug 2019 12:14:57 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Cache-Control": "no-cache",
"elapsed-time": "206",
"OData-Version": "4.0",
"Expires": "-1",
"Content-Length": "795",
"Body": "{\"@odata.context\":\"https://azs-sdk86b96718fcdb.search.windows.net/indexes('hotels')/$metadata#Collection(Microsoft.Azure.Search.V2019_05_06.IndexResult)\",\"value\":[{\"key\":\"1\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"2\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"3\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"4\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"5\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"6\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"7\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"8\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"9\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"10\",\"status\":true,\"errorMessage\":null,\"statusCode\":201}]}",
"Preference-Applied": "odata.include-annotations=\"*\"",
"Content-Type": "application/json; odata.metadata=minimal"
}
},
{
"Method": "POST",
"Uri": "https://azs-sdk86b96718fcdb.search.windows.net/indexes('hotels')/docs/search.post.search?api-version=2019-05-06",
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Response": {
"Pragma": "no-cache",
"retry-after": "0",
"request-id": "f67dbb8c-90c0-42df-9249-6e42437891e5",
"StatusCode": "200",
"Date": "Thu, 22 Aug 2019 12:14:59 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Cache-Control": "no-cache",
"elapsed-time": "34",
"OData-Version": "4.0",
"Expires": "-1",
"Content-Length": "114",
"Body": "{\"@odata.context\":\"https://azs-sdk86b96718fcdb.search.windows.net/indexes('hotels')/$metadata#docs(*)\",\"value\":[]}",
"Preference-Applied": "odata.include-annotations=\"*\"",
"Content-Type": "application/json; odata.metadata=minimal"
}
}
],
"variables": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"networkCallRecords": [
{
"Method": "POST",
"Uri": "https://azs-sdk41d21851476d.search.windows.net/indexes('hotels')/docs/search.index?api-version=2019-05-06",
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Response": {
"Pragma": "no-cache",
"retry-after": "0",
"request-id": "9c4a4854-ab49-482e-98ae-6784ffdce506",
"StatusCode": "200",
"Date": "Thu, 22 Aug 2019 11:31:06 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Cache-Control": "no-cache",
"elapsed-time": "325",
"OData-Version": "4.0",
"Expires": "-1",
"Content-Length": "795",
"Body": "{\"@odata.context\":\"https://azs-sdk41d21851476d.search.windows.net/indexes('hotels')/$metadata#Collection(Microsoft.Azure.Search.V2019_05_06.IndexResult)\",\"value\":[{\"key\":\"1\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"2\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"3\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"4\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"5\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"6\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"7\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"8\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"9\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"10\",\"status\":true,\"errorMessage\":null,\"statusCode\":201}]}",
"Preference-Applied": "odata.include-annotations=\"*\"",
"Content-Type": "application/json; odata.metadata=minimal"
}
},
{
"Method": "POST",
"Uri": "https://azs-sdk41d21851476d.search.windows.net/indexes('hotels')/docs/search.post.search?api-version=2019-05-06",
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Response": {
"Pragma": "no-cache",
"retry-after": "0",
"request-id": "a7d4e71b-b702-4f8e-b482-0312884c6b1e",
"StatusCode": "200",
"Date": "Thu, 22 Aug 2019 11:31:08 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Cache-Control": "no-cache",
"elapsed-time": "193",
"OData-Version": "4.0",
"Expires": "-1",
"Content-Length": "172",
"Body": "{\"@odata.context\":\"https://azs-sdk41d21851476d.search.windows.net/indexes('hotels')/$metadata#docs(*)\",\"value\":[{\"@search.score\":1.0,\"HotelName\":\"Roach Motel\",\"Rating\":1}]}",
"Preference-Applied": "odata.include-annotations=\"*\"",
"Content-Type": "application/json; odata.metadata=minimal"
}
}
],
"variables": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"networkCallRecords": [
{
"Method": "POST",
"Uri": "https://azs-sdk19d56030fd09.search.windows.net/indexes('hotels')/docs/search.index?api-version=2019-05-06",
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Response": {
"Pragma": "no-cache",
"retry-after": "0",
"request-id": "e7fba5c4-9d5c-4b4b-b1fe-1857949ca77e",
"StatusCode": "200",
"Date": "Thu, 22 Aug 2019 16:00:35 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Cache-Control": "no-cache",
"elapsed-time": "167",
"OData-Version": "4.0",
"Expires": "-1",
"Content-Length": "795",
"Body": "{\"@odata.context\":\"https://azs-sdk19d56030fd09.search.windows.net/indexes('hotels')/$metadata#Collection(Microsoft.Azure.Search.V2019_05_06.IndexResult)\",\"value\":[{\"key\":\"1\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"2\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"3\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"4\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"5\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"6\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"7\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"8\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"9\",\"status\":true,\"errorMessage\":null,\"statusCode\":201},{\"key\":\"10\",\"status\":true,\"errorMessage\":null,\"statusCode\":201}]}",
"Preference-Applied": "odata.include-annotations=\"*\"",
"Content-Type": "application/json; odata.metadata=minimal"
}
},
{
"Method": "POST",
"Uri": "https://azs-sdk19d56030fd09.search.windows.net/indexes('hotels')/docs/search.post.search?api-version=2019-05-06",
"Headers": {
"Content-Type": "application/json; charset=utf-8"
},
"Response": {
"Pragma": "no-cache",
"retry-after": "0",
"request-id": "0efd19ae-b539-4c9c-8218-943f2859de4b",
"StatusCode": "400",
"Date": "Thu, 22 Aug 2019 16:00:37 GMT",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Cache-Control": "no-cache",
"elapsed-time": "79",
"OData-Version": "4.0",
"Expires": "-1",
"Content-Length": "148",
"Body": "{\"error\":{\"code\":\"\",\"message\":\"Failed to parse query string at line 1, column 8. See https://aka.ms/azure-search-full-query for supported syntax.\"}}",
"Preference-Applied": "odata.include-annotations=\"*\"",
"Content-Language": "en",
"Content-Type": "application/json; odata.metadata=minimal"
}
}
],
"variables": []
}

0 comments on commit ce69fbf

Please sign in to comment.