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

test(csi-1130): api and db lib tests #389

Merged
merged 3 commits into from
Jan 29, 2025
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
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"@mojaloop/event-sdk": "14.1.3",
"@mojaloop/inter-scheme-proxy-cache-lib": "2.3.1",
"@mojaloop/ml-number": "11.2.6",
"@mojaloop/ml-schema-transformer-lib": "^2.5.1",
"@mojaloop/ml-schema-transformer-lib": "^2.5.2",
"@mojaloop/sdk-standard-components": "^19.6.4",
"ajv": "8.17.1",
"ajv-keywords": "5.1.0",
Expand Down
15 changes: 8 additions & 7 deletions src/data/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ const LOCAL_ENUM = require('../lib/enum')
* Abstracts operations against the database
*/
class Database {
constructor (config, log) {
constructor (config, log, queryBuilder) {
this.config = config
this.log = log || logger.child({
context: this.constructor.name
})
this.queryBuilder = queryBuilder
}

/**
Expand All @@ -62,7 +63,7 @@ class Database {
* @returns {promise}
*/
async connect () {
this.queryBuilder = new Knex(this.config.database)
this.queryBuilder = this.queryBuilder || new Knex(this.config.database)

return this
}
Expand All @@ -72,14 +73,14 @@ class Database {
}

/**
* async utility for getting a transaction object from knex
*
* @returns {undefined}
*/
* async utility for getting a transaction object from knex
*
* @returns {undefined}
*/
async newTransaction () {
return new Promise((resolve, reject) => {
try {
this.queryBuilder.transaction(txn => {
return this.queryBuilder.transaction(txn => {
return resolve(txn)
})
} catch (err) {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/api/quotes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,18 @@ describe('POST /quotes API Tests -->', () => {
expect(spyErrorLog).toHaveBeenCalledTimes(1)
expect(spyErrorLog.mock.calls[0][0]).toContain(error.message)
})

it('should rethrow errors if metrics are disabled', async () => {
// Arrange
const error = new Error('Create Quote Test Error')
Producer.produceMessage = jest.fn(async () => { throw error })

const mockRequest = mocks.mockHttpRequest()
const { handler } = mocks.createMockHapiHandler()
mockRequest.server.app.config.instrumentationMetricsDisabled = true

// Act / Assert
await expect(() => quotesApi.post(mockContext, mockRequest, handler))
.rejects.toThrowError(error.message)
})
})
26 changes: 26 additions & 0 deletions test/unit/api/quotes/{id}.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ describe('/quotes/{id} API Tests -->', () => {
expect(spyErrorLog).toHaveBeenCalledTimes(1)
expect(spyErrorLog.mock.calls[0][0]).toContain(error.message)
})

it('should rethrow error if mertics is diabled', async () => {
// Arrange
const error = new Error('Get Quote Test Error')
Producer.produceMessage = jest.fn(async () => { throw error })
const mockRequest = mocks.mockHttpRequest()
const { handler } = mocks.createMockHapiHandler()
mockRequest.server.app.config.instrumentationMetricsDisabled = true

// Act
await expect(() => quotesApi.get(mockContext, mockRequest, handler))
.rejects.toThrowError(error.message)
})
})

describe('PUT /quotes/{id} Endpoint Tests', () => {
Expand Down Expand Up @@ -197,6 +210,19 @@ describe('/quotes/{id} API Tests -->', () => {
expect(spyErrorLog).toHaveBeenCalledTimes(1)
expect(spyErrorLog.mock.calls[0][0]).toContain(error.message)
})

it('should rethrow error if mertics is diabled', async () => {
// Arrange
const error = new Error('Put Quote Test Error')
Producer.produceMessage = jest.fn(async () => { throw error })
const mockRequest = mocks.mockHttpRequest()
const { handler } = mocks.createMockHapiHandler()
mockRequest.server.app.config.instrumentationMetricsDisabled = true

// Act
await expect(() => quotesApi.put(mockContext, mockRequest, handler))
.rejects.toThrowError(error.message)
})
})

describe('PUT /quotes/{id}/error API Tests -->', () => {
Expand Down
44 changes: 32 additions & 12 deletions test/unit/data/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ Logger.isDebugEnabled = jest.fn(() => true)
Logger.isErrorEnabled = jest.fn(() => true)
Logger.isInfoEnabled = jest.fn(() => true)

let database

/**
* @function mockKnexBuilder
* @description Stubs out a set of Knex calls in order
Expand Down Expand Up @@ -85,6 +83,8 @@ describe('/database', () => {
raw: jest.fn()
}

let database

describe('raw queries', () => {
const config = {}

Expand All @@ -103,18 +103,38 @@ describe('/database', () => {
expect(database.queryBuilder).not.toBeUndefined()
})

// describe('initializes a transaction', () => {
// it('returns a transaction in a promise', async () => {
// // Arrange
// mockKnex.transaction.mockReturnValueOnce('testTx')
describe('transaction', () => {
it('should resolve with a transaction', async () => {
const mockTransaction = {}
const queryBuilder = { transaction: jest.fn() }
queryBuilder.transaction.mockImplementation((callback) => {
callback(mockTransaction)
})
const database = new Database(config, Logger, queryBuilder)

const result = await database.newTransaction()

// // Act
// const result = await database.newTransaction()
expect(result).toBe(mockTransaction)
expect(queryBuilder.transaction).toHaveBeenCalled()
})

// // Assert
// expect(result).toBe('testTx')
// })
// })
it('should reject with an error', async () => {
const mockError = new Error('Transaction error')
const queryBuilder = { transaction: jest.fn() }
const mockTransaction = {}
queryBuilder.transaction.mockImplementation((callback) => {
callback(mockTransaction)
})
const database = new Database(config, Logger, queryBuilder)

queryBuilder.transaction.mockImplementation(() => {
throw mockError
})

await expect(database.newTransaction()).rejects.toThrow('Transaction error')
expect(queryBuilder.transaction).toHaveBeenCalled()
})
})

describe('isConnected', () => {
it('returns true when connected', async () => {
Expand Down