Created
September 27, 2018 12:29
-
-
Save pyldin601/c3c95d074dfd91c777baaad7834b2a90 to your computer and use it in GitHub Desktop.
Test that embeds database and executes query on it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as knex from 'knex'; | |
import * as Docker from 'dockerode'; | |
import { sleep } from '../util/time'; | |
import { waitForDatabase } from '../test/waitForDatabase'; | |
const docker = new Docker({ socketPath: '/var/run/docker.sock' }); | |
let dbContainer: Docker.Container; | |
let knexClient: knex; | |
beforeAll(async () => { | |
dbContainer = await docker.createContainer({ | |
Image: 'pldin601/postgres:latest', | |
Env: ['POSTGRES_USER=coins', 'POSTGRES_PASSWORD=coins', 'POSTGRES_DB=coins'], | |
ExposedPorts: { | |
'5432/tcp': {}, | |
}, | |
HostConfig: { | |
PortBindings: { '5432/tcp': [{ HostPort: '' }] }, | |
}, | |
}); | |
await dbContainer.start(); | |
const { NetworkSettings: { Ports } } = await dbContainer.inspect(); | |
const port = Ports['5432/tcp'][0].HostPort; | |
knexClient = knex({ | |
client: 'postgresql', | |
connection: { | |
host: '127.0.0.1', | |
user: 'coins', | |
password: 'coins', | |
database: 'coins', | |
port, | |
}, | |
}); | |
await waitForDatabase(knexClient); | |
}, 15000); | |
test('Return correct time from database', async () => { | |
const result = await knexClient.select(knexClient.raw('NOW() as time')).first(); | |
console.log(result['time']); | |
}); | |
afterAll(async () => { | |
await dbContainer.kill(); | |
await dbContainer.remove(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment