-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82a9967
commit 4f922dc
Showing
4 changed files
with
331 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 108 additions & 12 deletions
120
...migrations/migrations/165-default_empty_metadata/specs/165-default_empty_metadata.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,137 @@ | ||
import { Db } from 'mongodb'; | ||
|
||
import testingDB from 'api/utils/testing_db'; | ||
import migration from '../index.js'; | ||
import { Fixture } from '../types.js'; | ||
import { fixtures } from './fixtures.js'; | ||
import migration from '../index'; | ||
import { Entity, Fixture } from '../types'; | ||
import { fixtures, correctFixtures } from './fixtures'; | ||
|
||
let db: Db | null; | ||
|
||
const initTest = async (fixture: Fixture) => { | ||
await testingDB.setupFixturesAndContext(fixture); | ||
db = testingDB.mongodb!; | ||
migration.reindex = false; | ||
migration.batchSize = 4; | ||
|
||
await migration.up(db); | ||
}; | ||
|
||
beforeAll(async () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
jest.spyOn(process.stdout, 'write').mockImplementation((str: string | Uint8Array) => true); | ||
// jest.spyOn(process.stdout, 'write').mockImplementation((str: string | Uint8Array) => true); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingDB.tearDown(); | ||
}); | ||
|
||
describe('migration test', () => { | ||
beforeAll(async () => { | ||
await initTest(fixtures); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe(165); | ||
}); | ||
|
||
it('should be tested', async () => { | ||
expect(true).toBe(false); | ||
describe('on a correct database', () => { | ||
beforeAll(async () => { | ||
await initTest(correctFixtures); | ||
}); | ||
|
||
it('should do nothing', async () => { | ||
const entities = await db!.collection<Entity>('entities').find().toArray(); | ||
expect(entities).toEqual(correctFixtures.entities); | ||
}); | ||
|
||
it('should not signal a reindex', async () => { | ||
expect(migration.reindex).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should check if a reindex is needed', async () => { | ||
expect(migration.reindex).toBe(undefined); | ||
describe('on a faulty database', () => { | ||
beforeAll(async () => { | ||
await initTest(fixtures); | ||
}); | ||
|
||
const correctEmptyMetadata = { | ||
text: [], | ||
select: [], | ||
relationship: [], | ||
}; | ||
|
||
const expectedCorrectMetadata = [correctEmptyMetadata, correctEmptyMetadata]; | ||
|
||
it('should not modify correct entities', async () => { | ||
const entities = await db! | ||
.collection<Entity>('entities') | ||
.find({ sharedId: 'correct_entity_sharedId' }) | ||
.toArray(); | ||
expect(entities).toEqual(correctFixtures.entities); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
title: 'entity_without_metadata', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_undefined_metadata', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_null_metadata', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_empty_metadata', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_undefined_metadata_properties', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_null_metadata_properties', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_empty_metadata_properties', | ||
expectedMetadata: expectedCorrectMetadata, | ||
}, | ||
{ | ||
title: 'entity_with_assymetric_metadata', | ||
expectedMetadata: [ | ||
{ | ||
text: [], | ||
select: [], | ||
relationship: [], | ||
}, | ||
{ | ||
text: [], | ||
select: [], | ||
relationship: [], | ||
}, | ||
], | ||
}, | ||
{ | ||
title: 'other_template', | ||
expectedMetadata: [ | ||
{ | ||
text: [], | ||
select: [], | ||
}, | ||
{ | ||
text: [], | ||
select: [], | ||
}, | ||
], | ||
}, | ||
])('should fix case: $title', async ({ title, expectedMetadata }) => { | ||
const sharedId = `${title}_sharedId`; | ||
const entities = await db!.collection<Entity>('entities').find({ sharedId }).toArray(); | ||
const metadata = entities.map(e => e.metadata); | ||
expect(metadata).toEqual(expectedMetadata); | ||
}); | ||
|
||
it('should signal a reindex', async () => { | ||
expect(migration.reindex).toBe(true); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.