From 9e35f97b3e1f1ff1b888d6784ea6216082aee25e Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Fri, 26 Jan 2024 14:28:35 -0600 Subject: [PATCH] chore: ensure test db is safely/freshly created --- .docker/scripts/test_mysql | 4 ++-- .docker/scripts/test_postgresql | 4 ++-- .docker/scripts/test_sqlite | 4 ++-- .github/PULL_REQUEST_TEMPLATE.md | 4 ++-- .github/workflows/ruby.yml | 8 +++---- .gitignore | 2 ++ README.md | 2 +- Rakefile | 38 +++++++++++++++++++++++++++----- test/test_helper.rb | 16 +++++++------- 9 files changed, 55 insertions(+), 27 deletions(-) diff --git a/.docker/scripts/test_mysql b/.docker/scripts/test_mysql index 2cdf9acde..6b224a953 100644 --- a/.docker/scripts/test_mysql +++ b/.docker/scripts/test_mysql @@ -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; diff --git a/.docker/scripts/test_postgresql b/.docker/scripts/test_postgresql index 6da5b5031..257a2c36a 100644 --- a/.docker/scripts/test_postgresql +++ b/.docker/scripts/test_postgresql @@ -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; diff --git a/.docker/scripts/test_sqlite b/.docker/scripts/test_sqlite index 1829d8641..00bc1f708 100644 --- a/.docker/scripts/test_sqlite +++ b/.docker/scripts/test_sqlite @@ -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; diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index c7c3f312a..94e4d11f4 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -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. @@ -23,4 +23,4 @@ ### Reviewer Checklist: - [ ] Maintains compliance with JSON:API -- [ ] Adequate test coverage exists to prevent regressions \ No newline at end of file +- [ ] Adequate test coverage exists to prevent regressions diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 983d39913..9253b5730 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -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' @@ -65,4 +65,4 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true - name: Run tests - run: bundle exec rake test + run: bundle exec rake diff --git a/.gitignore b/.gitignore index 663f28c51..eee146d75 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,8 @@ coverage test/log test_db test_db-journal +jr_test +jr_test-journal .idea *.iml *.override.yml diff --git a/README.md b/README.md index aedeb9d5a..96d2c6a52 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/Rakefile b/Rakefile index 01619ed8e..562a34ac4 100644 --- a/Rakefile +++ b/Rakefile @@ -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' @@ -33,3 +57,5 @@ namespace :test do end end end + +task default: [:"test:prepare_database", :test] diff --git a/test/test_helper.rb b/test/test_helper.rb index 9f8f03b2c..dad15f25d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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'