Skip to content

Commit 06de8ca

Browse files
committed
Merge remote-tracking branch 'upstream/v1' into additional-resources
2 parents 461a3da + 9dd31b4 commit 06de8ca

File tree

7 files changed

+321
-40
lines changed

7 files changed

+321
-40
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: 12 additions & 4 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"
@@ -151,16 +156,19 @@ class ServerlessDynamodbLocal {
151156
config && config.start,
152157
this.options
153158
);
154-
155-
dynamodbLocal.start(options);
159+
if (!options.noStart) {
160+
dynamodbLocal.start(options);
161+
}
156162
return BbPromise.resolve()
157163
.then(() => options.migrate && this.migrateHandler())
158164
.then(() => options.seed && this.seedHandler());
159165
}
160166

161167
endHandler() {
162-
this.serverlessLog('DynamoDB - stopping local database');
163-
dynamodbLocal.stop(this.port);
168+
if (!this.options.noStart) {
169+
this.serverlessLog("DynamoDB - stopping local database");
170+
dynamodbLocal.stop(this.port);
171+
}
164172
}
165173

166174
getDefaultStack() {

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: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,68 @@
1-
var should = require("should");
2-
var request = require("request");
3-
var expect = require("chai").expect;
4-
var util = require("util");
5-
6-
var assert = require('chai').assert;
7-
var config = require('../index');
8-
9-
describe('Unit Test', function () {
10-
it('Check endpoint', function () {
11-
before(function () {
12-
config.listen(8989);
13-
});
14-
after(function () {
15-
console.log('after');
16-
});
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);
1720
});
21+
});
22+
});
1823

19-
it('Check timeout test to 1000ms', () => {
20-
this.timeout(1000);
21-
assert.ok(true);
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+
});
2233
});
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+
});
2346
});
2447

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)