Skip to content

Commit

Permalink
Add bookshop test for drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady committed Aug 30, 2023
1 parent dc47ee2 commit 15b658b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
61 changes: 37 additions & 24 deletions test/unit/draft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {})
Expand All @@ -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()
})
})
9 changes: 9 additions & 0 deletions test/unit/files/draft/catalog-service.cds
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions test/unit/files/draft/data-model.cds
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 15b658b

Please sign in to comment.