Fixed tests and prepared for mocha
This commit is contained in:
@@ -5,9 +5,9 @@ module.exports = cds.service.impl (function(){
|
|||||||
// ( Note: we explicitly specify the namespace to support embedded reuse )
|
// ( Note: we explicitly specify the namespace to support embedded reuse )
|
||||||
const { Reviews, Likes } = this.entities ('sap.capire.reviews')
|
const { Reviews, Likes } = this.entities ('sap.capire.reviews')
|
||||||
|
|
||||||
this.before (['CREATE','UPDATE','DELETE'], 'Reviews', req => {
|
// this.before (['CREATE','UPDATE'], 'Reviews', req => {
|
||||||
if (!req.data.rating) req.data.rating = Math.round(Math.random()*4)+1
|
// 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
|
// 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 )
|
// ( Note: req.on.succeeded ensures we only do that if there's no error )
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
const _model = __dirname+'/..'
|
const _model = __dirname+'/..'
|
||||||
const cds = require ('@sap/cds')
|
const cds = require ('@sap/cds')
|
||||||
|
const {expect} = require('chai').use(require('chai-subset'))
|
||||||
|
|
||||||
describe('messaging tests', ()=>{
|
describe('messaging tests', ()=>{
|
||||||
|
|
||||||
it ('should bootstrap sqlite in-memory db', async()=>{
|
it ('should bootstrap sqlite in-memory db', async()=>{
|
||||||
const db = await cds.deploy (_model) .to ('sqlite::memory:')
|
const db = await cds.deploy (_model) .to ('sqlite::memory:')
|
||||||
expect (db.model) .toBeDefined()
|
expect (db.model) .not.undefined
|
||||||
})
|
})
|
||||||
|
|
||||||
let srv
|
let srv
|
||||||
it ('should serve reviews services', async()=>{
|
it ('should serve reviews services', async()=>{
|
||||||
srv = await cds.serve('ReviewsService') .from (_model)
|
srv = await cds.serve('ReviewsService') .from (_model)
|
||||||
expect (srv.name) .toMatch ('ReviewsService')
|
expect (srv.name) .to.match (/ReviewsService/)
|
||||||
})
|
})
|
||||||
|
|
||||||
let N=0, received=[], M=0
|
let N=0, received=[], M=0
|
||||||
@@ -24,12 +25,9 @@ describe('messaging tests', ()=>{
|
|||||||
})
|
})
|
||||||
|
|
||||||
it ('should add review', async ()=>{
|
it ('should add review', async ()=>{
|
||||||
const review = {
|
const review = { subject: "201", title: "Captivating", rating: ++N }
|
||||||
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 response = await srv.create ('Reviews') .entries (review)
|
const response = await srv.create ('Reviews') .entries (review)
|
||||||
expect (response) .toMatchObject (review)
|
expect (response) .to.containSubset (review)
|
||||||
},100)
|
},100)
|
||||||
|
|
||||||
it ('should add more reviews', ()=> Promise.all ([
|
it ('should add more reviews', ()=> Promise.all ([
|
||||||
@@ -56,9 +54,9 @@ describe('messaging tests', ()=>{
|
|||||||
|
|
||||||
it ('should have received all messages', async()=> {
|
it ('should have received all messages', async()=> {
|
||||||
await new Promise((done)=>setImmediate(done))
|
await new Promise((done)=>setImmediate(done))
|
||||||
expect(M).toBe(N)
|
expect(M).equals(N)
|
||||||
expect(received.length).toBe(N)
|
expect(received.length).equals(N)
|
||||||
expect(received.map(m=>m.data)).toEqual([
|
expect(received.map(m=>m.data)).to.deep.equal([
|
||||||
{ subject: '201', rating: 1 },
|
{ subject: '201', rating: 1 },
|
||||||
{ subject: '201', rating: 1.5 },
|
{ subject: '201', rating: 1.5 },
|
||||||
{ subject: '201', rating: 2 },
|
{ subject: '201', rating: 2 },
|
||||||
Reference in New Issue
Block a user