This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: wrap and extend mock-couch for e2e test setups
- Loading branch information
Showing
4 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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); | ||
|
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 @@ | ||
// 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 }); | ||
} | ||
} |