Skip to content

Commit

Permalink
Fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StorytellerCZ committed Aug 23, 2023
1 parent b6df1de commit f8b8428
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
5 changes: 3 additions & 2 deletions migrations_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ Migrations.migrateTo = async function(command) {
* @returns {Promise<void>}
*/
Migrations.getVersion = async function() {
return await this._getControl().version;
const control = await this._getControl()
return control.version;
};

/**
Expand Down Expand Up @@ -293,7 +294,7 @@ Migrations._migrateTo = async function(version, rerun) {

/**
* gets the current control record, optionally creating it if non-existent
* @returns {Promise<*>}
* @returns {Promise<{ version: number, locked: boolean }>}
* @private
*/
Migrations._getControl = async function() {
Expand Down
44 changes: 40 additions & 4 deletions migrations_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Tinytest.addAsync('Migrates up once and only once.', async function(test) {
// migrates once
await Migrations.migrateTo('latest');
test.equal(run, ['u1']);
test.equal(Migrations.getVersion(), 1);
test.equal(await Migrations.getVersion(), 1);

// shouldn't do anything
await Migrations.migrateTo('latest');
test.equal(run, ['u1']);
test.equal(Migrations.getVersion(), 1);
test.equal(await Migrations.getVersion(), 1);
});

Tinytest.addAsync('Migrates up once and back down.', async function(test) {
Expand Down Expand Up @@ -125,7 +125,7 @@ Tinytest.addAsync('Tests migrating down', async function(test) {
test.equal(await Migrations.getVersion(), 2);

// should throw as migration u2 has no down method and remain at the save ver
test.throws(async function() {
await test.throwsAsync(async function() {
await Migrations.migrateTo('1');
}, /Cannot migrate/);
test.equal(run, ['u1', 'u2', 'u3', 'd3']);
Expand Down Expand Up @@ -204,7 +204,7 @@ Tinytest.addAsync('Checks that version is updated if subsequent migration fails'
});

// migrate up, which should throw
test.throws(async function() {
await test.throwsAsync(async function() {
await Migrations.migrateTo('latest');
});
test.equal(run, ['u1']);
Expand Down Expand Up @@ -355,3 +355,39 @@ Tinytest.addAsync(
Migrations.options.logger = null;
},
);

Tinytest.addAsync('Async migrations', async function(test) {
await Migrations._reset();
let num = 10

Migrations.add({
version: 1,
up: async function() {
num = 20
},
down: async function () {
num = 10
}
});

await Migrations.migrateTo(1);
test.equal(num, 20)

await Migrations.migrateTo(0);
test.equal(num, 10)
})

// Tinytest.addAsync('Fail migration when not a function', async function(test) {
// await Migrations._reset();
//
// Migrations.add({
// version: 1,
// name: 'Failure of a migration',
// up: 'this should fail'
// });
//
// test.throwsAsync(async function() {
// await Migrations.migrateTo('latest');
// }, /Migration must supply an up function/);
//
// })

0 comments on commit f8b8428

Please sign in to comment.