From 4dba95a082974d5f5c31035f65a31ec0ea7f03d4 Mon Sep 17 00:00:00 2001 From: Ben Atkins Date: Fri, 9 May 2014 11:05:58 -0400 Subject: [PATCH] Fix ActiveSupport::TestCase tests for MySQL --- CHANGELOG.md | 1 + paper_trail.gemspec | 1 + .../migrate/20110208155312_set_up_test_tables.rb | 2 +- test/dummy/db/schema.rb | 1 + test/functional/modular_sinatra_test.rb | 3 +-- test/functional/sinatra_test.rb | 3 +-- test/test_helper.rb | 14 ++++++++++++++ 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b1748cd..51d239e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ become the live instance. - If `destroy` actions are tracked for a versioned model, invoking `destroy` on the model will cause the corresponding version that gets generated to be assigned as the source version (`model_instance#version_association_name`, usually `model_instance#version`). + - Gem is now tested against `MySQL` and `PostgreSQL` in addition to `SQLite`. ## 3.0.1 diff --git a/paper_trail.gemspec b/paper_trail.gemspec index e4bbdfc6..bdcbd747 100644 --- a/paper_trail.gemspec +++ b/paper_trail.gemspec @@ -31,6 +31,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'rack-test', '>= 0.6' s.add_development_dependency 'rspec-rails', '~> 2.14' s.add_development_dependency 'generator_spec' + s.add_development_dependency 'database_cleaner', '~> 1.2' # JRuby support for the test ENV unless defined?(JRUBY_VERSION) diff --git a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb index da60aa57..70a9bc85 100644 --- a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +++ b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb @@ -5,7 +5,7 @@ class SetUpTestTables < ActiveRecord::Migration t.text :a_text t.integer :an_integer t.float :a_float - t.decimal :a_decimal + t.decimal :a_decimal, :precision => 6, :scale => 4 t.datetime :a_datetime t.time :a_time t.date :a_date diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index e6fde32a..8f957c43 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -71,6 +71,7 @@ ActiveRecord::Schema.define(:version => 20110208155312) do t.integer "article_id" t.string "ip" t.string "user_agent" + t.text :object_changes end add_index "versions", ["item_type", "item_id"], :name => "index_versions_on_item_type_and_item_id" diff --git a/test/functional/modular_sinatra_test.rb b/test/functional/modular_sinatra_test.rb index c33b4b40..86c67f63 100644 --- a/test/functional/modular_sinatra_test.rb +++ b/test/functional/modular_sinatra_test.rb @@ -29,7 +29,6 @@ class ModularSinatraTest < ActionDispatch::IntegrationTest end test 'baseline' do - assert_nil Widget.first assert_nil Widget.create.versions.first.whodunnit end @@ -38,7 +37,7 @@ class ModularSinatraTest < ActionDispatch::IntegrationTest should "sets the `user_for_paper_trail` from the `current_user` method" do get '/test' assert_equal 'Hello', last_response.body - widget = Widget.first + widget = Widget.last assert_not_nil widget assert_equal 'foo', widget.name assert_equal 1, widget.versions.size diff --git a/test/functional/sinatra_test.rb b/test/functional/sinatra_test.rb index 9cb52746..6820955f 100644 --- a/test/functional/sinatra_test.rb +++ b/test/functional/sinatra_test.rb @@ -30,7 +30,6 @@ class SinatraTest < ActionDispatch::IntegrationTest end test 'baseline' do - assert_nil Widget.first assert_nil Widget.create.versions.first.whodunnit end @@ -39,7 +38,7 @@ class SinatraTest < ActionDispatch::IntegrationTest should "sets the `user_for_paper_trail` from the `current_user` method" do get '/test' assert_equal 'Hai', last_response.body - widget = Widget.first + widget = Widget.last assert_not_nil widget assert_equal 'bar', widget.name assert_equal 1, widget.versions.size diff --git a/test/test_helper.rb b/test/test_helper.rb index 75e7faad..c187f74b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,10 +5,15 @@ unless File.exists?(File.expand_path('../../test/dummy/config/database.yml', __F warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first" end +def using_mysql? + @using_mysql ||= ActiveRecord::Base.connection_config[:adapter].to_sym == :mysql2 +end + require File.expand_path("../dummy/config/environment.rb", __FILE__) require "rails/test_help" require 'shoulda' require 'ffaker' +require 'database_cleaner' if using_mysql? Rails.backtrace_cleaner.remove_silencers! @@ -18,9 +23,18 @@ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__ # Load support files Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } +# DatabaseCleaner is apparently necessary for doing proper transactions within MySQL (ugh) +DatabaseCleaner.strategy = :truncation if using_mysql? + # global setup block resetting Thread.current class ActiveSupport::TestCase + if using_mysql? + self.use_transactional_fixtures = false + setup { DatabaseCleaner.start } + end + teardown do + DatabaseCleaner.clean if using_mysql? Thread.current[:paper_trail] = nil end end