Skip to content

Instantly share code, notes, and snippets.

@marcocastignoli
Created January 23, 2024 12:38
Show Gist options
  • Select an option

  • Save marcocastignoli/e60dbcb58ff1d744cf07ac3aeb2cab6f to your computer and use it in GitHub Desktop.

Select an option

Save marcocastignoli/e60dbcb58ff1d744cf07ac3aeb2cab6f to your computer and use it in GitHub Desktop.
sourcify postgres test mock
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({}, {});
});
});
@marcocastignoli
Copy link
Author

with this I set the returned query results each time .query is called

storageService.sourcifyDatabase.databasePool.setQueryResults([
      { rowCounts: 0 },
    ]);

@marcocastignoli
Copy link
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