mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-06 21:16:28 +00:00
106 lines
5.3 KiB
JavaScript
106 lines
5.3 KiB
JavaScript
const makeKnex = require('knex');
|
|
const migration = require('../../infrastructure/database/migrations/20261124000100_create_transaction_number_counter');
|
|
const createAllocator = require('../../infrastructure/database/transaction-number');
|
|
|
|
const connection = process.env.TOPUP_TEST_NUMBER_DB_URL;
|
|
const integration = connection ? describe : describe.skip;
|
|
|
|
integration('transaction number allocation in PostgreSQL', () => {
|
|
let firstDb;
|
|
let secondDb;
|
|
let first;
|
|
let second;
|
|
|
|
beforeAll(async () => {
|
|
const url = new URL(connection);
|
|
if (url.hostname !== '127.0.0.1' || url.pathname !== '/topup_test') {
|
|
throw new Error('Number allocation tests require a disposable localhost topup_test database');
|
|
}
|
|
const settings = { client: 'pg', connection, pool: { min: 0, max: 4 } };
|
|
firstDb = makeKnex(settings);
|
|
secondDb = makeKnex(settings);
|
|
first = createAllocator(firstDb);
|
|
second = createAllocator(secondDb);
|
|
// Gagal bila skema sudah ada agar pengujian hanya memakai database kosong.
|
|
await firstDb.raw(`
|
|
CREATE SCHEMA trx;
|
|
CREATE TABLE trx.t_d_reservation_no_validate (c_reservation_no varchar(50) PRIMARY KEY);
|
|
CREATE TABLE trx.t_d_reservation_balance (c_reservation_no varchar(50));
|
|
CREATE TABLE trx.t_d_load_balance_no_validate (c_load_balance_no varchar(50) PRIMARY KEY);
|
|
CREATE TABLE trx.t_d_load_balance (c_load_balance_no varchar(50));
|
|
CREATE TABLE trx.t_d_load_balance_history (c_load_balance_no varchar(50));
|
|
`);
|
|
await migration.up(firstDb);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await Promise.all([firstDb?.destroy(), secondDb?.destroy()]);
|
|
});
|
|
|
|
test.each([
|
|
['RBL', 't_d_reservation_balance', 't_d_reservation_no_validate', 'c_reservation_no'],
|
|
['LBL', 't_d_load_balance_history', 't_d_load_balance_no_validate', 'c_load_balance_no'],
|
|
])('%s claims 100 distinct numbers across independent pools, starting after legacy data', async (type, table, claim, column) => {
|
|
const prefix = `${type}KCI261124`;
|
|
await firstDb(`trx.${table}`).insert([
|
|
{ [column]: `${prefix}000400` },
|
|
{ [column]: `${prefix}000XYZ` },
|
|
{ [column]: `${type}ABC261124999999` },
|
|
]);
|
|
await firstDb(`trx.${claim}`).insert({ [column]: `${prefix}000401` });
|
|
|
|
const issued = await Promise.all(Array.from({ length: 100 }, (_, index) =>
|
|
(index % 2 === 0 ? first : second)(type, 'KCI', '261124')
|
|
));
|
|
const sequences = issued.map(({ sequence }) => sequence).sort((a, b) => a - b);
|
|
|
|
expect(sequences).toEqual(Array.from({ length: 100 }, (_, index) => 402 + index));
|
|
expect(new Set(issued.map(({ number }) => number)).size).toBe(100);
|
|
expect(issued.every(({ number, sequence }) => number === `${prefix}${String(sequence).padStart(6, '0')}`)).toBe(true);
|
|
expect(await firstDb(`trx.${claim}`).where(column, 'like', `${prefix}%`)).toHaveLength(101);
|
|
});
|
|
|
|
test('continues after an old writer claims a block of LBL numbers', async () => {
|
|
const prefix = 'LBLOLD261124';
|
|
await firstDb('trx.t_d_load_balance').insert({ c_load_balance_no: `${prefix}000041` });
|
|
await expect(first('LBL', 'OLD', '261124')).resolves.toEqual({ number: `${prefix}000042`, sequence: 42 });
|
|
await firstDb('trx.t_d_load_balance_no_validate').insert(
|
|
Array.from({ length: 10 }, (_, index) => ({ c_load_balance_no: `${prefix}0000${43 + index}` }))
|
|
);
|
|
await expect(second('LBL', 'OLD', '261124')).resolves.toEqual({ number: `${prefix}000053`, sequence: 53 });
|
|
});
|
|
|
|
test('shares the fallback partner prefix and starts a fresh counter on the next date', async () => {
|
|
const issued = await Promise.all([first('RBL', null, '261124'), second('RBL', 'XXX', '261124')]);
|
|
expect(issued.map(({ number }) => number).sort()).toEqual(['RBLXXX261124000001', 'RBLXXX261124000002']);
|
|
await expect(second('RBL', 'XXX', '261125')).resolves.toEqual({ number: 'RBLXXX261125000001', sequence: 1 });
|
|
});
|
|
|
|
test('reports exhaustion without returning a seven-digit sequence', async () => {
|
|
await firstDb('trx.t_d_reservation_balance').insert([
|
|
{ c_reservation_no: 'RBLMAX261124999998' },
|
|
{ c_reservation_no: 'RBLEND261124999999' },
|
|
]);
|
|
await expect(first('RBL', 'MAX', '261124')).resolves.toEqual({ number: 'RBLMAX261124999999', sequence: 999999 });
|
|
await expect(second('RBL', 'MAX', '261124')).rejects.toThrow('sequence exhausted');
|
|
await expect(second('RBL', 'END', '261124')).rejects.toThrow('sequence exhausted');
|
|
});
|
|
|
|
test('rolls the counter back when PostgreSQL rejects the new claim', async () => {
|
|
const prefix = 'RBLERR261124';
|
|
await expect(first('RBL', 'ERR', '261124')).resolves.toEqual({ number: `${prefix}000001`, sequence: 1 });
|
|
await firstDb.raw(`
|
|
ALTER TABLE trx.t_d_reservation_no_validate ADD CONSTRAINT reject_fixture
|
|
CHECK (c_reservation_no <> 'RBLERR261124000002')
|
|
`);
|
|
try {
|
|
await expect(second('RBL', 'ERR', '261124')).rejects.toMatchObject({ code: '23514' });
|
|
const counter = await firstDb('trx.t_transaction_number_counter').where({ number_prefix: prefix }).first();
|
|
expect(counter.last_value).toBe(1);
|
|
} finally {
|
|
await firstDb.raw('ALTER TABLE trx.t_d_reservation_no_validate DROP CONSTRAINT reject_fixture');
|
|
}
|
|
await expect(second('RBL', 'ERR', '261124')).resolves.toEqual({ number: `${prefix}000002`, sequence: 2 });
|
|
});
|
|
});
|