-
Notifications
You must be signed in to change notification settings - Fork 81
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
Showing
42 changed files
with
726 additions
and
627 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
app/api/migrations/migrations/161-update-translations/index.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable import/no-default-export */ | ||
import { Db } from 'mongodb'; | ||
|
||
interface Translation { | ||
key: string; | ||
} | ||
|
||
const newKeys: Translation[] = [ | ||
{ key: 'Do you want to delete the following items?' }, | ||
{ key: 'Already exists' }, | ||
]; | ||
|
||
const deletedKeys: Translation[] = [ | ||
{ key: 'Are you sure you want to delete this relationship type?' }, | ||
{ key: 'Cannot delete relationship type:' }, | ||
{ key: 'Confirm deletion of relationship type:' }, | ||
{ key: 'Currently connections only need a title.' }, | ||
{ key: 'This relationship type is being used and cannot be deleted.' }, | ||
]; | ||
|
||
export default { | ||
delta: 161, | ||
|
||
reindex: false, | ||
|
||
name: 'update_translations', | ||
|
||
description: 'Updates some translations in settings', | ||
|
||
async up(db: Db) { | ||
const settings = await db.collection('settings').findOne(); | ||
const languages = settings?.languages | ||
.map((l: any) => l.key) | ||
.filter((value: string, index: number, array: any[]) => array.indexOf(value) === index); | ||
|
||
await db.collection('translationsV2').deleteMany({ | ||
key: { $in: deletedKeys.concat(newKeys).map(k => k.key) }, | ||
'context.id': 'System', | ||
}); | ||
|
||
const insertMany = languages.map(async (l: any) => | ||
db.collection('translationsV2').insertMany( | ||
newKeys.map(k => ({ | ||
key: k.key, | ||
value: k.key, | ||
language: l, | ||
context: { id: 'System', type: 'Uwazi UI', label: 'User Interface' }, | ||
})) | ||
) | ||
); | ||
await Promise.all(insertMany); | ||
|
||
process.stdout.write(`${this.name}...\r\n`); | ||
}, | ||
}; | ||
|
||
export { newKeys, deletedKeys }; |
59 changes: 59 additions & 0 deletions
59
app/api/migrations/migrations/161-update-translations/specs/161-update_translations.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Db } from 'mongodb'; | ||
import testingDB from 'api/utils/testing_db'; | ||
import migration, { newKeys, deletedKeys } from '../index'; | ||
import { fixtures } from './fixtures'; | ||
import { Fixture } from '../types'; | ||
|
||
let db: Db | null; | ||
|
||
const initTest = async (fixture: Fixture) => { | ||
await testingDB.setupFixturesAndContext(fixture); | ||
db = testingDB.mongodb!; | ||
await migration.up(db); | ||
}; | ||
|
||
describe('migration update translations of settings', () => { | ||
beforeAll(async () => { | ||
jest.spyOn(process.stdout, 'write').mockImplementation((_str: string | Uint8Array) => true); | ||
await initTest(fixtures); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingDB.disconnect(); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe(161); | ||
}); | ||
|
||
it('should delete old translations', async () => { | ||
const translations = await testingDB | ||
.mongodb!.collection('translationsV2') | ||
.find({ key: { $in: deletedKeys.map(k => k.key) } }) | ||
.toArray(); | ||
|
||
expect(translations).toEqual([]); | ||
}); | ||
|
||
it('should NOT delete other translations', async () => { | ||
const translations = await testingDB | ||
.mongodb!.collection('translationsV2') | ||
.find({ key: 'Im cool' }) | ||
.toArray(); | ||
|
||
expect(translations.length).toBe(2); | ||
}); | ||
|
||
it('should add new translations per language', async () => { | ||
const translations = await testingDB | ||
.mongodb!.collection('translationsV2') | ||
.find({ key: { $in: newKeys.map(k => k.key) } }) | ||
.toArray(); | ||
|
||
expect(translations.length).toBe(4); | ||
}); | ||
|
||
it('should be idempotent (do not throw an error on multiple runs)', async () => { | ||
await expect(migration.up(testingDB.mongodb!)).resolves.toBe(undefined); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
app/api/migrations/migrations/161-update-translations/specs/fixtures.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import db from 'api/utils/testing_db'; | ||
import { Fixture } from '../types'; | ||
|
||
const fixtures: Fixture = { | ||
settings: [{ _id: db.id(), languages: [{ key: 'en' }, { key: 'es' }] }], | ||
translationsV2: [ | ||
{ | ||
_id: db.id(), | ||
language: 'en', | ||
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' }, | ||
key: 'Current value:', | ||
value: 'Current value:', | ||
}, | ||
{ | ||
_id: db.id(), | ||
language: 'es', | ||
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' }, | ||
key: 'Current value:', | ||
value: 'Current value:', | ||
}, | ||
{ | ||
_id: db.id(), | ||
language: 'en', | ||
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' }, | ||
key: 'Im cool', | ||
value: 'Im cool', | ||
}, | ||
{ | ||
_id: db.id(), | ||
language: 'es', | ||
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' }, | ||
key: 'Im cool', | ||
value: 'Im cool', | ||
}, | ||
], | ||
}; | ||
|
||
export { fixtures }; |
6 changes: 6 additions & 0 deletions
6
app/api/migrations/migrations/161-update-translations/types.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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface Fixture { | ||
settings: any; | ||
translationsV2: any; | ||
} | ||
|
||
export type { Fixture }; |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.