Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
Tests for Eventbrite transformer lambda (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairjcbrown committed Oct 28, 2018
1 parent 0d65e45 commit eb97537
Show file tree
Hide file tree
Showing 6 changed files with 3,453 additions and 1 deletion.
1,458 changes: 1,458 additions & 0 deletions lambdas/tests/eventbrite/handlers/__snapshots__/tranformer.test.js.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"pagination": {
"object_count": 0,
"page_number": 1,
"page_size": 0,
"page_count": 1,
"has_more_items": false
},
"events": [],
"location": {
"latitude": "54.642513",
"within": "96.5606400369",
"longitude": "-6.762739"
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"pagination": {
"object_count": 0,
"page_number": 1,
"page_size": 0,
"page_count": 1,
"has_more_items": false
},
"events": [{ "invalid": true }],
"location": {
"latitude": "54.642513",
"within": "96.5606400369",
"longitude": "-6.762739"
}
}

Large diffs are not rendered by default.

212 changes: 211 additions & 1 deletion lambdas/tests/eventbrite/handlers/tranformer.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,218 @@
const transformer = require("../../../eventbrite/handlers/transformer");
const { prefix, resolved } = require("../test-utils");
const { getFromS3 } = require(`${prefix}/node_modules/aws-lambda-data-utils`);
const { uploadTo } = require(`${prefix}/utils`);
const transformer = require(`${prefix}/handlers/transformer`);

const requireData = filename =>
JSON.stringify(require(`./test-data/${filename}.json`)); // eslint-disable-line global-require

const emptyEventsList = requireData("events-payload-empty");
const invalidEventsList = requireData("events-payload-invalid");
const incompleteEventsList = requireData("events-payload-incomplete");
const populatedEventsList = requireData("events-payload-populated");

const context = null;
let event;

describe("Eventbrite transformer", function() {
beforeEach(function() {
jest.clearAllMocks();
event = null;
uploadTo.mockImplementation(resolved({ key: "path/to/new/file.json" }));
});

it("is a lambda handler", function() {
expect(typeof transformer).toBe("object");
expect(typeof transformer.transform).toBe("function");
});

describe("when no new file is added to the bucket", function() {
beforeEach(function() {
event = {
Records: []
};
});

it("does not return an error and returns empty message", function(done) {
transformer.transform(event, context, function(err, response) {
expect(err).toBe(null);
expect(response).toEqual({ message: [] });
done();
});
});

it("does not uploads anything", function(done) {
transformer.transform(event, context, function() {
expect(uploadTo).toHaveBeenCalledTimes(0);
done();
});
});
});

describe("when a new file is added to the bucket", function() {
beforeEach(function() {
event = {
Records: [
{
s3: {
bucket: "test-muxer-bucket",
object: { Key: "path/to/events.json" }
}
}
]
};
});

describe("which contains no events", function() {
beforeEach(function() {
getFromS3.mockImplementation(resolved({ Body: emptyEventsList }));
});

it("does not return an error and returns message", function(done) {
transformer.transform(event, context, function(err, response) {
expect(err).toBe(null);
expect(response).toEqual({ message: ["path/to/new/file.json"] });
done();
});
});

it("uploads empty events list", function(done) {
transformer.transform(event, context, function() {
expect(uploadTo).toHaveBeenCalledTimes(1);
expect(uploadTo).toHaveBeenCalledWith(
"muxer-transformed-events",
expect.any(Function),
[]
);
done();
});
});
});

describe("which contains an invalid event", function() {
beforeEach(function() {
getFromS3.mockImplementation(resolved({ Body: invalidEventsList }));
});

it("returns an error", function(done) {
transformer.transform(event, context, function(err, response) {
expect(err).toEqual(
new TypeError("Cannot read property 'utc' of undefined")
);
expect(response).toBe(null);
done();
});
});

it("does not upload data", function(done) {
transformer.transform(event, context, function() {
expect(uploadTo).not.toHaveBeenCalled();
done();
});
});
});

describe("which contains incomplete events", function() {
beforeEach(function() {
jest.spyOn(global.console, "log").mockImplementation(() => {});
getFromS3.mockImplementation(resolved({ Body: incompleteEventsList }));
});

it("does not return an error and returns message", function(done) {
transformer.transform(event, context, function(err, response) {
expect(err).toBe(null);
expect(response).toEqual({ message: ["path/to/new/file.json"] });
done();
});
});

it("uploads transformed events list, without incomplete events", function(done) {
transformer.transform(event, context, function() {
expect(uploadTo).toHaveBeenCalledTimes(1);
expect(uploadTo.mock.calls[0][2].length).toBe(4);
expect(uploadTo.mock.calls).toMatchSnapshot();
done();
});
});

it("outputs warning", function(done) {
/* eslint-disable no-console */
transformer.transform(event, context, function() {
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(
"WARNING: some events generated were not valid!"
);
done();
});
});
});

describe("which contains complete events", function() {
beforeEach(function() {
getFromS3.mockImplementation(resolved({ Body: populatedEventsList }));
});

it("does not return an error and returns message", function(done) {
transformer.transform(event, context, function(err, response) {
expect(err).toBe(null);
expect(response).toEqual({ message: ["path/to/new/file.json"] });
done();
});
});

it("uploads transformed events list", function(done) {
transformer.transform(event, context, function() {
expect(uploadTo).toHaveBeenCalledTimes(1);
expect(uploadTo.mock.calls[0][2].length).toBe(5);
expect(uploadTo.mock.calls).toMatchSnapshot();
done();
});
});
});
});

describe("when multiple new files are added to the bucket", function() {
beforeEach(function() {
event = {
Records: [
{
s3: {
bucket: "test-muxer-bucket",
object: { Key: "path/to/events-1.json" }
}
},
{
s3: {
bucket: "test-muxer-bucket",
object: { Key: "path/to/events-2.json" }
}
}
]
};

getFromS3
.mockReturnValueOnce(resolved({ Body: populatedEventsList })())
.mockReturnValueOnce(resolved({ Body: populatedEventsList })());
});

it("does not return an error and returns messages", function(done) {
transformer.transform(event, context, function(err, response) {
expect(err).toBe(null);
expect(response).toEqual({
message: ["path/to/new/file.json", "path/to/new/file.json"]
});
done();
});
});

it("uploads transformed events lists", function(done) {
transformer.transform(event, context, function() {
expect(uploadTo).toHaveBeenCalledTimes(2);
expect(uploadTo.mock.calls[0][2].length).toBe(5);
expect(uploadTo.mock.calls[1][2].length).toBe(5);
expect(uploadTo.mock.calls).toMatchSnapshot();
done();
});
});
});
});

0 comments on commit eb97537

Please sign in to comment.