2024-07-11 12:27:16 +00:00
|
|
|
const {models} = require('vn-loopback/server/server');
|
|
|
|
|
|
|
|
describe('itemMinimumQuantity model', () => {
|
|
|
|
const itemFk = 5;
|
2024-07-22 08:27:24 +00:00
|
|
|
const quantity = 100;
|
2024-07-11 12:27:16 +00:00
|
|
|
const warehouseFk = 60;
|
|
|
|
|
2024-07-22 08:27:24 +00:00
|
|
|
beforeEach(async() => {
|
2024-07-11 12:27:16 +00:00
|
|
|
await models.ItemMinimumQuantity.destroyAll({where: {itemFk: itemFk}});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('CRUD operations', () => {
|
|
|
|
it('should create a new itemMinimumQuantity record', async() => {
|
|
|
|
const newRecord = {
|
|
|
|
itemFk: itemFk,
|
2024-07-22 08:27:24 +00:00
|
|
|
quantity: quantity,
|
2024-07-11 12:27:16 +00:00
|
|
|
started: Date.vnNew(),
|
|
|
|
ended: new Date(Date.vnNew().setFullYear(Date.vnNew().getFullYear() + 1)),
|
|
|
|
warehouseFk: warehouseFk
|
|
|
|
};
|
|
|
|
|
|
|
|
const createdRecord = await models.ItemMinimumQuantity.create(newRecord);
|
|
|
|
|
|
|
|
expect(createdRecord).toBeDefined();
|
|
|
|
expect(createdRecord.quantity).toEqual(newRecord.quantity);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read an existing itemMinimumQuantity record', async() => {
|
|
|
|
const newRecord = {
|
|
|
|
itemFk: itemFk,
|
2024-07-22 08:27:24 +00:00
|
|
|
quantity: quantity,
|
2024-07-11 12:27:16 +00:00
|
|
|
started: Date.vnNew(),
|
|
|
|
ended: new Date(Date.vnNew().setFullYear(Date.vnNew().getFullYear() + 2)),
|
|
|
|
warehouseFk: warehouseFk
|
|
|
|
};
|
|
|
|
|
|
|
|
await models.ItemMinimumQuantity.create(newRecord);
|
|
|
|
|
|
|
|
const existingRecord = await models.ItemMinimumQuantity.findOne({where: {itemFk: itemFk}});
|
|
|
|
|
|
|
|
expect(existingRecord).toBeDefined();
|
|
|
|
expect(existingRecord.itemFk).toEqual(itemFk);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update an existing itemMinimumQuantity record', async() => {
|
|
|
|
const newRecord = {
|
|
|
|
itemFk: itemFk,
|
2024-07-22 08:27:24 +00:00
|
|
|
quantity: quantity,
|
2024-07-11 12:27:16 +00:00
|
|
|
started: Date.vnNew(),
|
|
|
|
ended: new Date(Date.vnNew().setFullYear(Date.vnNew().getFullYear() + 3)),
|
|
|
|
warehouseFk: warehouseFk
|
|
|
|
};
|
|
|
|
|
|
|
|
await models.ItemMinimumQuantity.create(newRecord);
|
|
|
|
|
|
|
|
const newQuantity = 150;
|
|
|
|
const existingRecord = await models.ItemMinimumQuantity.findOne({where: {itemFk: itemFk}});
|
|
|
|
existingRecord.quantity = newQuantity;
|
|
|
|
|
|
|
|
await existingRecord.save();
|
|
|
|
const updatedRecord = await models.ItemMinimumQuantity.findOne({where: {itemFk: itemFk}});
|
|
|
|
|
|
|
|
expect(updatedRecord.quantity).toEqual(newQuantity);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('validation and constraints', () => {
|
|
|
|
it('should enforce unique constraint on itemFk, started, ended, and warehouseFk', async() => {
|
|
|
|
const newRecord = {
|
|
|
|
itemFk: itemFk,
|
2024-07-22 08:27:24 +00:00
|
|
|
quantity: quantity,
|
2024-07-11 12:27:16 +00:00
|
|
|
started: Date.vnNew(),
|
|
|
|
ended: Date.vnNew().setFullYear(Date.vnNew().getFullYear() + 5),
|
|
|
|
warehouseFk: warehouseFk
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await models.ItemMinimumQuantity.create(newRecord);
|
|
|
|
await models.ItemMinimumQuantity.create(newRecord);
|
|
|
|
} catch (e) {
|
|
|
|
expect(e).toBeDefined();
|
2024-07-22 08:27:24 +00:00
|
|
|
expect(e.code).toContain('ER_SIGNAL_EXCEPTION');
|
2024-07-11 12:27:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow null values for ended and warehouseFk', async() => {
|
|
|
|
const newRecord = {
|
|
|
|
itemFk: itemFk,
|
2024-07-22 08:27:24 +00:00
|
|
|
quantity: quantity,
|
2024-07-11 12:27:16 +00:00
|
|
|
started: Date.vnNew(),
|
|
|
|
ended: null,
|
|
|
|
warehouseFk: null
|
|
|
|
};
|
|
|
|
|
|
|
|
const createdRecord = await models.ItemMinimumQuantity.create(newRecord);
|
|
|
|
|
|
|
|
expect(createdRecord).toBeDefined();
|
|
|
|
expect(createdRecord.ended).toBeNull();
|
|
|
|
expect(createdRecord.warehouseFk).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|