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

Adding logging to the db export service #227

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 3 additions & 7 deletions app/controllers/data/data.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ async function tearDown (_request, h) {
/**
* Triggers export of all relevant tables to CSV and then uploads them to S3
*/
async function dbExport (_request, _h) {
global.GlobalNotifier.omg('Starting db export service ')

async function dbExport (_request, h) {
try {
await DbExportService.go()

global.GlobalNotifier.omg('Finished export service')
return { status: 'successful' }
return h.response().code(204)
} catch (error) {
global.GlobalNotifier.omfg(`Error: ${error.message}`)
return { status: `Error: ${error.message}` }
return Boom.badImplementation(error.message)
}
}

Expand Down
3 changes: 0 additions & 3 deletions app/services/db-export/compress-files.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ const zlib = require('node:zlib')
*/
async function go (filePath) {
if (!fs.existsSync(filePath)) {
global.GlobalNotifier.omfg(`ERROR: ${filePath} did not successfully compress to gzip.`)

return false
}

await _compressFile(filePath)
global.GlobalNotifier.omg(`${filePath} successfully compressed to gzip.`)

return `${filePath}.gz`
}
Expand Down
3 changes: 0 additions & 3 deletions app/services/db-export/export-data-files.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ async function go (tableConvertedToCsv, tableName) {

try {
await fs.writeFile(filePath, tableConvertedToCsv)
global.GlobalNotifier.omg(`${tableName} exported successfully`)

return filePath
} catch (error) {
global.GlobalNotifier.omfg(`${tableName} Export request errored`, error)

return false
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/services/db-export/send-to-s3-bucket.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ async function _uploadToBucket (params, fileName) {

try {
await s3Client.send(command)
global.GlobalNotifier.omg(`The file ${fileName} was uploaded successfully`)

return true
} catch (error) {
global.GlobalNotifier.omfg(`ERROR uploading file: ${error.message}`)
return false
}
}
Expand Down
8 changes: 2 additions & 6 deletions test/controllers/data/data.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ describe('Data controller', () => {

it('displays the correct message', async () => {
const response = await server.inject(options)
const payload = JSON.parse(response.payload)

expect(response.statusCode).to.equal(200)
expect(payload.status).to.equal('successful')
expect(response.statusCode).to.equal(204)
})
})

Expand All @@ -89,10 +87,8 @@ describe('Data controller', () => {

it('displays the error message', async () => {
const response = await server.inject(options)
const payload = JSON.parse(response.payload)

expect(payload.status).to.equal('Error: Error')
expect(response.statusCode).to.equal(200)
expect(response.statusCode).to.equal(500)
})
})
})
Expand Down
14 changes: 0 additions & 14 deletions test/services/db-export/compress-files.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code
Expand All @@ -15,19 +14,8 @@ const fs = require('fs')
const CompressFilesService = require('../../../app/services/db-export/compress-files.service.js')

describe('Compress files service', () => {
let notifierStub
let filePath

beforeEach(() => {
notifierStub = { omg: Sinon.stub(), omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
delete global.GlobalNotifier
})

describe('when successful', () => {
beforeEach(() => {
filePath = 'test/fixtures/compress-files.service.csv'
Expand All @@ -43,7 +31,6 @@ describe('Compress files service', () => {

expect(result).to.equal(`${filePath}.gz`)
expect(fs.existsSync(`${filePath}.gz`)).to.equal(true)
expect(notifierStub.omg.calledWith(`${filePath} successfully compressed to gzip.`)).to.be.true()
})
})

Expand All @@ -57,7 +44,6 @@ describe('Compress files service', () => {

expect(result).to.equal(false)
expect(fs.existsSync((`${filePath}.gz`))).to.equal(false)
expect(notifierStub.omfg.calledWith(`ERROR: ${filePath} did not successfully compress to gzip.`)).to.be.true()
})
})
})
14 changes: 0 additions & 14 deletions test/services/db-export/export-data-files.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code
Expand Down Expand Up @@ -49,17 +48,6 @@ const csvValues = [

describe('Export data files service', () => {
const tableName = 'billing_charge_categories'
let notifierStub

beforeEach(() => {
notifierStub = { omg: Sinon.stub(), omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
delete global.GlobalNotifier
})

let filePath

Expand All @@ -82,7 +70,6 @@ describe('Export data files service', () => {

expect(fs.existsSync(filePath)).to.equal(true)
expect(returnedResult).to.equal('/tmp/billing_charge_categories.csv')
expect(notifierStub.omg.calledWith('billing_charge_categories exported successfully')).to.be.true()
})
})

Expand All @@ -92,7 +79,6 @@ describe('Export data files service', () => {
const result = await ExportDataFilesService.go(data, tableName)

expect(result).to.equal(false)
expect(notifierStub.omfg.calledWith('billing_charge_categories Export request errored')).to.be.true()
})
})
})
31 changes: 4 additions & 27 deletions test/services/db-export/send-to-s3-bucket.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const Sinon = require('sinon')
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const path = require('path')

// Things we need to stub
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3')

Expand All @@ -19,26 +16,18 @@ const SendToS3BucketService = require('../../../app/services/db-export/send-to-s

describe('Send to S3 bucket service', () => {
let s3Stub
let notifierStub

beforeEach(() => {
notifierStub = { omg: Sinon.stub(), omfg: Sinon.stub() }
global.GlobalNotifier = notifierStub
})

afterEach(() => {
Sinon.restore()
delete global.GlobalNotifier
})

describe('when successful', () => {
beforeEach(() => {
// Stub the S3 Client's send method, which is used to run the 'put object' command
s3Stub = Sinon.stub(S3Client.prototype, 'send')
})

afterEach(() => {
Sinon.restore()
})

const filePath = 'test/fixtures/compress-files.service.csv'
const fileName = path.basename(filePath)

it('uploads a file to the S3 bucket', async () => {
await SendToS3BucketService.go(filePath)
Expand All @@ -56,12 +45,6 @@ describe('Send to S3 bucket service', () => {

expect(result).to.equal(true)
})

it('logs a success message', async () => {
await SendToS3BucketService.go(filePath)

expect(notifierStub.omg.calledWith(`The file ${fileName} was uploaded successfully`)).to.be.true()
})
})

describe('when unsuccessful', () => {
Expand Down Expand Up @@ -89,12 +72,6 @@ describe('Send to S3 bucket service', () => {

expect(result).to.equal(false)
})

it('logs the error', async () => {
await SendToS3BucketService.go(filePath)

expect(notifierStub.omfg.calledWith('ERROR uploading file: Error')).to.be.true()
})
})
})
})