Skip to content

🎨 Modernize test harness #74

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

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.config
.yardoc
Gemfile.lock
gemfiles/*.lock
InstalledFiles
_yardoc
coverage
Expand Down
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

# For default test setup with postgresql
# NOTE: If you want to run tests against MySQL do:
# docker compose --file docker-compose.mysql.yml up -d
# BUNDLE_GEMFILE=gemfiles/mysql.gemfile bundle install
# BUNDLE_GEMFILE=gemfiles/mysql.gemfile DB_ADAPTER=mysql bundle exec rake spec
# docker compose down
gem 'pg'
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,20 @@ The tests require a database. We've provided a simple `docker-compose.yml` that
it trivial to run the tests against PostgreSQL. Simply run `docker compose up -d`
followed by `rake spec`. When you're done, run `docker compose down` to stop the database.

In order to use another database, simply define the `DATABASE_URL` environment variable
In order to use another PostgreSQL database, simply define the `DATABASE_URL` environment variable
appropriately.

### Test against MySQL

If you want to run tests against MySQL do:

```shell
docker compose --file docker-compose.mysql.yml up -d
BUNDLE_GEMFILE=gemfiles/mysql.gemfile bundle install
BUNDLE_GEMFILE=gemfiles/mysql.gemfile DB_ADAPTER=mysql bundle exec rake spec
docker compose down
```

## License

`ActiveRecord::JSONValidator` is © 2013-2022 [Mirego](https://www.mirego.com) and may be freely distributed under the [New BSD license](https://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/activerecord_json_validator/blob/master/LICENSE.md) file.
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.2'

services:
mysql:
image: mysql:8.1
ports:
- 3306:3306
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_DATABASE=activerecord_json_validator_test
5 changes: 5 additions & 0 deletions gemfiles/mysql.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gemspec path: '../'

gem 'mysql2', "~> 0.5.5"
5 changes: 5 additions & 0 deletions gemfiles/postgresql.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gemspec path: '../'

gem 'pg'
13 changes: 11 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

require 'active_support/all'
require 'rspec'
require 'pg'
require 'support/database/database_adapter'

adapter = case ENV['DB_ADAPTER']
when 'mysql'
require 'support/database/mysql_adapter'
'mysql'
else
require 'pg'
require 'support/database/postgresql_adapter'
'postgresql'
end

require 'activerecord_json_validator'

Expand All @@ -17,7 +27,6 @@
config.include ModelMacros

config.before :each do
adapter = ENV['DB_ADAPTER'] || 'postgresql'
setup_database(adapter: adapter, database: 'activerecord_json_validator_test')
end
end
18 changes: 18 additions & 0 deletions spec/support/database/database_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'active_record'
class DatabaseAdapter
attr_accessor :database

def initialize(opts = {})
self.database = opts[:database]
end

def reset_database!
raise 'Define reset_database! in subclasses!'
end

def establish_connection!
raise 'Define establish_connection! in subclasses!'
end
end
18 changes: 18 additions & 0 deletions spec/support/database/mysql_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require_relative 'database_adapter'

class MysqlAdapter < DatabaseAdapter
def reset_database!
ActiveRecord::Base.connection.execute("SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_schema = '#{database}';")
end

def establish_connection!
ActiveRecord::Base.establish_connection(
ENV.fetch(
'DATABASE_URL',
'mysql2://root:@127.0.0.1:3306/activerecord_json_validator_test?pool=5'
)
)
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# frozen_string_literal: true

require 'pg'
require_relative 'database_adapter'

class PostgresqlAdapter < DatabaseAdapter
def reset_database!
ActiveRecord::Base.connection.execute('drop schema public cascade;')
ActiveRecord::Base.connection.execute('create schema public;')
end

def establish_connection!
ActiveRecord::Base.establish_connection(
ENV.fetch(
'DATABASE_URL',
'postgres://postgres@localhost/activerecord_json_validator_test'
)
)
end
end
11 changes: 0 additions & 11 deletions spec/support/macros/database/database_adapter.rb

This file was deleted.

9 changes: 0 additions & 9 deletions spec/support/macros/database/mysql_adapter.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/support/macros/database_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def run_migration(&block)
end

def setup_database(opts = {})
adapter = "#{opts[:adapter].capitalize}Adapter".constantize.new(database: opts[:database])
adapter = "#{opts[:adapter].classify}Adapter".constantize.new(database: opts[:database])
adapter.establish_connection!
adapter.reset_database!

Expand Down