-
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.
Merge branch 'development' into release
- Loading branch information
Showing
16 changed files
with
268 additions
and
39 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
47 changes: 47 additions & 0 deletions
47
app/api/migrations/migrations/39-remove_wrong_text_references/index.js
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,47 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
export default { | ||
delta: 39, | ||
|
||
name: 'remove_wrong_text_references', | ||
|
||
description: 'remove wrongly created text references and log them', | ||
|
||
async up(db) { | ||
process.stdout.write(`${this.name}...\r\n`); | ||
|
||
await db | ||
.collection('connections') | ||
.updateMany( | ||
{ 'reference.text': { $exists: false }, 'reference.selectionRectangles': { $size: 0 } }, | ||
{ $unset: { reference: '' } } | ||
); | ||
|
||
const cursor = await db | ||
.collection('connections') | ||
.find({ 'reference.text': { $exists: true }, 'reference.selectionRectangles': { $size: 0 } }); | ||
|
||
while (await cursor.hasNext()) { | ||
const connection = await cursor.next(); | ||
|
||
const connectionsOnHub = await db | ||
.collection('connections') | ||
.find({ hub: connection.hub }) | ||
.toArray(); | ||
|
||
if (connectionsOnHub.length === 2) { | ||
await db | ||
.collection('connections') | ||
.deleteMany({ _id: { $in: connectionsOnHub.map(c => c._id) } }); | ||
|
||
connectionsOnHub.forEach(c => { | ||
process.stdout.write(JSON.stringify(c, null, ' ')); | ||
process.stdout.write('\r\n'); | ||
}); | ||
} else { | ||
await db.collection('connections').deleteOne(connection); | ||
process.stdout.write(JSON.stringify(connection, null, ' ')); | ||
process.stdout.write('\r\n'); | ||
} | ||
} | ||
}, | ||
}; |
67 changes: 67 additions & 0 deletions
67
.../migrations/39-remove_wrong_text_references/specs/39-remove_wrong_text_references.spec.js
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,67 @@ | ||
import testingDB from 'api/utils/testing_db'; | ||
import migration from '../index.js'; | ||
import fixtures from './fixtures.js'; | ||
|
||
describe('migration remove_wrong_text_references', () => { | ||
beforeEach(async () => { | ||
spyOn(process.stdout, 'write'); | ||
await testingDB.clearAllAndLoad(fixtures); | ||
await migration.up(testingDB.mongodb); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingDB.disconnect(); | ||
}); | ||
|
||
it('should have a delta number', () => { | ||
expect(migration.delta).toBe(39); | ||
}); | ||
|
||
it('should remove remove all connections with text but empty selectionRectangles', async () => { | ||
const connections = await testingDB.mongodb | ||
.collection('connections') | ||
.find({ 'reference.text': { $exists: true } }) | ||
.toArray(); | ||
|
||
expect(connections).toEqual([ | ||
expect.objectContaining({ reference: { text: 'reference 1', selectionRectangles: [{}] } }), | ||
expect.objectContaining({ reference: { text: 'reference 2', selectionRectangles: [{}] } }), | ||
]); | ||
}); | ||
|
||
it('should remove references belonging to deleted references hubs (when only one remains)', async () => { | ||
const connections = await testingDB.mongodb | ||
.collection('connections') | ||
.find({ hub: { $exists: true } }) | ||
.toArray(); | ||
|
||
expect(connections).toEqual([ | ||
expect.objectContaining({ hub: 'hub3' }), | ||
expect.objectContaining({ hub: 'hub3' }), | ||
]); | ||
}); | ||
|
||
it('should log the removed connections to stdout', async () => { | ||
expect(process.stdout.write).toHaveBeenCalledWith(expect.stringMatching('wrong reference 1')); | ||
expect(process.stdout.write).toHaveBeenCalledWith( | ||
expect.stringMatching('wrong reference 1 partner') | ||
); | ||
expect(process.stdout.write).toHaveBeenCalledWith(expect.stringMatching('wrong reference 2')); | ||
expect(process.stdout.write).toHaveBeenCalledWith( | ||
expect.stringMatching('wrong reference 2 partner') | ||
); | ||
expect(process.stdout.write).toHaveBeenCalledWith(expect.stringMatching('wrong reference 3')); | ||
}); | ||
|
||
it(`should remove empty SelectionRectangles from references that do not have reference text | ||
(mongoose was creating this empty array by default)`, async () => { | ||
const connections = await testingDB.mongodb | ||
.collection('connections') | ||
.find({ | ||
reference: { $exists: false }, | ||
}) | ||
.toArray(); | ||
|
||
expect(connections.length).toEqual(2); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
app/api/migrations/migrations/39-remove_wrong_text_references/specs/fixtures.js
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,19 @@ | ||
export default { | ||
connections: [ | ||
{ reference: { text: 'reference 1', selectionRectangles: [{}] } }, | ||
{ reference: { text: 'reference 2', selectionRectangles: [{}] } }, | ||
{ hub: 'hub1', reference: { text: 'wrong reference 1', selectionRectangles: [] } }, | ||
{ hub: 'hub2', reference: { text: 'wrong reference 2', selectionRectangles: [] } }, | ||
{ hub: 'hub3', reference: { text: 'wrong reference 3', selectionRectangles: [] } }, | ||
{ hub: 'hub1', reference: { text: 'wrong reference 1 partner' } }, | ||
{ hub: 'hub2', reference: { text: 'wrong reference 2 partner' } }, | ||
|
||
// | ||
{ hub: 'hub3', reference: {} }, | ||
{ hub: 'hub3', reference: {} }, | ||
|
||
// | ||
{ reference: { selectionRectangles: [] } }, | ||
{ reference: { selectionRectangles: [] } }, | ||
], | ||
}; |
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 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 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 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 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 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,47 @@ | ||
import { Application, Request, Response, NextFunction } from 'express'; | ||
import request from 'supertest'; | ||
|
||
import { testingDB } from 'api/utils/testing_db'; | ||
import errorLog from 'api/log/errorLog'; | ||
import { setUpApp } from 'api/utils/testingRoutes'; | ||
|
||
import routes from '../routes'; | ||
|
||
jest.mock( | ||
'../../auth/authMiddleware.ts', | ||
() => () => (_req: Request, _res: Response, next: NextFunction) => { | ||
next(); | ||
} | ||
); | ||
|
||
describe('relationships routes', () => { | ||
const app: Application = setUpApp(routes); | ||
|
||
beforeEach(async () => { | ||
spyOn(errorLog, 'error'); | ||
await testingDB.clearAllAndLoad({ | ||
settings: [{ languages: [{ key: 'en', default: true }] }], | ||
}); | ||
}); | ||
|
||
afterAll(async () => testingDB.disconnect()); | ||
|
||
describe('POST/bulk', () => { | ||
it('should validate connections', async () => { | ||
const { body } = await request(app) | ||
.post('/api/relationships/bulk') | ||
.send({ save: [{ notAllowedProperty: 'test' }], delete: [] }); | ||
|
||
expect(body.error).toBe('validation failed'); | ||
}); | ||
|
||
it('should throw an especial 500 error when selectionRectangles is sent empty', async () => { | ||
const { body, status } = await request(app) | ||
.post('/api/relationships/bulk') | ||
.send({ save: [{ reference: { text: 'test', selectionRectangles: [] } }], delete: [] }); | ||
|
||
expect(status).toBe(500); | ||
expect(body.error.match(/selectionRectangles should not be empty/)).not.toBe(null); | ||
}); | ||
}); | ||
}); |
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,10 @@ | ||
/* eslint-disable max-statements */ | ||
import Ajv from 'ajv'; | ||
import { wrapValidator } from 'shared/tsUtils'; | ||
import { connectionSchema } from 'shared/types/connectionSchema'; | ||
|
||
const ajv = Ajv({ allErrors: true }); | ||
|
||
export const validateConnectionSchema = wrapValidator( | ||
ajv.compile({ $async: true, type: 'array', items: connectionSchema }) | ||
); |
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 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 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
Oops, something went wrong.