Skip to content

Commit

Permalink
Fixed migration (#818)
Browse files Browse the repository at this point in the history
Fixed migration
  • Loading branch information
SamGodwin2 authored Dec 18, 2019
2 parents 85110af + d40e9ae commit dcadb93
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 52 deletions.
11 changes: 10 additions & 1 deletion eq-author-api/middleware/runQuestionnaireMigrations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { saveQuestionnaire } = require("../utils/datastore");
const {
saveQuestionnaire,
getQuestionnaireMetaById,
} = require("../utils/datastore");
const { merge } = require("lodash");

module.exports = logger => ({ currentVersion, migrations }) => async (
req,
Expand All @@ -16,6 +20,11 @@ module.exports = logger => ({ currentVersion, migrations }) => async (
}

try {
const questionnaireMeta = await getQuestionnaireMetaById(
req.questionnaire.id
);
merge(questionnaireMeta, { ...req.questionnaire });
req.questionnaire = questionnaireMeta;
req.questionnaire = await migrations
.slice(req.questionnaire.version)
.reduce((questionnaire, migration) => {
Expand Down
34 changes: 33 additions & 1 deletion eq-author-api/middleware/runQuestionnaireMigrations.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const runQuestionnaireMigrations = require("./runQuestionnaireMigrations");
const { buildContext } = require("../tests/utils/contextBuilder");
require("../utils/datastore");

jest.mock("../utils/datastore", () => ({
...require.requireActual("../utils/datastore"),
getQuestionnaireMetaById: jest.fn(() => ({
createdAt: 1576579844508,
createdBy: "a5570fd6-af3a-4192-8286-f66ac304ba39",
isPublic: true,
history:
'[{"id":"creationEvent","publishStatus":"Questionnaire created","questionnaireTitle":"Helo","userId":"a5570fd6-af3a-4192-8286-f66ac304ba39","time":"2019-12-17T10:50:44.508Z","type":"system"}]',
id: "f8b144f5-a723-4c4c-acac-1c23f9b9dddf",
title: "Helo",
type: "Social",
publishStatus: "Unpublished",
updatedAt: 1576590370548,
})),
}));

describe("runQuestionnaireMigrations", () => {
let res, req, next, migrations, logger;
Expand All @@ -23,7 +40,6 @@ describe("runQuestionnaireMigrations", () => {
req = {};
let migrationOne = jest.fn(() => req.questionnaire);
migrations = [migrationOne];

await runQuestionnaireMigrations(logger)({
migrations,
currentVersion: migrations.length,
Expand Down Expand Up @@ -89,6 +105,22 @@ describe("runQuestionnaireMigrations", () => {
expect(req.questionnaire.version).toEqual(1);
});

it("Should merge the meta and questionnaire objects", async () => {
req.questionnaire.version = 0;
let migrationOne = jest.fn(() => req.questionnaire);
migrations = [migrationOne];
await runQuestionnaireMigrations(logger)({
migrations,
currentVersion: migrations.length,
})(req, res, next);

expect(next).toHaveBeenCalled();
expect(migrationOne).not.toHaveBeenCalledWith({
publishStatus: "Unpublished",
sections: expect.any(Array),
});
});

it("should migrate data and set version correctly", async () => {
req.questionnaire.version = 1;

Expand Down
9 changes: 1 addition & 8 deletions eq-author-api/migrations/addTypeToHistoryEvent.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
const { getQuestionnaireMetaById, saveModel } = require("../utils/datastore");
const { QuestionnaireModel } = require("../db/models/DynamoDB");

module.exports = async function addTypeToHistoryEvent(questionnaire) {
const metadata = await getQuestionnaireMetaById(questionnaire.id);

metadata.history.map(item => {
questionnaire.history.map(item => {
if (!item.type) {
item.type = item.hasOwnProperty("bodyText") ? "note" : "system";
}
return item;
});

await saveModel(new QuestionnaireModel(metadata));

return questionnaire;
};
61 changes: 19 additions & 42 deletions eq-author-api/migrations/addTypeToHistoryEvent.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
const { cloneDeep } = require("lodash");
const addTypeToHistoryEvent = require("./addTypeToHistoryEvent.js");

const { getQuestionnaireMetaById, saveModel } = require("../utils/datastore");
const { QuestionnaireModel } = require("../db/models/DynamoDB");

describe("addTypeToHistoryEvent", () => {
let questionnaire, historyEvent, metadata, creationEvent;
let questionnaire, historyEvent, noteEvent, creationEvent;
const questionnaireId = "aca64645-98ef-43ae-8b77-d749b4e89a05";

beforeEach(async () => {
questionnaire = { id: questionnaireId };
creationEvent = {
id: "123",
publishStatus: "Questionnaire created",
Expand All @@ -24,16 +20,17 @@ describe("addTypeToHistoryEvent", () => {
userId: "12345",
time: new Date(),
};
metadata = {
noteEvent = {
id: "fdfsdfds-sdfsdf-dfdsf-dsfdsf",
bodyText: "Some note..",
questionnaireTitle: `This is a test`,
userId: "12345",
time: new Date(),
};
questionnaire = {
id: questionnaireId,
isPublic: true,
createdAt: 1574682388976,
updatedAt: 1574682388976,
type: "Social",
history: [creationEvent],
history: [creationEvent, historyEvent, noteEvent],
};

await saveModel(new QuestionnaireModel(metadata));
});

it("should be deterministic", () => {
Expand All @@ -43,60 +40,40 @@ describe("addTypeToHistoryEvent", () => {
});

it("should add system type when there is no body text", async () => {
metadata.history[1] = historyEvent;

const createdMetadata = await saveModel(new QuestionnaireModel(metadata));
expect(createdMetadata.history[1].type).toBeUndefined();

await addTypeToHistoryEvent(questionnaire);

const migratedMetadata = await getQuestionnaireMetaById(questionnaireId);
expect(migratedMetadata.history[0].type).toBe("system");
expect(questionnaire.history[0].type).toBe("system");
});

it("should add note type when there is body text", async () => {
metadata.history[1] = { ...historyEvent, bodyText: "Some note.." };

const createdMetadata = await saveModel(new QuestionnaireModel(metadata));
expect(createdMetadata.history[1].type).toBeUndefined();

await addTypeToHistoryEvent(questionnaire);

const migratedMetadata = await getQuestionnaireMetaById(questionnaireId);
expect(migratedMetadata.history[1].type).toBe("note");
expect(questionnaire.history[2].type).toBe("note");
});

it("should not change type when it already exists", async () => {
metadata.history[1] = {
questionnaire.history[1] = {
...historyEvent,
bodyText: "Some note..",
type: "note",
};

const createdMetadata = await saveModel(new QuestionnaireModel(metadata));
expect(createdMetadata.history[1].type).toBe("note");

await addTypeToHistoryEvent(questionnaire);

const migratedMetadata = await getQuestionnaireMetaById(questionnaireId);
expect(migratedMetadata.history[0].type).toBe("system");
expect(migratedMetadata.history[1].type).toBe("note");
expect(questionnaire.history[0].type).toBe("system");
expect(questionnaire.history[1].type).toBe("note");
});

it("should work with multiple history items", async () => {
metadata.history = [
questionnaire.history = [
creationEvent,
{ ...historyEvent, bodyText: "Some note.." },
{ ...historyEvent, type: "testType" },
];

await saveModel(new QuestionnaireModel(metadata));

await addTypeToHistoryEvent(questionnaire);

const migratedMetadata = await getQuestionnaireMetaById(questionnaireId);
expect(migratedMetadata.history[0].type).toBe("system");
expect(migratedMetadata.history[1].type).toBe("note");
expect(migratedMetadata.history[2].type).toBe("testType");
expect(questionnaire.history[0].type).toBe("system");
expect(questionnaire.history[1].type).toBe("note");
expect(questionnaire.history[2].type).toBe("testType");
});
});

0 comments on commit dcadb93

Please sign in to comment.