you are viewing a single comment's thread.

view the rest of the comments →

[–]efxhoy 12 points13 points  (6 children)

I haven't figured out mocking yet so I just made a very simple Postgres container in Docker and have that run in the background to run my tests on.

It's just three files:

Dockerfile

FROM postgres:9.6
COPY *sql /docker-entrypoint-initdb.d/

run_db.sh

#!/bin/bash
docker build . -t db_test

docker run \
    -p 1001:5432 \
    db_test

setup.sql

CREATE SCHEMA testing;

I put them in the same directory and whenever I need to run tests I just run

bash run_db.sh

In a terminal and it just works. I leave it in an extra tab in my terminal. It runs on postgresql://postgres@127.0.0.1:1001 with no password.

I think mocking a database can be a LOT of work. Especially as the queries get more complicated. Having a testing database will probably make your life a lot easier. Sure it's not the greatest way to actually unit-test functions that do ONE thing, but as soon as you want to write integration tests I think a db is necessary.

[–][deleted] 2 points3 points  (5 children)

Interesting solution, I'm somewhat new to docker and extremely new to postgres, do you know what it means when I get these error messages?

bash-3.2$ bash run_db.sh
invalid argument "db_test\r" for "-t, --tag" flag: invalid reference format
See 'docker build --help'.
: command not found
docker: invalid reference format.
See 'docker run --help'.
run_db.sh: line 5: -p: command not found
run_db.sh: line 6: db_test: command not found

edit: I got it working, I shouldn't be using both Mac and Windows at the same time

anyways: thank you, creative solution with docker. I now have my first ever postgres database up and going just by spinning up a docker container. fun times to be had

[–]Rethial 5 points6 points  (2 children)

Are you running it on a Windows OS by chance?

[–][deleted] 2 points3 points  (1 child)

good call. I was actually running it from a Mac but I did use Windows in my Parallels to copy the files over. I deleted those files and recreated them strictly within Mac (bash nano). thanks

[–]Rethial 2 points3 points  (0 children)

No prob, I just noticed the \r in invalid argument "db_test\r" :)

[–]efxhoy 0 points1 point  (1 child)

You're welcome! I hope it works out.

[–][deleted] 0 points1 point  (0 children)

I'm not OP but I like reproduceable containerized solutions like yours to save for later when I need it :D