Skip to content

Commit 9e35f97

Browse files
committed
chore: ensure test db is safely/freshly created
1 parent 07888a9 commit 9e35f97

File tree

9 files changed

+55
-27
lines changed

9 files changed

+55
-27
lines changed

.docker/scripts/test_mysql

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ bundle update;
66
RUBY_VERSION=$(ruby -v);
77

88
echo "Testing With MySQL and Ruby $RUBY_VERSION";
9-
export DATABASE_URL="mysql2://test:password@mysql:3306/test"
10-
bundle exec rake test;
9+
export DATABASE_URL="mysql2://test:password@mysql:3306/jr_test"
10+
bundle exec rake;

.docker/scripts/test_postgresql

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ bundle update;
66
RUBY_VERSION=$(ruby -v);
77

88
echo "Testing With PostgreSQL and Ruby $RUBY_VERSION";
9-
export DATABASE_URL="postgresql://postgres:password@postgres:5432/test"
10-
bundle exec rake test;
9+
export DATABASE_URL="postgresql://postgres:password@postgres:5432/jr_test"
10+
bundle exec rake;

.docker/scripts/test_sqlite

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ bundle update;
66
RUBY_VERSION=$(ruby -v);
77

88
echo "Testing With SQLite and Ruby $RUBY_VERSION";
9-
export DATABASE_URL="sqlite3:test_db"
10-
bundle exec rake test;
9+
export DATABASE_URL="sqlite3:jr_test"
10+
bundle exec rake;

.github/PULL_REQUEST_TEMPLATE.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

@@ -23,4 +23,4 @@
2323

2424
### Reviewer Checklist:
2525
- [ ] Maintains compliance with JSON:API
26-
- [ ] Adequate test coverage exists to prevent regressions
26+
- [ ] Adequate test coverage exists to prevent regressions

.github/workflows/ruby.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ jobs:
4747
- '7.0'
4848
- '6.1'
4949
database_url:
50-
- sqlite3:test_db
51-
- postgresql://postgres:password@localhost:5432/test
52-
- mysql2://root:[email protected]:3306/test
50+
- sqlite3:jr_test
51+
- postgresql://postgres:password@localhost:5432/jr_test
52+
- mysql2://root:[email protected]:3306/jr_test
5353
# exclude:
5454
# - ruby: '3.1'
5555
# rails: '6.0'
@@ -65,4 +65,4 @@ jobs:
6565
ruby-version: ${{ matrix.ruby }}
6666
bundler-cache: true
6767
- name: Run tests
68-
run: bundle exec rake test
68+
run: bundle exec rake

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ coverage
2121
test/log
2222
test_db
2323
test_db-journal
24+
jr_test
25+
jr_test-journal
2426
.idea
2527
*.iml
2628
*.override.yml

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 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)
1+
# 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)
22

33
[![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)
44

Rakefile

+32-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,41 @@ Rake::TestTask.new do |t|
88
t.test_files = FileList['test/**/*_test.rb']
99
end
1010

11-
task default: [:test]
12-
13-
desc 'Run benchmarks'
1411
namespace :test do
12+
13+
desc 'prepare test database'
14+
task :prepare_database do
15+
database_url = ENV["DATABASE_URL"]
16+
next if database_url.nil? || database_url.empty?
17+
require "active_record/railtie"
18+
connection_class = ActiveRecord::Base
19+
connection_class.establish_connection(database_url)
20+
database_config = connection_class
21+
.connection_db_config
22+
.configuration_hash
23+
database_adapter = database_config.fetch(:adapter)
24+
database_name = database_config.fetch(:database)
25+
database_username = database_config[:username]
26+
case database_adapter
27+
when :postgresql, "postgresql"
28+
sh "psql -c 'DROP DATABASE IF EXISTS #{database_name};' -U #{database_username};"
29+
sh "psql -c 'CREATE DATABASE #{database_name};' -U #{database_username};"
30+
when :mysql2, "mysql2"
31+
sh "mysql -c 'DROP DATABASE IF EXISTS #{database_name};' -U #{database_username};"
32+
sh "mysql -c 'CREATE DATABASE #{database_name};' -U #{database_username};"
33+
else
34+
nil # nothing to do for this database_adapter
35+
end
36+
puts "Preparing to run #{database_adapter} tests"
37+
connection_class.connection.disconnect!
38+
end
39+
40+
desc 'Run benchmarks'
1541
Rake::TestTask.new(:benchmark) do |t|
1642
t.pattern = 'test/benchmark/*_benchmark.rb'
1743
end
18-
end
1944

20-
desc 'Test bug report template'
21-
namespace :test do
45+
desc 'Test bug report template'
2246
namespace :bug_report_template do
2347
task :rails_5 do
2448
puts 'Test bug report templates'
@@ -33,3 +57,5 @@ namespace :test do
3357
end
3458
end
3559
end
60+
61+
task default: [:"test:prepare_database", :test]

test/test_helper.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
require 'database_cleaner'
33

44
# To run tests with coverage:
5-
# COVERAGE=true bundle exec rake test
5+
# COVERAGE=true bundle exec rake
66

77
# To test on a specific rails version use this:
8-
# export RAILS_VERSION=5.2.4.4; bundle update; bundle exec rake test
9-
# export RAILS_VERSION=6.0.3.4; bundle update; bundle exec rake test
10-
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake test
8+
# export RAILS_VERSION=5.2.4.4; bundle update; bundle exec rake
9+
# export RAILS_VERSION=6.0.3.4; bundle update; bundle exec rake
10+
# export RAILS_VERSION=6.1.1; bundle update; bundle exec rake
1111

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

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

1818
if ENV['COVERAGE']
1919
SimpleCov.start do
2020
end
2121
end
2222

23-
ENV['DATABASE_URL'] ||= "sqlite3:test_db"
23+
ENV['DATABASE_URL'] ||= "sqlite3:jr_test"
2424

2525
require 'active_record/railtie'
2626
require 'minitest/mock'

0 commit comments

Comments
 (0)