Hey guys,
I am having some struggle with unittesting my custom action which works on draft tables.
From the beginning: I know we use cds version 5.8 which is very old. But the idea is first to expand our test suite significantly before we change the major release.
Scenario is the following. I have an app to maintain material master data using drafts. My custom action is only callable in draft mode and creates supplier specific EAN/GTIN data from in draft tables available data of GTINs and Info Records. The custom action should create all non existing supplier specific entries.
So for testing I wonder if I can do a delete/cancel on my draft data to play aound with different scenarios close to user interaction.
I tried to follow the example from the SFLIGHT sample to get into touch with using draft data in unit tests, but deletion isn't covered I think.
So I tried something like this:
await DELETE(`products/AdditionalGTIN(ID=${toDeletID},IsActiveEntity=false)`);
Also some normal DELETE Queries on the draft tables using cds facade object or an service instance. Not able to make it work.
Hoping for your help. Maybe I made a quite simple mistake.
Regards Max
My test file looks like this. I know it is hurting the principle of independent test cases a little bit, but in that case of UI close implementation I think it's quite okay.:
const cds = require('@sap/cds/lib');
const { expect, GET, POST, PATCH, axios, data } = cds.test(__dirname + '/../../..');
axios.defaults.auth = { username: 'IT_Test', password: 'hallo' }
const SAVE = (url) => POST (url+'/ProductService.draftActivate')
describe('Logic of ProductService for entity AdditionalGTIN', () => {
let oProductDraftGlobal, oInfrecDraftGlobal, oUoMDraftGlobal, aGTINsDraftGlobal = [];
beforeAll(async () => {
const { data: oProductDraft } = await POST(`/products/Products`, { UIProductCategory: '00' });
oProductDraftGlobal = oProductDraft;
const { data: oInfrecDraft } = await POST(`/products/PurchaseInfoRecords`, { product_ID: oProductDraft.ID, Supplier: 'TEST_LIEF1'});
oInfrecDraftGlobal = oInfrecDraft;
})
describe('Handler for Action customCreate', () => {
it('no existing units of measure at execution', async () => {
// not intressting for the problem....
});
it('no existing gtins at execution', async () => {
// not intressting for the problem....
});
it('first creation for supplier', async () => {
// create some GTINs and run action for the first expecting initial supplier specific GTINs are created
});
it('using supplier again without changes', async () => {
// cruns action again expecting no new supplier specific gtins are created
});
it('using supplier again after deleting one additional GTIN', async () => {
// delete one entry and run action again, expecting the deleted one is recreated
const aAdditionalGTINs = await SELECT.from('ProductService.AdditionalGTIN_drafts');
const toDeletID = aAdditionalGTINs[1].ID;
// THIS LINE DOES NOT WORK --->
await DELETE(`products/AdditionalGTIN(ID=${toDeletID},IsActiveEntity=false)`);
// <--- THIS LINE DOES NOT WORK
// run action and check the results.....
});
})
describe('Handler for before PATCH', () => {
// not intressting for the problem....
})
})