UNCLASSIFIED - NO CUI

Skip to content
Snippets Groups Projects
Verified Commit 2ba38729 authored by Douglas Lagemann's avatar Douglas Lagemann
Browse files

Add dataLayer unit tests, tweak dataLayer to match changes in hello-express

parent ed466e87
No related branches found
No related tags found
1 merge request!12BULL-3221: Finish Express mvp
...@@ -18,7 +18,7 @@ exports.runMigrations = async () => { ...@@ -18,7 +18,7 @@ exports.runMigrations = async () => {
await migrations.run('init'); await migrations.run('init');
await migrations.run('migrate'); await migrations.run('migrate');
await client.end(); await client.end();
} };
/** /**
* Check connectivity with the database and report results. * Check connectivity with the database and report results.
...@@ -32,18 +32,18 @@ exports.healthCheck = async () => { ...@@ -32,18 +32,18 @@ exports.healthCheck = async () => {
await client.end(); await client.end();
} catch (err) { } catch (err) {
console.log(`Database connection error during health check: ${err}`); console.error(`Database connection error during health check: ${err}`);
return { return {
status: 500, healthy: false,
message: "Internal Server Error" message: "Unable to connect to database."
}; };
} }
return { return {
status: 200, healthy: true,
message: "Database connection succeeded. Health check passed." message: "Database connection succeeded. Health check passed."
}; };
} };
/** /**
* Return the version number stored in the database. * Return the version number stored in the database.
...@@ -57,4 +57,4 @@ exports.getDbVersion = async () => { ...@@ -57,4 +57,4 @@ exports.getDbVersion = async () => {
await client.end(); await client.end();
return queryResult.rows[0].version_number; return queryResult.rows[0].version_number;
} };
const { Client } = require('pg')
const { CommandsRunner, PsqlDriver } = require('node-db-migration');
const { getDbVersion, healthCheck, runMigrations } = require('./dataLayer');
jest.mock('pg');
jest.mock('node-db-migration');
afterEach(() => {
jest.restoreAllMocks();
});
describe('Data', () => {
describe('runMigrations', () => {
it('connects to the database and runs migrations', async () => {
await runMigrations();
// Make sure a client was constructed with a postgres connection string
expect(Client).toHaveBeenCalled();
expect(Client.mock.calls[0][0].connectionString).toContain('postgresql://');
// Assert that the client connects, executes the expected migration commands, and disconnects.
const mockClientInstance = Client.mock.instances[0];
expect(mockClientInstance.connect).toHaveBeenCalled();
expect(PsqlDriver).toHaveBeenCalledWith(mockClientInstance);
const mockPsqlDriverInstance = PsqlDriver.mock.instances[0];
expect(CommandsRunner).toHaveBeenCalledWith({
driver: mockPsqlDriverInstance,
directoryWithScripts: expect.any(String)
});
const mockCommandsRunnerInstance = CommandsRunner.mock.instances[0];
expect(mockCommandsRunnerInstance.run).toHaveBeenCalledTimes(2);
expect(mockCommandsRunnerInstance.run).toHaveBeenCalledWith('init');
expect(mockCommandsRunnerInstance.run).toHaveBeenCalledWith('migrate');
expect(mockClientInstance.end).toHaveBeenCalled();
});
});
describe('healthCheck', () => {
it('returns healthy if database connection succeeds', async () => {
const result = await healthCheck();
expect(Client).toHaveBeenCalled();
expect(Client.mock.calls[0][0].connectionString).toContain('postgresql://');
const mockClientInstance = Client.mock.instances[0];
expect(mockClientInstance.connect).toHaveBeenCalled();
expect(mockClientInstance.end).toHaveBeenCalled();
expect(result).toEqual({
healthy: true,
message: "Database connection succeeded. Health check passed."
});
});
it('returns unhealthy if database connection fails', async () => {
Client.mockImplementation(() => {
return {
connect: () => {
throw new Error("Boom!");
}
}
});
jest.spyOn(console, 'error').mockReturnValue();
const result = await healthCheck();
expect(result).toEqual({
healthy: false,
message: "Unable to connect to database."
});
expect(console.error).toHaveBeenCalled();
});
});
describe('getDbVersion', () => {
it('queries the database for the version and returns the result', async () => {
const mockConnect = jest.fn();
const mockQuery = jest.fn().mockResolvedValue({ rows: [ { version_number: 41 } ] });
const mockEnd = jest.fn();
Client.mockImplementation(() => {
return {
connect: mockConnect,
query: mockQuery,
end: mockEnd
}
});
const result = await getDbVersion();
expect(result).toEqual(41);
expect(Client).toHaveBeenCalled();
expect(Client.mock.calls[0][0].connectionString).toContain('postgresql://');
expect(mockConnect).toHaveBeenCalledTimes(1);
expect(mockQuery).toHaveBeenCalled();
expect(mockEnd).toHaveBeenCalled();
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment