2024-04-16 06:45:03 +00:00
|
|
|
describe('exchangeRateUpdate functionality', function() {
|
|
|
|
const axios = require('axios');
|
|
|
|
const models = require('vn-loopback/server/server').models;
|
2025-01-07 09:45:51 +00:00
|
|
|
let tx; let options;
|
2024-04-16 06:45:03 +00:00
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
function formatYmd(d) {
|
|
|
|
const mm = (d.getMonth() + 1).toString().padStart(2, '0');
|
|
|
|
const dd = d.getDate().toString().padStart(2, '0');
|
|
|
|
return `${d.getFullYear()}-${mm}-${dd}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(async() => {
|
|
|
|
await tx.rollback();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async() => {
|
|
|
|
tx = await models.Sale.beginTransaction({});
|
|
|
|
options = {transaction: tx};
|
|
|
|
spyOn(axios, 'get').and.returnValue(Promise.resolve({data: ''}));
|
2024-04-16 06:45:03 +00:00
|
|
|
});
|
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
it('should process XML data and create rates', async function() {
|
|
|
|
const d1 = Date.vnNew();
|
|
|
|
const d4 = Date.vnNew();
|
|
|
|
d4.setDate(d4.getDate() + 1);
|
|
|
|
const xml = `<Cube>
|
|
|
|
<Cube time='${formatYmd(d1)}'>
|
|
|
|
<Cube currency='USD' rate='1.1'/>
|
|
|
|
<Cube currency='CNY' rate='1.2'/>
|
|
|
|
</Cube>
|
|
|
|
<Cube time='${formatYmd(d4)}'>
|
|
|
|
<Cube currency='USD' rate='1.3'/>
|
|
|
|
</Cube>
|
|
|
|
</Cube>`;
|
|
|
|
axios.get.and.returnValue(Promise.resolve({data: xml}));
|
2024-04-16 06:45:03 +00:00
|
|
|
spyOn(models.ReferenceRate, 'findOne').and.returnValue(Promise.resolve(null));
|
|
|
|
spyOn(models.ReferenceRate, 'create').and.returnValue(Promise.resolve());
|
2025-01-07 09:45:51 +00:00
|
|
|
await models.InvoiceIn.exchangeRateUpdate(options);
|
2024-04-16 06:45:03 +00:00
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
expect(models.ReferenceRate.create).toHaveBeenCalledTimes(3);
|
2024-04-16 06:45:03 +00:00
|
|
|
});
|
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
it('should handle no data', async function() {
|
2024-04-16 06:45:03 +00:00
|
|
|
axios.get.and.returnValue(Promise.resolve({}));
|
|
|
|
spyOn(models.ReferenceRate, 'create');
|
2025-01-07 09:45:51 +00:00
|
|
|
let e;
|
2024-04-16 06:45:03 +00:00
|
|
|
try {
|
2025-01-07 09:45:51 +00:00
|
|
|
await models.InvoiceIn.exchangeRateUpdate(options);
|
|
|
|
} catch (err) {
|
|
|
|
e = err;
|
2024-04-16 06:45:03 +00:00
|
|
|
}
|
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
expect(e.message).toBe('No cubes found. Exiting the method.');
|
|
|
|
expect(models.ReferenceRate.create).not.toHaveBeenCalled();
|
2024-04-16 06:45:03 +00:00
|
|
|
});
|
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
it('should handle errors', async function() {
|
2024-04-16 06:45:03 +00:00
|
|
|
axios.get.and.returnValue(Promise.reject(new Error('Network error')));
|
2025-01-07 09:45:51 +00:00
|
|
|
let e;
|
2024-04-16 06:45:03 +00:00
|
|
|
try {
|
2025-01-07 09:45:51 +00:00
|
|
|
await models.InvoiceIn.exchangeRateUpdate(options);
|
|
|
|
} catch (err) {
|
|
|
|
e = err;
|
2024-04-16 06:45:03 +00:00
|
|
|
}
|
|
|
|
|
2025-01-07 09:45:51 +00:00
|
|
|
expect(e).toBeDefined();
|
|
|
|
expect(e.message).toBe('Network error');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update existing rate', async function() {
|
|
|
|
const existingRate = await models.ReferenceRate.findOne({
|
|
|
|
order: 'id DESC'
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
if (!existingRate) return fail('No ReferenceRate records in DB');
|
|
|
|
|
|
|
|
const currency = await models.Currency.findById(existingRate.currencyFk, null, options);
|
|
|
|
|
|
|
|
const xml = `<Cube>
|
|
|
|
<Cube time='${formatYmd(existingRate.dated)}'>
|
|
|
|
<Cube currency='${currency.code}' rate='2.22'/>
|
|
|
|
</Cube>
|
|
|
|
</Cube>`;
|
|
|
|
|
|
|
|
axios.get.and.returnValue(Promise.resolve({data: xml}));
|
|
|
|
|
|
|
|
await models.InvoiceIn.exchangeRateUpdate(options);
|
|
|
|
|
|
|
|
const updatedRate = await models.ReferenceRate.findById(existingRate.id, null, options);
|
|
|
|
|
|
|
|
expect(updatedRate.value).toBeCloseTo('2.22');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not update if same rate', async function() {
|
|
|
|
const existingRate = await models.ReferenceRate.findOne({order: 'id DESC'}, options);
|
|
|
|
if (!existingRate) return fail('No existing ReferenceRate in DB');
|
|
|
|
|
|
|
|
const currency = await models.Currency.findById(existingRate.currencyFk, null, options);
|
|
|
|
|
|
|
|
const oldValue = existingRate.value;
|
|
|
|
const xml = `<Cube>
|
|
|
|
<Cube time='${formatYmd(existingRate.dated)}'>
|
|
|
|
<Cube currency='${currency.code}' rate='${oldValue}'/>
|
|
|
|
</Cube>
|
|
|
|
</Cube>`;
|
|
|
|
|
|
|
|
axios.get.and.returnValue(Promise.resolve({data: xml}));
|
|
|
|
|
|
|
|
await models.InvoiceIn.exchangeRateUpdate(options);
|
|
|
|
|
|
|
|
const updatedRate = await models.ReferenceRate.findById(existingRate.id, null, options);
|
|
|
|
|
|
|
|
expect(updatedRate.value).toBe(oldValue);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should backfill missing dates', async function() {
|
|
|
|
const lastRate = await models.ReferenceRate.findOne({order: 'dated DESC'}, options);
|
|
|
|
if (!lastRate) return fail('No existing ReferenceRate data in DB');
|
|
|
|
|
|
|
|
const currency = await models.Currency.findById(lastRate.currencyFk, null, options);
|
|
|
|
|
|
|
|
const d1 = new Date(lastRate.dated);
|
|
|
|
d1.setDate(d1.getDate() + 1);
|
|
|
|
const d4 = new Date(lastRate.dated);
|
|
|
|
d4.setDate(d4.getDate() + 4);
|
|
|
|
|
|
|
|
const xml = `<Cube>
|
|
|
|
<Cube time='${formatYmd(d1)}'>
|
|
|
|
<Cube currency='${currency.code}' rate='1.0'/>
|
|
|
|
</Cube>
|
|
|
|
<Cube time='${formatYmd(d4)}'>
|
|
|
|
<Cube currency='${currency.code}' rate='2.0'/>
|
|
|
|
</Cube>
|
|
|
|
</Cube>`;
|
|
|
|
|
|
|
|
axios.get.and.returnValue(Promise.resolve({data: xml}));
|
|
|
|
|
|
|
|
const beforeCount = await models.ReferenceRate.count({}, options);
|
|
|
|
await models.InvoiceIn.exchangeRateUpdate(options);
|
|
|
|
const afterCount = await models.ReferenceRate.count({}, options);
|
|
|
|
|
|
|
|
expect(afterCount - beforeCount).toBe(4);
|
2024-04-16 06:45:03 +00:00
|
|
|
});
|
|
|
|
});
|