2024-04-16 06:45:03 +00:00
|
|
|
describe('exchangeRateUpdate functionality', function() {
|
|
|
|
const axios = require('axios');
|
|
|
|
const models = require('vn-loopback/server/server').models;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
spyOn(axios, 'get').and.returnValue(Promise.resolve({
|
|
|
|
data: `<Cube>
|
|
|
|
<Cube time='2024-04-12'>
|
|
|
|
<Cube currency='USD' rate='1.1'/>
|
|
|
|
<Cube currency='CNY' rate='1.2'/>
|
|
|
|
</Cube>
|
|
|
|
</Cube>`
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should process XML data and update or create rates in the database', async function() {
|
|
|
|
spyOn(models.ReferenceRate, 'findOne').and.returnValue(Promise.resolve(null));
|
|
|
|
spyOn(models.ReferenceRate, 'create').and.returnValue(Promise.resolve());
|
|
|
|
|
2024-04-22 09:45:58 +00:00
|
|
|
await models.InvoiceIn.exchangeRateUpdate();
|
2024-04-16 06:45:03 +00:00
|
|
|
|
|
|
|
expect(models.ReferenceRate.create).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create or update rates when no XML data is available', async function() {
|
|
|
|
axios.get.and.returnValue(Promise.resolve({}));
|
|
|
|
spyOn(models.ReferenceRate, 'create');
|
|
|
|
|
|
|
|
let thrownError = null;
|
|
|
|
try {
|
2024-04-22 09:45:58 +00:00
|
|
|
await models.InvoiceIn.exchangeRateUpdate();
|
2024-04-16 06:45:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
thrownError = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(thrownError.message).toBe('No cubes found. Exiting the method.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle errors gracefully', async function() {
|
|
|
|
axios.get.and.returnValue(Promise.reject(new Error('Network error')));
|
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
2024-04-22 09:45:58 +00:00
|
|
|
await models.InvoiceIn.exchangeRateUpdate();
|
2024-04-16 06:45:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
expect(error.message).toBe('Network error');
|
|
|
|
});
|
|
|
|
});
|