Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore test.ts usages #8

Merged
merged 14 commits into from
Jan 16, 2024
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ jobs:
- name: Run Unit Tests
run: |
npm install
npm install file:. --no-save --force
chgeo marked this conversation as resolved.
Show resolved Hide resolved
npm run test
30 changes: 16 additions & 14 deletions apis/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ type Intersect<T extends readonly unknown[]> = T extends [infer Head, ...infer T
* Base class for linked Associations from reflected models.
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
*/
export type Association = new(_?:object) => LinkedAssociation
export const Association: new(_?:object) => LinkedAssociation

/**
* Base class for linked Compositions from reflected models.
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-Association)
*/
export type Composition = new(_?:object) => LinkedAssociation
export const Composition: new(_?:object) => LinkedAssociation

/**
* Base class for linked entities from reflected models.
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-entity)
*/
export type entity = new(_?:object) => LinkedEntity
export type event = new(_?:object) => linked & csn.struct
export type type = new(_?:object) => linked & csn.type
export type array = new(_?:object) => linked & csn.type
export type struct = new(_?:object) => linked & csn.struct
export const entity: new(_?:object) => LinkedEntity
export const event: new(_?:object) => linked & csn.struct
export const type: new(_?:object) => linked & csn.type
export const array: new(_?:object) => linked & csn.type
export const struct: new(_?:object) => linked & csn.struct
daogrady marked this conversation as resolved.
Show resolved Hide resolved

// infer (query : cqn, model : csn) : LinkedDefinition
export const builtin: {
Expand All @@ -34,13 +36,13 @@ export const builtin: {
* @see [capire](https://cap.cloud.sap/docs/node.js/cds-reflect#cds-builtin-classes)
*/
classes: {
Association: Association
Composition: Composition
entity: entity
event: event
type: type
array: array
struct: struct
Association: typeof Association
Composition: typeof Composition
entity: typeof entity
event: typeof event
type: typeof type
array: typeof array
struct: typeof struct
service: service
}
types: {}
Expand Down
2 changes: 1 addition & 1 deletion apis/linked.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface linked {
interface LinkedEntity extends linked, entity {
constructor (properties: object)
keys: Definitions
drafts: LinkedEntity
drafts: LinkedEntity | undefined
chgeo marked this conversation as resolved.
Show resolved Hide resolved
}

interface LinkedAssociation extends linked, Association {
Expand Down
2 changes: 1 addition & 1 deletion apis/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type filename = string
* The effective CDS model loaded during bootstrapping, which contains all service and entity definitions,
* including required services.
*/
export const model : LinkedCSN | undefined // was ?: LinkedCSN
export let model : LinkedCSN | undefined // was ?: LinkedCSN
chgeo marked this conversation as resolved.
Show resolved Hide resolved

/**
* Provides a set of methods to parse a given model, query or expression.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"devDependencies": {
"@sap/cds": "^7.4.0",
"@types/jest": "^29.5.11",
"@types/node": "^20",
"axios": "^1.6.2",
"chai": "^4.3.10",
Expand Down
266 changes: 266 additions & 0 deletions test/typescript/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import cds, { User, Query } from '@sap/cds';
import { struct } from '@sap/cds/apis/csn';

import {
Service,
EventContext,
Request,
Event,
// User, >> already imported above

ApplicationService,
MessagingService,
DatabaseService,
RemoteService,

Association,
Composition,
// entity,
event,
// type,
array,
// struct,
service,

} from '@sap/cds';


describe('runtime tests', () => {
chgeo marked this conversation as resolved.
Show resolved Hide resolved

let {Test} = cds.test
let test = new Test
let log = test.log()

it('cds.test', () => {
expect(test.test).toBe(test)
expect(typeof test.run).toBe('function')
expect(typeof test.in).toBe('function')
expect(typeof test.log).toBe('function')
expect(typeof test.data.reset).toBe('function')
console.log('foo')
expect(log.output).toBe('foo\n') // only works in jest/mocha (which trigger beforeAll)
console.log('bar')
expect(log.output).toBe('foo\nbar\n')
log.release()
console.log('car')
expect(log.output).toBe('')
chgeo marked this conversation as resolved.
Show resolved Hide resolved
})

it('misc', () => {

if (global.false) {
let q1 : Query
let x1 = q1.INSERT.entries[1].foo

let csn = cds.parse(`entity Foo {}`)
let Foo = csn.definitions.Foo
Foo.query

let e = new cds.entity

let x = csn.definitions.Foo
x instanceof cds.entity
if (x.kind === 'entity') {
x.query
let z = x['@foo']
}

let {bar} = Foo.elements
let baz = (<struct>bar).elements.baz

let m = cds.linked(csn)
let Foo2 = m.definitions.Foo
Foo2.name
Foo2.keys
Foo2.is('entity')
}

cds.model = cds.linked({})
let foo = cds.extend({foo:1}).with({bar:2},{car:3})
foo.foo
foo.bar

cds.model = cds.linked({})
{
let {
requires,
// plugins,
version,
env,
home,
// cli,
root,

// compiler,
compile,
resolve,
load,
get,
parse ,
// minify,
extend,
// deploy,
// localize,
// build,

reflect,
linked,
// infer,
builtin,
Association,
Composition,
entity,
event,
type,
array,
struct,
service,
// impl: (/** @type {(this:Service, srv:Service, ...etc) => any} */ srv) => srv,
// protocols: lazy => require('./srv/protocols'),
// bindings: lazy => require('./srv/bindings'),
// factory: lazy => require('./srv/factory'),
// /** @type Service[] */ providers: []

services,

server,
serve,
connect,
// middlewares,
// odata,
// auth,

// emit,

Service,
EventContext,
Request,
Event,
User,

// Services, Protocols and Periphery
ApplicationService,
MessagingService,
DatabaseService,
RemoteService,

// Contexts and Transactions
context,
spawn,
tx,

// Helpers
utils,
// error,
// exec,
test,
log,
debug,
lazify,
lazified,
// clone,
exit,

ql,
entities,

run,
foreach,
stream,
read,
create,
insert,
update,
delete : _delete,
// disconnect,
transaction,

} = cds
}

cds.service.impl(srv => {
srv.on('READ', 'Books', () => [{ID:1}])
})

cds.env.requires.db = { kind: 'sqlite' }
// let { Books } = cds.entities

expect (Service && cds.Service).toBe(Service)
expect (Request && cds.Request).toBe(Request)
expect (Event && cds.Event).toBe(Event)
expect (User && cds.User).toBe(User)
expect (cds.linked)

class MyService extends cds.ApplicationService {}
console.log (cds.linked(cds.parse(`entity Foo {}`)))
console.log (cds.linked.toString())

SELECT.from('Books').where({ID:1})

let ua = new User
expect (ua.id).toBe('anonymous')
expect(ua.is('any'))
expect(!ua.is('authenticated-user'))

let u2 = new cds.User('me')
expect (u2.id).toBe('me')
expect(u2.is('any'))
expect(u2.is('authenticated-user'))

let r = new Request ({event:'foo',data:{foo:'bar'},headers:{x:11}})
expect(r.event).toBe('foo')
expect(r.data.foo).toBe('bar')
expect(r.headers.x).toBe(11)

expect (cds.Association)
expect (cds.Composition)

cds.env.fiori.lean_draft = false
let Books = new cds.entity({name:'Books', elements: { HasDraftEntity:true }})
expect(Books.name).toBe('Books')
expect(Books.drafts!.name).toBe('Books_drafts')
let q = SELECT.from(Books).where({ID:1})
// expect(q.SELECT.from.ref[0]).toBe('Books') // FIXME: from.ref should work
daogrady marked this conversation as resolved.
Show resolved Hide resolved
DELETE.from(Books.drafts || Books).where({ID:1})
UPDATE(Books.drafts || Books).with({}).where({ID:1})
INSERT.into(Books.drafts || Books).entries({})
cds.env.fiori.lean_draft = true

if (global.false) {
let srv = new cds.Service
let { Books } = srv.entities
Books.name
SELECT.from(Books.drafts || Books).where({ID:1})
srv.before('READ', Books, console.log)
srv.after('READ', Books, console.log)
srv.on('READ', Books, console.log)
srv.before('READ', Books.drafts, console.log)
srv.after('READ', Books.drafts, console.log)
srv.on('READ', Books.drafts, console.log)
srv.before('READ', [ Books, Books.drafts ], console.log)
srv.after('READ', [ Books, Books.drafts ], console.log)
srv.on('READ', [ Books, Books.drafts ], console.log)
}
if (global.false) {
let csn = cds.compile(`entity Foo { key ID: Integer; name: String; }`).to.csn()
let { Foo } = csn.definitions!
// Foo.name //> error: .name is not defined on unlinked CSN definitions
let m = cds.linked(csn)
let { Bar } = m.definitions
Bar.name //> ok: .name is defined on linked definitions
SELECT.from(Bar.drafts || Bar).where({ID:1})
}

cds.service.impl(srv => { srv.on('READ', Books, console.log) })
cds.service.impl(function(){ this.on('READ', Books, console.log) })
cds.service.impl(async function(){ this.on('READ', Books, console.log) })
cds.service.impl(async srv => { srv.on('READ', Books, console.log) })

// if (req instanceof cds.Association) console.log()

let model = cds.parse.cdl(`entity Foo { key ID: Integer; name: String; }`)
cds.linked(model)

})

})
Loading