Skip to content

Commit

Permalink
A first step at making sure DynamoDB works
Browse files Browse the repository at this point in the history
  • Loading branch information
daemonsy committed Jul 1, 2017
1 parent 1c600f8 commit db422d8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 1 addition & 2 deletions handlers/store-favorite-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ module.exports = function(alexa) {

fetchMTAStatus(statuses => {
let closestLine = closestLineMatcher(statuses, 'nameGroup', heardNameGroup);

this.attributes["trackedTrainLines"] = (this.attributes["trackedTrainLines"] || []).concat([closestLine]);
this.attributes["trackedTrainLines"] = (this.attributes["trackedTrainLines"] || []).concat([closestLine.nameGroup]);

this.emit(':tell', `Your favorite line is ${literalize(closestLine.nameGroup)}`);
});
Expand Down
33 changes: 27 additions & 6 deletions tests/handlers/store-favorite-line-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import test from 'ava';
import uuidv4 from "uuid/v4";

import LambdaTester from 'lambda-tester';
import fetchMock from 'fetch-mock';
Expand All @@ -9,18 +10,38 @@ import createAlexaHandler from '../helpers/create-alexa-handler.js';

import dynamoDBClient from "../helpers/dynamo-db-client.js";

test.beforeEach(t => {
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));
});

test.afterEach(fetchMock.restore);

const readParams = function(tableName, event) {
return {
TableName: tableName,
ConsistentRead: true,
Key: {
userId: {
S: event.session.user.userId
}
}
}
}
test.cb('handling "tell NYC subway to track the ACE train"', t => {
t.plan(0);
t.plan(1);

const event = JSON.parse(fs.readFileSync(process.cwd() + '/tests/fixtures/events/store-favorite-line-ace.json'));
fetchMock.once(process.env.MTA_STATUS_URL, fs.readFileSync(process.cwd() + '/tests/fixtures/mta-status.xml', 'utf-8'));

const alexaHandler = createAlexaHandler({ storeFavoriteLine })
const tableName = uuidv4();
const alexaHandler = createAlexaHandler({ storeFavoriteLine }, tableName)

LambdaTester(alexaHandler)
.event(event)
.expectSucceed(r => {
t.end();
fetchMock.restore();
setTimeout(() => {
dynamoDBClient.getItem(readParams(tableName, event), (error, data) => {
t.is(data.Item.mapAttr.M.trackedTrainLines.L[0].S, "ACE");
t.end();
});
}, 100);
});
});
4 changes: 2 additions & 2 deletions tests/helpers/create-alexa-handler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Alexa = require('alexa-sdk');
const dynamoDBClient = require('./dynamo-db-client.js');

module.exports = function(handlers) {
module.exports = function(handlers, tableName) {
return (event, context, callback) => {
var alexa = Alexa.handler(event, context);
alexa.appId = process.env.APPLICATION_ID;

alexa.dynamoDBClient = dynamoDBClient;
alexa.dynamoDBTableName = 'nyc-subway-test';
alexa.dynamoDBTableName = tableName;

alexa.registerHandlers(handlers);
alexa.execute();
Expand Down

0 comments on commit db422d8

Please sign in to comment.