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

coercify matchers for expected numbers #1088

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions test/odata.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
const cds = require('@sap/cds/lib')
const { GET, POST, PATCH, axios, expect } = cds.test(__dirname+'/..')
const { GET, POST, PATCH, axios, chai, expect } = cds.test(__dirname+'/..')
const EDIT = (url) => POST (url+'/TravelService.draftEdit',{})
const SAVE = (url) => POST (url+'/TravelService.draftActivate')
axios.defaults.headers['content-type'] = 'application/json;IEEE754Compatible=true' // REVISIT: can be removed when @sap/cds 5.1.5 is released?
axios.defaults.auth = { username: 'alice', password: 'admin' }



// decimals and int64s as strings with cds^8
chai.Assertion.addMethod('containCoerced', function (exp) {
const _coercify = exp => { for (const [k, v] of Object.entries(exp)) exp[k] = typeof v === 'number' ? ev => ev == v : exp[k] }
if (Array.isArray(exp)) exp.forEach(_coercify)
else if (exp && typeof exp === 'object') _coercify(exp)
new chai.Assertion(this._obj).to.containSubset(exp)
})
chai.Assertion.addMethod('equalCoerced', function (exp) {
new chai.Assertion(this._obj).to.satisfy(val => val == exp)
})



describe ("Basic Querying", () => {

it ("should read from row references", async()=>{
Expand Down Expand Up @@ -56,7 +71,7 @@ describe('Basic OData', () => {

it('GET /processor/Travel', async () => {
const { data } = await GET(`/processor/Travel?$filter=TravelUUID eq '00667221A8E4645C17002DF03754AB66'`)
expect(data.value).to.containSubset([{
expect(data.value).to.containCoerced([{
BeginDate: '2023-08-02',
BookingFee: 60,
createdAt: expectedValue => /2023-07-16T18:42:07\.000(0000)?Z/.test(expectedValue), // timestamp precision increase with cds^7
Expand Down Expand Up @@ -134,7 +149,7 @@ describe('Basic OData', () => {
})

const { data: newTravel } = await SAVE (`/processor/Travel(TravelUUID='${newDraft.TravelUUID}',IsActiveEntity=false)`)
expect(newTravel).to.contain({ TravelID: 4134, TotalPrice: 11 })
expect(newTravel).to.containCoerced({ TravelID: 4134, TotalPrice: 11 })
})

it ('re-calculates totals after booking fee changed', async ()=>{
Expand All @@ -144,7 +159,7 @@ describe('Basic OData', () => {
let Supplement = `/processor/BookingSupplement(BookSupplUUID='85D87221A8E4645C17002DF03754AB66',IsActiveEntity=false)`

let { data:draft } = await EDIT (Travel4133)
expect(draft).to.containSubset({
expect(draft).to.containCoerced({
TotalPrice: 7375,
TravelID: 4133,
})
Expand Down Expand Up @@ -173,43 +188,43 @@ describe('Basic OData', () => {
let { data: { TotalPrice } } = await GET (`/processor/Travel(TravelUUID='76757221A8E4645C17002DF03754AB66',${_active})?
$select=TotalPrice
`)
expect(TotalPrice).to.eql(expected)
expect(TotalPrice).to.equalCoerced(expected)
}
})

it('deduct discount multiple times does not end up in error', async () => {
const { data: res1 } = await GET `/processor/Travel(TravelUUID='52657221A8E4645C17002DF03754AB66',IsActiveEntity=true)`
expect(res1).to.contain({ TotalPrice: 900, BookingFee: 20 })
expect(res1).to.containCoerced({ TotalPrice: 900, BookingFee: 20 })

const { data: res2 } = await POST(
`/processor/Travel(TravelUUID='52657221A8E4645C17002DF03754AB66',IsActiveEntity=true)/TravelService.deductDiscount`,
{ percent: 11 }
)
expect(res2).to.contain({ TotalPrice: 897.8, BookingFee: 17.8 })
expect(res2).to.containCoerced({ TotalPrice: 897.8, BookingFee: 17.8 })

const { data: res3 } = await POST(
`/processor/Travel(TravelUUID='52657221A8E4645C17002DF03754AB66',IsActiveEntity=true)/TravelService.deductDiscount`,
{ percent: 11 }
)
expect(res3).to.contain({ TotalPrice: 895.842, BookingFee: 15.842 })
expect(res3).to.containCoerced({ TotalPrice: 895.842, BookingFee: 15.842 })

const { data: res4 } = await POST(
`/processor/Travel(TravelUUID='52657221A8E4645C17002DF03754AB66',IsActiveEntity=true)/TravelService.deductDiscount`,
{ percent: 11 }
)
// rounded to 3 decimals
expect(res4).to.contain({ TotalPrice: 894.099, BookingFee: 14.099 })
expect(res4).to.containCoerced({ TotalPrice: 894.099, BookingFee: 14.099 })
})

it('allows deducting discounts on drafts as well', async ()=>{
const Active = `/processor/Travel(TravelUUID='93657221A8E4645C17002DF03754AB66',IsActiveEntity=true)`
const Draft = `/processor/Travel(TravelUUID='93657221A8E4645C17002DF03754AB66',IsActiveEntity=false)`

const { data:res0 } = await GET (Active)
expect(res0).to.contain({ TravelID:66, TotalPrice: 729, BookingFee: 10 })
expect(res0).to.containCoerced({ TravelID:66, TotalPrice: 729, BookingFee: 10 })

const { data:res1 } = await EDIT (Active)
expect(res1).to.contain({ TotalPrice: 729, BookingFee: 10 })
expect(res1).to.containCoerced({ TotalPrice: 729, BookingFee: 10 })

// Change the Travel's dates to avoid validation errors
const today = new Date().toISOString().split('T')[0]
Expand All @@ -218,15 +233,15 @@ describe('Basic OData', () => {
await PATCH (Draft, { EndDate: tomorrow })

const { data:res2 } = await POST (`${Draft}/TravelService.deductDiscount`, { percent: 50 })
expect(res2).to.contain({ TotalPrice: 724, BookingFee: 5 })
expect(res2).to.containCoerced({ TotalPrice: 724, BookingFee: 5 })

const { data:res3 } = await GET (Draft)
expect(res3).to.contain({ TotalPrice: 724, BookingFee: 5 })
expect(res3).to.containCoerced({ TotalPrice: 724, BookingFee: 5 })

await SAVE (Draft)

const { data:res4 } = await GET (Active)
expect(res4).to.contain({ TotalPrice: 724, BookingFee: 5 })
expect(res4).to.containCoerced({ TotalPrice: 724, BookingFee: 5 })
})

})
Loading