Skip to content

Commit b1b936f

Browse files
authored
Merge branch 'v1' into add-host-option
2 parents 9f22442 + ede3329 commit b1b936f

File tree

7 files changed

+358
-26
lines changed

7 files changed

+358
-26
lines changed

.travis.yml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,43 @@ node_js:
44

55
sudo: false
66

7+
before_install:
8+
- cd test
9+
- mkdir sample-template
10+
- cd sample-template
11+
- npm init --yes
12+
713
install:
14+
- npm install --save-dev serverless-offline
15+
- npm install -g [email protected]
16+
- npm install aws-sdk
17+
- npm install --save dynamodb-doc-client-wrapper
18+
- serverless create --template aws-nodejs
19+
- cd ..
20+
- cd ..
21+
- npm link
22+
- cd test/sample-template/node_modules
23+
- npm link serverless-dynamodb-local
24+
- cd ..
25+
- sed -i '18 a plugins:' serverless.yml
26+
- sed -i '19 a - serverless-offline' serverless.yml
27+
- sed -i '20 a - serverless-dynamodb-local' serverless.yml
28+
- cd ..
29+
- cd ..
30+
- npm install --save-dev mocha
31+
- npm install --save-dev should
32+
- npm install --save expect
33+
- npm install --save request
34+
- npm install --save chai
835
- travis_retry npm install
36+
- cd test/sample-template
937

38+
before_script:
39+
- sls dynamodb install
40+
- sls dynamodb start -p 3000 &
41+
- cd ..
42+
- cd ..
43+
- npm run test
44+
1045
script:
11-
- npm test
46+
- npm test

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ custom:
6565
inMemory: true
6666
migrate: true
6767
seed: true
68+
# Uncomment only if you already have a DynamoDB running locally
69+
# noStart: true
6870
```
6971

7072
## Migrations: sls dynamodb migrate

index.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class ServerlessDynamodbLocal {
6666
}
6767
}
6868
},
69+
noStart: {
70+
shortcut: "n",
71+
default: false,
72+
usage: "Do not start DynamoDB local (in case it is already running)",
73+
},
6974
remove: {
7075
lifecycleEvents: ["removeHandler"],
7176
usage: "Removes local DynamoDB"
@@ -157,30 +162,60 @@ class ServerlessDynamodbLocal {
157162
config && config.start,
158163
this.options
159164
);
160-
161-
dynamodbLocal.start(options);
165+
if (!options.noStart) {
166+
dynamodbLocal.start(options);
167+
}
162168
return BbPromise.resolve()
163169
.then(() => options.migrate && this.migrateHandler())
164170
.then(() => options.seed && this.seedHandler());
165171
}
166172

167173
endHandler() {
168-
this.serverlessLog('DynamoDB - stopping local database');
169-
dynamodbLocal.stop(this.port);
174+
if (!this.options.noStart) {
175+
this.serverlessLog("DynamoDB - stopping local database");
176+
dynamodbLocal.stop(this.port);
177+
}
170178
}
171179

172-
/**
173-
* Gets the table definitions
174-
*/
175-
get tables() {
176-
const resources = this.service.resources.Resources;
180+
getDefaultStack() {
181+
return _.get(this.service, "resources");
182+
}
183+
184+
getAdditionalStacks() {
185+
return _.values(_.get(this.service, "custom.additionalStacks", {}));
186+
}
187+
188+
hasAdditionalStacksPlugin() {
189+
return _.get(this.service, "plugins", []).includes("serverless-plugin-additional-stacks");
190+
}
191+
192+
getTableDefinitionsFromStack(stack) {
193+
const resources = _.get(stack, "Resources", []);
177194
return Object.keys(resources).map((key) => {
178195
if (resources[key].Type === "AWS::DynamoDB::Table") {
179196
return resources[key].Properties;
180197
}
181198
}).filter((n) => n);
182199
}
183200

201+
/**
202+
* Gets the table definitions
203+
*/
204+
get tables() {
205+
let stacks = [];
206+
207+
const defaultStack = this.getDefaultStack();
208+
if (defaultStack) {
209+
stacks.push(defaultStack);
210+
}
211+
212+
if (this.hasAdditionalStacksPlugin()) {
213+
stacks = stacks.concat(this.getAdditionalStacks());
214+
}
215+
216+
return stacks.map((stack) => this.getTableDefinitionsFromStack(stack)).reduce((tables, tablesInStack) => tables.concat(tablesInStack), []);
217+
}
218+
184219
/**
185220
* Gets the seeding sources
186221
*/

package.json

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-dynamodb-local",
3-
"version": "0.2.22",
3+
"version": "0.2.24",
44
"engines": {
55
"node": ">=4.0"
66
},
@@ -12,29 +12,34 @@
1212
"url": "https://github.com/99xt/serverless-dynamodb-local"
1313
},
1414
"keywords": [
15-
"serverless framework plugin",
16-
"serverless applications",
17-
"serverless plugins",
18-
"api gateway",
19-
"lambda",
20-
"dynamodb",
21-
"dynamodb local",
22-
"aws",
23-
"aws lambda",
24-
"aws dynamodb",
25-
"amazon",
26-
"amazon web services",
27-
"serverless.com"
28-
],
15+
"serverless framework plugin",
16+
"serverless applications",
17+
"serverless plugins",
18+
"api gateway",
19+
"lambda",
20+
"dynamodb",
21+
"dynamodb local",
22+
"aws",
23+
"aws lambda",
24+
"aws dynamodb",
25+
"amazon",
26+
"amazon web services",
27+
"serverless.com"
28+
],
2929
"main": "index.js",
3030
"bin": {},
3131
"scripts": {
32-
"test": "echo \"Warning: no test specified\" && exit 0"
32+
"test": "mocha ./test"
3333
},
3434
"dependencies": {
3535
"aws-sdk": "^2.7.0",
3636
"bluebird": "^3.4.6",
3737
"dynamodb-localhost": "^0.0.5",
3838
"lodash": "^4.17.0"
39+
},
40+
"devDependencies": {
41+
"chai": "^4.1.1",
42+
"mocha": "^3.5.0",
43+
"should": "^11.2.1"
3944
}
4045
}

test/indexTest.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"use strict";
2+
//Define the modules required to mocha testing
3+
const assert = require("chai").assert;
4+
const http = require ("http");
5+
const expect = require("chai").expect;
6+
const should = require("should");
7+
const aws = require ("aws-sdk");
8+
const seeder = require("../src/seeder.js");
9+
const dataApp = require("../index.js");
10+
11+
describe("Port function",function(){
12+
it("Port should return number",function(){
13+
let myport = dataApp.prototype.port;
14+
assert(typeof myport, "number");
15+
});
16+
17+
it("Port value should be >= 0 and < 65536",function () {
18+
http.get(`http://localhost:${dataApp.prototype.port}`, function (response) {
19+
assert.equal(response.statusCode, 200);
20+
});
21+
});
22+
});
23+
24+
describe("Check the dynamodb function",function(){
25+
it("Endpoint should listen to the port",function () {
26+
let server;
27+
before(function () {
28+
server = dynamodbOptions.listen(port);
29+
});
30+
after(function () {
31+
assert.ok;
32+
});
33+
});
34+
35+
it("Should be an object",function(){
36+
let dynamoOptions = dataApp.prototype.dynamodbOptions;
37+
let raw = new aws.DynamoDB(dynamoOptions);
38+
raw.should.be.type("object");
39+
});
40+
41+
it("Should be an object",function(){
42+
let dynamoOptions = dataApp.prototype.dynamodbOptions;
43+
let doc = new aws.DynamoDB(dynamoOptions);
44+
doc.should.be.type("object");
45+
});
46+
});
47+
48+
describe ("Start handler function",function(){
49+
it ("Should not be null",function(){
50+
let handler = dataApp.prototype.startHandler;
51+
assert(handler =! null);
52+
});
53+
});
54+
55+
56+
describe ("createTable functon",function(){
57+
it ("Should check as a function",function(){
58+
const tbl = dataApp.prototype.createTable;
59+
assert.equal(typeof tbl, "function");
60+
});
61+
});
62+
63+
describe ("Check the Seeder file",function(){
64+
it("Table name shoud be a string",function(){
65+
let tblName = seeder.writeSeeds.name;
66+
expect(tblName).to.be.a("string");
67+
});
68+
});

test/testItems.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"use strict";
2+
const assert = require("chai").assert;
3+
const http = require ("http");
4+
const expect = require("chai").expect;
5+
const should = require("should");
6+
const aws = require ("aws-sdk");
7+
8+
aws.config.update({ accessKeyId: "localAccessKey", secretAccessKey: "localSecretAccessKey", region: "localRegion"});
9+
var db = new aws.DynamoDB({ endpoint: "http://localhost:8000" });
10+
var dbClient = new aws.DynamoDB.DocumentClient({ endpoint: "http://localhost:8000"});
11+
12+
describe("#Add Items", function() {
13+
this.timeout(50000);
14+
it("should add item to table", function(done) {
15+
{
16+
var params = {
17+
TableName:"MyMovie",
18+
Item:{
19+
"year": 2017,
20+
"title": "Big Movie",
21+
"info":{
22+
"plot": "Nothing happens at all.",
23+
"rating": 0
24+
}
25+
}
26+
};
27+
dbClient.put(params, function(err, data) {
28+
if (err) {
29+
should.not.exist(data);
30+
} else {
31+
assert.isNotNull(data.Attributes);
32+
assert.isOk(true, "This will pass");
33+
}
34+
done();
35+
});
36+
};
37+
});
38+
});
39+
40+
describe("#Update Items", function(){
41+
this.timeout(50000);
42+
it("should update the items", function(done){
43+
var params = {
44+
TableName:"MyMovie",
45+
Key:{
46+
"year": 2017,
47+
"title": "Big Movie"
48+
},
49+
UpdateExpression: "set info.rating = :r, info.plot=:p, info.actors=:a",
50+
ExpressionAttributeValues:{
51+
":r":5.4,
52+
":p":"Everything happens all at once.",
53+
":a":["Steve", "Jonson", "Cethie"]
54+
},
55+
ReturnValues:"UPDATED_NEW"
56+
};
57+
dbClient.update(params, function(err,data){
58+
if(err){
59+
should.exist(err);
60+
} else {
61+
assert.isNotNull(data.Attributes);
62+
assert.isOk(true, "This will pass");
63+
}
64+
done();
65+
});
66+
});
67+
});
68+
69+
describe("#Delete Items", function(){
70+
this.timeout(50000);
71+
it("should delete the items", function(done){
72+
var params = {
73+
TableName:"Movies10",
74+
Key:{
75+
"year":2017,
76+
"title":"Big Movie"
77+
},
78+
ConditionExpression:"info.rating <= :val",
79+
ExpressionAttributeValues: {
80+
":val": 5.0
81+
}
82+
};
83+
dbClient.delete(params, function(err, data) {
84+
if (err) {
85+
should.exist(err);
86+
} else {
87+
should.not.exist(data);
88+
}
89+
done();
90+
});
91+
});
92+
});
93+
94+
describe("#Retrieving from database",function(){
95+
this.timeout(50000);
96+
var params = {
97+
TableName : "Movies10",
98+
KeyConditionExpression: "#yr = :yyyy",
99+
ExpressionAttributeNames:{
100+
"#yr": "year"
101+
},
102+
ExpressionAttributeValues: {
103+
":yyyy":2017
104+
}
105+
};
106+
107+
it ("Getting data from the table", function(done){
108+
this.timeout(50000);
109+
dbClient.query(params, function(err, data) {
110+
if (err) {
111+
should.exist(err);
112+
} else {
113+
data.Items.forEach(function(item) {
114+
assert.equal(item.year + ": " + item.title, "2017: Big Movie", "==Matching the values.");
115+
});
116+
}
117+
done();
118+
});
119+
});
120+
});

0 commit comments

Comments
 (0)