Skip to content

Commit

Permalink
chore: ensure test db is safely/freshly created
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Jan 26, 2024
1 parent 07888a9 commit 9e35f97
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .docker/scripts/test_mysql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ bundle update;
RUBY_VERSION=$(ruby -v);

echo "Testing With MySQL and Ruby $RUBY_VERSION";
export DATABASE_URL="mysql2://test:password@mysql:3306/test"
bundle exec rake test;
export DATABASE_URL="mysql2://test:password@mysql:3306/jr_test"
bundle exec rake;
4 changes: 2 additions & 2 deletions .docker/scripts/test_postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ bundle update;
RUBY_VERSION=$(ruby -v);

echo "Testing With PostgreSQL and Ruby $RUBY_VERSION";
export DATABASE_URL="postgresql://postgres:password@postgres:5432/test"
bundle exec rake test;
export DATABASE_URL="postgresql://postgres:password@postgres:5432/jr_test"
bundle exec rake;
4 changes: 2 additions & 2 deletions .docker/scripts/test_sqlite
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ bundle update;
RUBY_VERSION=$(ruby -v);

echo "Testing With SQLite and Ruby $RUBY_VERSION";
export DATABASE_URL="sqlite3:test_db"
bundle exec rake test;
export DATABASE_URL="sqlite3:jr_test"
bundle exec rake;
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- [ ] I've checked to ensure there aren't other open [Pull Requests](https://github.com/cerebris/jsonapi-resources/pulls) for the same update/change.
- [ ] I've submitted a [ticket](https://github.com/cerebris/jsonapi-resources/issues) for my issue if one did not already exist.
- [ ] My submission passes all tests. (Please run the full test suite locally to cut down on noise from travis failures.)
- [ ] My submission passes all tests. (Please run the full test suite locally to cut down on noise from CI failures.)
- [ ] I've used Github [auto-closing keywords](https://help.github.com/articles/closing-issues-via-commit-messages/) in the commit message or the description.
- [ ] I've added/updated tests for this change.

Expand All @@ -23,4 +23,4 @@

### Reviewer Checklist:
- [ ] Maintains compliance with JSON:API
- [ ] Adequate test coverage exists to prevent regressions
- [ ] Adequate test coverage exists to prevent regressions
8 changes: 4 additions & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ jobs:
- '7.0'
- '6.1'
database_url:
- sqlite3:test_db
- postgresql://postgres:password@localhost:5432/test
- mysql2://root:root@127.0.0.1:3306/test
- sqlite3:jr_test
- postgresql://postgres:password@localhost:5432/jr_test
- mysql2://root:root@127.0.0.1:3306/jr_test
# exclude:
# - ruby: '3.1'
# rails: '6.0'
Expand All @@ -65,4 +65,4 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run tests
run: bundle exec rake test
run: bundle exec rake
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ coverage
test/log
test_db
test_db-journal
jr_test
jr_test-journal
.idea
*.iml
*.override.yml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# JSONAPI::Resources [![Gem Version](https://badge.fury.io/rb/jsonapi-resources.svg)](https://badge.fury.io/rb/jsonapi-resources) [![Build Status](https://secure.travis-ci.org/cerebris/jsonapi-resources.svg?branch=master)](http://travis-ci.org/cerebris/jsonapi-resources) [![Code Climate](https://codeclimate.com/github/cerebris/jsonapi-resources/badges/gpa.svg)](https://codeclimate.com/github/cerebris/jsonapi-resources)
# JSONAPI::Resources [![Gem Version](https://badge.fury.io/rb/jsonapi-resources.svg)](https://badge.fury.io/rb/jsonapi-resources) [![Code Climate](https://codeclimate.com/github/cerebris/jsonapi-resources/badges/gpa.svg)](https://codeclimate.com/github/cerebris/jsonapi-resources)

[![Join the chat at https://gitter.im/cerebris/jsonapi-resources](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cerebris/jsonapi-resources?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Expand Down
38 changes: 32 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,41 @@ Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
end

task default: [:test]

desc 'Run benchmarks'
namespace :test do

desc 'prepare test database'
task :prepare_database do
database_url = ENV["DATABASE_URL"]
next if database_url.nil? || database_url.empty?
require "active_record/railtie"
connection_class = ActiveRecord::Base
connection_class.establish_connection(database_url)
database_config = connection_class
.connection_db_config
.configuration_hash
database_adapter = database_config.fetch(:adapter)
database_name = database_config.fetch(:database)
database_username = database_config[:username]
case database_adapter
when :postgresql, "postgresql"
sh "psql -c 'DROP DATABASE IF EXISTS #{database_name};' -U #{database_username};"
sh "psql -c 'CREATE DATABASE #{database_name};' -U #{database_username};"
when :mysql2, "mysql2"
sh "mysql -c 'DROP DATABASE IF EXISTS #{database_name};' -U #{database_username};"
sh "mysql -c 'CREATE DATABASE #{database_name};' -U #{database_username};"
else
nil # nothing to do for this database_adapter
end
puts "Preparing to run #{database_adapter} tests"
connection_class.connection.disconnect!
end

desc 'Run benchmarks'
Rake::TestTask.new(:benchmark) do |t|
t.pattern = 'test/benchmark/*_benchmark.rb'
end
end

desc 'Test bug report template'
namespace :test do
desc 'Test bug report template'
namespace :bug_report_template do
task :rails_5 do
puts 'Test bug report templates'
Expand All @@ -33,3 +57,5 @@ namespace :test do
end
end
end

task default: [:"test:prepare_database", :test]
16 changes: 8 additions & 8 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
require 'database_cleaner'

# To run tests with coverage:
# COVERAGE=true bundle exec rake test
# COVERAGE=true bundle exec rake

# To test on a specific rails version use this:
# export RAILS_VERSION=5.2.4.4; bundle update; bundle exec rake test
# export RAILS_VERSION=6.0.3.4; bundle update; bundle exec rake test
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake test
# export RAILS_VERSION=5.2.4.4; bundle update; bundle exec rake
# export RAILS_VERSION=6.0.3.4; bundle update; bundle exec rake
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake

# We are no longer having Travis test Rails 4.2.11., but you can try it with:
# export RAILS_VERSION=4.2.11; bundle update rails; bundle exec rake test
# We are no longer having CI test Rails 4.2.11., but you can try it with:
# export RAILS_VERSION=4.2.11; bundle update rails; bundle exec rake

# To Switch rails versions and run a particular test order:
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake TESTOPTS="--seed=39333" test
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake TESTOPTS="--seed=39333"

if ENV['COVERAGE']
SimpleCov.start do
end
end

ENV['DATABASE_URL'] ||= "sqlite3:test_db"
ENV['DATABASE_URL'] ||= "sqlite3:jr_test"

require 'active_record/railtie'
require 'minitest/mock'
Expand Down

0 comments on commit 9e35f97

Please sign in to comment.