def _prepare_indexer(self, endpoint, api_key, name="sample-indexer", ds_name="sample-datasource", id_name="hotels"): con_str = self.settings.AZURE_STORAGE_CONNECTION_STRING self.scrubber.register_name_pair(con_str, 'connection_string') container = SearchIndexerDataContainer(name='searchcontainer') data_source = SearchIndexerDataSourceConnection( name=ds_name, type="azureblob", connection_string=con_str, container=container) client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key)) ds = client.create_datasource(data_source) index_name = id_name fields = [{ "name": "hotelId", "type": "Edm.String", "key": True, "searchable": False }] index = SearchIndex(name=index_name, fields=fields) ind = SearchIndexClient( endpoint, AzureKeyCredential(api_key)).create_index(index) return SearchIndexer(name=name, data_source_name=ds.name, target_index_name=ind.name)
def test_delete_datasource(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key)) data_source = self._create_datasource() result = client.create_datasource(data_source) assert len(client.get_datasources()) == 1 client.delete_datasource("sample-datasource") assert len(client.get_datasources()) == 0
def test_list_datasource(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key)) data_source1 = self._create_datasource() data_source2 = self._create_datasource(name="another-sample") created1 = client.create_datasource(data_source1) created2 = client.create_datasource(data_source2) result = client.get_datasources() assert isinstance(result, list) assert set(x.name for x in result) == {"sample-datasource", "another-sample"}
def _create_datasource(): # Here we create a datasource. As mentioned in the description we have stored it in # "searchcontainer" ds_client = SearchIndexerClient(service_endpoint, AzureKeyCredential(key)) container = SearchIndexerDataContainer(name='searchcontainer') data_source = ds_client.create_datasource( name="hotel-datasource", type="azureblob", connection_string=connection_string, container=container) return data_source
def test_create_or_update_datasource(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key)) data_source = self._create_datasource() created = client.create_datasource(data_source) assert len(client.get_datasources()) == 1 data_source.description = "updated" client.create_or_update_datasource(data_source) assert len(client.get_datasources()) == 1 result = client.get_datasource("sample-datasource") assert result.name == "sample-datasource" assert result.description == "updated"
def test_delete_datasource_string_if_unchanged(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key)) data_source = self._create_datasource() created = client.create_datasource(data_source) etag = created.e_tag # Now update the data source data_source.description = "updated" client.create_or_update_datasource(data_source) # prepare data source data_source.e_tag = etag # reset to the original datasource with pytest.raises(ValueError): client.delete_datasource( data_source.name, match_condition=MatchConditions.IfNotModified)
def test_get_datasource(self, api_key, endpoint, index_name, **kwargs): client = SearchIndexerClient(endpoint, AzureKeyCredential(api_key)) data_source = self._create_datasource() created = client.create_datasource(data_source) result = client.get_datasource("sample-datasource") assert result.name == "sample-datasource"