Files
Online-Topup-V1/application/tests/database-read-paths.integration.test.js
T
2026-11-24 12:48:27 +07:00

116 lines
5.1 KiB
JavaScript

const { Registry } = require('prom-client');
const { observePool, poolMetrics } = require('../../infrastructure/database/pool-metrics');
jest.mock('../../infrastructure/database/knex', () => require('knex')({
client: 'pg',
connection: process.env.TOPUP_TEST_READ_DB_URL,
pool: { min: 0, max: 1 },
}));
const connection = process.env.TOPUP_TEST_READ_DB_URL;
const integration = connection ? describe : describe.skip;
integration('hot read paths in PostgreSQL', () => {
let db;
let history;
let reserve;
let load;
let pos;
beforeAll(async () => {
const url = new URL(connection);
if (url.hostname !== '127.0.0.1' || url.pathname !== '/topup_reads_test') {
throw new Error('Read-path tests require a disposable localhost topup_reads_test database');
}
db = require('../../infrastructure/database/knex');
observePool('primary', db.client.pool);
history = require('../modules/history/history.model');
reserve = require('../modules/reservebalance/reservebalance.model');
load = require('../modules/loadbalance/loadbalance.model');
pos = require('../modules/pos/pos.model');
await db.raw(`
CREATE SCHEMA trx;
CREATE SCHEMA log;
CREATE TABLE t_m_global_parameter (c_code text, c_status text, c_value text);
CREATE TABLE log.t_log_card (
c_card_number text, c_status text, m_balance_after integer,
m_amount integer, d_log timestamp, i_log_name integer
);
CREATE TABLE trx.t_d_reservation_balance (
c_mid text, c_tid text, c_card_number text, c_ref_no text,
c_partner_name text, c_merchant_name text, c_terminal_name text,
c_reservation_no text, d_reservation timestamp, i_reservation_amount integer,
i_issuer_fee integer, i_channel_fee integer, c_status text, c_status_name text
);
CREATE TABLE trx.t_d_load_balance (
c_mid text, c_tid text, c_card_number text, c_ref_no text,
c_partner_name text, c_merchant_name text, c_terminal_name text, c_load_balance_no text,
d_load_balance timestamp, i_topup_amount integer, c_status text, c_status_name text, i_load_balance integer
);
CREATE TABLE trx.t_d_load_balance_detail (i_load_balance integer, i_issuer_fee integer, i_channel_fee integer);
`);
});
afterAll(async () => { await db?.destroy(); });
test('reads reservation expiry in one statement and ignores inactive values', async () => {
await db('t_m_global_parameter').insert([
{ c_code: 'HSM_EXP_RSV_VAL', c_status: 'A', c_value: '2' },
{ c_code: 'HSM_EXP_RSV_MEA', c_status: 'A', c_value: 'days' },
{ c_code: 'HSM_EXP_RSV_VAL', c_status: 'I', c_value: '15' },
]);
const query = jest.fn();
db.on('query', query);
try {
expect(await reserve.getReservationExpiration()).toEqual({ time: 2, measure: 'days' });
expect(query).toHaveBeenCalledTimes(1);
} finally {
db.removeListener('query', query);
}
});
test('monthly aggregation includes the first instant and excludes the next month', async () => {
await db('log.t_log_card').insert([
['2026-10-31 23:59:59', 100], ['2026-11-01 00:00:00', 10],
['2026-11-30 23:59:59', 20], ['2026-12-01 00:00:00', 100],
].map(([d_log, m_amount]) => ({
c_card_number: 'card', c_status: 'S', m_balance_after: 100, i_log_name: 21, d_log, m_amount,
})));
for (const model of [reserve, load, pos]) {
const total = await model.getAccumulationBalanceInThisMonthFromCardHistory('card', '2026', '11');
expect(Number(total.accumulation_balance)).toBe(30);
}
});
test('filters history in both branches and retains history without dates', async () => {
const key = { c_mid: 'MID', c_tid: 'TID', c_card_number: 'card' };
await db('trx.t_d_reservation_balance').insert([
{ ...key, c_reservation_no: 'RBL-IN', d_reservation: '2026-11-24 00:00:00' },
{ ...key, c_reservation_no: 'RBL-OUT', d_reservation: '2026-11-23 23:59:59' },
]);
await db('trx.t_d_load_balance').insert([
{ ...key, c_load_balance_no: 'LBL-IN', d_load_balance: '2026-11-24 23:59:59' },
{ ...key, c_load_balance_no: 'LBL-OUT', d_load_balance: '2026-11-25 00:00:00' },
]);
const rows = await history.historyByCardNumber('MID', 'TID', 'card', '2026-11-24', '2026-11-24');
expect(rows.map(({ unique_number }) => unique_number).sort()).toEqual(['LBL-IN', 'RBL-IN']);
expect(await history.historyByCardNumber('MID', 'TID', 'card')).toHaveLength(4);
});
test('exposes pool statistics without querying PostgreSQL for the scrape', async () => {
const registry = new Registry();
for (const metric of poolMetrics) registry.registerMetric(metric);
const query = jest.fn();
db.on('query', query);
try {
const body = await registry.metrics();
expect(body).toContain('topuponline_db_pool_connections{pool="primary",state="max"} 1');
expect(body).toContain('topuponline_db_pool_pending_acquires{pool="primary"} 0');
expect(body).toContain('topuponline_db_pool_acquire_seconds_count{pool="primary",outcome="success"}');
expect(query).not.toHaveBeenCalled();
} finally {
db.removeListener('query', query);
}
});
});