Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 34e19b2

Browse files
committed
Upd migration, added tests
1 parent 42e3254 commit 34e19b2

File tree

6 files changed

+191
-159
lines changed

6 files changed

+191
-159
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@
3636
"*.{css,sass,scss}.d.ts": true
3737
},
3838

39-
"cSpell.words": ["Popconfirm", "Sider"]
39+
"cSpell.words": ["Popconfirm", "Sider", "Yadro"]
4040
}

src/base/MigrationRunner.test.ts

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import MigrationErrorCodes from '../types/MigrationErrorCodes';
55

66
describe('MigrationRunner tests', () => {
77
describe('Constructor tests', () => {
8+
test(`Should be fine`, () => {
9+
expect(
10+
new MigrationRunner([
11+
{ version: 0, schema: {} },
12+
{ version: 1, schema: {}, migration: () => null },
13+
])
14+
).toBeDefined();
15+
});
16+
817
test(`Throw ErrorCode=${MigrationErrorCodes.NoMigrations} NoMigrations`, () => {
918
expect(() => new MigrationRunner([])).toThrow(
1019
`schemaMigrations can't be empty`
@@ -29,66 +38,79 @@ describe('MigrationRunner tests', () => {
2938
});
3039

3140
describe('Migration tests', () => {
32-
test('Test migration with 3 iterations', () => {
33-
type TypeV0 = { data: number };
34-
type TypeV1 = { data: number[]; __version: number };
35-
type TypeV2 = {
36-
data: number[];
37-
additionalData: string;
38-
__version: number;
39-
};
40-
const schemaV0: JSONSchemaType<TypeV0> = {
41-
type: 'object',
42-
properties: {
43-
data: { type: 'number' },
41+
type TypeV0 = { data: number };
42+
type TypeV1 = { data: number[]; __version: number };
43+
type TypeV2 = {
44+
data: number[];
45+
additionalData: string;
46+
__version: number;
47+
};
48+
const schemaV0: JSONSchemaType<TypeV0> = {
49+
type: 'object',
50+
properties: {
51+
data: { type: 'number' },
52+
},
53+
required: ['data'],
54+
};
55+
const schemaV1: JSONSchemaType<TypeV1> = {
56+
type: 'object',
57+
properties: {
58+
__version: { type: 'number' },
59+
data: { type: 'array', items: { type: 'number' } },
60+
},
61+
required: ['data', '__version'],
62+
};
63+
const schemaV2: JSONSchemaType<TypeV2> = {
64+
type: 'object',
65+
properties: {
66+
__version: { type: 'number' },
67+
data: { type: 'array', items: { type: 'number' } },
68+
additionalData: { type: 'string' },
69+
},
70+
required: ['data', '__version', 'additionalData'],
71+
};
72+
const migrations: SchemaMigration[] = [
73+
{ version: 0, schema: schemaV0 },
74+
{
75+
version: 1,
76+
schema: schemaV1,
77+
migration(item: TypeV0): TypeV1 {
78+
return {
79+
data: [item.data],
80+
__version: 1,
81+
};
4482
},
45-
required: ['data'],
46-
};
47-
const schemaV1: JSONSchemaType<TypeV1> = {
48-
type: 'object',
49-
properties: {
50-
__version: { type: 'number' },
51-
data: { type: 'array', items: { type: 'number' } },
52-
},
53-
required: ['data', '__version'],
54-
};
55-
const schemaV2: JSONSchemaType<TypeV2> = {
56-
type: 'object',
57-
properties: {
58-
__version: { type: 'number' },
59-
data: { type: 'array', items: { type: 'number' } },
60-
additionalData: { type: 'string' },
83+
},
84+
{
85+
version: 2,
86+
schema: schemaV2,
87+
migration(item: TypeV1): TypeV2 {
88+
return {
89+
data: item.data,
90+
additionalData: 'test',
91+
__version: 2,
92+
};
6193
},
62-
required: ['data', '__version', 'additionalData'],
94+
},
95+
];
96+
97+
test('Test migration with 3 iterations', () => {
98+
const dataV0: TypeV0 = { data: 777 };
99+
const expectedData: TypeV2 = {
100+
data: [777],
101+
additionalData: 'test',
102+
__version: 2,
63103
};
64-
const migrations: SchemaMigration[] = [
65-
{ version: 0, schema: schemaV0 },
66-
{
67-
version: 1,
68-
schema: schemaV1,
69-
migration(item: TypeV0): TypeV1 {
70-
return {
71-
data: [item.data] as number[],
72-
__version: 1,
73-
};
74-
},
75-
},
76-
{
77-
version: 2,
78-
schema: schemaV2,
79-
migration(item: TypeV1): TypeV2 {
80-
return {
81-
data: item.data,
82-
additionalData: 'test',
83-
__version: 2,
84-
};
85-
},
86-
},
87-
];
104+
const mr = new MigrationRunner(migrations);
88105

89-
const dataV0: TypeV0 = { data: 77 };
106+
const resultData = mr.runMigration(dataV0);
107+
expect(resultData).toStrictEqual(expectedData);
108+
});
109+
110+
test('Test continue migration', () => {
111+
const dataV0: TypeV1 = { data: [777], __version: 1 };
90112
const expectedData: TypeV2 = {
91-
data: [77],
113+
data: [777],
92114
additionalData: 'test',
93115
__version: 2,
94116
};
@@ -97,7 +119,9 @@ describe('MigrationRunner tests', () => {
97119
const resultData = mr.runMigration(dataV0);
98120
expect(resultData).toStrictEqual(expectedData);
99121
});
122+
});
100123

124+
describe('Migration errors', () => {
101125
test(`Throw ErrorCode=${MigrationErrorCodes.ValidationFailed} ValidationFailed`, () => {
102126
type TypeV0 = { data: number; text: string; prop: { test: 1 } };
103127

@@ -123,15 +147,15 @@ describe('MigrationRunner tests', () => {
123147

124148
expect(() => mr.runMigration(dataV0)).toThrow(
125149
[
126-
'Migration to version=0. Schema validation error. Found next errors:',
150+
'Migration to version=1. Schema validation error. Found next errors:',
127151
'"/": "must have required property \'data\'"',
128152
'"/text": "must be string"',
129153
'"/prop": "must have required property \'test\'"',
130154
].join('\n')
131155
);
132156
});
133157

134-
test(`Throw ErrorCode=${MigrationErrorCodes.MigrationNotFound} MigrationNotFound`, () => {
158+
test(`Throw ErrorCode=${MigrationErrorCodes.MigrationNotFound} MigrationNotFound - no migration for version=1`, () => {
135159
type TypeV0 = { data: number };
136160

137161
const schemaV0: JSONSchemaType<TypeV0> = {
@@ -152,7 +176,7 @@ describe('MigrationRunner tests', () => {
152176
const mr = new MigrationRunner(migrations);
153177

154178
expect(() => mr.runMigration(dataV0)).toThrow(
155-
/Migration {undefined->\d} not found/
179+
/Migration {\d->\d} not found/
156180
);
157181
});
158182

0 commit comments

Comments
 (0)