Skip to content

Commit

Permalink
Fix migration tests so work for any version of OED
Browse files Browse the repository at this point in the history
The migration tests did not account for a major version of 1 and above.
This fixes that and also makes them more general so they work with any
major, minor and patch version.
  • Loading branch information
huss committed Sep 29, 2023
1 parent 33483a2 commit 0e3ace8
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/server/test/migration/migrateDatabaseInvalidTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,32 @@ const { mocha, expect, testDB } = require('../common');

const Migration = require('../../models/Migration');
const { migrateAll } = require('../../migrations/migrateDatabase');
const VERSION = require('../../version');

// Puts the parts of the semantic version together separated by period.
function getVersion(major, minor, patch) {
return (major + '.' + minor + '.' + patch);
}

const versionLists = ['0.100.0-0.200.0', '0.200.0-0.300.0', '0.300.0-0.100.0', '0.100.0-0.400.0', '0.200.1-0.500.0'];
// The current version of OED.
const startMajor = Number(VERSION.major);
const startMinor = Number(VERSION.minor);
const startPatch = Number(VERSION.patch);
// The possible migrations to use in testing.
// They all displace from the current version so they work even when the OED version changes.
const versionLists = [
// Valid from above the current version to the next one.
getVersion(startMajor, startMinor + 100, startPatch) + '-' + getVersion(startMajor, startMinor + 200, startPatch),
// Valid for the next version migration.
getVersion(startMajor, startMinor + 200, startPatch) + '-' + getVersion(startMajor, startMinor + 300, startPatch),
// Invalid since goes backwards from the last 1 above.
getVersion(startMajor, startMinor + 300, startPatch) + '-' + getVersion(startMajor, startMinor + 100, startPatch),
// Valid from the first one after current to a higher one that is different.
getVersion(startMajor, startMinor + 100, startPatch) + '-' + getVersion(startMajor, startMinor + 400, startPatch),
// Valid from the second one after current to a higher one that is different.
// Note increase the patch version so you cannot get to the final version in this migration from the current version.
getVersion(startMajor, startMinor + 200, startPatch + 1) + '-' + getVersion(startMajor, startMinor + 500, startPatch),
]
const migrationList = [];
const called = [false, false, false, false, false];

Expand All @@ -30,31 +53,37 @@ for (let i = 0; i < versionLists.length; i++) {
mocha.describe('Migration Invalid', () => {
mocha.beforeEach(async () => {
const conn = testDB.getConnection();
await new Migration(undefined, '0.0.0', '0.100.0').insert(conn);
// Add migration from the current version to the first one above for migrations.
await new Migration(undefined, getVersion(startMajor, startMinor, startPatch),
getVersion(startMajor, startMinor + 100, startPatch)).insert(conn);
});

mocha.it('should fail because of down migration', async () => {
const conn = testDB.getConnection();
await expect(migrateAll('0.500.0', migrationList, conn))
.to.be.rejectedWith('Migration fromVersion 0.300.0 is more recent than toVersion 0.100.0');
await expect(migrateAll(getVersion(startMajor, startMinor + 500, startPatch), migrationList, conn))
.to.be.rejectedWith('Migration fromVersion ' + getVersion(startMajor, startMinor + 300, startPatch) +
' is more recent than toVersion 1.100.0');
});

mocha.it('should fail because there is no path', async () => {
const conn = testDB.getConnection();
const list = migrationList.filter(e => e.fromVersion !== '0.300.0');
await expect(migrateAll('0.500.0', list, conn))
// Get rid of the invalid conversion so all OK.
const list = migrationList.filter(e => e.fromVersion !== getVersion(startMajor, startMinor + 300, startPatch));
await expect(migrateAll(getVersion(startMajor, startMinor + 500, startPatch), list, conn))
.to.be.rejectedWith('No path found');
});

mocha.it('should fail because there is no version in the list', async () => {
const list = migrationList.filter(e => e.fromVersion !== '0.300.0');
await expect(migrateAll('0.600.0', list, conn))
.to.be.rejectedWith('Could not find version 0.600.0 from the registered migration list');
// Get rid of the invalid conversion so all OK.
const list = migrationList.filter(e => e.fromVersion !== getVersion(startMajor, startMinor + 300, startPatch));
await expect(migrateAll(getVersion(startMajor, startMinor + 600, startPatch), list, conn))
.to.be.rejectedWith('Could not find version 1.600.0 from the registered migration list');
});

mocha.it('should fail because the current version is the highest Version', async () => {
const list = migrationList.filter(e => e.fromVersion !== '0.300.0');
await expect(migrateAll('0.100.0', list, conn))
.to.be.rejectedWith('You have the highest version');
// Get rid of the invalid conversion so all OK.
const list = migrationList.filter(e => e.fromVersion !== getVersion(startMajor, startMinor + 300, startPatch));
await expect(migrateAll('1.100.0', list, conn))
.to.be.rejectedWith('You have the highest version');
});
});

0 comments on commit 0e3ace8

Please sign in to comment.