Skip to content

Commit e311f34

Browse files
author
Imran
committed
added more tests
1 parent 0b55d1b commit e311f34

File tree

4 files changed

+169
-36
lines changed

4 files changed

+169
-36
lines changed

.vscode/launch.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Run tests",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
9+
"args": [
10+
"test/**/*.js",
11+
"-t",
12+
"60000",
13+
"--color"
14+
],
15+
"env": {
16+
"AWS_REGION": "eu-west-2"
17+
},
18+
"cwd": "${workspaceRoot}"
19+
},
20+
]
21+
}

package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A Serverless v1.x plugin that will add a global table and replica region for all tables deployed in a serverless stack.",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "mocha test"
7+
"test": "mocha test -t 4000"
88
},
99
"repository": {
1010
"type": "git",
@@ -28,7 +28,10 @@
2828
"devDependencies": {
2929
"aws-sdk-mock": "^4.3.0",
3030
"chai": "^4.1.2",
31+
"chai-as-promised": "^7.1.1",
3132
"eslint": "^5.6.0",
32-
"mocha": "^5.2.0"
33+
"mocha": "^5.2.0",
34+
"sinon": "^7.2.2",
35+
"sinon-chai": "^3.3.0"
3336
}
3437
}

test/creating-global-tables.js

+128-34
Original file line numberDiff line numberDiff line change
@@ -2,90 +2,179 @@
22

33
const CreateDynamoDBGlobalTables = require('../src/index.js');
44
const AWSMock = require('aws-sdk-mock');
5-
require('chai').should();
5+
var chai = require('chai');
6+
var sinon = require('sinon');
7+
var sinonChai = require('sinon-chai');
8+
var chaiAsPromised = require('chai-as-promised');
9+
chai.should();
10+
chai.use(sinonChai);
11+
chai.use(chaiAsPromised);
612

713
describe('Creating a global dynamodb tables', () => {
8-
let serverless, options, plugin;
14+
let serverless, plugin;
15+
let createGlobalTables = sinon.stub().resolves();
16+
let addReplicas = sinon.stub().resolves();
917

10-
describe.only('when the stack does not contain any dynamodb tables', () => {
18+
before(async () => {
19+
AWSMock.mock('DynamoDB', 'createGlobalTable', createGlobalTables);
20+
AWSMock.mock('DynamoDB', 'updateGlobalTable', addReplicas);
21+
});
22+
23+
describe('when the stack does not contain any dynamodb tables', () => {
1124

12-
const createdGlobalTables = [];
13-
const addedReplicas = [];
1425
before(async () => {
1526
serverless = given_a_serverless_stack_without_any_tables();
16-
AWSMock.mock('DynamoDB', 'createGlobalTable', params => createdGlobalTables.push(params));
17-
AWSMock.mock('DynamoDB', 'updateGlobalTable', params => addedReplicas.push(params));
1827
plugin = new CreateDynamoDBGlobalTables(serverless);
1928
await plugin.createGlobalTables();
2029
});
2130

31+
after(() => {
32+
createGlobalTables.reset();
33+
addReplicas.reset();
34+
});
35+
2236
it('does not create any global tables', () => {
23-
createdGlobalTables.should.be.empty;
37+
createGlobalTables.should.not.have.been.called;
2438
});
2539

2640
it('does not add any replicas', () => {
27-
addedReplicas.should.be.empty;
41+
addReplicas.should.not.have.been.called;
2842
});
2943
});
3044

31-
describe('when the stack contains some dynamodb tables', () => {
45+
describe('when the stack contains dynamodb tables that have not yet been deployed', () => {
3246

33-
before(() => {
47+
before(async () => {
48+
let createGlobalTables = sinon.stub().resolves();
49+
let addReplicas = sinon.stub().resolves();
50+
AWSMock.mock('DynamoDB', 'createGlobalTable', createGlobalTables);
51+
AWSMock.mock('DynamoDB', 'updateGlobalTable', addReplicas);
3452
serverless = given_a_serverless_stack_with_some_tables();
35-
3653
plugin = new CreateDynamoDBGlobalTables(serverless);
54+
await plugin.createGlobalTables();
3755
});
3856

39-
it('enables them in the plugin', () => {
57+
after(() => {
58+
createGlobalTables.reset();
59+
addReplicas.reset();
60+
});
4061

62+
it('creates a global table for all table resource', () => {
63+
createGlobalTables.should.have.been.calledTwice;
64+
});
65+
66+
it('the first global table is created with a replication region', () => {
67+
createGlobalTables.should.have.been.calledWith({
68+
GlobalTableName: 'table-one',
69+
ReplicationGroup: [{ RegionName: 'eu-west-2' }]
70+
});
71+
});
72+
73+
it('the second global table is created with a replication region', () => {
74+
createGlobalTables.should.have.been.calledWith({
75+
GlobalTableName: 'table-two',
76+
ReplicationGroup: [{ RegionName: 'eu-west-2' }]
77+
});
4178
});
4279
});
4380

4481
describe('when the dynamodb global table already exists', () => {
4582

46-
before(() => {
83+
before(async () => {
4784
serverless = given_a_serverless_stack_with_some_tables();
48-
serverless.service.custom.apiGatewayCloudWatchSettings = { metricsEnabled: false };
49-
85+
createGlobalTables.rejects({ code: 'GlobalTableAlreadyExistsException' });
5086
plugin = new CreateDynamoDBGlobalTables(serverless);
87+
await plugin.createGlobalTables();
88+
});
89+
90+
after(() => {
91+
createGlobalTables.reset();
92+
addReplicas.reset();
93+
});
94+
95+
it('adds a replica table for all table resource', () => {
96+
addReplicas.should.have.been.calledTwice;
97+
});
98+
99+
it('adds the first table replica to the existing global table', () => {
100+
addReplicas.should.have.been.calledWith({
101+
GlobalTableName: 'table-one',
102+
ReplicaUpdates: [
103+
{ Create: { RegionName: 'eu-west-2' } }
104+
]
105+
});
51106
});
52107

53-
it('disables them in the plugin', () => {
54-
expect(plugin.metricsEnabled).to.be.false;
108+
it('adds the second table replica to the existing global table', () => {
109+
addReplicas.should.have.been.calledWith({
110+
GlobalTableName: 'table-one',
111+
ReplicaUpdates: [
112+
{ Create: { RegionName: 'eu-west-2' } }
113+
]
114+
});
55115
});
56116
});
57117

58118
describe('when the replication group already exists', () => {
59119

60-
before(() => {
120+
before(async () => {
121+
serverless = given_a_serverless_stack_with_some_tables();
122+
addReplicas.rejects({ code: 'ReplicaAlreadyExistsException' });
123+
plugin = new CreateDynamoDBGlobalTables(serverless);
124+
await plugin.createGlobalTables();
125+
});
126+
127+
after(() => {
128+
createGlobalTables.reset();
129+
addReplicas.reset();
130+
});
131+
132+
it('attempts to add a replica table for all table resource', () => {
133+
addReplicas.should.have.been.calledTwice;
134+
});
135+
});
61136

137+
describe('when an exception occurs creating the global table', () => {
138+
139+
before(async () => {
62140
serverless = given_a_serverless_stack_with_some_tables();
63-
serverless.service.custom.apiGatewayCloudWatchSettings = { metricsEnabled: true };
64-
options = { stage: 'test', region: 'us-east-1' };
141+
createGlobalTables.rejects({ code: 'UnhandledException' });
142+
plugin = new CreateDynamoDBGlobalTables(serverless);
143+
});
65144

66-
plugin = new CreateDynamoDBGlobalTables(serverless, options);
145+
after(() => {
146+
createGlobalTables.reset();
147+
addReplicas.reset();
67148
});
68149

69-
it('uses the stage option', () => {
70-
expect(plugin.stage).to.equal(options.stage);
150+
it('throws an exception', () => {
151+
plugin.createGlobalTables().should.be.rejected;
71152
});
153+
});
72154

73-
it('not use the provider stage', () => {
74-
expect(plugin.stage).to.not.equal(serverless.service.provider.stage);
155+
describe('when an exception occurs adding a replica table', () => {
156+
157+
before(async () => {
158+
serverless = given_a_serverless_stack_with_some_tables();
159+
addReplicas.rejects({ code: 'UnhandledException' });
160+
plugin = new CreateDynamoDBGlobalTables(serverless);
75161
});
76162

77-
it('uses the region option', () => {
78-
expect(plugin.region).to.equal(options.region);
163+
after(() => {
164+
createGlobalTables.reset();
165+
addReplicas.reset();
79166
});
80167

81-
it('not use the provider region', () => {
82-
expect(plugin.region).to.not.equal(serverless.service.provider.region);
168+
it('throws an exception', () => {
169+
plugin.createGlobalTables().should.be.rejected;
83170
});
84171
});
85172
});
86173

174+
87175
const given_a_serverless_stack_without_any_tables = () => ({
88176
getProvider: () => ({ getRegion: () => 'eu-west-2' }),
177+
cli: { consoleLog: () => { } },
89178
service: {
90179
resources: {
91180
Resources: {
@@ -97,16 +186,21 @@ const given_a_serverless_stack_without_any_tables = () => ({
97186

98187
const given_a_serverless_stack_with_some_tables = () => ({
99188
getProvider: () => ({ getRegion: () => 'eu-west-2' }),
189+
cli: { consoleLog: () => { } },
100190
service: {
101191
resources: {
102192
Resources: {
103193
'table-one': {
104-
TableName: 'table-one',
105-
Type: 'AWS::DynamoDB::Table'
194+
Type: 'AWS::DynamoDB::Table',
195+
Properties: {
196+
TableName: 'table-one'
197+
}
106198
},
107199
'table-two': {
108-
TableName: 'table-two',
109-
Type: 'AWS::DynamoDB::Table'
200+
Type: 'AWS::DynamoDB::Table',
201+
Properties: {
202+
TableName: 'table-two'
203+
}
110204
}
111205
}
112206
}

0 commit comments

Comments
 (0)