test(ordrestyring): preserve DatabaseService query contract
This commit is contained in:
@@ -6,7 +6,10 @@ const model = (rows = [], options = {}) => new OrdrestyringReadModel({
|
||||
|
||||
describe('consumer read model safety contract', () => {
|
||||
test('binds a database service pool lazily after application initialization', async () => {
|
||||
const databaseService = { pool: null };
|
||||
const databaseService = { pool: null, query: async (sql, params) => {
|
||||
const [rows] = await databaseService.pool.execute(sql, params);
|
||||
return rows;
|
||||
} };
|
||||
const readModel = new OrdrestyringReadModel(databaseService, { env: {} });
|
||||
databaseService.pool = { execute: jest.fn().mockResolvedValue([[{ case_number: 'A' }]]) };
|
||||
await expect(readModel.cases(['A'])).resolves.toEqual([{ case_number: 'A' }]);
|
||||
|
||||
@@ -73,6 +73,10 @@ class OrdrestyringReadModel {
|
||||
}
|
||||
|
||||
async rows(sql, params = []) {
|
||||
if (this.connection && Object.prototype.hasOwnProperty.call(this.connection, 'pool')
|
||||
&& typeof this.connection.query === 'function') {
|
||||
return this.connection.query(sql, params);
|
||||
}
|
||||
const connection = this.connection?.pool || this.connection;
|
||||
if (!connection) throw new Error('Ordrestyring canonical database unavailable');
|
||||
if (connection.execute) {
|
||||
|
||||
Reference in New Issue
Block a user