Guest User

Untitled

a guest
Sep 27th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import * as knex from 'knex';
  2. import * as Docker from 'dockerode';
  3. import { sleep } from '../util/time';
  4. import { waitForDatabase } from '../test/waitForDatabase';
  5.  
  6. const docker = new Docker({ socketPath: '/var/run/docker.sock' });
  7.  
  8. let dbContainer: Docker.Container;
  9. let knexClient: knex;
  10.  
  11. beforeAll(async () => {
  12. dbContainer = await docker.createContainer({
  13. Image: 'pldin601/postgres:latest',
  14. Env: ['POSTGRES_USER=coins', 'POSTGRES_PASSWORD=coins', 'POSTGRES_DB=coins'],
  15. ExposedPorts: {
  16. '5432/tcp': {},
  17. },
  18. HostConfig: {
  19. PortBindings: { '5432/tcp': [{ HostPort: '' }] },
  20. },
  21. });
  22.  
  23. await dbContainer.start();
  24.  
  25. const { NetworkSettings: { Ports } } = await dbContainer.inspect();
  26.  
  27. const port = Ports['5432/tcp'][0].HostPort;
  28.  
  29. knexClient = knex({
  30. client: 'postgresql',
  31. connection: {
  32. host: '127.0.0.1',
  33. user: 'coins',
  34. password: 'coins',
  35. database: 'coins',
  36. port,
  37. },
  38. });
  39.  
  40. await waitForDatabase(knexClient);
  41. }, 15000);
  42.  
  43. test('Return correct time from database', async () => {
  44. const result = await knexClient.select(knexClient.raw('NOW() as time')).first();
  45. console.log(result['time']);
  46. });
  47.  
  48. afterAll(async () => {
  49. await dbContainer.kill();
  50. await dbContainer.remove();
  51. });
Add Comment
Please, Sign In to add comment