Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilaravi committed May 9, 2016
1 parent 7077359 commit 34ba9b1
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 5 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"create-s3": ". ./scripts/create-s3-bucket.sh",
"deploy": "node ./node_modules/dpl/dpl.js",
"create-api": ". ./scripts/create-api.sh",
"deploy-app": "npm run deploy-lambda && npm run create-api"
"deploy-app": "npm run deploy-lambda && npm run create-api",
"test": "node ./node_modules/.bin/mocha ./test/**/*.js",
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha ./test/**/*.js --report lcov -- -R spec",
},
"repository": {
"type": "git",
Expand All @@ -31,7 +33,10 @@
"homepage": "https://github.com/nikhilaravi/serverless-graphql#readme",
"devDependencies": {
"aws-sdk": "^2.3.7",
"dpl": "^3.0.1"
"dpl": "^3.0.1",
"istanbul": "^0.4.2",
"mocha": "^2.4.5",
"simple-mock": "^0.6.0",
},
"files_to_deploy": [
"package.json",
Expand Down
4 changes: 2 additions & 2 deletions scripts/create-api.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

## TO BE SET
api_name="serverless-graphql"
lambda_function="serverless-graphql"
api_name="serverless-graphql-v1"
lambda_function="serverless-graphql-v1"
###

api_description="Graphql endpoint"
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-s3-bucket.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bucket="serverless-test-database"
bucket="serverless-database"
region="eu-west-1"

aws s3api create-bucket \
Expand Down
19 changes: 19 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const invokeQuery = require('./schema/fixtures').suggestionsQuery;

var assert = require('assert');
var index = require('../index.js');
describe('Invoke Test', () => {
it('invokes the lambda with a suggestions query', (done) => {
const event = {
query: invokeQuery,
variables: {
query: 'Hello'
}
};
index.handler(event, {}, (err, res) => {
assert.equal(err, null);
assert.equal(res.data.suggestions.length > 0, true);
done();
});
});
});
38 changes: 38 additions & 0 deletions test/schema/addTrackMutation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var graphql = require('graphql').graphql;
var assert = require('assert');
const simple = require('simple-mock');

var addTrack = require('../../lib/schema/mutation/addTrackMutation.js');
var root = require('../../lib/schema').root;
var playlistService = require('../../lib/services/playlistService.js');

var introspect = require('../utils/introspectGraphQL');
var schemaHelper = require('../utils/schemaHelper');

var addTrackMutation = require('./fixtures').addTrackMutation;

describe('Suggestions schema', function () {
it('should be possible to introspect the playlistQuery schema', function (done) {
var schema = schemaHelper.createQuerySchema(addTrack.addTrackMutation);
introspect.introspectGraphQL(schema, done);
});

it('should be able to execute the suggestionsQuery', function (done) {
var id = {
id: '123456'
};
var stub = simple.mock(playlistService, 'addTrack').resolvesWith(id);
var expectedResult = {
'data': {
'addTrack': id
}
};
graphql(root, addTrackMutation, null, {}).then(function (result) {
assert.deepEqual(result, expectedResult);
stub.restore();
done();
}).catch(done);
});
});
29 changes: 29 additions & 0 deletions test/schema/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const playlistQuery = `
query {
playlist {
name,
artist,
url,
imageUrl
}
}
`;

export const suggestionsQuery = `
query($query: String) {
suggestions {
name,
artist,
url,
imageUrl
}
}
`;

export const addTrackMutation = `
mutation addTrackMutation($name: String, $artist: String, $url: String, $imageUrl: String) {
addTrack(name: $name, artist: $artist, url: $url, imageUrl: $imageUrl) {
id
}
}
`;
41 changes: 41 additions & 0 deletions test/schema/playlistQuery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var graphql = require('graphql').graphql;
var assert = require('assert');
const simple = require('simple-mock');

var playlist = require('../../lib/schema/query/playlistQuery.js');
var root = require('../../lib/schema').root;
var playlistService = require('../../lib/services/playlistService.js');

var introspect = require('../utils/introspectGraphQL');
var schemaHelper = require('../utils/schemaHelper');

var playlistQuery = require('./fixtures').playlistQuery;

describe('Playlist schema', function () {
it('should be possible to introspect the playlistQuery schema', function (done) {
var schema = schemaHelper.createQuerySchema(playlist.playlistQuery);
introspect.introspectGraphQL(schema, done);
});

it('should be able to execute the playlistQuery', function (done) {
var playlist = [{
name: 'Hello',
artist: 'Adele',
url: 'url',
imageUrl: 'imageUrl'
}];
var stub = simple.mock(playlistService, 'retrievePlaylist').resolvesWith(playlist);
var expectedResult = {
'data': {
'playlist': playlist
}
};
graphql(root, playlistQuery, null, {}).then(function (result) {
assert.deepEqual(result, expectedResult);
stub.restore();
done();
}).catch(done);
});
});
41 changes: 41 additions & 0 deletions test/schema/suggestionsQuery.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var graphql = require('graphql').graphql;
var assert = require('assert');
const simple = require('simple-mock');

var suggestions = require('../../lib/schema/query/suggestionsQuery.js');
var root = require('../../lib/schema').root;
var suggestionsService = require('../../lib/services/suggestionsService.js');

var introspect = require('../utils/introspectGraphQL');
var schemaHelper = require('../utils/schemaHelper');

var suggestionsQuery = require('./fixtures').suggestionsQuery;

describe('Suggestions schema', function () {
it('should be possible to introspect the playlistQuery schema', function (done) {
var schema = schemaHelper.createQuerySchema(suggestions.suggestionsQuery);
introspect.introspectGraphQL(schema, done);
});

it('should be able to execute the suggestionsQuery', function (done) {
var songSuggestions = [{
name: 'Hello',
artist: 'Adele',
url: 'url',
imageUrl: 'imageUrl'
}];
var stub = simple.mock(suggestionsService, 'retrieveSongSuggestions').resolvesWith(songSuggestions);
var expectedResult = {
'data': {
'suggestions': songSuggestions
}
};
graphql(root, suggestionsQuery, null, {}).then(function (result) {
assert.deepEqual(result, expectedResult);
stub.restore();
done();
}).catch(done);
});
});
77 changes: 77 additions & 0 deletions test/services/playlistService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const simple = require('simple-mock');
const assert = require('assert');
const lambdaInvoke = require('../../lib/utils/lambda-invoke-promise');
const playlistService = require('../../lib/services/playlistService');

const track = {
name: 'Hello',
artist: 'Adele',
url: 'url',
imageUrl: 'url'
};

const suggestions = [{
name: 'Hello',
artist: 'Adele',
url: 'url',
imageUrl: 'url'
}];

describe('Playlist service', () => {
afterEach(function (done) {
simple.restore();
done();
});

it('retrieveSuggestions: returns an array of song suggestions', done => {
const result = {
data: {
suggestions: result
}
};
simple.mock(lambdaInvoke, 'invoke').resolveWith(suggestions);

playlistService.retrieveSuggestions('Fire', 10).then(data => {
assert.deepEqual(data, result);
done();
}).catch(done);
});

it('retrieveSuggestions: will return an error due to request error', done => {
const error = 'Big bad error';
simple.mock(lambdaInvoke, 'invoke').rejectWith(error);

playlistService.retrieveSuggestions('some song', 10).then(data => {
assert.deepEqual(data, error);
done();
}).catch(done);
});

it('addTrack: calls the s3 save micro service and returns a track id', done => {
const result = {
data: {
addTrack: {
id: '1234456'
}
}
};

simple.mock(lambdaInvoke, 'invoke').resolveWith({id: '1234456'});

playlistService.addTrack(track).then(data => {
assert.deepEqual(data, result);
done();
}).catch(done);
});
it('addTrack: will return an error due to request error', done => {
const error = 'Big bad error';
simple.mock(lambdaInvoke, 'invoke').rejectWith(error);

playlistService.addTrack(track).then(data => {
assert.deepEqual(data, error);
done();
}).catch(done);
});
});
18 changes: 18 additions & 0 deletions test/test-helpers.js/introspectGraphQL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var graphql = require('graphql');
var introspectionQuery = require('graphql/utilities').introspectionQuery;

var q = exports;

q.introspectGraphQL = function (schema, done) {
graphql.graphql(schema, introspectionQuery).then(function (result) {
if (result.errors && result.errors.length) {
return done(new Error(result.errors[0].message));
}
done();
}).catch(function (err) {
console.log('ERR', err);
done(err);
});
};
36 changes: 36 additions & 0 deletions test/test-helpers.js/schemaHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var graphql = require('graphql');

var createQuerySchema = function (query, field) {
return new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name: 'RootQuery',
fields: function () {
var x = {};
x[field || 'type'] = query;
return x;
}
})
});
};

var createMutationSchema = function (mutation, field) {
return new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name: 'RootQuery',
fields: {
helperField: { type: graphql.GraphQLString }
}
}),
mutation: new graphql.GraphQLObjectType({
name: 'RootMutation',
fields: function () {
var x = {};
x[field || 'type'] = mutation;
return x;
}
})
});
};

exports.createQuerySchema = createQuerySchema;
exports.createMutationSchema = createMutationSchema;
30 changes: 30 additions & 0 deletions test/utils/lambda-invoke-promise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var simple = require('simple-mock');
var AwsHelper = require('aws-lambda-helper');
var Code = require('code');
var expect = Code.expect;
var lambdaInvoke = require('../../lib/utils/lambda-invoke-promise');

describe('lambda invoke promise util', function () {
afterEach(function (done) {
simple.restore();
done();
});

it('will invoke a lambda and return a promise', function (done) {
simple.mock(AwsHelper.Lambda, 'invoke').callbackWith(null, 'some data');

lambdaInvoke.invoke({some: 'params'}).then(function (data) {
expect(data).equals('some data');
done();
}).catch(done);
});

it('will throw an error when the lambda.invoke failed', function (done) {
simple.mock(AwsHelper.Lambda, 'invoke').callbackWith('Big error');

lambdaInvoke.invoke({some: 'params'}).catch(function (error) {
expect(error).equals('Big error');
done();
});
});
});

0 comments on commit 34ba9b1

Please sign in to comment.