Fixed tests and prepared for mocha

This commit is contained in:
Daniel
2020-03-03 09:08:11 +01:00
parent 531b6cbf69
commit ea01007716
7 changed files with 11 additions and 13 deletions

View File

@@ -5,9 +5,9 @@ module.exports = cds.service.impl (function(){
// ( Note: we explicitly specify the namespace to support embedded reuse )
const { Reviews, Likes } = this.entities ('sap.capire.reviews')
this.before (['CREATE','UPDATE','DELETE'], 'Reviews', req => {
if (!req.data.rating) req.data.rating = Math.round(Math.random()*4)+1
})
// this.before (['CREATE','UPDATE'], 'Reviews', req => {
// if (!req.data.rating) req.data.rating = Math.round(Math.random()*4)+1
// })
// Emit an event to inform subscribers about new avg ratings for reviewed subjects
// ( Note: req.on.succeeded ensures we only do that if there's no error )

View File

@@ -1,17 +1,18 @@
const _model = __dirname+'/..'
const cds = require ('@sap/cds')
const {expect} = require('chai').use(require('chai-subset'))
describe('messaging tests', ()=>{
it ('should bootstrap sqlite in-memory db', async()=>{
const db = await cds.deploy (_model) .to ('sqlite::memory:')
expect (db.model) .toBeDefined()
expect (db.model) .not.undefined
})
let srv
it ('should serve reviews services', async()=>{
srv = await cds.serve('ReviewsService') .from (_model)
expect (srv.name) .toMatch ('ReviewsService')
expect (srv.name) .to.match (/ReviewsService/)
})
let N=0, received=[], M=0
@@ -24,12 +25,9 @@ describe('messaging tests', ()=>{
})
it ('should add review', async ()=>{
const review = {
ID: 111 + (++N), // FIXME: why does the generic handler not fill this in automatically ?!? --> it does so when the request comes in via Postman / OData
subject: "201", title: "Captivating", rating: N
}
const review = { subject: "201", title: "Captivating", rating: ++N }
const response = await srv.create ('Reviews') .entries (review)
expect (response) .toMatchObject (review)
expect (response) .to.containSubset (review)
},100)
it ('should add more reviews', ()=> Promise.all ([
@@ -56,9 +54,9 @@ describe('messaging tests', ()=>{
it ('should have received all messages', async()=> {
await new Promise((done)=>setImmediate(done))
expect(M).toBe(N)
expect(received.length).toBe(N)
expect(received.map(m=>m.data)).toEqual([
expect(M).equals(N)
expect(received.length).equals(N)
expect(received.map(m=>m.data)).to.deep.equal([
{ subject: '201', rating: 1 },
{ subject: '201', rating: 1.5 },
{ subject: '201', rating: 2 },