-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6127bd0
commit cedf6aa
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
migrations/20241223103243-fix-and-add-resourceType-to-lessons.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = { | ||
async up(db) { | ||
const lessonsCollection = db.collection('lessons') | ||
|
||
await lessonsCollection.updateMany({ resourceType: 'lessons' }, { $set: { resourceType: 'lesson' } }) | ||
|
||
await lessonsCollection.updateMany({ resourceType: { $exists: false } }, { $set: { resourceType: 'lesson' } }) | ||
}, | ||
|
||
async down(db) { | ||
const lessonsCollection = db.collection('lessons') | ||
|
||
await lessonsCollection.updateMany({ resourceType: 'lesson' }, { $set: { resourceType: 'lessons' } }) | ||
|
||
await lessonsCollection.updateMany({ resourceType: 'lesson' }, { $unset: { resourceType: '' } }) | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/test/migrations/20241223103243-fix-and-add-resourceType-to-lessons.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const mongoose = require('mongoose') | ||
const Lessons = require('~/models/lesson') | ||
const { serverInit, serverCleanup, stopServer } = require('~/test/setup') | ||
const migration = require('@root/migrations/20241223103243-fix-and-add-resourceType-to-lessons') | ||
|
||
describe('20241223082318-update-resource-type-in-lessons', () => { | ||
let server | ||
|
||
beforeAll(async () => { | ||
;({ server } = await serverInit()) | ||
}) | ||
|
||
afterEach(async () => { | ||
await serverCleanup() | ||
}) | ||
|
||
afterAll(async () => { | ||
await stopServer(server) | ||
}) | ||
|
||
const insertLessons = async (data) => { | ||
await Lessons.insertMany(data) | ||
} | ||
|
||
const getLessons = async () => { | ||
return Lessons.find({}).lean() | ||
} | ||
|
||
const lessonBase = { | ||
content: '<p>'.concat('Content 1 '.repeat(10), '</p>'), | ||
description: 'This is a test description.', | ||
author: new mongoose.Types.ObjectId() | ||
} | ||
|
||
it('should update resourceType to "lesson" for documents with "resourceType: lessons" or missing field (up)', async () => { | ||
await insertLessons([ | ||
{ | ||
...lessonBase, | ||
title: 'Lesson 1', | ||
resourceType: 'lesson', | ||
otherField: 'value1' | ||
}, | ||
{ | ||
...lessonBase, | ||
title: 'Lesson 2', | ||
resourceType: 'quiz', | ||
otherField: 'value2' | ||
} | ||
]) | ||
|
||
await migration.up(mongoose.connection.db) | ||
|
||
const updatedDocs = await getLessons() | ||
|
||
expect(updatedDocs).toHaveLength(2) | ||
|
||
updatedDocs.forEach((doc) => { | ||
if (doc.title === 'Lesson 1') { | ||
expect(doc.resourceType).toBe('lesson') | ||
} else if (doc.title === 'Lesson 2') { | ||
expect(doc.resourceType).toBe('quiz') | ||
} | ||
}) | ||
}) | ||
|
||
it('should revert resourceType to "lessons" or unset it (down)', async () => { | ||
await insertLessons([ | ||
{ | ||
...lessonBase, | ||
title: 'Lesson 1', | ||
resourceType: 'lesson', | ||
otherField: 'value1' | ||
}, | ||
{ | ||
...lessonBase, | ||
title: 'Lesson 2', | ||
resourceType: 'quiz', | ||
otherField: 'value2' | ||
} | ||
]) | ||
|
||
await migration.down(mongoose.connection.db) | ||
|
||
const revertedDocs = await getLessons() | ||
|
||
expect(revertedDocs).toHaveLength(2) | ||
|
||
revertedDocs.forEach((doc) => { | ||
if (doc.title === 'Lesson 1') { | ||
expect(doc.resourceType).toBe('lessons') | ||
} else if (doc.title === 'Lesson 2') { | ||
expect(doc.resourceType).toBe('quiz') | ||
} | ||
}) | ||
}) | ||
}) |