-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |