mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-06-05 09:10:49 +00:00
Migrate legacy db test to integration folder
This commit is contained in:
parent
f19f70d690
commit
f835db9509
@ -1,10 +1,9 @@
|
|||||||
import * as Bluebird from 'bluebird';
|
import * as Bluebird from 'bluebird';
|
||||||
import { knex, Knex } from 'knex';
|
import { knex, Knex } from 'knex';
|
||||||
|
import { promises as fs } from 'fs';
|
||||||
|
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
import prepare = require('~/test-lib/prepare');
|
|
||||||
import * as constants from '~/lib/constants';
|
import * as constants from '~/lib/constants';
|
||||||
import { exists } from '~/lib/fs-utils';
|
|
||||||
|
|
||||||
async function createOldDatabase(path: string) {
|
async function createOldDatabase(path: string) {
|
||||||
const db = knex({
|
const db = knex({
|
||||||
@ -41,35 +40,28 @@ async function createOldDatabase(path: string) {
|
|||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Database Migrations', () => {
|
async function restoreDb() {
|
||||||
before(async () => {
|
await fs.unlink(constants.databasePath).catch(() => {
|
||||||
await prepare();
|
/* NOOP */
|
||||||
});
|
});
|
||||||
|
// Reset the module cache to allow the database to be initialized again
|
||||||
|
delete require.cache[require.resolve('~/src/db')];
|
||||||
|
}
|
||||||
|
|
||||||
after(() => {
|
describe('db', () => {
|
||||||
// @ts-expect-error
|
afterEach(async () => {
|
||||||
constants.databasePath = process.env.DATABASE_PATH;
|
await restoreDb();
|
||||||
delete require.cache[require.resolve('~/src/db')];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates a database at the path passed on creation', async () => {
|
it('creates a database at the path passed on creation', async () => {
|
||||||
const databasePath = process.env.DATABASE_PATH_2!;
|
|
||||||
// @ts-expect-error
|
|
||||||
constants.databasePath = databasePath;
|
|
||||||
delete require.cache[require.resolve('~/src/db')];
|
|
||||||
|
|
||||||
const testDb = await import('~/src/db');
|
const testDb = await import('~/src/db');
|
||||||
await testDb.initialized();
|
await testDb.initialized();
|
||||||
expect(await exists(databasePath)).to.be.true;
|
await expect(fs.access(constants.databasePath)).to.not.be.rejected;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds new fields and removes old ones in an old database', async () => {
|
it('migrations add new fields and removes old ones in an old database', async () => {
|
||||||
const databasePath = process.env.DATABASE_PATH_3!;
|
// Create a database with an older schema
|
||||||
|
const knexForDB = await createOldDatabase(constants.databasePath);
|
||||||
const knexForDB = await createOldDatabase(databasePath);
|
|
||||||
// @ts-expect-error
|
|
||||||
constants.databasePath = databasePath;
|
|
||||||
delete require.cache[require.resolve('~/src/db')];
|
|
||||||
const testDb = await import('~/src/db');
|
const testDb = await import('~/src/db');
|
||||||
await testDb.initialized();
|
await testDb.initialized();
|
||||||
await Bluebird.all([
|
await Bluebird.all([
|
||||||
@ -94,28 +86,18 @@ describe('Database Migrations', () => {
|
|||||||
.to.eventually.be.true,
|
.to.eventually.be.true,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('Database', () => {
|
|
||||||
let db: typeof import('~/src/db');
|
|
||||||
|
|
||||||
before(async () => {
|
|
||||||
await prepare();
|
|
||||||
db = await import('~/src/db');
|
|
||||||
});
|
|
||||||
it('initializes correctly, running the migrations', () => {
|
|
||||||
return expect(db.initialized()).to.be.fulfilled;
|
|
||||||
});
|
|
||||||
it('creates a database at the path from an env var', async () => {
|
|
||||||
expect(await exists(process.env.DATABASE_PATH!)).to.be.true;
|
|
||||||
});
|
|
||||||
it('creates a deviceConfig table with a single default value', async () => {
|
it('creates a deviceConfig table with a single default value', async () => {
|
||||||
const deviceConfig = await db.models('deviceConfig').select();
|
const testDb = await import('~/src/db');
|
||||||
|
await testDb.initialized();
|
||||||
|
const deviceConfig = await testDb.models('deviceConfig').select();
|
||||||
expect(deviceConfig).to.have.lengthOf(1);
|
expect(deviceConfig).to.have.lengthOf(1);
|
||||||
expect(deviceConfig).to.deep.equal([{ targetValues: '{}' }]);
|
expect(deviceConfig).to.deep.equal([{ targetValues: '{}' }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows performing transactions', () => {
|
it('allows performing transactions', async () => {
|
||||||
return db.transaction((trx) => expect(trx.commit()).to.be.fulfilled);
|
const testDb = await import('~/src/db');
|
||||||
|
await testDb.initialized();
|
||||||
|
return testDb.transaction((trx) => expect(trx.commit()).to.be.fulfilled);
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user