Skip to content

Commit

Permalink
Revert "feat(@aws-amplify/geo): searchForSuggestions API (aws-amplify…
Browse files Browse the repository at this point in the history
…#9516)" (aws-amplify#9747)

This reverts commit bb60060.
  • Loading branch information
TreTuna authored Mar 30, 2022
1 parent d522ab3 commit a1640db
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 407 deletions.
135 changes: 3 additions & 132 deletions packages/geo/__tests__/Geo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import { Credentials } from '@aws-amplify/core';
import {
LocationClient,
SearchPlaceIndexForTextCommand,
SearchPlaceIndexForSuggestionsCommand,
SearchPlaceIndexForPositionCommand,
SearchPlaceIndexForTextCommand,
} from '@aws-sdk/client-location';

import { GeoClass } from '../src/Geo';
Expand Down Expand Up @@ -46,18 +45,6 @@ LocationClient.prototype.send = jest.fn(async command => {
],
};
}
if (command instanceof SearchPlaceIndexForSuggestionsCommand) {
return {
Results: [
{
Text: 'star',
},
{
Text: 'not star',
},
],
};
}
});

describe('Geo', () => {
Expand Down Expand Up @@ -207,7 +194,7 @@ describe('Geo', () => {
});

describe('searchByText', () => {
const testString = 'star';
const testString = 'starbucks';

test('should search with just text input', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
Expand Down Expand Up @@ -314,129 +301,13 @@ describe('Geo', () => {
geo.configure(awsConfig);
geo.removePluggable('AmazonLocationService');

const testString = 'starbucks';
await expect(geo.searchByText(testString)).rejects.toThrow(
'No plugin found in Geo for the provider'
);
});
});

describe('searchForSuggestions', () => {
const testString = 'star';
const testResults = ['star', 'not star'];

test('should search with just text input', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);

const results = await geo.searchForSuggestions(testString);
expect(results).toEqual(testResults);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
expect(input).toEqual({
Text: testString,
IndexName: awsConfig.geo.amazon_location_service.search_indices.default,
});
});

test('should search using given options with biasPosition', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);

const searchOptions: SearchByTextOptions = {
biasPosition: [12345, 67890],
countries: ['USA'],
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
};
const results = await geo.searchForSuggestions(testString, searchOptions);
expect(results).toEqual(testResults);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
expect(input).toEqual({
Text: testString,
IndexName: searchOptions.searchIndexName,
BiasPosition: searchOptions.biasPosition,
FilterCountries: searchOptions.countries,
MaxResults: searchOptions.maxResults,
});
});

test('should search using given options with searchAreaConstraints', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);

const searchOptions: SearchByTextOptions = {
searchAreaConstraints: [123, 456, 789, 321],
countries: ['USA'],
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
};
const results = await geo.searchForSuggestions(testString, searchOptions);
expect(results).toEqual(testResults);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
expect(input).toEqual({
Text: testString,
IndexName: searchOptions.searchIndexName,
FilterBBox: searchOptions.searchAreaConstraints,
FilterCountries: searchOptions.countries,
MaxResults: searchOptions.maxResults,
});
});

test('should throw an error if both BiasPosition and SearchAreaConstraints are given in the options', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);

const searchOptions: SearchByTextOptions = {
countries: ['USA'],
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
biasPosition: [12345, 67890],
searchAreaConstraints: [123, 456, 789, 321],
};

await expect(
geo.searchForSuggestions(testString, searchOptions)
).rejects.toThrow(
'BiasPosition and SearchAreaConstraints are mutually exclusive, please remove one or the other from the options object'
);
});

test('should fail if there is no provider', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);
geo.removePluggable('AmazonLocationService');

await expect(geo.searchForSuggestions(testString)).rejects.toThrow(
'No plugin found in Geo for the provider'
);
});
});

describe('searchByCoordinates', () => {
const testCoordinates: Coordinates = [12345, 67890];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { Credentials } from '@aws-amplify/core';
import {
LocationClient,
SearchPlaceIndexForTextCommand,
SearchPlaceIndexForSuggestionsCommand,
SearchPlaceIndexForPositionCommand,
} from '@aws-sdk/client-location';

Expand Down Expand Up @@ -44,18 +43,6 @@ LocationClient.prototype.send = jest.fn(async command => {
],
};
}
if (command instanceof SearchPlaceIndexForSuggestionsCommand) {
return {
Results: [
{
Text: 'star',
},
{
Text: 'not star',
},
],
};
}
});

describe('AmazonLocationServiceProvider', () => {
Expand Down Expand Up @@ -160,7 +147,7 @@ describe('AmazonLocationServiceProvider', () => {
});

describe('searchByText', () => {
const testString = 'star';
const testString = 'starbucks';

test('should search with just text input', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
Expand Down Expand Up @@ -307,156 +294,6 @@ describe('AmazonLocationServiceProvider', () => {
});
});

describe('searchForSuggestions', () => {
const testString = 'star';
const testResults = ['star', 'not star'];

test('should search with just text input', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure(awsConfig.geo.amazon_location_service);

const results = await locationProvider.searchForSuggestions(testString);

expect(results).toEqual(testResults);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
expect(input).toEqual({
Text: testString,
IndexName: awsConfig.geo.amazon_location_service.search_indices.default,
});
});

test('should use biasPosition when given', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure(awsConfig.geo.amazon_location_service);

const searchOptions: SearchByTextOptions = {
countries: ['USA'],
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
biasPosition: [12345, 67890],
};

const results = await locationProvider.searchForSuggestions(
testString,
searchOptions
);
expect(results).toEqual(testResults);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;

expect(input).toEqual({
Text: testString,
IndexName: searchOptions.searchIndexName,
BiasPosition: searchOptions.biasPosition,
FilterCountries: searchOptions.countries,
MaxResults: searchOptions.maxResults,
});
});

test('should use searchAreaConstraints when given', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure(awsConfig.geo.amazon_location_service);

const searchOptions: SearchByTextOptions = {
countries: ['USA'],
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
searchAreaConstraints: [123, 456, 789, 321],
};

const resultsWithConstraints =
await locationProvider.searchForSuggestions(testString, searchOptions);
expect(resultsWithConstraints).toEqual(testResults);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
expect(input).toEqual({
Text: testString,
IndexName: searchOptions.searchIndexName,
FilterBBox: searchOptions.searchAreaConstraints,
FilterCountries: searchOptions.countries,
MaxResults: searchOptions.maxResults,
});
});

test('should throw an error if both BiasPosition and SearchAreaConstraints are given in the options', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure(awsConfig.geo.amazon_location_service);

const searchOptions: SearchByTextOptions = {
countries: ['USA'],
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
biasPosition: [12345, 67890],
searchAreaConstraints: [123, 456, 789, 321],
};

await expect(
locationProvider.searchForSuggestions(testString, searchOptions)
).rejects.toThrow(
'BiasPosition and SearchAreaConstraints are mutually exclusive, please remove one or the other from the options object'
);
});

test('should fail if credentials are invalid', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve();
});

const locationProvider = new AmazonLocationServiceProvider();

await expect(
locationProvider.searchForSuggestions(testString)
).rejects.toThrow('No credentials');
});

test('should fail if _getCredentials fails ', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.reject();
});

const locationProvider = new AmazonLocationServiceProvider();

await expect(
locationProvider.searchForSuggestions(testString)
).rejects.toThrow('No credentials');
});

test('should fail if there are no search index resources', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const locationProvider = new AmazonLocationServiceProvider();
locationProvider.configure({});

await expect(
locationProvider.searchForSuggestions(testString)
).rejects.toThrow(
'No Search Index found, please run `amplify add geo` to add one and run `amplify push` after.'
);
});
});

describe('searchByCoordinates', () => {
const testCoordinates: Coordinates = [12345, 67890];

Expand Down
22 changes: 0 additions & 22 deletions packages/geo/src/Geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
GeoConfig,
Coordinates,
SearchByTextOptions,
SearchForSuggestionsResults,
SearchByCoordinatesOptions,
GeoProvider,
MapStyle,
Expand Down Expand Up @@ -155,27 +154,6 @@ export class GeoClass {
}
}

/**
* Search for search term suggestions based on input text
* @param {string} text - The text string that is to be search for
* @param {SearchByTextOptions} options? - Optional parameters to the search
* @returns {Promise<SearchForSuggestionsResults>} - Resolves to an array of search suggestion strings
*/
public async searchForSuggestions(
text: string,
options?: SearchByTextOptions
) {
const { providerName = DEFAULT_PROVIDER } = options || {};
const prov = this.getPluggable(providerName);

try {
return await prov.searchForSuggestions(text, options);
} catch (error) {
logger.debug(error);
throw error;
}
}

/**
* Reverse geocoding search via a coordinate point on the map
* @param coordinates - Coordinates array for the search input
Expand Down
Loading

0 comments on commit a1640db

Please sign in to comment.