Newer
Older
import { PROFILE_DOCTYPE } from 'doctypes'
import { Profile } from 'models'
import { Notes } from 'models/releaseNotes.model'
import { Schema } from 'models/schema.models'
import mockClient from 'tests/__mocks__/client.mock'
import { getError } from 'tests/__mocks__/testUtils'
Renovate-Bot
committed
import { MIGRATION_RESULT_FAILED } from './migration.data'
import { MigrationService } from './migration.service'
import { Migration, MigrationResult } from './migration.type'
const migrateSpy = jest.spyOn(Migrate, 'migrate')
describe('Migration service', () => {
const ms = new MigrationService(mockClient, jest.fn())
const releaseNotes: Notes = {
title: '',
description: '',
}
beforeEach(() => {
})
it('should run migrations', async () => {
const schema: Schema = { _id: '1', version: 0 }
const mockQueryResult: QueryResult<Schema[]> = {
data: [schema],
bookmark: '',
next: false,
skip: 0,
}
const migrations: Migration[] = [
{
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (client: Client, docs: any[]): Profile[] => {
return docs.map(doc => {
if (doc.mailToken) {
delete doc.mailToken
}
return doc
})
},
},
]
mockClient.query.mockResolvedValue(mockQueryResult)
await ms.runMigrations(migrations)
expect(migrateSpy).toHaveBeenCalledTimes(1)
it('should run migrations with one fail', async () => {
const schema: Schema = { _id: '1', version: 0 }
const mockQueryResult: QueryResult<Schema[]> = {
data: [schema],
bookmark: '',
next: false,
skip: 0,
}
const migrations: Migration[] = [
{
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (client: Client, docs: any[]): Profile[] => {
return []
},
},
]
const result: MigrationResult = {
type: MIGRATION_RESULT_FAILED,
errors: [],
}
const migrateSpyOneFail = migrateSpy.mockResolvedValueOnce(result)
mockClient.query.mockResolvedValue(mockQueryResult)
await ms.runMigrations(migrations)
expect(migrateSpyOneFail).toHaveBeenCalledTimes(2)
it('should not run migrations with two fail', async () => {
const schema: Schema = { _id: '1', version: 0 }
const mockQueryResult: QueryResult<Schema[]> = {
data: [schema],
bookmark: '',
next: false,
skip: 0,
}
const migrations: Migration[] = [
{
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (client: Client, docs: any[]): Profile[] => {
return []
},
},
]
const result: MigrationResult = {
type: MIGRATION_RESULT_FAILED,
errors: [],
}
mockClient.query.mockResolvedValue(mockQueryResult)
const migrateSpyTwoFailsKo = jest
.spyOn(Migrate, 'migrate')
.mockResolvedValueOnce(result)
.mockResolvedValueOnce(result)
const error = await getError(async () => ms.runMigrations(migrations))
expect(migrateSpyTwoFailsKo).toHaveBeenCalledTimes(2)
expect(migrateSpy).toHaveBeenCalledTimes(2)
expect(error).toEqual(new Error())
it('should skip migrations if schema number is up to date', async () => {
const schema: Schema = { _id: '1', version: 1 }
const mockQueryResult: QueryResult<Schema[]> = {
data: [schema],
bookmark: '',
next: false,
skip: 0,
}
const migrations: Migration[] = [
{
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
run: (client: Client, docs: any[]): Profile[] => {
mockClient.query.mockResolvedValue(mockQueryResult)
await ms.runMigrations(migrations)
expect(migrateSpy).toHaveBeenCalledTimes(0)
})
it('should run 2 migrations properly from a fresh instance and dont show releasenotes', async () => {
const schema: Schema = { _id: '1', version: 0 }
const mockQueryResult: QueryResult<Schema[]> = {
data: [schema],
bookmark: '',
next: false,
skip: 0,
const migrations: Migration[] = [
{
baseSchemaVersion: 0,
targetSchemaVersion: 1,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
releaseNotes: releaseNotes,
run: (client: Client, docs: any[]): Profile[] => {
return []
},
},
{
baseSchemaVersion: 1,
targetSchemaVersion: 2,
appVersion: '1.2.4',
description: 'Removing mailToken from profil',
docTypes: PROFILE_DOCTYPE,
releaseNotes: releaseNotes,
run: (client: Client, docs: any[]): Profile[] => {
return []
},
},
]
mockClient.query.mockResolvedValue(mockQueryResult)
const res = await ms.runMigrations(migrations)
expect(migrateSpy).toHaveBeenCalledTimes(2)