Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specs to be run with Docker #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,21 @@ I hope one day we will cover all the features of PG here !

### Running Tests

In order to run the test suite, you will need to have the PostgresSQL service locally available via a socket for access with psql. psql will attempt to use the 'postgres' user to create the test database. If you are working with a newly installed database that may not have the postgres user, this can be created with `createuser -s postgres`.
#### With local PostgreSQL
To run the test suite with local PostgreSQL, you will need to have the PostgresSQL service locally available via a socket for access with psql. psql will attempt to use the 'postgres' user to create the test database. If you are working with a newly installed database that may not have the postgres user, this can be created with `createuser -s postgres`.

Simply run crystal spec [--flags]

#### Using Docker
To run the test suite with Docker, you will need to have Docker installed, then start the postgreSQL service container with the following command

`docker run --name clear_db -e POSTGRES_PASSWORD=password -h localhost -p 5432:5432 -d postgres`

Then simply load ENV=docker when running crystal spec [--flags]

`ENV=docker crystal spec [--flags]`

You can also specify the name of the container by loading it into `DB_NAME` environment variable

## Contributors ✨

Expand Down
31 changes: 23 additions & 8 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@ class ::Crypto::Bcrypt::Password
DEFAULT_COST = 4
end

def initdb
system("echo \"DROP DATABASE IF EXISTS clear_spec;\" | psql -U postgres 1>/dev/null")
system("echo \"CREATE DATABASE clear_spec;\" | psql -U postgres 1>/dev/null")
DB_ENV = ENV.fetch("ENV", "local")

DB_NAME = ENV.fetch("DB_NAME", "clear_db")

system("echo \"DROP DATABASE IF EXISTS clear_secondary_spec;\" | psql -U postgres 1>/dev/null")
system("echo \"CREATE DATABASE clear_secondary_spec;\" | psql -U postgres 1>/dev/null")
system("echo \"CREATE TABLE models_post_stats (id serial PRIMARY KEY, post_id INTEGER);\" | psql -U postgres clear_secondary_spec 1>/dev/null")
def initdb
if DB_ENV == "local"
system("echo \"DROP DATABASE IF EXISTS clear_spec;\" | psql -U postgres 1>/dev/null")
system("echo \"CREATE DATABASE clear_spec;\" | psql -U postgres 1>/dev/null")

system("echo \"DROP DATABASE IF EXISTS clear_secondary_spec;\" | psql -U postgres 1>/dev/null")
system("echo \"CREATE DATABASE clear_secondary_spec;\" | psql -U postgres 1>/dev/null")
system("echo \"CREATE TABLE models_post_stats (id serial PRIMARY KEY, post_id INTEGER);\" | psql -U postgres clear_secondary_spec 1>/dev/null")
elsif DB_ENV == "docker"
system("docker exec -it #{DB_NAME} psql -c 'DROP DATABASE IF EXISTS clear_spec;' -U postgres")
system("docker exec -it #{DB_NAME} psql -c 'CREATE DATABASE clear_spec;' -U postgres")

system("docker exec -it #{DB_NAME} psql -c 'DROP DATABASE IF EXISTS clear_secondary_spec;' -U postgres")
system("docker exec -it #{DB_NAME} psql -c 'CREATE DATABASE clear_secondary_spec;' -U postgres")
system("docker exec -it #{DB_NAME} psql -c 'CREATE TABLE models_post_stats (id serial PRIMARY KEY, post_id INTEGER);' -U postgres clear_secondary_spec")
else
raise ArgumentError.new("Invalid ENV value")
end

Clear::SQL.init("postgres://postgres@localhost/clear_spec?retry_attempts=1&retry_delay=1&initial_pool_size=5")
Clear::SQL.init("secondary", "postgres://postgres@localhost/clear_secondary_spec?retry_attempts=1&retry_delay=1&initial_pool_size=5")
Clear::SQL.init("postgres://postgres:password@localhost:5432/clear_spec?retry_attempts=1&retry_delay=1&initial_pool_size=5")
Clear::SQL.init("secondary", "postgres://postgres:password@localhost:5432/clear_secondary_spec?retry_attempts=1&retry_delay=1&initial_pool_size=5")
end

Spec.before_suite do
Expand Down