From 15b658b6868dd91cc59b9edc193441ccdf4a2535 Mon Sep 17 00:00:00 2001 From: Daniel O'Grady Date: Wed, 30 Aug 2023 11:05:40 +0200 Subject: [PATCH] Add bookshop test for drafts --- test/unit/draft.test.js | 61 ++++++++++++++--------- test/unit/files/draft/catalog-service.cds | 9 ++++ test/unit/files/draft/data-model.cds | 21 ++++++++ 3 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 test/unit/files/draft/catalog-service.cds create mode 100644 test/unit/files/draft/data-model.cds diff --git a/test/unit/draft.test.js b/test/unit/draft.test.js index d49deadb..0cd91e07 100644 --- a/test/unit/draft.test.js +++ b/test/unit/draft.test.js @@ -7,11 +7,24 @@ const { ASTWrapper } = require('../ast') const { locations } = require('../util') const dir = locations.testOutput('draft_test') +const draftable_ = (entity, ast) => ast.find(n => n.name === entity && n.members.find(({name}) => name === 'drafts')) +const draftable = (entity, ast, plural = e => `${e}_`) => draftable_(entity, ast) && draftable_(plural(entity), ast) + +describe('bookshop', () => { + test('Projections Up and Down', async () => { + const paths = await cds2ts.compileFromFile(locations.unit.files('draft/catalog-service.cds'), { outputDirectory: dir }) + const service = new ASTWrapper(path.join(paths[1], 'index.ts')).tree + const model = new ASTWrapper(path.join(paths[2], 'index.ts')).tree + + expect(draftable('Book', service, () => 'Books')).toBeTruthy() + expect(draftable('Publisher', service, () => 'Publishers')).toBeTruthy() + expect(draftable('Book', model, () => 'Books')).toBeTruthy() + expect(draftable('Publisher', model, () => 'Publishers')).toBeTruthy() + }) +}) describe('@odata.draft.enabled', () => { let ast - const draftable_ = entity => ast.find(n => n.name === entity && n.members.find(({name}) => name === 'drafts')) - const draftable = entity => draftable_(entity) && draftable_(`${entity}_`) beforeAll(async () => { await fs.unlink(dir).catch(err => {}) @@ -22,48 +35,48 @@ describe('@odata.draft.enabled', () => { ast = new ASTWrapper(path.join(paths[1], 'index.ts')).tree }) - test('Direct Annotation', async () => expect(draftable('A')).toBeTruthy()) + test('Direct Annotation', async () => expect(draftable('A', ast)).toBeTruthy()) - test('First Level Inheritance', async () => expect(draftable('B')).toBeTruthy()) + test('First Level Inheritance', async () => expect(draftable('B', ast)).toBeTruthy()) - test('Explicit Override via Inheritance', async () => expect(draftable('C')).not.toBeTruthy()) + test('Explicit Override via Inheritance', async () => expect(draftable('C', ast)).not.toBeTruthy()) - test('Inheritance of Explicit Override', async () => expect(draftable('D')).not.toBeTruthy()) + test('Inheritance of Explicit Override', async () => expect(draftable('D', ast)).not.toBeTruthy()) - test('Declaration With true', async () => expect(draftable('E')).toBeTruthy()) + test('Declaration With true', async () => expect(draftable('E', ast)).toBeTruthy()) - test('Multiple Inheritance With Most Significant true', async () => expect(draftable('F')).toBeTruthy()) + test('Multiple Inheritance With Most Significant true', async () => expect(draftable('F', ast)).toBeTruthy()) - test('Multiple Inheritance With Most Significant false', async () => expect(draftable('G')).not.toBeTruthy()) + test('Multiple Inheritance With Most Significant false', async () => expect(draftable('G', ast)).not.toBeTruthy()) test('Draftable by Association/ Composition', async () => { - expect(draftable('H')).not.toBeTruthy() - expect(draftable('I')).not.toBeTruthy() - expect(draftable('J')).not.toBeTruthy() - expect(draftable('K')).not.toBeTruthy() + expect(draftable('H', ast)).not.toBeTruthy() + expect(draftable('I', ast)).not.toBeTruthy() + expect(draftable('J', ast)).not.toBeTruthy() + expect(draftable('K', ast)).not.toBeTruthy() }) test('Unchanged by Association/ Composition', async () => { - expect(draftable('L')).not.toBeTruthy() - expect(draftable('M')).not.toBeTruthy() + expect(draftable('L', ast)).not.toBeTruthy() + expect(draftable('M', ast)).not.toBeTruthy() }) test('Precedence Over Explicit Annotation', async () => { - expect(draftable('P')).toBeTruthy() - expect(draftable('Q')).toBeTruthy() + expect(draftable('P', ast)).toBeTruthy() + expect(draftable('Q', ast)).toBeTruthy() }) - test('Via Projection', async () => expect(draftable('PA')).toBeTruthy()) + test('Via Projection', async () => expect(draftable('PA', ast)).toBeTruthy()) test('Transitive Via Projection and Composition', async () => { - expect(draftable('ProjectedReferrer')).toBeTruthy() - expect(draftable('Referrer')).toBeTruthy() - expect(draftable('Referenced')).toBeTruthy() + expect(draftable('ProjectedReferrer', ast)).toBeTruthy() + expect(draftable('Referrer', ast)).toBeTruthy() + expect(draftable('Referenced', ast)).toBeTruthy() }) test('Transitive Via Multiple Levels of Projection', async () => { - expect(draftable('Foo')).toBeTruthy() - expect(draftable('ProjectedFoo')).toBeTruthy() - expect(draftable('ProjectedProjectedFoo')).toBeTruthy() + expect(draftable('Foo', ast)).toBeTruthy() + expect(draftable('ProjectedFoo', ast)).toBeTruthy() + expect(draftable('ProjectedProjectedFoo', ast)).toBeTruthy() }) }) \ No newline at end of file diff --git a/test/unit/files/draft/catalog-service.cds b/test/unit/files/draft/catalog-service.cds new file mode 100644 index 00000000..75e3d7d2 --- /dev/null +++ b/test/unit/files/draft/catalog-service.cds @@ -0,0 +1,9 @@ +namespace bookshop.service; + +using bookshop as my from './data-model'; + +service CatalogService { + @odata.draft.enabled + entity Books as projection on my.Books; + entity Publishers as projection on my.Publishers; +} \ No newline at end of file diff --git a/test/unit/files/draft/data-model.cds b/test/unit/files/draft/data-model.cds new file mode 100644 index 00000000..d885452a --- /dev/null +++ b/test/unit/files/draft/data-model.cds @@ -0,0 +1,21 @@ +namespace bookshop; + +type User : String(255); +aspect cuid { key ID : UUID; } +aspect managed { + createdAt : Timestamp @cds.on.insert : $now; + createdBy : User @cds.on.insert : $user; + modifiedAt : Timestamp @cds.on.insert : $now @cds.on.update : $now; + modifiedBy : User @cds.on.insert : $user @cds.on.update : $user; +} + +entity Books : managed, cuid { + title : String; + publishers : Composition of many Publishers + on publishers.book = $self; +} + +entity Publishers : managed, cuid { + name : String; + book : Association to Books; +} \ No newline at end of file