directus

Форк
0
110 строк · 3.4 Кб
1
import type { Knex } from 'knex';
2
import knex from 'knex';
3
import { createTracker, MockClient, Tracker } from 'knex-mock-client';
4
import type { MockedFunction } from 'vitest';
5
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
6
import run from './run.js';
7

8
describe('run', () => {
9
	let db: MockedFunction<Knex>;
10
	let tracker: Tracker;
11

12
	beforeAll(() => {
13
		db = vi.mocked(knex.default({ client: MockClient }));
14
		tracker = createTracker(db);
15
	});
16

17
	afterEach(() => {
18
		tracker.reset();
19
	});
20

21
	describe('when passed the argument up', () => {
22
		it('returns "Nothing To Upgrade" if no directus_migrations', async () => {
23
			tracker.on.select('directus_migrations').response(['Empty']);
24

25
			await run(db, 'up').catch((e: Error) => {
26
				expect(e).toBeInstanceOf(Error);
27
				expect(e.message).toBe('Nothing to upgrade');
28
			});
29
		});
30

31
		it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
32
			tracker.on.select('directus_migrations').response([]);
33

34
			await run(db, 'up').catch((e: Error) => {
35
				expect(e).toBeInstanceOf(Error);
36
				expect(e.message).toBe('Method implemented in the dialect driver');
37
			});
38
		});
39

40
		it('returns undefined if the migration is successful', async () => {
41
			tracker.on.select('directus_migrations').response([
42
				{
43
					version: '20201028A',
44
					name: 'Remove Collection Foreign Keys',
45
					timestamp: '2021-11-27 11:36:56.471595-05',
46
				},
47
			]);
48

49
			tracker.on.delete('directus_relations').response([]);
50
			tracker.on.insert('directus_migrations').response(['Remove System Relations', '20201029A']);
51

52
			expect(await run(db, 'up')).toBe(undefined);
53
		});
54
	});
55

56
	describe('when passed the argument down', () => {
57
		it('returns "Nothing To downgrade" if no valid directus_migrations', async () => {
58
			tracker.on.select('directus_migrations').response(['Empty']);
59

60
			await run(db, 'down').catch((e: Error) => {
61
				expect(e).toBeInstanceOf(Error);
62
				expect(e.message).toBe(`Couldn't find migration`);
63
			});
64
		});
65

66
		it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
67
			tracker.on.select('directus_migrations').response([]);
68

69
			await run(db, 'down').catch((e: Error) => {
70
				expect(e).toBeInstanceOf(Error);
71
				expect(e.message).toBe('Nothing to downgrade');
72
			});
73
		});
74

75
		it(`returns "Couldn't find migration" if an invalid migration object is supplied`, async () => {
76
			tracker.on.select('directus_migrations').response([
77
				{
78
					version: '202018129A',
79
					name: 'Fake Migration',
80
					timestamp: '2020-00-32 11:36:56.471595-05',
81
				},
82
			]);
83

84
			await run(db, 'down').catch((e: Error) => {
85
				expect(e).toBeInstanceOf(Error);
86
				expect(e.message).toBe(`Couldn't find migration`);
87
			});
88
		});
89
	});
90

91
	describe('when passed the argument latest', () => {
92
		it('returns "Nothing To downgrade" if no valid directus_migrations', async () => {
93
			tracker.on.select('directus_migrations').response(['Empty']);
94

95
			await run(db, 'latest').catch((e: Error) => {
96
				expect(e).toBeInstanceOf(Error);
97
				expect(e.message).toBe(`Method implemented in the dialect driver`);
98
			});
99
		});
100

101
		it('returns "Method implemented in the dialect driver" if no directus_migrations', async () => {
102
			tracker.on.select('directus_migrations').response([]);
103

104
			await run(db, 'latest').catch((e: Error) => {
105
				expect(e).toBeInstanceOf(Error);
106
				expect(e.message).toBe('Method implemented in the dialect driver');
107
			});
108
		});
109
	});
110
});
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.