Skip to content

Commit

Permalink
test(purchases): unit test FK link
Browse files Browse the repository at this point in the history
This commit adds a unit test to ensure the foreign keys work and that
values are deleted from the purchase_item table when they are removed
from the purchase table.
  • Loading branch information
jniles committed Mar 17, 2021
1 parent ae636bf commit 704ed6f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
4 changes: 1 addition & 3 deletions server/controllers/finance/purchases.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ async function remove(req, res, next) {

const WAITING_CONFIRMATION = 1;
if (record.status_id !== WAITING_CONFIRMATION) {
throw new BadRequest('Cannot remove a purchase order that is confirmed');
throw new BadRequest('Can only remove purchase orders that have been confirmed.');
}

// there is no financial writings about purchase orders, so we simply need to delete it
Expand Down Expand Up @@ -628,9 +628,7 @@ function resetPurchaseIntervall(purchaseUuid) {
return db.exec(sql, [db.bid(purchaseUuid)])
.then((rows) => {
const inventories = rows.map(item => item.inventory_uuid);

const transactions = purchaceIntervalleSetting(inventories);

return transactions.execute();
})
.then((items) => {
Expand Down
8 changes: 6 additions & 2 deletions test/server-unit/db.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function DatabaseUnitTests() {
db = require('../../server/lib/db');
});


it('should check the connection to mysql', (done) => {
db.pool.getConnection(err => {
if (err) {
Expand All @@ -19,7 +18,12 @@ function DatabaseUnitTests() {
});
});

it('#should try to retrieve data from a specific table (unit)', (done) => {
it('#exec() should retrieve a promise result', async () => {
const [result] = await db.exec('SELECT 1 + 1 AS two;');
expect(result).to.deep.equal({ two : 2 });
});

it('should try to retrieve data from a specific table (unit)', (done) => {
db.exec('SELECT * FROM unit LIMIT 2')
.then(rows => {
expect(rows).to.have.lengthOf(2);
Expand Down
42 changes: 42 additions & 0 deletions test/server-unit/purchases/purchases.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint global-require:off */
const { expect } = require('chai');
const uuid = require('uuid').v4;

function PurchaseUnitTests() {
let db;
before(() => {
db = require('../../../server/lib/db');
});

it('should remove purchase items on purchase delete', async () => {
// test setup - create a purchase order
const puid = uuid();
const prednisone = 'c3fd5a02-6a75-49fc-b2f3-76ee4c3fbfb7';

// create the purchase order
await db.exec(`
INSERT INTO purchase VALUES
(HUID('${puid}'), 2, 1, 300, 2, HUID('3ac4e83c-65f2-45a1-8357-8b025003d793'),
DATE_ADD(CURRENT_DATE, INTERVAL -1725 DAY), CURRENT_TIMESTAMP, 1, NULL, NULL, 1);
`);

// create a purchase item linked to the above purchase order.
await db.exec(`INSERT INTO purchase_item VALUES
(HUID('${uuid()}'), HUID('${puid}'), HUID('${prednisone}'), 2000, 0.15, (2000 * 0.15));`);

const [purchase] = await db.exec(`SELECT * FROM purchase WHERE uuid = HUID('${puid}');`);
expect(purchase.cost).to.equal(300);

// okay, now that we know our data is built, actually test if the data is deleted
// correctly.
await db.exec(`DELETE FROM purchase WHERE uuid = HUID('${puid}');`);

const purchases = await db.exec(`SELECT * FROM purchase WHERE uuid = HUID('${puid}');`);
expect(purchases).to.have.length(0);
const items = await db.exec(`SELECT * FROM purchase_item WHERE purchase_uuid = HUID('${puid}');`);
expect(items).to.have.length(0);
});

}

describe('Purchase Orders', PurchaseUnitTests);

0 comments on commit 704ed6f

Please sign in to comment.