Skip to content

Commit

Permalink
chore(geo): tagged release for @geo 9/14/21 (#8900)
Browse files Browse the repository at this point in the history
* choree(geo): better error handling when missing location service resources (#8899)

* chore(geo): update config type

* chore(geo): better error handling when missing resources

* chore(geo): add test for no search index by coordinates

* chore(geo): update error messaging to remind to `amplify push`
  • Loading branch information
TreTuna authored Sep 14, 2021
1 parent eb18000 commit c4fe1c4
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 19 deletions.
70 changes: 69 additions & 1 deletion packages/geo/__tests__/Geo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {

import { GeoClass } from '../src/Geo';
import { AmazonLocationServiceProvider } from '../src/Providers/AmazonLocationServiceProvider';
import { SearchByTextOptions } from '../src/types';
import {
Coordinates,
SearchByCoordinatesOptions,
SearchByTextOptions,
} from '../src/types';

import {
credentials,
Expand Down Expand Up @@ -284,4 +288,68 @@ describe('Geo', () => {
);
});
});

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

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

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

const results = await geo.searchByCoordinates(testCoordinates);
expect(results).toEqual(testPlaceCamelCase);

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

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

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

const searchOptions: SearchByCoordinatesOptions = {
maxResults: 40,
searchIndexName: 'geoJSSearchCustomExample',
};
const results = await geo.searchByCoordinates(
testCoordinates,
searchOptions
);
expect(results).toEqual(testPlaceCamelCase);

const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
expect(input).toEqual({
Position: testCoordinates,
IndexName: searchOptions.searchIndexName,
MaxResults: searchOptions.maxResults,
});
});

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.searchByCoordinates(testCoordinates)).rejects.toThrow(
'No plugin found in Geo for the provider'
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ describe('AmazonLocationServiceProvider', () => {
'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({});

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

describe('searchByCoordinates', () => {
Expand Down Expand Up @@ -332,5 +345,20 @@ describe('AmazonLocationServiceProvider', () => {
locationProvider.searchByCoordinates(testCoordinates)
).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({});

expect(
locationProvider.searchByCoordinates(testCoordinates)
).rejects.toThrow(
'No Search Index found, please run `amplify add geo` to add one and ensure to run `amplify push` after.'
);
});
});
});
52 changes: 35 additions & 17 deletions packages/geo/src/Providers/AmazonLocationServiceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ export class AmazonLocationServiceProvider implements GeoProvider {
* @returns {AmazonLocationServiceMapStyle[]}- Array of available map resources
*/
public getAvailableMaps(): AmazonLocationServiceMapStyle[] {
if (!this._config.maps) {
throw new Error(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
}
this._verifyMapResources();

const mapStyles: AmazonLocationServiceMapStyle[] = [];
const availableMaps = this._config.maps.items;
Expand All @@ -111,16 +107,7 @@ export class AmazonLocationServiceProvider implements GeoProvider {
* @returns {AmazonLocationServiceMapStyle} - Map resource set as the default in amplify config
*/
public getDefaultMap(): AmazonLocationServiceMapStyle {
if (!this._config.maps) {
throw new Error(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
}
if (!this._config.maps.default) {
throw new Error(
"No default map resource found in amplify config, run 'amplify add geo' to create one and ensure to run `amplify push` after"
);
}
this._verifyMapResources();

const mapName = this._config.maps.default;
const style = this._config.maps.items[mapName].style;
Expand All @@ -144,6 +131,8 @@ export class AmazonLocationServiceProvider implements GeoProvider {
throw new Error('No credentials');
}

this._verifySearchIndex(options?.searchIndexName);

/**
* Setup the searchInput
*/
Expand Down Expand Up @@ -215,6 +204,8 @@ export class AmazonLocationServiceProvider implements GeoProvider {
throw new Error('No credentials');
}

this._verifySearchIndex(options?.searchIndexName);

const locationServiceInput: SearchPlaceIndexForPositionCommandInput = {
Position: coordinates,
IndexName: this._config.search_indices.default,
Expand Down Expand Up @@ -265,12 +256,39 @@ export class AmazonLocationServiceProvider implements GeoProvider {
const credentials = await Credentials.get();
if (!credentials) return false;
const cred = Credentials.shear(credentials);
logger.debug('set credentials for storage', cred);
logger.debug('Set credentials for storage. Credentials are:', cred);
this._config.credentials = cred;
return true;
} catch (error) {
logger.warn('ensure credentials error', error);
logger.warn('Ensure credentials error. Credentials are:', error);
return false;
}
}

private _verifyMapResources() {
if (!this._config.maps) {
const errorString =
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after";
logger.warn(errorString);
throw new Error(errorString);
}
if (!this._config.maps.default) {
const errorString =
"No default map resource found in amplify config, run 'amplify add geo' to create one and ensure to run `amplify push` after";
logger.warn(errorString);
throw new Error(errorString);
}
}

private _verifySearchIndex(optionalSearchIndex?: string) {
if (
(!this._config.search_indices || !this._config.search_indices.default) &&
!optionalSearchIndex
) {
const errorString =
'No Search Index found, please run `amplify add geo` to add one and ensure to run `amplify push` after.';
logger.warn(errorString);
throw new Error(errorString);
}
}
}
5 changes: 4 additions & 1 deletion packages/geo/src/types/Geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export interface GeoConfig {
items: {};
default: string;
};
place_indexes?: {};
search_indices?: {
items: string[];
default: string;
};
};
}

Expand Down

0 comments on commit c4fe1c4

Please sign in to comment.