Skip to content

Commit f33244d

Browse files
Implement test cases for data migration
1 parent a85be40 commit f33244d

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

tests/backend/migrations/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Migrations
2+
3+
These test cases are used to ensure that data migrations continue to work
4+
correctly.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import api from '$endpoints';
2+
import { getDataDir, getPrivateDataDir } from '$lib/server/data/dataDir';
3+
import { unzip } from '$lib/server/zip';
4+
import { rimraf } from 'rimraf';
5+
6+
/** Extract the given zip file into the data dir, then refresh the data to trigger a migration */
7+
export async function migrateDataFromZip(zipFile: string) {
8+
await rimraf(getDataDir());
9+
await unzip(zipFile, getDataDir());
10+
await api().debug.dataRefresh();
11+
}
12+
13+
/**
14+
* Extract the given zip file into the private data dir, then refresh the data to trigger a
15+
* migration.
16+
*/
17+
export async function migratePrivateDataFromZip(zipFile: string) {
18+
await rimraf(getPrivateDataDir());
19+
await unzip(zipFile, getPrivateDataDir());
20+
await api().debug.dataRefresh();
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Migrations / v0.6.6
2+
3+
Migration test case for `v0.6.6`.
4+
5+
## Data
6+
7+
Overall structure:
8+
9+
* `group`
10+
* `listed`
11+
* `unlisted`
12+
13+
Of note:
14+
15+
* `listed` is a listed child of `group`
16+
* `unlisted` is not listed as a child of `group`
17+
* `listed` has a URL of `https://example.com`
18+
* `listed` and `unlisted` link to each other
19+
20+
## Private data
21+
22+
* Username: `maddy`
23+
* Password: `Maddy123#`
3 KB
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { ApiClient } from '$endpoints';
2+
import { beforeEach, describe, expect, it } from 'vitest';
3+
import { setup } from '../../helpers';
4+
import { migrateDataFromZip, migratePrivateDataFromZip } from '../migration';
5+
import path from 'path';
6+
import itemId from '$lib/itemId';
7+
8+
let api: ApiClient;
9+
10+
beforeEach(async () => {
11+
api = (await setup()).api;
12+
});
13+
14+
describe('Public data', () => {
15+
beforeEach(async () => {
16+
await migrateDataFromZip(path.join(__dirname, 'data.zip'));
17+
});
18+
19+
it('Correctly creates all items', async () => {
20+
await expect(api.item(itemId.ROOT).info.get()).resolves.toMatchObject({
21+
children: ['group']
22+
});
23+
24+
await expect(api.item(itemId.fromStr('/group')).info.get()).resolves.toMatchObject({
25+
// Only listed child is listed
26+
children: ['listed']
27+
});
28+
29+
// Both children resolve
30+
await expect(api.item(itemId.fromStr('/group/listed')).info.get()).toResolve();
31+
await expect(api.item(itemId.fromStr('/group/unlisted')).info.get()).toResolve();
32+
});
33+
34+
it('Migrates URL sections correctly', async () => {
35+
await expect(api.item(itemId.fromStr('/group/listed')).info.get()).resolves.toMatchObject({
36+
sections: expect.arrayContaining([
37+
{
38+
type: 'site',
39+
url: 'https://example.com',
40+
// Uses default icon and label
41+
icon: null,
42+
label: null,
43+
},
44+
])
45+
});
46+
});
47+
48+
it('Migrates link sections correctly', async () => {
49+
await expect(api.item(itemId.fromStr('/group/listed')).info.get()).resolves.toMatchObject({
50+
sections: expect.arrayContaining([
51+
{
52+
type: 'links',
53+
label: 'See also',
54+
items: ['/group/unlisted'],
55+
style: 'card',
56+
},
57+
])
58+
});
59+
});
60+
});
61+
62+
it('Migrates private data correctly', async () => {
63+
await migratePrivateDataFromZip(path.join(__dirname, 'private_data.zip'));
64+
// We can still log in
65+
await expect(api.admin.auth.login('maddy', 'Maddy123#')).toResolve();
66+
});
697 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)