Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
test: wrap and extend mock-couch for e2e test setups
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Feb 29, 2024
1 parent 8d0ae22 commit ff40029
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 1 deletion.
189 changes: 189 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config test/jest.config.js"
"test:e2e": "jest --config test/jest.config.js",
"postinstall": "patch-package"
},
"dependencies": {
"@nestjs/axios": "^3.0.2",
Expand Down Expand Up @@ -54,6 +55,7 @@
"eslint-plugin-prettier": "^5.1.3",
"jest": "29.7.0",
"jest-openapi": "^0.14.2",
"patch-package": "^8.0.0",
"prettier": "^3.2.5",
"rimraf": "^3.0.2",
"supertest": "^6.3.4",
Expand Down
13 changes: 13 additions & 0 deletions patches/mock-couch+0.1.11.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/mock-couch/index.js b/node_modules/mock-couch/index.js
index 952390b..aecb2b3 100644
--- a/node_modules/mock-couch/index.js
+++ b/node_modules/mock-couch/index.js
@@ -110,6 +110,8 @@ function MockCouch(server, options) {

this.addDB = require('./lib/addDB');
this.addDoc = require('./lib/addDoc');
+
+ this._restifyServer = server;
}
util.inherits(MockCouch, events.EventEmitter);

59 changes: 59 additions & 0 deletions test/mock-couch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
import { EntityDoc } from '../src/report-changes/storage/database-changes.service';

const mockCouch = require('mock-couch');

/**
* Wrapper for mock-couch to provide typing.
* see https://chris-l.github.io/mock-couch/
*/
export class MockCouch {
private server: any;

constructor(port?: number) {
this.server = mockCouch.createServer();

this.server._restifyServer.post('/:db/_find', (req: any, res: any) =>
this._find(req, res),
);

this.listen(port ?? 5984);
}

listen(port: number): void {
this.server.listen(port);
}

close(): void {
this.server.close();
}

addDB(dbName: string, docs: any[]) {
this.server.addDB(dbName, docs);
}

addDoc(dbName: string, doc: any) {
this.server.addDoc(dbName, doc);
}

private _find(req: any, res: any) {
const db = this.server.databases[req.params.db];
if (!db) {
res.send(404, { error: 'not_found', reason: 'no_db_file' });
return;
}

let findResults: EntityDoc[] = [];

const selector = req.body?.selector;
if (selector['calculation.id']) {
for (const [id, doc] of Object.entries<EntityDoc>(db)) {
if (doc['calculation']?.id === selector['calculation.id']['$eq']) {
findResults.push(doc);
}
}
}

res.send(200, { docs: findResults });
}
}

0 comments on commit ff40029

Please sign in to comment.