102 lines
3.6 KiB
JavaScript
102 lines
3.6 KiB
JavaScript
|
const {models} = require('vn-loopback/server/server');
|
||
|
|
||
|
describe('itemMinimumQuantity model', () => {
|
||
|
const itemFk = 5;
|
||
|
const warehouseFk = 60;
|
||
|
|
||
|
beforeAll(async() => {
|
||
|
await models.ItemMinimumQuantity.destroyAll({where: {itemFk: itemFk}});
|
||
|
});
|
||
|
|
||
|
describe('CRUD operations', () => {
|
||
|
it('should create a new itemMinimumQuantity record', async() => {
|
||
|
const newRecord = {
|
||
|
itemFk: itemFk,
|
||
|
quantity: 100,
|
||
|
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,
|
||
|
quantity: 100,
|
||
|
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,
|
||
|
quantity: 100,
|
||
|
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,
|
||
|
quantity: 100,
|
||
|
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();
|
||
|
expect(e.code).toContain('ER_DUP_ENTRY');
|
||
|
}
|
||
|
});
|
||
|
|
||
|
it('should allow null values for ended and warehouseFk', async() => {
|
||
|
const newRecord = {
|
||
|
itemFk: itemFk,
|
||
|
quantity: 100,
|
||
|
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();
|
||
|
});
|
||
|
});
|
||
|
});
|