mirror of
https://github.com/directus/directus.git
synced 2026-01-25 17:27:57 -05:00
* base neg tests * more tests for migrations run() * tests passing * updated test Co-authored-by: Jay Cammarano <jaycammarano@gmail.com> Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
98 lines
3.3 KiB
TypeScript
98 lines
3.3 KiB
TypeScript
import knex, { Knex } from 'knex';
|
|
import { getTracker, MockClient, Tracker } from 'knex-mock-client';
|
|
import run from '../../../src/database/migrations/run';
|
|
|
|
describe('run', () => {
|
|
let db: jest.Mocked<Knex>;
|
|
let tracker: Tracker;
|
|
|
|
beforeAll(() => {
|
|
db = knex({ client: MockClient }) as jest.Mocked<Knex>;
|
|
tracker = getTracker();
|
|
});
|
|
|
|
afterEach(() => {
|
|
tracker.reset();
|
|
});
|
|
|
|
describe('when passed the argument up', () => {
|
|
it('returns "Nothing To Upgrade" if no directus_migrations', async () => {
|
|
tracker.on.select('directus_migrations').response(['Empty']);
|
|
|
|
await run(db, 'up').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe('Nothing to upgrade');
|
|
});
|
|
});
|
|
it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
|
|
tracker.on.select('directus_migrations').response([]);
|
|
|
|
await run(db, 'up').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe('Method implemented in the dialect driver');
|
|
});
|
|
});
|
|
it('returns undefined if the migration is successful', async () => {
|
|
tracker.on.select('directus_migrations').response([
|
|
{
|
|
version: '20201028A',
|
|
name: 'Remove Collection Foreign Keys',
|
|
timestamp: '2021-11-27 11:36:56.471595-05',
|
|
},
|
|
]);
|
|
tracker.on.delete('directus_relations').response([]);
|
|
tracker.on.insert('directus_migrations').response(['Remove System Relations', '20201029A']);
|
|
|
|
expect(await run(db, 'up')).toBe(undefined);
|
|
});
|
|
});
|
|
describe('when passed the argument down', () => {
|
|
it('returns "Nothing To downgrade" if no valid directus_migrations', async () => {
|
|
tracker.on.select('directus_migrations').response(['Empty']);
|
|
|
|
await run(db, 'down').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe(`Couldn't find migration`);
|
|
});
|
|
});
|
|
it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
|
|
tracker.on.select('directus_migrations').response([]);
|
|
|
|
await run(db, 'down').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe('Nothing to downgrade');
|
|
});
|
|
});
|
|
it(`returns "Couldn't find migration" if an invalid migration object is supplied`, async () => {
|
|
tracker.on.select('directus_migrations').response([
|
|
{
|
|
version: '202018129A',
|
|
name: 'Fake Migration',
|
|
timestamp: '2020-00-32 11:36:56.471595-05',
|
|
},
|
|
]);
|
|
await run(db, 'down').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe(`Couldn't find migration`);
|
|
});
|
|
});
|
|
});
|
|
describe('when passed the argument latest', () => {
|
|
it('returns "Nothing To downgrade" if no valid directus_migrations', async () => {
|
|
tracker.on.select('directus_migrations').response(['Empty']);
|
|
|
|
await run(db, 'latest').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe(`Method implemented in the dialect driver`);
|
|
});
|
|
});
|
|
it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
|
|
tracker.on.select('directus_migrations').response([]);
|
|
await run(db, 'latest').catch((e: Error) => {
|
|
expect(e).toBeInstanceOf(Error);
|
|
expect(e.message).toBe('Method implemented in the dialect driver');
|
|
});
|
|
});
|
|
});
|
|
});
|