2022-08-17 23:35:08 +00:00
|
|
|
import * as constants from '~/lib/constants';
|
|
|
|
import * as db from '~/src/db';
|
2021-05-05 20:32:31 +00:00
|
|
|
import * as sinon from 'sinon';
|
|
|
|
|
|
|
|
// Creates a test database and returns a query builder
|
|
|
|
export async function createDB() {
|
|
|
|
const oldDatabasePath = process.env.DATABASE_PATH;
|
|
|
|
|
|
|
|
// for testing we use an in memory database
|
|
|
|
process.env.DATABASE_PATH = ':memory:';
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
constants.databasePath = process.env.DATABASE_PATH;
|
|
|
|
|
|
|
|
// Cleanup the module cache in order to have it reloaded in the local context
|
2022-08-17 23:35:08 +00:00
|
|
|
delete require.cache[require.resolve('~/src/db')];
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Initialize the database module
|
2022-09-06 18:03:23 +00:00
|
|
|
await db.initialized();
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Get the knex instance to allow queries to the db
|
2021-07-08 18:31:18 +00:00
|
|
|
const { models, upsertModel } = db;
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// This is hacky but haven't found another way to do it,
|
|
|
|
// stubbing the db methods here ensures the module under test
|
|
|
|
// is using the database we want
|
|
|
|
sinon.stub(db, 'models').callsFake(models);
|
2021-07-08 18:31:18 +00:00
|
|
|
sinon.stub(db, 'upsertModel').callsFake(upsertModel);
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
// Returns a query builder instance for the given
|
|
|
|
// table in order perform data operations
|
|
|
|
models,
|
|
|
|
|
|
|
|
// Resets the database to initial value post
|
|
|
|
// migrations
|
|
|
|
async reset() {
|
|
|
|
// Reset the contents of the db
|
2021-07-08 18:31:18 +00:00
|
|
|
await db.transaction(async (trx: any) => {
|
2021-05-05 20:32:31 +00:00
|
|
|
const result = await trx.raw(`
|
|
|
|
SELECT name, sql
|
|
|
|
FROM sqlite_master
|
|
|
|
WHERE type='table'`);
|
|
|
|
for (const r of result) {
|
|
|
|
// We don't run the migrations again
|
|
|
|
if (r.name !== 'knex_migrations') {
|
|
|
|
await trx.raw(`DELETE FROM ${r.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The supervisor expects this value to already have
|
|
|
|
// been pre-populated
|
|
|
|
await trx('deviceConfig').insert({ targetValues: '{}' });
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reset stub call history
|
|
|
|
(db.models as sinon.SinonStub).resetHistory();
|
|
|
|
(db.upsertModel as sinon.SinonStub).resetHistory();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Destroys the in-memory database and resets environment
|
|
|
|
async destroy() {
|
2021-07-08 18:31:18 +00:00
|
|
|
// Remove data from the in memory database just in case
|
|
|
|
this.reset();
|
2021-05-05 20:32:31 +00:00
|
|
|
|
|
|
|
// Restore the old datbase path
|
|
|
|
process.env.DATABASE_PATH = oldDatabasePath;
|
|
|
|
|
|
|
|
// Restore stubs
|
|
|
|
(db.models as sinon.SinonStub).restore();
|
|
|
|
(db.upsertModel as sinon.SinonStub).restore();
|
|
|
|
|
2021-07-08 18:31:18 +00:00
|
|
|
// Restore the constants
|
2021-05-05 20:32:31 +00:00
|
|
|
// @ts-ignore
|
|
|
|
constants.databasePath = process.env.DATABASE_PATH;
|
2021-07-08 18:31:18 +00:00
|
|
|
|
|
|
|
// Cleanup the module cache in order to have it reloaded
|
|
|
|
// correctly next time it's used
|
2022-08-17 23:35:08 +00:00
|
|
|
delete require.cache[require.resolve('~/src/db')];
|
2021-05-05 20:32:31 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export type TestDatabase = UnwrappedPromise<ReturnType<typeof createDB>>;
|