Created
January 23, 2024 12:38
-
-
Save marcocastignoli/e60dbcb58ff1d744cf07ac3aeb2cab6f to your computer and use it in GitHub Desktop.
sourcify postgres test mock
This file contains hidden or 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
| const { expect } = require("chai"); | |
| const { StorageService } = require("../dist/server/services/StorageService"); | |
| const config = require("config"); | |
| const { EventEmitter } = require("stream"); | |
| class PostgresPoolMock extends EventEmitter { | |
| queryResults = []; | |
| queryIndex = 0; | |
| constructor(...args) { | |
| super(); | |
| this.emit("construct", args); | |
| } | |
| reset() { | |
| this.queryResults = []; | |
| this.queryIndex = 0; | |
| } | |
| setQueryResults(queryResults) { | |
| this.reset(); | |
| this.queryResults = queryResults; | |
| } | |
| query(...args) { | |
| this.emit("query", args); | |
| const res = this.queryResults[this.queryIndex]; | |
| if (this.queryIndex + 1 === this.queryResults.length) { | |
| this.reset(); | |
| } else { | |
| this.queryIndex++; | |
| } | |
| return res; | |
| } | |
| } | |
| describe("Database", () => { | |
| const storageService = new StorageService({ | |
| repositoryV1ServiceOptions: { | |
| ipfsApi: process.env.IPFS_API, | |
| repositoryPath: "./dist/data/mock-repositoryV1", | |
| repositoryServerUrl: config.get("repositoryV1.serverUrl"), | |
| }, | |
| sourcifyDatabaseServiceOptions: { | |
| postgres: { | |
| host: "", | |
| port: "", | |
| database: "", | |
| user: "", | |
| password: "", | |
| }, | |
| }, | |
| }); | |
| storageService.sourcifyDatabase.databasePool = new PostgresPoolMock(); | |
| it.only("storeMatch", (done) => { | |
| storageService.sourcifyDatabase.databasePool.setQueryResults([ | |
| { rowCounts: 0 }, | |
| ]); | |
| storageService.sourcifyDatabase.databasePool.on("query", console.log); | |
| storageService.storeMatch({}, {}); | |
| }); | |
| }); |
Author
Author
with this I can see if all the right queries are being called
storageService.sourcifyDatabase.databasePool.on("query", console.log);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with this I set the returned query results each time .query is called