From 35b96a095c2b1038f0d77115fadc9b55d9abe2a1 Mon Sep 17 00:00:00 2001 From: Antreas Paschalis Date: Thu, 22 Aug 2019 10:53:18 +0100 Subject: [PATCH 1/3] SDE-170: Scheduled Daily Digests Test --- .circleci/config.yml | 3 +++ .gitignore | 3 +++ functions/scheduledDailyDigests.js | 9 +++---- tests/scheduledDailyDigests.test.js | 40 +++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tests/scheduledDailyDigests.test.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 637aed8..2386287 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -41,6 +41,9 @@ jobs: - run: name: Run ESLint command: npm run lint + - run: + name: Run Unit Tests + command: npm test serverless-deploy: docker: - image: circleci/node:10.15.1 diff --git a/.gitignore b/.gitignore index f9dda6c..49b3cc8 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,6 @@ circleci.exe # Env directories env/ + +# VS Code Config Directory +.vscode/ \ No newline at end of file diff --git a/functions/scheduledDailyDigests.js b/functions/scheduledDailyDigests.js index 5730e12..0cde863 100644 --- a/functions/scheduledDailyDigests.js +++ b/functions/scheduledDailyDigests.js @@ -1,11 +1,10 @@ import { DynamoDB, Lambda } from 'aws-sdk'; -const dynamoDbDocumentClient = new DynamoDB.DocumentClient(); -const lambda = new Lambda(); - -const { SUBSCRIPTIONS_TABLE: TableName, USER_DIGEST_LAMBDA_NAME: FunctionName } = process.env; - export const handler = async () => { + const { SUBSCRIPTIONS_TABLE: TableName, USER_DIGEST_LAMBDA_NAME: FunctionName } = process.env; + const dynamoDbDocumentClient = new DynamoDB.DocumentClient(); + const lambda = new Lambda(); + const scanParams = { TableName }; const { Items } = await dynamoDbDocumentClient.scan(scanParams).promise(); await Promise.all( diff --git a/tests/scheduledDailyDigests.test.js b/tests/scheduledDailyDigests.test.js new file mode 100644 index 0000000..86616d3 --- /dev/null +++ b/tests/scheduledDailyDigests.test.js @@ -0,0 +1,40 @@ +import { DynamoDB, Lambda } from 'aws-sdk'; +import { handler } from '../functions/scheduledDailyDigests'; + +jest.mock('aws-sdk'); + +const subscriptionsTableData = { + Items: [ + { userId: 1, subscriptions: [] }, + { userId: 2, subscriptions: ['test'] }, + { userId: 3, subscriptions: ['test1', 'test2'] } + ] +}; + +const mockDynamoDbScan = { + promise: jest.fn() +}; + +const mockLambdaInvoke = { + promise: jest.fn() +}; + +DynamoDB.DocumentClient.mockImplementation(() => ({ scan: () => mockDynamoDbScan })); + +Lambda.mockImplementation(() => ({ invoke: () => mockLambdaInvoke })); + +describe('scheduledDailyDigests function', () => { + beforeAll(() => { + process.env = Object.assign(process.env, { + SUBSCRIPTIONS_TABLE: 'subscriptions', + USER_DIGEST_LAMBDA_NAME: 'calculateUserDailyDigest' + }); + }); + + it('invokes calculateUserDailyDigest for each user with at least one subscription', async () => { + mockDynamoDbScan.promise.mockResolvedValue(subscriptionsTableData); + mockLambdaInvoke.promise.mockResolvedValue(); + await handler(); + expect(mockLambdaInvoke.promise).toHaveBeenCalledTimes(2); + }); +}); From aa749095b69554c9e937eaa34d03c8beab3e4292 Mon Sep 17 00:00:00 2001 From: Antreas Paschalis Date: Thu, 22 Aug 2019 10:56:37 +0100 Subject: [PATCH 2/3] SDE-170: Missing LF added --- tests/calculateUserDailyDigest.test.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/calculateUserDailyDigest.test.js diff --git a/tests/calculateUserDailyDigest.test.js b/tests/calculateUserDailyDigest.test.js new file mode 100644 index 0000000..05c7971 --- /dev/null +++ b/tests/calculateUserDailyDigest.test.js @@ -0,0 +1,7 @@ +import { handler } from '../functions/calculateUserDailyDigest'; + +describe('calculateUserDailyDigest function', () => { + it('passes a test', async () => { + expect(true).toEqual(true); + }); +}); From 113a1621f2e9994050fc01d3e3ee383dfcf22c3d Mon Sep 17 00:00:00 2001 From: Antreas Paschalis Date: Thu, 22 Aug 2019 11:05:16 +0100 Subject: [PATCH 3/3] SDE-170: Removed accidentally added file, missing LF --- .gitignore | 2 +- tests/calculateUserDailyDigest.test.js | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) delete mode 100644 tests/calculateUserDailyDigest.test.js diff --git a/.gitignore b/.gitignore index 49b3cc8..f5b1748 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,4 @@ circleci.exe env/ # VS Code Config Directory -.vscode/ \ No newline at end of file +.vscode/ diff --git a/tests/calculateUserDailyDigest.test.js b/tests/calculateUserDailyDigest.test.js deleted file mode 100644 index 05c7971..0000000 --- a/tests/calculateUserDailyDigest.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import { handler } from '../functions/calculateUserDailyDigest'; - -describe('calculateUserDailyDigest function', () => { - it('passes a test', async () => { - expect(true).toEqual(true); - }); -});