1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import json
from os.path import dirname, join, realpath
import pytest
from devtools_testutils import AzureMgmtTestCase
from azure_devtools.scenario_tests import ReplayableTest
from search_service_preparer import SearchServicePreparer, SearchResourceGroupPreparer
from azure.core import MatchConditions
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.search.documents.indexes.models import(
SynonymMap,
)
from azure.search.documents.indexes import SearchIndexClient
CWD = dirname(realpath(__file__))
SCHEMA = open(join(CWD, "hotel_schema.json")).read()
try:
BATCH = json.load(open(join(CWD, "hotel_small.json")))
except UnicodeDecodeError:
BATCH = json.load(open(join(CWD, "hotel_small.json"), encoding='utf-8'))
TIME_TO_SLEEP = 5
CONNECTION_STRING = 'DefaultEndpointsProtocol=https;AccountName=storagename;AccountKey=NzhL3hKZbJBuJ2484dPTR+xF30kYaWSSCbs2BzLgVVI1woqeST/1IgqaLm6QAOTxtGvxctSNbIR/1hW8yH+bJg==;EndpointSuffix=core.windows.net'
class SearchSynonymMapsClientTest(AzureMgmtTestCase):
FILTER_HEADERS = ReplayableTest.FILTER_HEADERS + ['api-key']
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_create_synonym_map(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
result = client.create_synonym_map(synonym_map)
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
assert len(client.get_synonym_maps()) == 1
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_delete_synonym_map(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
result = client.create_synonym_map(synonym_map)
assert len(client.get_synonym_maps()) == 1
client.delete_synonym_map("test-syn-map")
assert len(client.get_synonym_maps()) == 0
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_delete_synonym_map_if_unchanged(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
result = client.create_synonym_map(synonym_map)
etag = result.e_tag
synonym_map.synonyms = "\n".join([
"Washington, Wash. => WA",
])
client.create_or_update_synonym_map(synonym_map)
result.e_tag = etag
with pytest.raises(HttpResponseError):
client.delete_synonym_map(result, match_condition=MatchConditions.IfNotModified)
assert len(client.get_synonym_maps()) == 1
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_get_synonym_map(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
client.create_synonym_map(synonym_map)
assert len(client.get_synonym_maps()) == 1
result = client.get_synonym_map("test-syn-map")
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_get_synonym_maps(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
]
synonym_map_1 = SynonymMap(name="test-syn-map-1", synonyms=synonyms)
client.create_synonym_map(synonym_map_1)
synonyms = [
"Washington, Wash. => WA",
]
synonym_map_2 = SynonymMap(name="test-syn-map-2", synonyms=synonyms)
client.create_synonym_map(synonym_map_2)
result = client.get_synonym_maps()
assert isinstance(result, list)
assert all(isinstance(x, SynonymMap) for x in result)
assert set(x.name for x in result) == {"test-syn-map-1", "test-syn-map-2"}
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_create_or_update_synonym_map(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
client.create_synonym_map(synonym_map)
assert len(client.get_synonym_maps()) == 1
synonym_map.synonyms = ["Washington, Wash. => WA",]
client.create_or_update_synonym_map(synonym_map)
assert len(client.get_synonym_maps()) == 1
result = client.get_synonym_map("test-syn-map")
assert isinstance(result, SynonymMap)
assert result.name == "test-syn-map"
assert result.synonyms == [
"Washington, Wash. => WA",
]
@SearchResourceGroupPreparer(random_name_enabled=True)
@SearchServicePreparer(schema=SCHEMA, index_batch=BATCH)
def test_create_or_update_synonym_map_if_unchanged(self, api_key, endpoint, index_name, **kwargs):
client = SearchIndexClient(endpoint, AzureKeyCredential(api_key))
synonyms = [
"USA, United States, United States of America",
"Washington, Wash. => WA",
]
synonym_map = SynonymMap(name="test-syn-map", synonyms=synonyms)
result = client.create_synonym_map(synonym_map)
etag = result.e_tag
synonym_map.synonyms = [
"Washington, Wash. => WA",
]
client.create_or_update_synonym_map(synonym_map)
result.e_tag = etag
with pytest.raises(HttpResponseError):
client.create_or_update_synonym_map(result, match_condition=MatchConditions.IfNotModified)
|