Skip to content

Commit fb6749e

Browse files
authored
Merge pull request #108 from lakindu95/v1
Initial Mocha tests
2 parents cf46a80 + 3084877 commit fb6749e

File tree

5 files changed

+338
-21
lines changed

5 files changed

+338
-21
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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
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",

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(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(30000);
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(30000);
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(30000);
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(30000);
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(30000);
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+
});

test/testTable.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 dynamodbLocal = require("../index.js");
9+
10+
aws.config.update({ accessKeyId: "localAccessKey", secretAccessKey: "localSecretAccessKey", region: "localRegion"});
11+
var db = new aws.DynamoDB({ endpoint: 'http://localhost:8000' });
12+
13+
describe("Check Table operations", function() {
14+
describe("#create table", function() {
15+
this.timeout(50000);
16+
it("should create a table if not existing", function(done) {
17+
var params = {
18+
TableName : "Movies",
19+
KeySchema: [
20+
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
21+
{ AttributeName: "title", KeyType: "RANGE" } //Sort key
22+
],
23+
AttributeDefinitions: [
24+
{ AttributeName: "year", AttributeType: "N" },
25+
{ AttributeName: "title", AttributeType: "S" },
26+
{ AttributeName: "info", AttributeType: "S"}
27+
],
28+
ProvisionedThroughput: {
29+
ReadCapacityUnits: 1,
30+
WriteCapacityUnits: 1
31+
}
32+
};
33+
34+
var create = db.createTable(params, function(err, data) {
35+
if (err) {
36+
should.not.exist(err);
37+
} else {
38+
should.exist(data);
39+
data.TableDescription.should.have.property("TableName","Movies");
40+
assert.isNumber(data.TableDescription.ProvisionedThroughput.WriteCapacityUnits);
41+
42+
}
43+
done();
44+
});
45+
});
46+
});
47+
48+
describe("#delete table", function(){
49+
this.timeout(50000);
50+
it("should delete the table", function(done){
51+
var params = {"TableName":"Movies"};
52+
53+
db.deleteTable(params,function(err,data){
54+
if (err){
55+
should.not.exist(err);
56+
assert.isNull(data);
57+
} else {
58+
should.exist(data);
59+
}
60+
done();
61+
});
62+
});
63+
});
64+
65+
describe("#update table", function(){
66+
this.timeout(50000);
67+
it("should update the table", function(done){
68+
var params = {
69+
"AttributeDefinitions": [
70+
{
71+
"AttributeName": "batch",
72+
"AttributeType": "N"
73+
}
74+
],
75+
"ProvisionedThroughput": {
76+
"ReadCapacityUnits": 10,
77+
"WriteCapacityUnits": 10
78+
},
79+
"TableName": "Movies"
80+
}
81+
82+
db.updateTable(params,function(err,data){
83+
if (err){
84+
should.not.exist(data);
85+
} else {
86+
data.TableDescription.should.have.property("TableName","Movies");
87+
should.exist(data);
88+
}
89+
done();
90+
});
91+
});
92+
});
93+
describe("queue-handler", function() {
94+
this.timeout(50000);
95+
it("should connect to dynamodb and list tables", function(done) {
96+
db.listTables({}, function(err, data) {
97+
if(err){
98+
should.exist(err);
99+
} else {
100+
should.not.exist(data);
101+
}
102+
done();
103+
});
104+
});
105+
});
106+
107+
describe("#getItems", function() {
108+
this.timeout(50000);
109+
var tableDes = db.getItem({"TableName": "Movies"})
110+
it("Retrieve hostname from created tables", function() {
111+
assert.equal(tableDes.httpRequest.endpoint.hostname,"localhost");
112+
});
113+
114+
it("Retrieves the path of the table",function(){
115+
assert.equal(tableDes.httpRequest.path, "/");
116+
});
117+
});
118+
});

0 commit comments

Comments
 (0)