mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
129 lines
6.6 KiB
JavaScript
129 lines
6.6 KiB
JavaScript
const testDatabaseUrl = process.env.TOPUP_TEST_LOAD_BALANCE_DB_URL;
|
|
const integration = testDatabaseUrl ? describe : describe.skip;
|
|
|
|
jest.mock('../../infrastructure/database/knex', () => {
|
|
const connection = (...args) => mockTransaction(...args);
|
|
connection.transaction = (callback) => mockTransaction.transaction(callback);
|
|
return connection;
|
|
});
|
|
|
|
let mockTransaction;
|
|
|
|
integration('pembaruan Load Balance di PostgreSQL', () => {
|
|
let db;
|
|
let update;
|
|
|
|
beforeAll(() => {
|
|
const url = new URL(testDatabaseUrl);
|
|
if (url.hostname !== '127.0.0.1' || url.pathname !== '/topup_loadbalance_test') {
|
|
throw new Error('Gunakan database lokal topup_loadbalance_test khusus pengujian.');
|
|
}
|
|
|
|
db = require('knex')({ client: 'pg', connection: testDatabaseUrl, pool: { min: 0, max: 1 } });
|
|
const model = require('../modules/loadbalance/loadbalance.model');
|
|
jest.spyOn(model, 'getPartner').mockResolvedValue({ partner_name: 'Fixture Partner' });
|
|
({ loadingBalanceUpdate: update } = require('../modules/loadbalance/loadbalance.controller'));
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
// Skema fixture ikut dibatalkan bersama transaksi induk setelah pengujian.
|
|
mockTransaction = await db.transaction();
|
|
await mockTransaction.raw(`
|
|
CREATE SCHEMA trx;
|
|
CREATE SCHEMA log;
|
|
CREATE TABLE trx.t_d_load_balance (
|
|
i_load_balance serial PRIMARY KEY, c_load_balance_no text UNIQUE,
|
|
d_load_balance timestamp, c_ref_no text, m_balance integer,
|
|
c_return_sap text, c_status text, c_status_name text,
|
|
d_modify timestamp, i_modify integer, c_modify_username text, c_modify_fullname text
|
|
);
|
|
CREATE TABLE trx.t_d_load_balance_history (LIKE trx.t_d_load_balance INCLUDING DEFAULTS);
|
|
ALTER TABLE trx.t_d_load_balance_history
|
|
ADD COLUMN i_partner integer, ADD COLUMN c_partner_code text, ADD COLUMN c_partner_name text,
|
|
ADD COLUMN i_merchant integer, ADD COLUMN c_merchant_code text, ADD COLUMN c_merchant_name text,
|
|
ADD COLUMN c_mid text, ADD COLUMN i_terminal integer, ADD COLUMN c_terminal_code text,
|
|
ADD COLUMN c_terminal_name text, ADD COLUMN c_tid text, ADD COLUMN c_card_number text,
|
|
ADD COLUMN i_topup_amount integer, ADD COLUMN i_load_balance_no_seq integer, ADD COLUMN c_idm text,
|
|
ADD COLUMN d_create timestamp, ADD COLUMN i_create integer, ADD COLUMN c_create_username text,
|
|
ADD COLUMN c_create_fullname text, ADD COLUMN d_delete timestamp, ADD COLUMN i_delete integer,
|
|
ADD COLUMN c_delete_username text, ADD COLUMN c_delete_fullname text;
|
|
CREATE TABLE trx.t_d_load_balance_detail (
|
|
i_load_balance integer, i_reservation_balance integer, c_reservation_no text, i_amount integer,
|
|
i_issuer integer, c_issuer_code text, c_issuer_name text, i_issuer_fee integer,
|
|
i_channel integer, c_channel_code text, c_channel_name text, i_channel_fee integer
|
|
);
|
|
CREATE TABLE trx.t_d_load_balance_history_detail (
|
|
i_load_balance integer, i_reservation_balance integer, c_reservation_no text, i_amount integer,
|
|
i_issuer integer, i_issuer_code text, i_issuer_name text, i_issuer_fee integer,
|
|
i_channel integer, i_channel_code text, i_channel_name text, i_channel_fee integer
|
|
);
|
|
CREATE TABLE trx.t_d_reservation_balance (
|
|
c_reservation_no text, c_load_balance_no text, c_status text, c_status_name text,
|
|
c_flag_code text, d_load_balance timestamp, d_modify timestamp, i_modify integer,
|
|
c_modify_username text, c_modify_fullname text
|
|
);
|
|
CREATE TABLE trx.t_d_flag_counting (
|
|
d_request timestamp, c_card_number text, c_uid text, i_balance integer, c_flag text
|
|
);
|
|
CREATE TABLE log.t_log_card (
|
|
i_log_name integer, d_log timestamp, c_card_number text, c_source text, c_location text,
|
|
m_balance_before integer, m_amount integer, m_balance_after integer, j_new_data jsonb, j_old_data jsonb,
|
|
c_note text, c_status text, d_created timestamp, i_create integer, c_create_username text,
|
|
c_create_fullname text, c_category text
|
|
);
|
|
INSERT INTO trx.t_d_load_balance (c_load_balance_no, c_status, m_balance) VALUES ('LBL1', 'I', 50);
|
|
INSERT INTO trx.t_d_load_balance_detail VALUES (1, 7, 'RBL1', 20);
|
|
INSERT INTO trx.t_d_reservation_balance (c_reservation_no, c_load_balance_no, c_status)
|
|
VALUES ('RBL1', 'LBL1', 'I');
|
|
INSERT INTO trx.t_d_flag_counting (c_card_number, c_uid, i_balance, c_flag)
|
|
VALUES ('card-fixture', 'UID', 50, '1');
|
|
`);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (mockTransaction) await mockTransaction.rollback();
|
|
mockTransaction = undefined;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
if (db) await db.destroy();
|
|
});
|
|
|
|
const input = {
|
|
load_balance_number: 'LBL1', reservation_number: ['RBL1'],
|
|
mid: 'fixture-mid', tid: 'fixture-tid', card_number: 'card-fixture',
|
|
uid: 'uid', write_status: false, balance: 70, amount: 20, flag: '2',
|
|
};
|
|
|
|
test.each([
|
|
['saldo', "ALTER TABLE trx.t_d_load_balance ADD CHECK (c_status <> 'A')"],
|
|
['detail riwayat', 'ALTER TABLE trx.t_d_load_balance_history_detail ADD CHECK (i_amount < 0)'],
|
|
['audit kartu', 'ALTER TABLE log.t_log_card ADD CHECK (i_log_name <> 21)'],
|
|
])('kegagalan menulis %s membatalkan seluruh perubahan', async (_stage, constraint) => {
|
|
await mockTransaction.raw(constraint);
|
|
|
|
await expect(update(input)).resolves.toMatchObject({ code: '500' });
|
|
|
|
await expect(mockTransaction('trx.t_d_load_balance').first('c_status', 'm_balance'))
|
|
.resolves.toEqual({ c_status: 'I', m_balance: 50 });
|
|
await expect(mockTransaction('trx.t_d_reservation_balance').first('c_status'))
|
|
.resolves.toEqual({ c_status: 'I' });
|
|
await expect(mockTransaction('trx.t_d_flag_counting').first('i_balance'))
|
|
.resolves.toEqual({ i_balance: 50 });
|
|
await expect(mockTransaction('trx.t_d_load_balance_history').select('i_load_balance')).resolves.toEqual([]);
|
|
await expect(mockTransaction('trx.t_d_load_balance_history_detail').select('i_load_balance')).resolves.toEqual([]);
|
|
await expect(mockTransaction('log.t_log_card').select('i_log_name')).resolves.toEqual([]);
|
|
});
|
|
|
|
test('hasil sukses menyimpan saldo, riwayat, dan audit', async () => {
|
|
await expect(update(input)).resolves.toMatchObject({ code: '00' });
|
|
|
|
await expect(mockTransaction('trx.t_d_load_balance').first('c_status', 'm_balance'))
|
|
.resolves.toEqual({ c_status: 'A', m_balance: 70 });
|
|
await expect(mockTransaction('trx.t_d_load_balance_history_detail').first('i_amount'))
|
|
.resolves.toEqual({ i_amount: 20 });
|
|
await expect(mockTransaction('log.t_log_card').first('i_log_name'))
|
|
.resolves.toEqual({ i_log_name: 21 });
|
|
});
|
|
});
|