Skip to content

Commit

Permalink
round out unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanPan342 committed Jul 31, 2020
1 parent 7e9b775 commit a44a9b8
Show file tree
Hide file tree
Showing 8 changed files with 498 additions and 178 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ export abstract class GraphQLApiBase extends Resource implements IGraphQLApi {
*
* @param table The DynamoDB table backing this data source
* @param options The optional configuration for this data source
* @default name - 'DynamoDbCDKDefault'
* @default name - 'TableCDKDefault'
* description - undefined
*/
public addDynamoDbDataSource(table: ITable, options?: DataSourceOptions): DynamoDbDataSource {
const name = options?.name ?? 'DynamoDbCDKDefault';
const name = options?.name ?? 'TableCDKDefault';
return new DynamoDbDataSource(this, name, {
api: this,
table,
Expand Down
143 changes: 133 additions & 10 deletions packages/@aws-cdk/aws-appsync/test/appsync-dynamodb.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,93 @@
import '@aws-cdk/assert/jest';
import { MappingTemplate, PrimaryKey, Values } from '../lib';
import * as path from 'path';
import * as db from '@aws-cdk/aws-dynamodb';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';

function joined(str: string): string {
return str.replace(/\s+/g, '');
}

// GLOBAL GIVEN
let stack: cdk.Stack;
let api: appsync.GraphQLApi;
beforeEach(() => {
stack = new cdk.Stack();
api = new appsync.GraphQLApi(stack, 'baseApi', {
name: 'api',
schemaDefinition: appsync.SchemaDefinition.FILE,
schemaDefinitionFile: path.join(__dirname, 'appsync.test.graphql'),
});
});

describe('DynamoDb Data Source configuration', () => {
// GIVEN
let table: db.Table;
beforeEach(() => {
table = new db.Table(stack, 'table', {
partitionKey: {
name: 'id',
type: db.AttributeType.STRING,
},
});
});

test('default configuration produces name `TableCDKDefault`', () => {
// WHEN
api.addDynamoDbDataSource(table);

// EXPECT
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'AMAZON_DYNAMODB',
Name: 'TableCDKDefault',
});
});

test('appsync configures name correctly', () => {
// WHEN
api.addDynamoDbDataSource(table, {
name: 'custom',
});

// EXPECT
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'AMAZON_DYNAMODB',
Name: 'custom',
});
});

test('appsync configures name and description correctly', () => {
// WHEN
api.addDynamoDbDataSource(table, {
name: 'custom',
description: 'custom description',
});

// EXPECT
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'AMAZON_DYNAMODB',
Name: 'custom',
Description: 'custom description',
});
});

test('appsync errors when creating multiple dynamo db data sources with no configuration', () => {
// WHEN
const when = () => {
api.addDynamoDbDataSource(table);
api.addDynamoDbDataSource(table);
};

// EXPECT
expect(when).toThrow('There is already a Construct with name \'TableCDKDefault\' in GraphQLApi [baseApi]');
});
});

describe('DynamoDB Mapping Templates', () => {
test('PutItem projecting all', () => {
const template = MappingTemplate.dynamoDbPutItem(
PrimaryKey.partition('id').is('id'),
Values.projecting(),
const template = appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.projecting(),
);

const rendered = joined(template.renderTemplate());
Expand All @@ -28,9 +106,9 @@ describe('DynamoDB Mapping Templates', () => {
});

test('PutItem with invididual attributes', () => {
const template = MappingTemplate.dynamoDbPutItem(
PrimaryKey.partition('id').is('id'),
Values.attribute('val').is('ctx.args.val'),
const template = appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.attribute('val').is('ctx.args.val'),
);

const rendered = joined(template.renderTemplate());
Expand All @@ -50,9 +128,9 @@ describe('DynamoDB Mapping Templates', () => {
});

test('PutItem with additional attributes', () => {
const template = MappingTemplate.dynamoDbPutItem(
PrimaryKey.partition('id').is('id'),
Values.projecting().attribute('val').is('ctx.args.val'),
const template = appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition('id').is('id'),
appsync.Values.projecting().attribute('val').is('ctx.args.val'),
);

const rendered = joined(template.renderTemplate());
Expand All @@ -70,4 +148,49 @@ describe('DynamoDB Mapping Templates', () => {
}`),
);
});
});

describe('adding DynamoDb data source from imported api', () => {
// GIVEN
let table: db.Table;
beforeEach(() => {
table = new db.Table(stack, 'table', {
partitionKey: {
name: 'id',
type: db.AttributeType.STRING,
},
});
});

test('imported api can add DynamoDbDataSource from id', () => {
// WHEN
const importedApi = appsync.GraphQLApi.fromGraphqlApiAttributes(stack, 'importedApi', {
graphqlApiId: api.apiId,
});
importedApi.addDynamoDbDataSource(table);

// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'AMAZON_DYNAMODB',
ApiId: { 'Fn::GetAtt': [ 'baseApiCDA4D43A', 'ApiId' ],
},
});
});

test('imported api can add DynamoDbDataSource from attributes', () => {
// WHEN
const importedApi = appsync.GraphQLApi.fromGraphqlApiAttributes(stack, 'importedApi', {
graphqlApiId: api.apiId,
graphqlArn: api.arn,
});
importedApi.addDynamoDbDataSource(table);

// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'AMAZON_DYNAMODB',
ApiId: { 'Fn::GetAtt': [ 'baseApiCDA4D43A', 'ApiId' ],
},
});
});

});
106 changes: 106 additions & 0 deletions packages/@aws-cdk/aws-appsync/test/appsync-http.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import '@aws-cdk/assert/jest';
import * as path from 'path';
import * as cdk from '@aws-cdk/core';
import * as appsync from '../lib';

// GLOBAL GIVEN
let stack: cdk.Stack;
let api: appsync.GraphQLApi;
let endpoint: string;
beforeEach(() => {
stack = new cdk.Stack();
api = new appsync.GraphQLApi(stack, 'baseApi', {
name: 'api',
schemaDefinition: appsync.SchemaDefinition.FILE,
schemaDefinitionFile: path.join(__dirname, 'appsync.test.graphql'),
});
endpoint = 'aws.amazon.com';
});

describe('Http Data Source configuration', () => {

test('default configuration produces name `HttpCDKDefault`', () => {
// WHEN
api.addHttpDataSource(endpoint);

// EXPECT
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'HTTP',
Name: 'HttpCDKDefault',
});
});

test('appsync configures name correctly', () => {
// WHEN
api.addHttpDataSource(endpoint, {
name: 'custom',
});

// EXPECT
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'HTTP',
Name: 'custom',
});
});

test('appsync configures name and description correctly', () => {
// WHEN
api.addHttpDataSource(endpoint, {
name: 'custom',
description: 'custom description',
});

// EXPECT
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'HTTP',
Name: 'custom',
Description: 'custom description',
});
});

test('appsync errors when creating multiple http data sources with no configuration', () => {
// WHEN
const when = () => {
api.addHttpDataSource(endpoint);
api.addHttpDataSource(endpoint);
};

// EXPECT
expect(when).toThrow('There is already a Construct with name \'HttpCDKDefault\' in GraphQLApi [baseApi]');
});
});

describe('adding http data source from imported api', () => {
test('imported api can add HttpDataSource from id', () => {
// WHEN
const importedApi = appsync.GraphQLApi.fromGraphqlApiAttributes(stack, 'importedApi', {
graphqlApiId: api.apiId,
});
importedApi.addHttpDataSource(endpoint);

// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'HTTP',
ApiId: { 'Fn::GetAtt': [ 'baseApiCDA4D43A', 'ApiId' ],
},
});
});

test('imported api can add HttpDataSource from attributes', () => {
// WHEN
const importedApi = appsync.GraphQLApi.fromGraphqlApiAttributes(stack, 'importedApi', {
graphqlApiId: api.apiId,
graphqlArn: api.arn,
});
importedApi.addHttpDataSource(endpoint);

// THEN
expect(stack).toHaveResourceLike('AWS::AppSync::DataSource', {
Type: 'HTTP',
ApiId: { 'Fn::GetAtt': [ 'baseApiCDA4D43A', 'ApiId' ],
},
});
});
});


0 comments on commit a44a9b8

Please sign in to comment.