78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
import {setup} from '@holllo/test';
|
|
import {
|
|
migrate,
|
|
Migration,
|
|
SkipMigrationFn,
|
|
} from '../source/migration-helper.js';
|
|
|
|
await setup('migrate<T>', async (group) => {
|
|
group.test('T = Date', async (test) => {
|
|
const migrations: Array<Migration<Date, string, string>> = [
|
|
{
|
|
version: new Date('2022-01-01'),
|
|
migrate: async (data: string) => `${data} migrated`,
|
|
},
|
|
];
|
|
|
|
function offsetDate(offset: number): Date {
|
|
const d = new Date(migrations[0].version);
|
|
d.setDate(d.getDate() + offset);
|
|
return d;
|
|
}
|
|
|
|
const data = 'data';
|
|
test.equals(
|
|
await migrate(data, offsetDate(-1), migrations),
|
|
'data migrated',
|
|
);
|
|
test.equals(await migrate(data, offsetDate(1), migrations), data);
|
|
});
|
|
|
|
group.test('T = number', async (test) => {
|
|
const migrations: Array<Migration<number, string, string>> = [
|
|
{
|
|
version: 5,
|
|
migrate: async (data: string) => `${data} migrated`,
|
|
},
|
|
];
|
|
|
|
const data = 'data';
|
|
test.equals(await migrate(data, 4, migrations), 'data migrated');
|
|
test.equals(await migrate(data, 6, migrations), data);
|
|
});
|
|
|
|
group.test('T = {custom: number}', async (test) => {
|
|
type Custom = {
|
|
custom: number;
|
|
};
|
|
|
|
const skip: SkipMigrationFn<Custom> = (a, b) => a.custom > b.custom;
|
|
const migrations: Array<Migration<Custom, string, string>> = [
|
|
{
|
|
version: {custom: 5},
|
|
migrate: async (data: string) => `${data} migrated`,
|
|
},
|
|
];
|
|
|
|
const data = 'data';
|
|
test.equals(await migrate(data, {custom: 4}, migrations, skip), data);
|
|
test.equals(
|
|
await migrate(data, {custom: 6}, migrations, skip),
|
|
'data migrated',
|
|
);
|
|
});
|
|
|
|
group.test('T = string', async (test) => {
|
|
const migrations: Array<Migration<string, string, string>> = [
|
|
{
|
|
version: '0.2.4',
|
|
migrate: async (data: string) => `${data} migrated`,
|
|
},
|
|
];
|
|
|
|
const data = 'data';
|
|
test.equals(await migrate(data, '0.2.3', migrations), 'data migrated');
|
|
test.equals(await migrate(data, '0.2.5', migrations), data);
|
|
});
|
|
});
|