From db422d894819c842dd17bbbb8f3ea411319ae4f9 Mon Sep 17 00:00:00 2001 From: Damon Aw Date: Fri, 30 Jun 2017 22:56:35 -0400 Subject: [PATCH] A first step at making sure DynamoDB works --- handlers/store-favorite-line.js | 3 +- tests/handlers/store-favorite-line-test.js | 33 ++++++++++++++++++---- tests/helpers/create-alexa-handler.js | 4 +-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/handlers/store-favorite-line.js b/handlers/store-favorite-line.js index 97fb1c2..a42735e 100644 --- a/handlers/store-favorite-line.js +++ b/handlers/store-favorite-line.js @@ -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)}`); }); diff --git a/tests/handlers/store-favorite-line-test.js b/tests/handlers/store-favorite-line-test.js index 4717fe3..d9bf908 100644 --- a/tests/handlers/store-favorite-line-test.js +++ b/tests/handlers/store-favorite-line-test.js @@ -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'; @@ -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); }); }); diff --git a/tests/helpers/create-alexa-handler.js b/tests/helpers/create-alexa-handler.js index 6dd5b87..9dcfa0a 100644 --- a/tests/helpers/create-alexa-handler.js +++ b/tests/helpers/create-alexa-handler.js @@ -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();