Skip to content

Commit

Permalink
updated tests to use Promis.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
nikiwycherley committed Nov 13, 2023
1 parent bf4d1c4 commit 1845058
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 180 deletions.
56 changes: 21 additions & 35 deletions test/getMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ lab.experiment('getMessage', () => {
}
}
// mock service
service.getMessage = (query, params) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
getmessage: {
alert: '<alert xmlns="urn:oasis:names:tc:emergency:cap:1.2">test</alert>'
}
}]
})
})
}
service.getMessage = (query, params) => Promise.resolve({
rows: [{
getmessage: {
alert: '<alert xmlns="urn:oasis:names:tc:emergency:cap:1.2">test</alert>'
}
}]
})
})

lab.test('Correct data test', async () => {
Expand All @@ -36,35 +32,28 @@ lab.experiment('getMessage', () => {
})

lab.test('No data found test', async () => {
service.getMessage = (query, params) => {
return new Promise((resolve, reject) => {
resolve({
rows: []
})
})
}
service.getMessage = (query, params) => Promise.resolve({
rows: []
})

const err = await Code.expect(getMessage(event)).to.reject()
Code.expect(err.message).to.equal('No message found')
})

lab.test('Incorrect database rows object', async () => {
service.getMessage = (query, params) => {
return new Promise((resolve, reject) => {
resolve({
rows: 1
})
})
}
service.getMessage = (query, params) => Promise.resolve({
rows: 1
})

const err = await Code.expect(getMessage(event)).to.reject()
Code.expect(err.message).to.equal('No message found')
})

lab.test('Incorrect database rows object', async () => {
service.getMessage = (query, params) => {
return new Promise((resolve, reject) => {
resolve({})
})
}
service.getMessage = (query, params) => Promise.resolve({
rows: {}
})

const err = await Code.expect(getMessage(event)).to.reject()
Code.expect(err.message).to.equal('No message found')
})
Expand All @@ -80,11 +69,8 @@ lab.experiment('getMessage', () => {
})

lab.test('Error test', async () => {
service.getMessage = (query, params) => {
return new Promise((resolve, reject) => {
reject(new Error('test error'))
})
}
service.getMessage = (query, params) => Promise.reject(new Error('test error'))

const err = await Code.expect(getMessage(event)).to.reject()
Code.expect(err.message).to.equal('test error')
})
Expand Down
240 changes: 95 additions & 145 deletions test/processMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,24 @@ lab.experiment('processMessage', () => {
resolve()
})
}
service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2'
}]
})
})
}
service.getLastMessage = (id) => Promise.resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2'
}]
})
})

lab.test('Correct data test with no previous alert on test', async () => {
service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: []
})
})
}
service.putMessage = (query) => {
return new Promise((resolve, reject) => {
Code.expect(query.values[2]).to.be.empty()
Code.expect(query.values[1]).to.equal('Alert')
resolve()
})
}
service.getLastMessage = (id) => Promise.resolve({
rows: []
})

service.putMessage = (query) => Promise.resolve().then(() => {
Code.expect(query.values[2]).to.be.empty()
Code.expect(query.values[1]).to.equal('Alert')
})

const ret = await processMessage(capAlert)
Code.expect(ret.statusCode).to.equal(200)
Code.expect(ret.body.identifier).to.equal('4eb3b7350ab7aa443650fc9351f02940E')
Expand Down Expand Up @@ -108,27 +99,20 @@ lab.experiment('processMessage', () => {
const config = require('../config/config.json')
config.aws.stage = 'ea'

service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
expires: tomorrow,
sent: yesterday
}]
})
})
}

service.putMessage = (query) => {
return new Promise((resolve, reject) => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
Code.expect(query.values[1]).to.equal('Update')
resolve()
})
}
service.getLastMessage = (id) => Promise.resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
expires: tomorrow,
sent: yesterday
}]
})

service.putMessage = (query) => Promise.resolve().then(() => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
Code.expect(query.values[1]).to.equal('Update')
})

const ret = await processMessage(capAlert)
Code.expect(ret.statusCode).to.equal(200)
Expand All @@ -144,28 +128,21 @@ lab.experiment('processMessage', () => {
const config = require('../config/config.json')
config.aws.stage = 'ea'

service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
expires: tomorrow,
sent: yesterday,
references: yesterday.toISOString()
}]
})
})
}

service.putMessage = (query) => {
return new Promise((resolve, reject) => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
Code.expect(query.values[1]).to.equal('Update')
resolve()
})
}
service.getLastMessage = (id) => Promise.resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
expires: tomorrow,
sent: yesterday,
references: yesterday.toISOString()
}]
})

service.putMessage = (query) => Promise.resolve().then(() => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
Code.expect(query.values[1]).to.equal('Update')
})

const ret = await processMessage(capAlert)
Code.expect(ret.statusCode).to.equal(200)
Expand All @@ -180,28 +157,22 @@ lab.experiment('processMessage', () => {
lab.test('Correct alert data test with an active on production', async () => {
const config = require('../config/config.json')
config.aws.stage = 'ea'
service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
sent: yesterday,
expires: tomorrow,
msgType: 'Alert'
}]
})
})
}

service.putMessage = (query) => {
return new Promise((resolve, reject) => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[1]).to.equal('Update')
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
resolve()
})
}
service.getLastMessage = (id) => Promise.resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
sent: yesterday,
expires: tomorrow,
msgType: 'Alert'
}]
})

service.putMessage = (query) => Promise.resolve().then(() => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[1]).to.equal('Update')
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
})

const ret = await processMessage(capAlert)
Code.expect(ret.statusCode).to.equal(200)
Expand All @@ -217,28 +188,21 @@ lab.experiment('processMessage', () => {
const config = require('../config/config.json')
config.aws.stage = 'ea'

service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
sent: yesterday,
expires: tomorrow,
msgType: 'Alert'
}]
})
})
}

service.putMessage = (query) => {
return new Promise((resolve, reject) => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[1]).to.equal('Update')
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
resolve()
})
}
service.getLastMessage = (id) => Promise.resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
sent: yesterday,
expires: tomorrow,
msgType: 'Alert'
}]
})

service.putMessage = (query) => Promise.resolve().then(() => {
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[1]).to.equal('Update')
Code.expect(query.values[2]).to.contain(yesterday.toISOString().substring(0, yesterday.toISOString().length - 5))
})

const ret = await processMessage(capUpdate)
Code.expect(ret.statusCode).to.equal(200)
Expand All @@ -259,53 +223,39 @@ lab.experiment('processMessage', () => {
})

lab.test('Database error', async () => {
service.putMessage = (query) => {
return new Promise((resolve, reject) => {
reject(new Error('unit test error'))
})
}
service.putMessage = (query) => Promise.reject(new Error('unit test error'))
const err = await Code.expect(processMessage(capAlert)).to.reject()
Code.expect(err.message).to.equal('unit test error')
})

lab.test('Database error 2', async () => {
service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
reject(new Error('unit test error'))
})
}
service.getLastMessage = (id) => Promise.reject(new Error('unit test error'))

const err = await Code.expect(processMessage(capAlert)).to.reject()
Code.expect(err.message).to.equal('unit test error')
})

lab.test('Correct data test for processMessage where previous message is active and has reference', async () => {
const config = require('../config/config.json')
config.aws.stage = 'ea'

service.getLastMessage = (id) => {
return new Promise((resolve, reject) => {
resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
expires: tomorrow,
sent: yesterday,
references: 'Previous_Active_Message'
}]
})
})
}

service.putMessage = (query) => {
return new Promise((resolve, reject) => {
const lastDate = moment(yesterday).utc().format('YYYY-MM-DDTHH:mm:ssZ')
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[1]).to.equal('Update')
Code.expect(query.values[2]).to.contain(`Previous_Active_Message www.gov.uk/environment-agency,4eb3b7350ab7aa443650fc9351f2,${lastDate}`)
Code.expect(query.values[2]).to.not.contain('00:00+00:00')
resolve()
})
}
// Replace the trivial promise with Promise.resolve
service.getLastMessage = (id) => Promise.resolve({
rows: [{
id: '51',
identifier: '4eb3b7350ab7aa443650fc9351f2',
expires: tomorrow,
sent: yesterday,
references: 'Previous_Active_Message'
}]
})

service.putMessage = (query) => Promise.resolve().then(() => {
const lastDate = moment(yesterday).utc().format('YYYY-MM-DDTHH:mm:ssZ')
Code.expect(query.values[2]).to.not.be.empty()
Code.expect(query.values[1]).to.equal('Update')
Code.expect(query.values[2]).to.contain(`Previous_Active_Message www.gov.uk/environment-agency,4eb3b7350ab7aa443650fc9351f2,${lastDate}`)
Code.expect(query.values[2]).to.not.contain('00:00+00:00')
})

const ret = await processMessage(capAlert)
Code.expect(ret.statusCode).to.equal(200)
Expand Down

0 comments on commit 1845058

Please sign in to comment.