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

Add docker setup for more consistent testing #77

Merged
merged 11 commits into from
Oct 31, 2023
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM ruby:3.2.2

WORKDIR /app

ADD . /app

# Install nodejs
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client \
&& apt-get clean

# Update bundler to correct version
RUN gem install bundler:2.4.3

# Install any needed packages
RUN bundle install
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ source "https://rubygems.org"
#
gem 'sdoc', :git => 'https://github.com/pond/sdoc.git', :branch => 'master'

group :test do
gem 'pry'
gem 'pry-nav'
end

gemspec
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ GEM
tzinfo (~> 2.0)
builder (3.2.4)
byebug (11.1.3)
coderay (1.1.3)
concurrent-ruby (1.2.2)
crass (1.0.6)
date (3.3.3)
Expand Down Expand Up @@ -121,6 +122,11 @@ GEM
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
pg (1.4.6)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-nav (1.0.0)
pry (>= 0.9.10, < 0.15)
psych (5.1.0)
stringio
racc (1.6.2)
Expand Down Expand Up @@ -198,6 +204,8 @@ DEPENDENCIES
byebug (~> 11.1)
doggo (~> 1.3)
pg (~> 1.4)
pry
pry-nav
rake (~> 13.0)
rdoc (~> 6.5)
rspec-rails (~> 6.0)
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,11 @@ bundle install

### Tests

For testing, two main options are available.
The first option is running the project locally. This is also the recommended way, as running the tests on a variety of setups and platforms increases the chance of finding platform- or setup-specific bugs.
The second option is utilising the existing Docker Compose setup provided in the project. You can use this if getting the project to work locally is hard or not feasible.

#### Testing on your machine
You will need to have PostgreSQL running. This database is chosen for tests to prove case-insensitive behaviour via detection of ILIKE in generated queries. Using SQLite would have resulted in a more conceptually self-contained test suite, but SQLite is case-insensitive by default and uses "LIKE" either way, making it hard to "see" if the query system is doing the right thing.

After `bundle install` and with PostgreSQL up, set up the test database with:
Expand All @@ -639,6 +644,16 @@ bundle exec rspec

You can get an idea of arising test coverage by opening `coverage/index.html` in your preferred web browser.

#### Testing with Docker (Compose)

In order to be able to utilise the Docker Compose, you will need to have Docker installed, with the Compose plugin. For an easy install of Docker, with a GUI and the Compose plugin preinstalled, please refer to [Docker Desktop](https://www.docker.com/products/docker-desktop/).

In order to set up the image, simply run `docker compose build` in a terminal of your choice, in the root of this project. This will download the required image and install the required libraries. After this is complete, running the tests is as easy as running the command `docker compose up test`.

As said in the previous section, test coverage may be analysed using `coverage/index.html` after running the project.

You can also open a raw terminal in this test container by running `docker run --rm test sh`. For more Compose commands, please refer to [the docker compose reference manual](https://docs.docker.com/compose/reference/).

### Internal documentation

Locally generated RDoc HTML seems to contain a more comprehensive and inter-linked set of pages than those available from `rubydoc.info`. You can (re)generate the internal [`rdoc` documentation](https://ruby-doc.org/stdlib-2.4.1/libdoc/rdoc/rdoc/RDoc/Markup.html#label-Supported+Formats) with:
Expand Down
11 changes: 11 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh
set -e

# go to dummy app (where the bundle and rake files are)
cd ./spec/apps/dummy
echo "== Creating fresh database... =="
bundle exec rake db:reset
cd ../../.. # nagivate back to root of project

echo "== Running tests... =="
bundle exec rspec
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '2.3'
services:
postgresql:
image: postgres:15
ports:
- 5432
environment:
- POSTGRES_PASSWORD=Sup3rSecr1tPasw8rd
- POSTGRES_USER=postgres
- PGDATA=/var/lib/postgresql/data/pgdata
volumes:
- postgresql:/var/lib/postgresql/data

test:
build: ./
volumes:
- .:/app
depends_on:
- postgresql
command: 'bin/test'
environment:
- RAILS_ENV=test
- DATABASE_URL=postgresql://postgres:Sup3rSecr1tPasw8rd@postgresql:5432/scimitar-test

volumes:
postgresql:
Empty file modified spec/apps/dummy/bin/bundle
100755 → 100644
Empty file.
Empty file modified spec/apps/dummy/bin/rails
100755 → 100644
Empty file.
Empty file modified spec/apps/dummy/bin/rake
100755 → 100644
Empty file.
Empty file modified spec/apps/dummy/bin/setup
100755 → 100644
Empty file.