Skip to content

Commit

Permalink
Add some test cases (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
georg-schwarz committed Dec 15, 2020
1 parent e596683 commit 6ba97e1
Show file tree
Hide file tree
Showing 7 changed files with 5,421 additions and 870 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
max_line_length = 120

[*.md]
max_line_length = off
trim_trailing_whitespace = false
3 changes: 2 additions & 1 deletion .github/workflows/pg-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npm ci
- run: npm run lint-ci
- run: npm run test
- run: npm run build
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['./src', './test']
}
6,164 changes: 5,305 additions & 859 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"/dist"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint src --fix",
"lint-ci": "eslint src --max-warnings=0",
"test": "jest --colors --verbose --detectOpenHandles",
"lint": "eslint src test --fix",
"lint-ci": "eslint src test --max-warnings=0",
"build": "tsc"
},
"dependencies": {
Expand All @@ -19,17 +19,19 @@
"@types/pg": "7.x"
},
"devDependencies": {
"@types/node": "^14.14.7",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"eslint": "^7.11.0",
"@types/node": "^14.14.13",
"@typescript-eslint/eslint-plugin": "^4.10.0",
"@typescript-eslint/parser": "^4.10.0",
"eslint": "^7.15.0",
"eslint-config-standard": "^14.1.1",
"eslint-config-standard-with-typescript": "19.0.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"typescript": "^4.0.5"
"jest": "^26.6.3",
"ts-jest": "^26.4.1",
"typescript": "^4.1.3"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
Expand Down
8 changes: 6 additions & 2 deletions src/postgresRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ export class PostgresRepository {
public async executeQuery (query: string, args: unknown[]): Promise<QueryResult> {
try {
const resultSet = await this.connectionPool.query(query, args)
console.debug(`[Query] "${query}" with values ${stringifiers.stringifyArray(args)}` +
console.debug(`[Query] "${query}" with values ${stringifiers.stringifyArray(args)} ` +
`led to ${resultSet.rowCount} results`)
return resultSet
} catch (error) {
console.error(`[Query] "${query}" with values ${stringifiers.stringifyArray(args)} failed: `, error)
console.error(`[Query] "${query}" with values ${stringifiers.stringifyArray(args)} failed:`, error)
throw error
}
}

public async close (): Promise<void> {
await this.connectionPool.end()
}
}
78 changes: 78 additions & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-env jest */
const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const { sleep } = require('@jvalue/node-dry-basics')

const { PostgresRepository } = require('../src/postgresRepository')

const CONTAINER_NAME = 'node-dry-pg-test-database'
const DATABASE_PORT = 5432
const DATABASE_USER = 'node-dry-pg-test'
const DATABASE_PASSWORD = 'test-pwd'
const TEST_TIMEOUT = 30000

const DB_CONNECTION_RETRIES = 10
const DB_CONNECTION_BACKOFF = 2000
const DB_STARTUP_TIME = 5000

const POOL_CONFIG = {
host: 'localhost',
port: DATABASE_PORT,
user: DATABASE_USER,
password: DATABASE_PASSWORD,
database: DATABASE_USER
}

async function startDatabase () {
await exec(`docker run -p ${DATABASE_PORT}:5432 --name ${CONTAINER_NAME} -d ` +
`-e POSTGRES_USER=${DATABASE_USER} -e POSTGRES_PASSWORD=${DATABASE_PASSWORD} ` +
'postgres:13-alpine')
}

describe('node-dry-pg query test', () => {
let postgresRepository

beforeEach(() => {
postgresRepository = new PostgresRepository(POOL_CONFIG)
})

afterEach(async () => {
await postgresRepository.close()
await exec(`docker stop ${CONTAINER_NAME}`)
await exec(`docker rm ${CONTAINER_NAME}`)
})

test('simple query', async () => {
await startDatabase()
await sleep(DB_STARTUP_TIME)

const result = await postgresRepository.executeQuery('SELECT 1', [])
expect(result).toBeDefined()
expect(result.rowCount).toEqual(1)
}, TEST_TIMEOUT)

test('waitsForConnection', async () => {
const waitForConnectionPromise = postgresRepository.waitForConnection(DB_CONNECTION_RETRIES, DB_CONNECTION_BACKOFF)
await startDatabase()

await waitForConnectionPromise

const result = await postgresRepository.executeQuery('SELECT 1', [])
expect(result).toBeDefined()
expect(result.rowCount).toEqual(1)
}, TEST_TIMEOUT)

test('handles connection loss', async () => {
await startDatabase()
await postgresRepository.waitForConnection(DB_CONNECTION_RETRIES, DB_CONNECTION_BACKOFF)

await exec(`docker stop ${CONTAINER_NAME}`)
await exec(`docker start ${CONTAINER_NAME}`)

await sleep(DB_STARTUP_TIME)

const result = await postgresRepository.executeQuery('SELECT 1', [])
expect(result).toBeDefined()
expect(result.rowCount).toEqual(1)
}, TEST_TIMEOUT)
})

0 comments on commit 6ba97e1

Please sign in to comment.