From 37eea838acd5bf0d13fc398ee7a3f44c0f258b6c Mon Sep 17 00:00:00 2001 From: Xiang Yan Date: Fri, 22 May 2020 11:18:56 -0700 Subject: [PATCH] Search synonym map (#11590) * Use SynonymMap rather than dict * remove AzureActiveDirectoryApplicationCredentials --- .../azure-search-documents/CHANGELOG.md | 1 + .../documents/indexes/_internal/_models.py | 106 ++++++++++++++++++ .../indexes/_internal/_search_index_client.py | 30 ++--- .../_internal/_search_indexer_client.py | 3 - .../documents/indexes/_internal/_utils.py | 46 +++++++- .../_internal/aio/_search_index_client.py | 26 +++-- .../documents/indexes/models/__init__.py | 11 +- .../tests/_test_utils.py | 13 --- .../async_tests/test_service_live_async.py | 30 +++-- .../tests/test_service_live.py | 34 +++--- 10 files changed, 215 insertions(+), 85 deletions(-) delete mode 100644 sdk/search/azure-search-documents/tests/_test_utils.py diff --git a/sdk/search/azure-search-documents/CHANGELOG.md b/sdk/search/azure-search-documents/CHANGELOG.md index 21ffd75e8d75..2506e9adc214 100644 --- a/sdk/search/azure-search-documents/CHANGELOG.md +++ b/sdk/search/azure-search-documents/CHANGELOG.md @@ -6,6 +6,7 @@ - Reorganized `SearchServiceClient` into `SearchIndexClient` & `SearchIndexerClient` #11507 - Split searchindex.json and searchservice.json models and operations into separate namespaces #11508 +- Now Search Synonym Map creation/update returns a model #11514 ## 1.0.0b3 (2020-05-04) diff --git a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_models.py b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_models.py index 6b2fd3491b35..2a71432725ed 100644 --- a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_models.py +++ b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_models.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See License.txt in the project root for # license information. # -------------------------------------------------------------------------- +import msrest.serialization from ._generated.models import LexicalAnalyzer, LexicalTokenizer @@ -87,3 +88,108 @@ def __init__(self, **kwargs): self.pattern = kwargs.get("pattern", r"\W+") self.flags = kwargs.get("flags", None) self.group = kwargs.get("group", -1) + + +class SearchResourceEncryptionKey(msrest.serialization.Model): + """A customer-managed encryption key in Azure Key Vault. Keys that you create and manage can be + used to encrypt or decrypt data-at-rest in Azure Cognitive Search, such as indexes and synonym maps. + + All required parameters must be populated in order to send to Azure. + + :param key_name: Required. The name of your Azure Key Vault key to be used to encrypt your data + at rest. + :type key_name: str + :param key_version: Required. The version of your Azure Key Vault key to be used to encrypt + your data at rest. + :type key_version: str + :param vault_uri: Required. The URI of your Azure Key Vault, also referred to as DNS name, that + contains the key to be used to encrypt your data at rest. An example URI might be https://my- + keyvault-name.vault.azure.net. + :type vault_uri: str + :param application_id: Required. An AAD Application ID that was granted the required access + permissions to the Azure Key Vault that is to be used when encrypting your data at rest. The + Application ID should not be confused with the Object ID for your AAD Application. + :type application_id: str + :param application_secret: The authentication key of the specified AAD application. + :type application_secret: str + """ + + _validation = { + 'key_name': {'required': True}, + 'key_version': {'required': True}, + 'vault_uri': {'required': True}, + } + + _attribute_map = { + 'key_name': {'key': 'keyVaultKeyName', 'type': 'str'}, + 'key_version': {'key': 'keyVaultKeyVersion', 'type': 'str'}, + 'vault_uri': {'key': 'keyVaultUri', 'type': 'str'}, + 'application_id': {'key': 'applicationId', 'type': 'str'}, + 'application_secret': {'key': 'applicationSecret', 'type': 'str'}, + } + + def __init__( + self, + **kwargs + ): + super(SearchResourceEncryptionKey, self).__init__(**kwargs) + self.key_name = kwargs['key_name'] + self.key_version = kwargs['key_version'] + self.vault_uri = kwargs['vault_uri'] + self.application_id = kwargs.get('application_id', None) + self.application_secret = kwargs.get('application_secret', None) + + +class SynonymMap(msrest.serialization.Model): + """Represents a synonym map definition. + + Variables are only populated by the server, and will be ignored when sending a request. + + All required parameters must be populated in order to send to Azure. + + :param name: Required. The name of the synonym map. + :type name: str + :ivar format: Required. The format of the synonym map. Only the 'solr' format is currently + supported. Default value: "solr". + :vartype format: str + :param synonyms: Required. A series of synonym rules in the specified synonym map format. The + rules must be separated by newlines. + :type synonyms: str + :param encryption_key: A description of an encryption key that you create in Azure Key Vault. + This key is used to provide an additional level of encryption-at-rest for your data when you + want full assurance that no one, not even Microsoft, can decrypt your data in Azure Cognitive + Search. Once you have encrypted your data, it will always remain encrypted. Azure Cognitive + Search will ignore attempts to set this property to null. You can change this property as + needed if you want to rotate your encryption key; Your data will be unaffected. Encryption with + customer-managed keys is not available for free search services, and is only available for paid + services created on or after January 1, 2019. + :type encryption_key: ~azure.search.documents.models.SearchResourceEncryptionKey + :param e_tag: The ETag of the synonym map. + :type e_tag: str + """ + + _validation = { + 'name': {'required': True}, + 'format': {'required': True, 'constant': True}, + 'synonyms': {'required': True}, + } + + _attribute_map = { + 'name': {'key': 'name', 'type': 'str'}, + 'format': {'key': 'format', 'type': 'str'}, + 'synonyms': {'key': 'synonyms', 'type': '[str]'}, + 'encryption_key': {'key': 'encryptionKey', 'type': 'SearchResourceEncryptionKey'}, + 'e_tag': {'key': '@odata\\.etag', 'type': 'str'}, + } + + format = "solr" + + def __init__( + self, + **kwargs + ): + super(SynonymMap, self).__init__(**kwargs) + self.name = kwargs['name'] + self.synonyms = kwargs['synonyms'] + self.encryption_key = kwargs.get('encryption_key', None) + self.e_tag = kwargs.get('e_tag', None) diff --git a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_index_client.py b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_index_client.py index a22c8ca062eb..5eb7a6850fe2 100644 --- a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_index_client.py +++ b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_index_client.py @@ -10,11 +10,12 @@ from azure.core.paging import ItemPaged from ._generated import SearchServiceClient as _SearchServiceClient -from ._generated.models import SynonymMap +from ._generated.models import SynonymMap as _SynonymMap from ._utils import ( delistize_flags_for_index, listize_flags_for_index, listize_synonyms, + pack_search_resource_encryption_key, get_access_conditions, normalize_endpoint, ) @@ -275,7 +276,7 @@ def get_synonym_maps(self, **kwargs): """List the Synonym Maps in an Azure Search service. :return: List of synonym maps - :rtype: list[dict] + :rtype: list[~azure.search.documents.indexes.models.SynonymMap] :raises: ~azure.core.exceptions.HttpResponseError .. admonition:: Example: @@ -290,17 +291,17 @@ def get_synonym_maps(self, **kwargs): """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) result = self._client.synonym_maps.list(**kwargs) - return [listize_synonyms(x) for x in result.as_dict()["synonym_maps"]] + return [listize_synonyms(x) for x in result.synonym_maps] @distributed_trace def get_synonym_map(self, name, **kwargs): - # type: (str, **Any) -> dict + # type: (str, **Any) -> SynonymMap """Retrieve a named Synonym Map in an Azure Search service :param name: The name of the Synonym Map to get :type name: str :return: The retrieved Synonym Map - :rtype: dict + :rtype: :class:`~azure.search.documents.indexes.models.SynonymMap` :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` .. admonition:: Example: @@ -315,7 +316,7 @@ def get_synonym_map(self, name, **kwargs): """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) result = self._client.synonym_maps.get(name, **kwargs) - return listize_synonyms(result.as_dict()) + return listize_synonyms(result) @distributed_trace def delete_synonym_map(self, synonym_map, **kwargs): @@ -356,7 +357,7 @@ def delete_synonym_map(self, synonym_map, **kwargs): @distributed_trace def create_synonym_map(self, name, synonyms, **kwargs): - # type: (str, Sequence[str], **Any) -> dict + # type: (str, Sequence[str], **Any) -> SynonymMap """Create a new Synonym Map in an Azure Search service :param name: The name of the Synonym Map to create @@ -364,7 +365,7 @@ def create_synonym_map(self, name, synonyms, **kwargs): :param synonyms: The list of synonyms in SOLR format :type synonyms: List[str] :return: The created Synonym Map - :rtype: dict + :rtype: ~azure.search.documents.indexes.models.SynonymMap .. admonition:: Example: @@ -378,13 +379,13 @@ def create_synonym_map(self, name, synonyms, **kwargs): """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) solr_format_synonyms = "\n".join(synonyms) - synonym_map = SynonymMap(name=name, synonyms=solr_format_synonyms) + synonym_map = _SynonymMap(name=name, synonyms=solr_format_synonyms) result = self._client.synonym_maps.create(synonym_map, **kwargs) - return listize_synonyms(result.as_dict()) + return listize_synonyms(result) @distributed_trace def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs): - # type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> dict + # type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> SynonymMap """Create a new Synonym Map in an Azure Search service, or update an existing one. @@ -395,7 +396,7 @@ def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs): :keyword match_condition: The match condition to use upon the etag :type match_condition: ~azure.core.MatchConditions :return: The created or updated Synonym Map - :rtype: dict + :rtype: ~azure.search.documents.indexes.models.SynonymMap """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) @@ -407,17 +408,18 @@ def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs): name = synonym_map.name if synonyms: synonym_map.synonyms = "\n".join(synonyms) + synonym_map.encryption_key = pack_search_resource_encryption_key(synonym_map.encryption_key) except AttributeError: name = synonym_map solr_format_synonyms = "\n".join(synonyms) - synonym_map = SynonymMap(name=name, synonyms=solr_format_synonyms) + synonym_map = _SynonymMap(name=name, synonyms=solr_format_synonyms) result = self._client.synonym_maps.create_or_update( synonym_map_name=name, synonym_map=synonym_map, error_map=error_map, **kwargs ) - return listize_synonyms(result.as_dict()) + return listize_synonyms(result) @distributed_trace def get_service_statistics(self, **kwargs): diff --git a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_indexer_client.py b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_indexer_client.py index a7c1967dabbe..e1059fd746c9 100644 --- a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_indexer_client.py +++ b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_search_indexer_client.py @@ -25,9 +25,6 @@ class SearchIndexerClient(HeadersMixin): """A client to interact with Azure search service Indexers. - This class is not normally instantiated directly, instead use - `get_indexers_client()` from a `SearchServiceClient` - """ _ODATA_ACCEPT = "application/json;odata.metadata=minimal" # type: str diff --git a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_utils.py b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_utils.py index e6cf1a808932..fe987c2183a7 100644 --- a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_utils.py +++ b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/_utils.py @@ -14,11 +14,19 @@ ResourceNotModifiedError, ) from ._generated.models import ( + AzureActiveDirectoryApplicationCredentials, + SearchResourceEncryptionKey as _SearchResourceEncryptionKey, + SynonymMap as _SynonymMap, SearchIndex, PatternAnalyzer as _PatternAnalyzer, PatternTokenizer as _PatternTokenizer, ) -from ._models import PatternAnalyzer, PatternTokenizer +from ._models import ( + PatternAnalyzer, + PatternTokenizer, + SynonymMap, + SearchResourceEncryptionKey, +) if TYPE_CHECKING: # pylint:disable=unused-import,ungrouped-imports @@ -154,10 +162,40 @@ def listize_flags_for_index(index): def listize_synonyms(synonym_map): - # type: (dict) -> dict - synonym_map["synonyms"] = synonym_map["synonyms"].split("\n") - return synonym_map + # type: (_SynonymMap) -> SynonymMap + return SynonymMap( + name=synonym_map.name, + synonyms=synonym_map.synonyms.split("\n"), + encryption_key=unpack_search_resource_encryption_key(synonym_map.encryption_key), + e_tag=synonym_map.e_tag + ) + +def pack_search_resource_encryption_key(search_resource_encryption_key): + # type: (SearchResourceEncryptionKey) -> _SearchResourceEncryptionKey + if not search_resource_encryption_key: + return None + access_credentials = AzureActiveDirectoryApplicationCredentials( + application_id=search_resource_encryption_key.application_id, + application_secret=search_resource_encryption_key.application_secret + ) + return _SearchResourceEncryptionKey( + key_name=search_resource_encryption_key.key_name, + key_version=search_resource_encryption_key.key_version, + vault_uri=search_resource_encryption_key.vault_uri, + access_credentials=access_credentials + ) +def unpack_search_resource_encryption_key(search_resource_encryption_key): + # type: (_SearchResourceEncryptionKey) -> SearchResourceEncryptionKey + if not search_resource_encryption_key: + return None + return SearchResourceEncryptionKey( + key_name=search_resource_encryption_key.key_name, + key_version=search_resource_encryption_key.key_version, + vault_uri=search_resource_encryption_key.vault_uri, + application_id=search_resource_encryption_key.access_credentials.application_id, + application_secret=search_resource_encryption_key.access_credentials.application_secret + ) def get_access_conditions(model, match_condition=MatchConditions.Unconditionally): # type: (Any, MatchConditions) -> Tuple[Dict[int, Any], Dict[str, bool]] diff --git a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/aio/_search_index_client.py b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/aio/_search_index_client.py index 0804a1ce087e..f15de97071e9 100644 --- a/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/aio/_search_index_client.py +++ b/sdk/search/azure-search-documents/azure/search/documents/indexes/_internal/aio/_search_index_client.py @@ -15,6 +15,7 @@ delistize_flags_for_index, listize_flags_for_index, listize_synonyms, + pack_search_resource_encryption_key, get_access_conditions, normalize_endpoint, ) @@ -274,11 +275,11 @@ async def analyze_text(self, index_name, analyze_request, **kwargs): @distributed_trace_async async def get_synonym_maps(self, **kwargs): - # type: (**Any) -> List[Dict[Any, Any]] + # type: (**Any) -> List[SynonymMap] """List the Synonym Maps in an Azure Search service. :return: List of synonym maps - :rtype: list[dict] + :rtype: list[~azure.search.documents.indexes.models.SynonymMap] :raises: ~azure.core.exceptions.HttpResponseError .. admonition:: Example: @@ -293,17 +294,17 @@ async def get_synonym_maps(self, **kwargs): """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) result = await self._client.synonym_maps.list(**kwargs) - return [listize_synonyms(x) for x in result.as_dict()["synonym_maps"]] + return [listize_synonyms(x) for x in result.synonym_maps] @distributed_trace_async async def get_synonym_map(self, name, **kwargs): - # type: (str, **Any) -> dict + # type: (str, **Any) -> SynonymMap """Retrieve a named Synonym Map in an Azure Search service :param name: The name of the Synonym Map to get :type name: str :return: The retrieved Synonym Map - :rtype: dict + :rtype: ~azure.search.documents.indexes.models.SynonymMap :raises: :class:`~azure.core.exceptions.ResourceNotFoundError` .. admonition:: Example: @@ -318,7 +319,7 @@ async def get_synonym_map(self, name, **kwargs): """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) result = await self._client.synonym_maps.get(name, **kwargs) - return listize_synonyms(result.as_dict()) + return listize_synonyms(result) @distributed_trace_async async def delete_synonym_map(self, synonym_map, **kwargs): @@ -360,7 +361,7 @@ async def delete_synonym_map(self, synonym_map, **kwargs): @distributed_trace_async async def create_synonym_map(self, name, synonyms, **kwargs): - # type: (str, Sequence[str], **Any) -> dict + # type: (str, Sequence[str], **Any) -> SynonymMap """Create a new Synonym Map in an Azure Search service :param name: The name of the Synonym Map to create @@ -368,7 +369,7 @@ async def create_synonym_map(self, name, synonyms, **kwargs): :param synonyms: A list of synonyms in SOLR format :type synonyms: List[str] :return: The created Synonym Map - :rtype: dict + :rtype: ~azure.search.documents.indexes.models.SynonymMap .. admonition:: Example: @@ -384,11 +385,11 @@ async def create_synonym_map(self, name, synonyms, **kwargs): solr_format_synonyms = "\n".join(synonyms) synonym_map = SynonymMap(name=name, synonyms=solr_format_synonyms) result = await self._client.synonym_maps.create(synonym_map, **kwargs) - return listize_synonyms(result.as_dict()) + return listize_synonyms(result) @distributed_trace_async async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwargs): - # type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> dict + # type: (Union[str, SynonymMap], Optional[Sequence[str]], **Any) -> SynonymMap """Create a new Synonym Map in an Azure Search service, or update an existing one. @@ -399,7 +400,7 @@ async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwarg :keyword match_condition: The match condition to use upon the etag :type match_condition: ~azure.core.MatchConditions :return: The created or updated Synonym Map - :rtype: dict + :rtype: ~azure.search.documents.indexes.models.SynonymMap """ kwargs["headers"] = self._merge_client_headers(kwargs.get("headers")) @@ -411,6 +412,7 @@ async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwarg name = synonym_map.name if synonyms: synonym_map.synonyms = "\n".join(synonyms) + synonym_map.encryption_key = pack_search_resource_encryption_key(synonym_map.encryption_key) except AttributeError: name = synonym_map solr_format_synonyms = "\n".join(synonyms) @@ -421,7 +423,7 @@ async def create_or_update_synonym_map(self, synonym_map, synonyms=None, **kwarg error_map=error_map, **kwargs ) - return listize_synonyms(result.as_dict()) + return listize_synonyms(result) @distributed_trace_async async def get_service_statistics(self, **kwargs): diff --git a/sdk/search/azure-search-documents/azure/search/documents/indexes/models/__init__.py b/sdk/search/azure-search-documents/azure/search/documents/indexes/models/__init__.py index f7f27f0e97c2..63e4cac9cfe4 100644 --- a/sdk/search/azure-search-documents/azure/search/documents/indexes/models/__init__.py +++ b/sdk/search/azure-search-documents/azure/search/documents/indexes/models/__init__.py @@ -35,7 +35,6 @@ AnalyzeResult, AnalyzedTokenInfo, AsciiFoldingTokenFilter, - AzureActiveDirectoryApplicationCredentials, CharFilter, CjkBigramTokenFilter, ClassicTokenizer, @@ -90,7 +89,6 @@ SearchIndexerDataContainer, SearchIndexerDataSource, SearchIndexerSkillset, - SearchResourceEncryptionKey, ScoringFunction, ScoringProfile, SentimentSkill, @@ -103,7 +101,6 @@ StopAnalyzer, StopwordsTokenFilter, Suggester, - SynonymMap, SynonymTokenFilter, TagScoringFunction, TagScoringParameters, @@ -116,7 +113,12 @@ WebApiSkill, WordDelimiterTokenFilter, ) -from .._internal._models import PatternAnalyzer, PatternTokenizer +from .._internal._models import ( + PatternAnalyzer, + PatternTokenizer, + SearchResourceEncryptionKey, + SynonymMap, +) __all__ = ( @@ -124,7 +126,6 @@ "AnalyzeResult", "AnalyzedTokenInfo", "AsciiFoldingTokenFilter", - "AzureActiveDirectoryApplicationCredentials", "CharFilter", "CjkBigramTokenFilter", "ClassicTokenizer", diff --git a/sdk/search/azure-search-documents/tests/_test_utils.py b/sdk/search/azure-search-documents/tests/_test_utils.py deleted file mode 100644 index 6b6401db0baa..000000000000 --- a/sdk/search/azure-search-documents/tests/_test_utils.py +++ /dev/null @@ -1,13 +0,0 @@ -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -from azure.search.documents.indexes.models import SynonymMap - - -def build_synonym_map_from_dict(synonym_map): - sm = SynonymMap(name=synonym_map["name"], synonyms=synonym_map["synonyms"]) - for k, v in synonym_map.items(): - setattr(sm, k, v) - return sm diff --git a/sdk/search/azure-search-documents/tests/async_tests/test_service_live_async.py b/sdk/search/azure-search-documents/tests/async_tests/test_service_live_async.py index f4542ced09b0..974276e8b89d 100644 --- a/sdk/search/azure-search-documents/tests/async_tests/test_service_live_async.py +++ b/sdk/search/azure-search-documents/tests/async_tests/test_service_live_async.py @@ -38,7 +38,6 @@ edm ) from azure.search.documents.indexes.aio import SearchIndexClient, SearchIndexerClient -from _test_utils import build_synonym_map_from_dict CWD = dirname(realpath(__file__)) SCHEMA = open(join(CWD, "..", "hotel_schema.json")).read() @@ -275,9 +274,9 @@ async def test_create_synonym_map(self, api_key, endpoint, index_name, **kwargs) "USA, United States, United States of America", "Washington, Wash. => WA", ]) - assert isinstance(result, dict) - assert result["name"] == "test-syn-map" - assert result["synonyms"] == [ + assert isinstance(result, SynonymMap) + assert result.name == "test-syn-map" + assert result.synonyms == [ "USA, United States, United States of America", "Washington, Wash. => WA", ] @@ -303,16 +302,15 @@ async def test_delete_synonym_map_if_unchanged(self, api_key, endpoint, index_na "USA, United States, United States of America", "Washington, Wash. => WA", ]) - sm_result = build_synonym_map_from_dict(result) - etag = sm_result.e_tag + etag = result.e_tag await client.create_or_update_synonym_map("test-syn-map", [ "Washington, Wash. => WA", ]) - sm_result.e_tag = etag + result.e_tag = etag with pytest.raises(HttpResponseError): - await client.delete_synonym_map(sm_result, match_condition=MatchConditions.IfNotModified) + await client.delete_synonym_map(result, match_condition=MatchConditions.IfNotModified) assert len(client.get_synonym_maps()) == 1 @SearchResourceGroupPreparer(random_name_enabled=True) @@ -325,9 +323,9 @@ async def test_get_synonym_map(self, api_key, endpoint, index_name, **kwargs): ]) assert len(await client.get_synonym_maps()) == 1 result = await client.get_synonym_map("test-syn-map") - assert isinstance(result, dict) - assert result["name"] == "test-syn-map" - assert result["synonyms"] == [ + assert isinstance(result, SynonymMap) + assert result.name == "test-syn-map" + assert result.synonyms == [ "USA, United States, United States of America", "Washington, Wash. => WA", ] @@ -344,8 +342,8 @@ async def test_get_synonym_maps(self, api_key, endpoint, index_name, **kwargs): ]) result = await client.get_synonym_maps() assert isinstance(result, list) - assert all(isinstance(x, dict) for x in result) - assert set(x['name'] for x in result) == {"test-syn-map-1", "test-syn-map-2"} + 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) @@ -360,9 +358,9 @@ async def test_create_or_update_synonym_map(self, api_key, endpoint, index_name, ]) assert len(await client.get_synonym_maps()) == 1 result = await client.get_synonym_map("test-syn-map") - assert isinstance(result, dict) - assert result["name"] == "test-syn-map" - assert result["synonyms"] == [ + assert isinstance(result, SynonymMap) + assert result.name == "test-syn-map" + assert result.synonyms == [ "Washington, Wash. => WA", ] diff --git a/sdk/search/azure-search-documents/tests/test_service_live.py b/sdk/search/azure-search-documents/tests/test_service_live.py index 755ac06c2b98..f9172b1e906f 100644 --- a/sdk/search/azure-search-documents/tests/test_service_live.py +++ b/sdk/search/azure-search-documents/tests/test_service_live.py @@ -35,7 +35,6 @@ edm ) from azure.search.documents.indexes import SearchIndexClient, SearchIndexerClient -from _test_utils import build_synonym_map_from_dict CWD = dirname(realpath(__file__)) SCHEMA = open(join(CWD, "hotel_schema.json")).read() @@ -257,9 +256,9 @@ def test_create_synonym_map(self, api_key, endpoint, index_name, **kwargs): "USA, United States, United States of America", "Washington, Wash. => WA", ]) - assert isinstance(result, dict) - assert result["name"] == "test-syn-map" - assert result["synonyms"] == [ + assert isinstance(result, SynonymMap) + assert result.name == "test-syn-map" + assert result.synonyms == [ "USA, United States, United States of America", "Washington, Wash. => WA", ] @@ -285,16 +284,15 @@ def test_delete_synonym_map_if_unchanged(self, api_key, endpoint, index_name, ** "USA, United States, United States of America", "Washington, Wash. => WA", ]) - sm_result = build_synonym_map_from_dict(result) - etag = sm_result.e_tag + etag = result.e_tag client.create_or_update_synonym_map("test-syn-map", [ "Washington, Wash. => WA", ]) - sm_result.e_tag = etag + result.e_tag = etag with pytest.raises(HttpResponseError): - client.delete_synonym_map(sm_result, match_condition=MatchConditions.IfNotModified) + client.delete_synonym_map(result, match_condition=MatchConditions.IfNotModified) assert len(client.get_synonym_maps()) == 1 @SearchResourceGroupPreparer(random_name_enabled=True) @@ -307,9 +305,9 @@ def test_get_synonym_map(self, api_key, endpoint, index_name, **kwargs): ]) assert len(client.get_synonym_maps()) == 1 result = client.get_synonym_map("test-syn-map") - assert isinstance(result, dict) - assert result["name"] == "test-syn-map" - assert result["synonyms"] == [ + assert isinstance(result, SynonymMap) + assert result.name == "test-syn-map" + assert result.synonyms == [ "USA, United States, United States of America", "Washington, Wash. => WA", ] @@ -326,8 +324,8 @@ def test_get_synonym_maps(self, api_key, endpoint, index_name, **kwargs): ]) result = client.get_synonym_maps() assert isinstance(result, list) - assert all(isinstance(x, dict) for x in result) - assert set(x['name'] for x in result) == {"test-syn-map-1", "test-syn-map-2"} + 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) @@ -342,9 +340,9 @@ def test_create_or_update_synonym_map(self, api_key, endpoint, index_name, **kwa ]) assert len(client.get_synonym_maps()) == 1 result = client.get_synonym_map("test-syn-map") - assert isinstance(result, dict) - assert result["name"] == "test-syn-map" - assert result["synonyms"] == [ + assert isinstance(result, SynonymMap) + assert result.name == "test-syn-map" + assert result.synonyms == [ "Washington, Wash. => WA", ] @@ -352,9 +350,9 @@ def test_create_or_update_synonym_map(self, api_key, endpoint, index_name, **kwa @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)) - result = build_synonym_map_from_dict(client.create_synonym_map("test-syn-map", [ + result = client.create_synonym_map("test-syn-map", [ "USA, United States, United States of America", - ])) + ]) etag = result.e_tag client.create_or_update_synonym_map("test-syn-map", [