Add migration unit tests
This commit is contained in:
parent
86a4ac072b
commit
a4aa371ceb
|
@ -1,6 +1,6 @@
|
|||
// Mock data
|
||||
|
||||
require("./utilities/test-config")
|
||||
require("../../../tests/utilities/dbConfig")
|
||||
|
||||
const database = require("../../../db")
|
||||
const { authenticateThirdParty } = require("../third-party-common")
|
||||
|
|
|
@ -15,7 +15,7 @@ const DB_LOOKUP = {
|
|||
],
|
||||
}
|
||||
|
||||
const getMigrationsDoc = async db => {
|
||||
exports.getMigrationsDoc = async db => {
|
||||
// get the migrations doc
|
||||
try {
|
||||
return await db.get(DocumentTypes.MIGRATIONS)
|
||||
|
@ -27,25 +27,21 @@ const getMigrationsDoc = async db => {
|
|||
}
|
||||
|
||||
exports.migrateIfRequired = async (migrationDb, migrationName, migrateFn) => {
|
||||
let db
|
||||
if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) {
|
||||
db = getGlobalDB()
|
||||
} else {
|
||||
throw new Error(`Unrecognised migration db [${migrationDb}]`)
|
||||
}
|
||||
|
||||
if (!DB_LOOKUP[migrationDb].includes(migrationName)) {
|
||||
throw new Error(
|
||||
`Unrecognised migration name [${migrationName}] for db [${migrationDb}]`
|
||||
)
|
||||
}
|
||||
return tryMigrate(db, migrationName, migrateFn)
|
||||
}
|
||||
|
||||
const tryMigrate = async (db, migrationName, migrateFn) => {
|
||||
try {
|
||||
const doc = await getMigrationsDoc(db)
|
||||
let db
|
||||
if (migrationDb === exports.MIGRATION_DBS.GLOBAL_DB) {
|
||||
db = getGlobalDB()
|
||||
} else {
|
||||
throw new Error(`Unrecognised migration db [${migrationDb}]`)
|
||||
}
|
||||
|
||||
if (!DB_LOOKUP[migrationDb].includes(migrationName)) {
|
||||
throw new Error(
|
||||
`Unrecognised migration name [${migrationName}] for db [${migrationDb}]`
|
||||
)
|
||||
}
|
||||
|
||||
const doc = await exports.getMigrationsDoc(db)
|
||||
// exit if the migration has been performed
|
||||
if (doc[migrationName]) {
|
||||
return
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`migrations should match snapshot 1`] = `
|
||||
Object {
|
||||
"_id": "migrations",
|
||||
"_rev": "1-af6c272fe081efafecd2ea49a8fcbb40",
|
||||
"user_email_view_casing": 1487076708000,
|
||||
}
|
||||
`;
|
|
@ -0,0 +1,60 @@
|
|||
require("../../tests/utilities/dbConfig")
|
||||
|
||||
const { migrateIfRequired, MIGRATION_DBS, MIGRATIONS, getMigrationsDoc } = require("../index")
|
||||
const database = require("../../db")
|
||||
const {
|
||||
StaticDatabases,
|
||||
} = require("../../db/utils")
|
||||
|
||||
Date.now = jest.fn(() => 1487076708000)
|
||||
let db
|
||||
|
||||
describe("migrations", () => {
|
||||
|
||||
const migrationFunction = jest.fn()
|
||||
|
||||
beforeEach(() => {
|
||||
db = database.getDB(StaticDatabases.GLOBAL.name)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
jest.clearAllMocks()
|
||||
await db.destroy()
|
||||
})
|
||||
|
||||
const validMigration = () => {
|
||||
return migrateIfRequired(MIGRATION_DBS.GLOBAL_DB, MIGRATIONS.USER_EMAIL_VIEW_CASING, migrationFunction)
|
||||
}
|
||||
|
||||
it("should run a new migration", async () => {
|
||||
await validMigration()
|
||||
expect(migrationFunction).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should match snapshot", async () => {
|
||||
await validMigration()
|
||||
const doc = await getMigrationsDoc(db)
|
||||
expect(doc).toMatchSnapshot()
|
||||
})
|
||||
|
||||
it("should skip a previously run migration", async () => {
|
||||
await validMigration()
|
||||
await validMigration()
|
||||
expect(migrationFunction).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("should reject an unknown migration name", async () => {
|
||||
expect(async () => {
|
||||
await migrateIfRequired(MIGRATION_DBS.GLOBAL_DB, "bogus_name", migrationFunction)
|
||||
}).rejects.toThrow()
|
||||
expect(migrationFunction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should reject an unknown database name", async () => {
|
||||
expect(async () => {
|
||||
await migrateIfRequired("bogus_db", MIGRATIONS.USER_EMAIL_VIEW_CASING, migrationFunction)
|
||||
}).rejects.toThrow()
|
||||
expect(migrationFunction).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
})
|
|
@ -1,5 +1,5 @@
|
|||
const PouchDB = require("pouchdb")
|
||||
const env = require("../../../../environment")
|
||||
const env = require("../../environment")
|
||||
|
||||
let POUCH_DB_DEFAULTS
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
const packageConfiguration = require("../../../../index")
|
||||
const packageConfiguration = require("../../index")
|
||||
const CouchDB = require("./db")
|
||||
packageConfiguration.init(CouchDB)
|
Loading…
Reference in New Issue