|
| 1 | +import MigrationRunner from './MigrationRunner'; |
| 2 | +import { SchemaMigration } from '../types/SchemaMigration'; |
| 3 | +import { JSONSchemaType } from 'ajv'; |
| 4 | + |
| 5 | +describe('MigrationRunner tests', () => { |
| 6 | + describe('constructor tests', () => { |
| 7 | + test('throw error when schemaMigrations is empty', () => { |
| 8 | + expect(() => new MigrationRunner([])).toThrow( |
| 9 | + 'schemaMigrations can`t be empty' |
| 10 | + ); |
| 11 | + }); |
| 12 | + |
| 13 | + test('throw error when there is no migration `version=0`', () => { |
| 14 | + expect(() => new MigrationRunner([{ version: 1, schema: {} }])).toThrow( |
| 15 | + 'schemaMigrations should have migration for `version=0`' |
| 16 | + ); |
| 17 | + }); |
| 18 | + |
| 19 | + test('throw error when versions doesn`t go one after the other', () => { |
| 20 | + expect( |
| 21 | + () => |
| 22 | + new MigrationRunner([ |
| 23 | + { version: 0, schema: {} }, |
| 24 | + { version: 2, schema: {} }, |
| 25 | + ]) |
| 26 | + ).toThrow('Each version should go one after the other'); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('migration tests', () => { |
| 31 | + test('Test migration', () => { |
| 32 | + type TypeV0 = { data: number }; |
| 33 | + type TypeV1 = { data: number[]; __version: number }; |
| 34 | + type TypeV2 = { |
| 35 | + data: number[]; |
| 36 | + additionalData: string; |
| 37 | + __version: number; |
| 38 | + }; |
| 39 | + const schemaV0: JSONSchemaType<TypeV0> = { |
| 40 | + type: 'object', |
| 41 | + properties: { |
| 42 | + data: { type: 'number' }, |
| 43 | + }, |
| 44 | + required: ['data'], |
| 45 | + }; |
| 46 | + const schemaV1: JSONSchemaType<TypeV1> = { |
| 47 | + type: 'object', |
| 48 | + properties: { |
| 49 | + __version: { type: 'number' }, |
| 50 | + data: { type: 'array', items: { type: 'number' } }, |
| 51 | + }, |
| 52 | + required: ['data', '__version'], |
| 53 | + }; |
| 54 | + const schemaV2: JSONSchemaType<TypeV2> = { |
| 55 | + type: 'object', |
| 56 | + properties: { |
| 57 | + __version: { type: 'number' }, |
| 58 | + data: { type: 'array', items: { type: 'number' } }, |
| 59 | + additionalData: { type: 'string' }, |
| 60 | + }, |
| 61 | + required: ['data', '__version', 'additionalData'], |
| 62 | + }; |
| 63 | + const migrations: SchemaMigration[] = [ |
| 64 | + { version: 0, schema: schemaV0 }, |
| 65 | + { |
| 66 | + version: 1, |
| 67 | + schema: schemaV1, |
| 68 | + migration(item: TypeV0): TypeV1 { |
| 69 | + return { |
| 70 | + data: [item.data], |
| 71 | + __version: 1, |
| 72 | + }; |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + version: 2, |
| 77 | + schema: schemaV2, |
| 78 | + migration(item: TypeV1): TypeV2 { |
| 79 | + return { |
| 80 | + data: item.data, |
| 81 | + additionalData: 'test', |
| 82 | + __version: 2, |
| 83 | + }; |
| 84 | + }, |
| 85 | + }, |
| 86 | + ]; |
| 87 | + |
| 88 | + const dataV0: TypeV0 = { data: 77 }; |
| 89 | + const expectedData: TypeV2 = { |
| 90 | + data: [77], |
| 91 | + additionalData: 'test', |
| 92 | + __version: 2, |
| 93 | + }; |
| 94 | + const mr = new MigrationRunner(migrations); |
| 95 | + |
| 96 | + const resultData = mr.runMigration(dataV0); |
| 97 | + expect(resultData).toStrictEqual(expectedData); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments