From 410b72fc1613798623d0e9407b0bc3da3ceaf52d Mon Sep 17 00:00:00 2001 From: Hugo Corbucci Date: Sat, 31 May 2014 21:34:29 -0500 Subject: [PATCH 01/20] Upgrading to ActiveRecord 3.2 --- Gemfile | 8 ++ Gemfile.lock | 92 +++++++++++++++++++ Rakefile | 2 +- .../unit_record_adapter.rb | 33 ++++--- lib/unit_record.rb | 9 +- lib/unit_record/association_stubbing.rb | 1 - lib/unit_record/disconnected_active_record.rb | 4 +- .../unit_record_adapter_test.rb | 9 +- test/db/schema.rb | 2 +- test/test_helper.rb | 30 ++---- test/unit_record/controller_test.rb | 51 ++++------ .../unit_record/disconnected_fixtures_test.rb | 4 +- 12 files changed, 168 insertions(+), 77 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f0f2627 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +source 'https://rubygems.org' + +group :development, :test do + gem 'rake' + gem 'rails', '3.2.18' + gem 'test-unit' + gem 'mocha', :require => 'mocha/setup' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..ebf5df1 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,92 @@ +GEM + remote: https://rubygems.org/ + specs: + actionmailer (3.2.18) + actionpack (= 3.2.18) + mail (~> 2.5.4) + actionpack (3.2.18) + activemodel (= 3.2.18) + activesupport (= 3.2.18) + builder (~> 3.0.0) + erubis (~> 2.7.0) + journey (~> 1.0.4) + rack (~> 1.4.5) + rack-cache (~> 1.2) + rack-test (~> 0.6.1) + sprockets (~> 2.2.1) + activemodel (3.2.18) + activesupport (= 3.2.18) + builder (~> 3.0.0) + activerecord (3.2.18) + activemodel (= 3.2.18) + activesupport (= 3.2.18) + arel (~> 3.0.2) + tzinfo (~> 0.3.29) + activeresource (3.2.18) + activemodel (= 3.2.18) + activesupport (= 3.2.18) + activesupport (3.2.18) + i18n (~> 0.6, >= 0.6.4) + multi_json (~> 1.0) + arel (3.0.3) + builder (3.0.4) + erubis (2.7.0) + hike (1.2.3) + i18n (0.6.9) + journey (1.0.4) + json (1.8.1) + mail (2.5.4) + mime-types (~> 1.16) + treetop (~> 1.4.8) + metaclass (0.0.1) + mime-types (1.25.1) + mocha (0.14.0) + metaclass (~> 0.0.1) + multi_json (1.10.1) + polyglot (0.3.5) + rack (1.4.5) + rack-cache (1.2) + rack (>= 0.4) + rack-ssl (1.3.4) + rack + rack-test (0.6.2) + rack (>= 1.0) + rails (3.2.18) + actionmailer (= 3.2.18) + actionpack (= 3.2.18) + activerecord (= 3.2.18) + activeresource (= 3.2.18) + activesupport (= 3.2.18) + bundler (~> 1.0) + railties (= 3.2.18) + railties (3.2.18) + actionpack (= 3.2.18) + activesupport (= 3.2.18) + rack-ssl (~> 1.3.2) + rake (>= 0.8.7) + rdoc (~> 3.4) + thor (>= 0.14.6, < 2.0) + rake (10.1.1) + rdoc (3.12.2) + json (~> 1.4) + sprockets (2.2.2) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + test-unit (2.5.5) + thor (0.19.1) + tilt (1.4.1) + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) + tzinfo (0.3.39) + +PLATFORMS + ruby + +DEPENDENCIES + mocha + rails (= 3.2.18) + rake + test-unit diff --git a/Rakefile b/Rakefile index e792aad..1ee5e5a 100644 --- a/Rakefile +++ b/Rakefile @@ -59,7 +59,7 @@ task :readme do sh "open #{file}" end -RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1 2.2.2 2.3.1] +RAILS_VERSIONS = %w[3.2.18] namespace :test do desc "test with multiple versions of rails" diff --git a/lib/active_record/connection_adapters/unit_record_adapter.rb b/lib/active_record/connection_adapters/unit_record_adapter.rb index 6f51cf8..3d33731 100644 --- a/lib/active_record/connection_adapters/unit_record_adapter.rb +++ b/lib/active_record/connection_adapters/unit_record_adapter.rb @@ -1,3 +1,8 @@ +class Visitor + def accept(ast) + '' + end +end class ActiveRecord::ConnectionAdapters::UnitRecordAdapter < ::ActiveRecord::ConnectionAdapters::AbstractAdapter EXCEPTION_MESSAGE = "ActiveRecord is disconnected; database access is unavailable in unit tests." @@ -21,6 +26,10 @@ def create_table(table_name, options={}) ActiveRecord::ConnectionAdapters::Column.new(c.name.to_s, c.default, c.sql_type, c.null) end end + + def primary_key(*args) + 'id' + end def native_database_types # Copied from the MysqlAdapter so ColumnDefinition#sql_type will work @@ -53,35 +62,31 @@ def change_strategy(new_strategy, &block) end end - def execute(sql, name = nil) + def execute(*args) raise_or_noop end - def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) - raise_or_noop - end if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 1 - - def select_rows(sql, name = nil) + def select_rows(*args) raise_or_noop [] end - def rename_table(table_name, new_name) + def rename_table(*args) raise_or_noop end - def change_column(table_name, column_name, type, options = {}) + def change_column(*args) raise_or_noop end - def change_column_default(table_name, column_name, default) + def change_column_default(*args) raise_or_noop end - def rename_column(table_name, column_name, new_column_name) + def rename_column(*args) raise_or_noop end - def add_foreign_key(table_name, column_names, target_table, key_name, options = {}) + def add_foreign_key(*args) raise_or_noop end @@ -89,13 +94,17 @@ def tables @cached_columns.keys end + def visitor + Visitor.new + end + protected def raise_or_noop(noop_return_value = nil) @strategy == :raise ? raise(EXCEPTION_MESSAGE) : noop_return_value end - def select(sql, name = nil) + def select(*args) raise_or_noop [] end end diff --git a/lib/unit_record.rb b/lib/unit_record.rb index 4a15435..6f2f279 100644 --- a/lib/unit_record.rb +++ b/lib/unit_record.rb @@ -10,8 +10,13 @@ def self.rails_version def self.base_rails_test_class if rails_version && rails_version >= "2.3.1" ActiveSupport::TestCase - else + elsif defined?(Test::Unit::TestCase) Test::Unit::TestCase + else + Class.new do + def self.disconnect!(*args) + end + end end end end @@ -27,7 +32,7 @@ def self.base_rails_test_class ActiveRecord::ConnectionAdapters::Column.send :include, UnitRecord::ColumnExtension ActiveRecord::Base.extend UnitRecord::DisconnectedActiveRecord UnitRecord.base_rails_test_class.extend UnitRecord::DisconnectedTestCase -Fixtures.extend UnitRecord::DisconnectedFixtures +Fixtures.extend UnitRecord::DisconnectedFixtures if defined?(Fixtures) ActiveRecord::Base.class_eval do def self.unit_record_connection(config) diff --git a/lib/unit_record/association_stubbing.rb b/lib/unit_record/association_stubbing.rb index 323293c..8db4988 100644 --- a/lib/unit_record/association_stubbing.rb +++ b/lib/unit_record/association_stubbing.rb @@ -4,7 +4,6 @@ module AssociationStubbing private def initialize_with_association_stubbing(attributes = {}) - attributes ||= {} associations = extract_associations attributes initialize_without_association_stubbing attributes stub_associations associations diff --git a/lib/unit_record/disconnected_active_record.rb b/lib/unit_record/disconnected_active_record.rb index 6a5af1a..3c32971 100644 --- a/lib/unit_record/disconnected_active_record.rb +++ b/lib/unit_record/disconnected_active_record.rb @@ -10,11 +10,11 @@ def disconnect!(options = {}) if options[:stub_associations] ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing end - Fixtures.disconnect! + Fixtures.disconnect! if defined?(Fixtures) UnitRecord.base_rails_test_class.disconnect! ActiveRecord::Migration.verbose = false ActiveRecord::Base.connection.change_strategy(:noop) do - load(RAILS_ROOT + "/db/schema.rb") + load(RAILS_ROOT + "/db/schema.rb") if defined?(RAILS_ROOT) end end end diff --git a/test/active_record/connection_adapters/unit_record_adapter_test.rb b/test/active_record/connection_adapters/unit_record_adapter_test.rb index c68190a..9600a09 100644 --- a/test/active_record/connection_adapters/unit_record_adapter_test.rb +++ b/test/active_record/connection_adapters/unit_record_adapter_test.rb @@ -8,12 +8,14 @@ ActiveRecord::Base.connection end - test "find(:all)" do + test "all arel" do ActiveRecord::Base.connection.change_strategy(:raise) do - assert_raises(RuntimeError) { Person.find(:all) } + assert_raises(RuntimeError) { + Person.all + } end ActiveRecord::Base.connection.change_strategy(:noop) do - assert_equal [], Person.find(:all) + assert_equal [], Person.all end end @@ -75,7 +77,6 @@ test "noop" do ActiveRecord::Base.connection.change_strategy(:noop) do assert_nil ActiveRecord::Base.connection.execute("SELECT 1") - assert_nil ActiveRecord::Base.connection.insert("INSERT INTO ...") assert_equal [], ActiveRecord::Base.connection.select_rows("SELECT * FROM people") assert_equal [], ActiveRecord::Base.connection.send(:select, "SELECT * FROM people") assert_nil ActiveRecord::Base.connection.rename_table("people", "persons") diff --git a/test/db/schema.rb b/test/db/schema.rb index a4b9ea8..ba36174 100644 --- a/test/db/schema.rb +++ b/test/db/schema.rb @@ -1,4 +1,4 @@ -ActiveRecord::Schema.define(:version => 0) do +ActiveRecord::Schema.define do create_table :preferences, :force => true do |t| t.column :some_count, :integer t.column :show_help, :boolean, :default => true diff --git a/test/test_helper.rb b/test/test_helper.rb index 4f57084..df577eb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,27 +4,17 @@ RAILS_ROOT = File.dirname(__FILE__) require 'rubygems' -require 'test/unit' +require 'rails/all' -if rails_version = ENV['RAILS_VERSION'] - gem "rails", rails_version +if defined?(Bundler) + # If you precompile assets before deploying to production, use this line + Bundler.require(*Rails.groups(:assets => %w(development test))) + # If you want your assets lazily compiled in production, use this line + # Bundler.require(:default, :assets, Rails.env) end -require "rails/version" -puts "==== Testing with Rails #{Rails::VERSION::STRING} ====" -require 'active_record' -require 'active_record/fixtures' -require "action_controller" -if Rails::VERSION::MAJOR == 2 - require "action_controller/test_case" -end -require "action_controller/test_process" -begin - gem "mocha" - require 'mocha' -rescue LoadError, Gem::LoadError - raise "need mocha to test" -end +require "action_controller/test_case" if Rails::VERSION::MAJOR == 2 + $LOAD_PATH << File.dirname(__FILE__) + "/../vendor/dust-0.1.6/lib" require 'dust' Test::Unit::TestCase.disallow_setup! @@ -60,11 +50,11 @@ class Pet < ActiveRecord::Base end class Foo < ActiveRecord::Base - set_table_name :foofoo + self.table_name = 'foofoo' end class DoesNotExist < ActiveRecord::Base - set_table_name "table_does_not_exist" + self.table_name = 'table_does_not_exist' end ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true diff --git a/test/unit_record/controller_test.rb b/test/unit_record/controller_test.rb index a711cf2..59f75b1 100644 --- a/test/unit_record/controller_test.rb +++ b/test/unit_record/controller_test.rb @@ -1,43 +1,28 @@ require File.dirname(__FILE__) + '/../test_helper' +class SampleApplication < Rails::Application +end +SampleApplication.routes.draw do + match '/sample/sample_action', :to => 'sample#sample_action' +end + + +SampleApplication.routes.draw do + match '/sample/sample_action', :to => 'sample#sample_action' +end class SampleController < ActionController::Base + include SampleApplication.routes.url_helpers def sample_action - render :text => "OK" + render :text => 'OK' end end -ActionController::Routing::Routes.add_route "/sample/sample_action", :controller => "sample", :action => "sample_action" +class ControllerTest < ActionController::TestCase + tests SampleController -if defined?(ActionController::TestCase) # Rails 2 - - class ControllerTest < ActionController::TestCase - tests SampleController - - test "render" do - get :sample_action - assert_equal "OK", @response.body - end - - if defined?(ActionController::Caching::SqlCache) # SqlCache goes away in Rails 2.3.1 - test "sql caching is enabled" do - assert_equal true, (SampleController < ActionController::Caching::SqlCache) - end - end + test 'render' do + @routes = SampleApplication.routes + get :sample_action + assert_equal 'OK', @response.body end - -else # Rails 1.x - - class ControllerTest < Test::Unit::TestCase - def setup - @controller = SampleController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - - test "render" do - get :sample_action - assert_equal "OK", @response.body - end - end - end diff --git a/test/unit_record/disconnected_fixtures_test.rb b/test/unit_record/disconnected_fixtures_test.rb index e80dcbe..52c182f 100644 --- a/test/unit_record/disconnected_fixtures_test.rb +++ b/test/unit_record/disconnected_fixtures_test.rb @@ -2,6 +2,8 @@ functional_tests do test "create_fixtures does nothing" do - assert_nothing_raised { Fixtures.create_fixtures } + if defined?(Fixtures) + assert_nothing_raised { Fixtures.create_fixtures } + end end end From 15c884db474233fd29bee531aba7d329b8f84093 Mon Sep 17 00:00:00 2001 From: Hugo Corbucci Date: Wed, 10 Dec 2014 17:08:43 -0900 Subject: [PATCH 02/20] Upgrading gems --- .gitignore | 4 +++- Gemfile.lock | 16 +++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 5b230d4..65d3c38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ doc pkg *.gem -.treasure_map.rb \ No newline at end of file +.treasure_map.rb +.bundle +vendor/bundle diff --git a/Gemfile.lock b/Gemfile.lock index ebf5df1..61ac7fa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -32,18 +32,19 @@ GEM builder (3.0.4) erubis (2.7.0) hike (1.2.3) - i18n (0.6.9) + i18n (0.6.11) journey (1.0.4) json (1.8.1) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) - metaclass (0.0.1) + metaclass (0.0.4) mime-types (1.25.1) - mocha (0.14.0) + mocha (1.1.0) metaclass (~> 0.0.1) multi_json (1.10.1) polyglot (0.3.5) + power_assert (0.2.2) rack (1.4.5) rack-cache (1.2) rack (>= 0.4) @@ -66,21 +67,22 @@ GEM rake (>= 0.8.7) rdoc (~> 3.4) thor (>= 0.14.6, < 2.0) - rake (10.1.1) + rake (10.4.2) rdoc (3.12.2) json (~> 1.4) - sprockets (2.2.2) + sprockets (2.2.3) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - test-unit (2.5.5) + test-unit (3.0.7) + power_assert thor (0.19.1) tilt (1.4.1) treetop (1.4.15) polyglot polyglot (>= 0.3.1) - tzinfo (0.3.39) + tzinfo (0.3.42) PLATFORMS ruby From 0181adbb19bf21614971248efde45bec2893aea8 Mon Sep 17 00:00:00 2001 From: Evan/Tracy Date: Thu, 6 Aug 2015 11:23:07 -0400 Subject: [PATCH 03/20] Add support for Rails 3.2 --- Gemfile | 5 +- Gemfile.lock | 86 ++++++++++++------- Rakefile | 6 +- lib/unit_record/association_stubbing.rb | 12 +-- test/unit_record/association_stubbing_test.rb | 12 +-- .../disconnected_active_record_test.rb | 16 ++-- unit-record.gemspec | 4 +- 7 files changed, 83 insertions(+), 58 deletions(-) diff --git a/Gemfile b/Gemfile index f0f2627..ea9a6bf 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,10 @@ source 'https://rubygems.org' group :development, :test do gem 'rake' - gem 'rails', '3.2.18' + gem 'rails', '3.2.21' gem 'test-unit' gem 'mocha', :require => 'mocha/setup' + gem 'pry' + gem 'pry-debugger' + gem 'pry-nav' end diff --git a/Gemfile.lock b/Gemfile.lock index 61ac7fa..74e962e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,12 @@ GEM remote: https://rubygems.org/ specs: - actionmailer (3.2.18) - actionpack (= 3.2.18) + actionmailer (3.2.21) + actionpack (= 3.2.21) mail (~> 2.5.4) - actionpack (3.2.18) - activemodel (= 3.2.18) - activesupport (= 3.2.18) + actionpack (3.2.21) + activemodel (= 3.2.21) + activesupport (= 3.2.21) builder (~> 3.0.0) erubis (~> 2.7.0) journey (~> 1.0.4) @@ -14,55 +14,73 @@ GEM rack-cache (~> 1.2) rack-test (~> 0.6.1) sprockets (~> 2.2.1) - activemodel (3.2.18) - activesupport (= 3.2.18) + activemodel (3.2.21) + activesupport (= 3.2.21) builder (~> 3.0.0) - activerecord (3.2.18) - activemodel (= 3.2.18) - activesupport (= 3.2.18) + activerecord (3.2.21) + activemodel (= 3.2.21) + activesupport (= 3.2.21) arel (~> 3.0.2) tzinfo (~> 0.3.29) - activeresource (3.2.18) - activemodel (= 3.2.18) - activesupport (= 3.2.18) - activesupport (3.2.18) + activeresource (3.2.21) + activemodel (= 3.2.21) + activesupport (= 3.2.21) + activesupport (3.2.21) i18n (~> 0.6, >= 0.6.4) multi_json (~> 1.0) arel (3.0.3) builder (3.0.4) + coderay (1.1.0) + columnize (0.9.0) + debugger (1.6.8) + columnize (>= 0.3.1) + debugger-linecache (~> 1.2.0) + debugger-ruby_core_source (~> 1.3.5) + debugger-linecache (1.2.0) + debugger-ruby_core_source (1.3.8) erubis (2.7.0) hike (1.2.3) - i18n (0.6.11) + i18n (0.7.0) journey (1.0.4) - json (1.8.1) + json (1.8.2) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) metaclass (0.0.4) + method_source (0.8.2) mime-types (1.25.1) mocha (1.1.0) metaclass (~> 0.0.1) - multi_json (1.10.1) + multi_json (1.11.0) polyglot (0.3.5) - power_assert (0.2.2) + power_assert (0.2.3) + pry (0.9.12.6) + coderay (~> 1.0) + method_source (~> 0.8) + slop (~> 3.4) + pry-debugger (0.2.3) + debugger (~> 1.3) + pry (>= 0.9.10, < 0.11.0) + pry-nav (0.2.4) + pry (>= 0.9.10, < 0.11.0) rack (1.4.5) rack-cache (1.2) rack (>= 0.4) rack-ssl (1.3.4) rack - rack-test (0.6.2) + rack-test (0.6.3) rack (>= 1.0) - rails (3.2.18) - actionmailer (= 3.2.18) - actionpack (= 3.2.18) - activerecord (= 3.2.18) - activeresource (= 3.2.18) - activesupport (= 3.2.18) + rails (3.2.21) + actionmailer (= 3.2.21) + actionpack (= 3.2.21) + activerecord (= 3.2.21) + activeresource (= 3.2.21) + activesupport (= 3.2.21) bundler (~> 1.0) - railties (= 3.2.18) - railties (3.2.18) - actionpack (= 3.2.18) - activesupport (= 3.2.18) + railties (= 3.2.21) + railties (3.2.21) + actionpack (= 3.2.21) + activesupport (= 3.2.21) rack-ssl (~> 1.3.2) rake (>= 0.8.7) rdoc (~> 3.4) @@ -70,25 +88,29 @@ GEM rake (10.4.2) rdoc (3.12.2) json (~> 1.4) + slop (3.4.7) sprockets (2.2.3) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - test-unit (3.0.7) + test-unit (3.1.1) power_assert thor (0.19.1) tilt (1.4.1) treetop (1.4.15) polyglot polyglot (>= 0.3.1) - tzinfo (0.3.42) + tzinfo (0.3.44) PLATFORMS ruby DEPENDENCIES mocha - rails (= 3.2.18) + pry + pry-debugger + pry-nav + rails (= 3.2.21) rake test-unit diff --git a/Rakefile b/Rakefile index 1ee5e5a..14016b3 100644 --- a/Rakefile +++ b/Rakefile @@ -31,7 +31,7 @@ gem_spec = Gem::Specification.new do |s| s.email = "daniel.manges@gmail.com" s.homepage = "http://unit-test-ar.rubyforge.org" s.rubyforge_project = "unit-test-ar" - + s.has_rdoc = false s.autorequire = "unit_record" @@ -59,7 +59,7 @@ task :readme do sh "open #{file}" end -RAILS_VERSIONS = %w[3.2.18] +RAILS_VERSIONS = %w[3.2.21] namespace :test do desc "test with multiple versions of rails" @@ -69,7 +69,7 @@ namespace :test do sh "RAILS_VERSION='#{rails_version}' rake test > /dev/null 2>&1" end end - + task :multi_verbose do (RAILS_VERSIONS - %w[]).each do |rails_version| task = defined?(Rcov) ? "rcov" : "test" diff --git a/lib/unit_record/association_stubbing.rb b/lib/unit_record/association_stubbing.rb index 8db4988..e938703 100644 --- a/lib/unit_record/association_stubbing.rb +++ b/lib/unit_record/association_stubbing.rb @@ -1,16 +1,16 @@ module UnitRecord module AssociationStubbing - + private - + def initialize_with_association_stubbing(attributes = {}) associations = extract_associations attributes initialize_without_association_stubbing attributes stub_associations associations end - + protected - + def extract_associations(attributes = {}) attributes.inject({}) do |associations,(attr,value)| next associations unless self.class.reflections.keys.include? attr @@ -20,7 +20,7 @@ def extract_associations(attributes = {}) associations end end - + def stub_associations(associations = {}) associations.each do |attr,value| self.stubs(attr).returns(value) @@ -29,7 +29,7 @@ def stub_associations(associations = {}) def self.included(klass) klass.class_eval do - unless (instance_methods + private_instance_methods).include?("initialize_without_association_stubbing") + unless (instance_methods + private_instance_methods).include?(:initialize_without_association_stubbing) alias_method_chain :initialize, :association_stubbing end end diff --git a/test/unit_record/association_stubbing_test.rb b/test/unit_record/association_stubbing_test.rb index 5edb341..63ab55f 100644 --- a/test/unit_record/association_stubbing_test.rb +++ b/test/unit_record/association_stubbing_test.rb @@ -1,19 +1,19 @@ require File.dirname(__FILE__) + "/../test_helper" functional_tests do - + test "stubbing a has_many" do pets = [stub, stub] person = Person.new :pets => pets assert_equal pets, person.pets end - + test "stubbing a belongs_to" do person = stub pet = Pet.new :person => person assert_equal person, pet.person end - + test "using correct classes does not stub" do person = Person.new(:first_name => "Dan") pet = Pet.new :person => person @@ -28,10 +28,10 @@ def person.first_name; "Dan"; end pet.person = Person.new(:first_name => "Tom") assert_equal "Dan", pet.person.first_name end - - test "multiple includes doesn't hurt" do + + test "multiple includes does not hurt" do ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing Person.new end - + end diff --git a/test/unit_record/disconnected_active_record_test.rb b/test/unit_record/disconnected_active_record_test.rb index 2f2b7ce..81ee778 100644 --- a/test/unit_record/disconnected_active_record_test.rb +++ b/test/unit_record/disconnected_active_record_test.rb @@ -1,6 +1,6 @@ require File.dirname(__FILE__) + "/../test_helper" -functional_tests do +functional_tests do test "find_by_sql gives disconnected exception message" do exception = nil begin @@ -10,31 +10,31 @@ assert_not_nil exception assert_equal "ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message end - + test "connected? is true" do assert_equal true, ActiveRecord::Base.connected? end - + test "disconnected? is true" do assert_equal true, ActiveRecord::Base.disconnected? end - + test "inspect does not blow up" do assert_nothing_raised { Person.inspect } end - + test "table_exists?" do assert_equal true, Person.table_exists? if ActiveRecord::Base.connection.respond_to?(:table_exists?) assert_equal false, ActiveRecord::Base.connection.table_exists?("bogus_table") end end - + test "setting a has_one association" do person = Person.new person.profile = Profile.new end - + test "boolean columns do type casting" do pref = Preference.new pref.show_help = "0" @@ -45,7 +45,7 @@ assert_equal true, pref.show_help assert_equal true, pref.show_help? end - + test "migrations are not verbose" do assert_equal false, ActiveRecord::Migration.verbose end diff --git a/unit-record.gemspec b/unit-record.gemspec index 49b1d98..d6f7133 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -3,12 +3,12 @@ Gem::Specification.new do |s| s.name = %q{unit_record} - s.version = "0.9.1" + s.version = "0.9.2" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Dan Manges"] s.autorequire = %q{unit_record} - s.date = %q{2009-04-22} + s.date = %q{2015-05-29} s.description = %q{UnitRecord enables unit testing without hitting the database.} s.email = %q{daniel.manges@gmail.com} s.files = ["lib/active_record/connection_adapters/unit_record_adapter.rb", "lib/unit_record.rb", "lib/unit_record/disconnected_active_record.rb", "lib/unit_record/disconnected_test_case.rb", "lib/unit_record/column_extension.rb", "lib/unit_record/disconnected_fixtures.rb", "lib/unit_record/association_stubbing.rb", "test/test_helper.rb", "test/active_record/connection_adapters/unit_record_adapter_test.rb", "test/db/schema.rb", "test/sample_spec.rb", "test/unit_record/disconnected_test_case_test.rb", "test/unit_record/column_test.rb", "test/unit_record/column_cacher_test.rb", "test/unit_record/unit_record_test.rb", "test/unit_record/disconnected_active_record_test.rb", "test/unit_record/association_stubbing_test.rb", "test/unit_record/controller_test.rb", "test/unit_record/column_extension_test.rb", "test/unit_record/disconnected_fixtures_test.rb", "vendor/dust-0.1.6/rakefile.rb", "vendor/dust-0.1.6/test/test_helper.rb", "vendor/dust-0.1.6/test/passing_unit_test.rb", "vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb", "vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb", "vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb", "vendor/dust-0.1.6/test/all_tests.rb", "vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb", "vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb", "vendor/dust-0.1.6/test/functional_test.rb", "vendor/dust-0.1.6/lib/dust.rb", "vendor/dust-0.1.6/lib/definition_error.rb", "vendor/dust-0.1.6/lib/object_extension.rb", "vendor/dust-0.1.6/lib/string_extension.rb", "vendor/dust-0.1.6/lib/array_extension.rb", "vendor/dust-0.1.6/lib/symbol_extension.rb", "vendor/dust-0.1.6/lib/test_case_extension.rb", "vendor/dust-0.1.6/lib/nil_extension.rb", "CHANGELOG", "LICENSE", "README.markdown", "Rakefile"] From 5550d88e723af2f40a1ae75121e2cd2c29c0ce8e Mon Sep 17 00:00:00 2001 From: Ryan/Brandon Date: Wed, 5 Aug 2015 14:14:31 -0400 Subject: [PATCH 04/20] Implement quote_column_name --- lib/active_record/connection_adapters/unit_record_adapter.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/active_record/connection_adapters/unit_record_adapter.rb b/lib/active_record/connection_adapters/unit_record_adapter.rb index 3d33731..f99cb02 100644 --- a/lib/active_record/connection_adapters/unit_record_adapter.rb +++ b/lib/active_record/connection_adapters/unit_record_adapter.rb @@ -98,6 +98,10 @@ def visitor Visitor.new end + def quote_column_name(column_name) + column_name.to_s + end + protected def raise_or_noop(noop_return_value = nil) From 795bbdb639cfd4fe7f0d6a0f9949bf392fbb94c9 Mon Sep 17 00:00:00 2001 From: Ryan/Brandon Date: Thu, 6 Aug 2015 11:34:22 -0400 Subject: [PATCH 05/20] Convert RAILS_ROOT to Rails.root --- lib/unit_record/disconnected_active_record.rb | 2 +- test/sample_spec.rb | 2 -- test/test_helper.rb | 9 ++++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/unit_record/disconnected_active_record.rb b/lib/unit_record/disconnected_active_record.rb index 3c32971..14dcc13 100644 --- a/lib/unit_record/disconnected_active_record.rb +++ b/lib/unit_record/disconnected_active_record.rb @@ -14,7 +14,7 @@ def disconnect!(options = {}) UnitRecord.base_rails_test_class.disconnect! ActiveRecord::Migration.verbose = false ActiveRecord::Base.connection.change_strategy(:noop) do - load(RAILS_ROOT + "/db/schema.rb") if defined?(RAILS_ROOT) + load(Rails.root.join('db', 'schema.rb')) if defined?(Rails) end end end diff --git a/test/sample_spec.rb b/test/sample_spec.rb index fae257c..9a92089 100644 --- a/test/sample_spec.rb +++ b/test/sample_spec.rb @@ -5,8 +5,6 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') -RAILS_ROOT = File.dirname(__FILE__) - if rails_version = ENV['RAILS_VERSION'] gem "rails", rails_version end diff --git a/test/test_helper.rb b/test/test_helper.rb index df577eb..951ff72 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,11 +1,18 @@ unless defined?(TEST_HELPER_LOADED) TEST_HELPER_LOADED = true $:.unshift(File.dirname(__FILE__) + '/../lib') -RAILS_ROOT = File.dirname(__FILE__) require 'rubygems' require 'rails/all' +module Rails + class << self + def root + @root ||= Pathname.new(File.dirname(__FILE__)) + end + end +end + if defined?(Bundler) # If you precompile assets before deploying to production, use this line Bundler.require(*Rails.groups(:assets => %w(development test))) From 77dd781b2e28e50a798690abaf87373d134b2bb9 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Thu, 3 Sep 2015 11:45:16 -0400 Subject: [PATCH 06/20] Move dependencies from Gemfile to gemspec --- .gitignore | 1 + Gemfile | 10 +--- Gemfile.lock | 116 -------------------------------------------- unit-record.gemspec | 4 ++ 4 files changed, 6 insertions(+), 125 deletions(-) delete mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index 65d3c38..945873c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ pkg *.gem .treasure_map.rb .bundle +Gemfile.lock vendor/bundle diff --git a/Gemfile b/Gemfile index ea9a6bf..fa75df1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,3 @@ source 'https://rubygems.org' -group :development, :test do - gem 'rake' - gem 'rails', '3.2.21' - gem 'test-unit' - gem 'mocha', :require => 'mocha/setup' - gem 'pry' - gem 'pry-debugger' - gem 'pry-nav' -end +gemspec diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 74e962e..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,116 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - actionmailer (3.2.21) - actionpack (= 3.2.21) - mail (~> 2.5.4) - actionpack (3.2.21) - activemodel (= 3.2.21) - activesupport (= 3.2.21) - builder (~> 3.0.0) - erubis (~> 2.7.0) - journey (~> 1.0.4) - rack (~> 1.4.5) - rack-cache (~> 1.2) - rack-test (~> 0.6.1) - sprockets (~> 2.2.1) - activemodel (3.2.21) - activesupport (= 3.2.21) - builder (~> 3.0.0) - activerecord (3.2.21) - activemodel (= 3.2.21) - activesupport (= 3.2.21) - arel (~> 3.0.2) - tzinfo (~> 0.3.29) - activeresource (3.2.21) - activemodel (= 3.2.21) - activesupport (= 3.2.21) - activesupport (3.2.21) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) - arel (3.0.3) - builder (3.0.4) - coderay (1.1.0) - columnize (0.9.0) - debugger (1.6.8) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.5) - debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.8) - erubis (2.7.0) - hike (1.2.3) - i18n (0.7.0) - journey (1.0.4) - json (1.8.2) - mail (2.5.4) - mime-types (~> 1.16) - treetop (~> 1.4.8) - metaclass (0.0.4) - method_source (0.8.2) - mime-types (1.25.1) - mocha (1.1.0) - metaclass (~> 0.0.1) - multi_json (1.11.0) - polyglot (0.3.5) - power_assert (0.2.3) - pry (0.9.12.6) - coderay (~> 1.0) - method_source (~> 0.8) - slop (~> 3.4) - pry-debugger (0.2.3) - debugger (~> 1.3) - pry (>= 0.9.10, < 0.11.0) - pry-nav (0.2.4) - pry (>= 0.9.10, < 0.11.0) - rack (1.4.5) - rack-cache (1.2) - rack (>= 0.4) - rack-ssl (1.3.4) - rack - rack-test (0.6.3) - rack (>= 1.0) - rails (3.2.21) - actionmailer (= 3.2.21) - actionpack (= 3.2.21) - activerecord (= 3.2.21) - activeresource (= 3.2.21) - activesupport (= 3.2.21) - bundler (~> 1.0) - railties (= 3.2.21) - railties (3.2.21) - actionpack (= 3.2.21) - activesupport (= 3.2.21) - rack-ssl (~> 1.3.2) - rake (>= 0.8.7) - rdoc (~> 3.4) - thor (>= 0.14.6, < 2.0) - rake (10.4.2) - rdoc (3.12.2) - json (~> 1.4) - slop (3.4.7) - sprockets (2.2.3) - hike (~> 1.2) - multi_json (~> 1.0) - rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) - test-unit (3.1.1) - power_assert - thor (0.19.1) - tilt (1.4.1) - treetop (1.4.15) - polyglot - polyglot (>= 0.3.1) - tzinfo (0.3.44) - -PLATFORMS - ruby - -DEPENDENCIES - mocha - pry - pry-debugger - pry-nav - rails (= 3.2.21) - rake - test-unit diff --git a/unit-record.gemspec b/unit-record.gemspec index d6f7133..920c964 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -17,6 +17,10 @@ Gem::Specification.new do |s| s.rubyforge_project = %q{unit-test-ar} s.rubygems_version = %q{1.3.1} s.summary = %q{UnitRecord enables unit testing without hitting the database.} + s.add_development_dependency 'rake', '> 0' + s.add_development_dependency 'rails', '3.2.22' + s.add_development_dependency 'test-unit', '3.1.1' + s.add_development_dependency 'mocha', '1.1.0' if s.respond_to? :specification_version then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION From 9534e869f84c8b9dca723c3f12dbf7bb8bc5245a Mon Sep 17 00:00:00 2001 From: Ryan/Sang Date: Fri, 11 Sep 2015 12:00:24 -0400 Subject: [PATCH 07/20] Only Bundler.require if running Rails greater than or equal to 3.1 --- test/test_helper.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 951ff72..8f79cc4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,11 +13,13 @@ def root end end -if defined?(Bundler) - # If you precompile assets before deploying to production, use this line - Bundler.require(*Rails.groups(:assets => %w(development test))) - # If you want your assets lazily compiled in production, use this line - # Bundler.require(:default, :assets, Rails.env) +if Rails::VERSION::MAJOR > 3 && Rails::VERSION::MINOR > 0 + if defined?(Bundler) + # If you precompile assets before deploying to production, use this line + Bundler.require(*Rails.groups(:assets => %w(development test))) + # If you want your assets lazily compiled in production, use this line + # Bundler.require(:default, :assets, Rails.env) + end end require "action_controller/test_case" if Rails::VERSION::MAJOR == 2 From aea1c675d3df982ca9a0dee866242e7c004a3f7a Mon Sep 17 00:00:00 2001 From: Ryan/Sang Date: Fri, 11 Sep 2015 12:12:30 -0400 Subject: [PATCH 08/20] Require dust through bundler --- test/test_helper.rb | 1 - unit-record.gemspec | 3 +- vendor/dust-0.1.6/README | 35 --------- vendor/dust-0.1.6/lib/array_extension.rb | 5 -- vendor/dust-0.1.6/lib/definition_error.rb | 20 ----- vendor/dust-0.1.6/lib/dust.rb | 8 -- vendor/dust-0.1.6/lib/nil_extension.rb | 5 -- vendor/dust-0.1.6/lib/object_extension.rb | 62 --------------- vendor/dust-0.1.6/lib/string_extension.rb | 5 -- vendor/dust-0.1.6/lib/symbol_extension.rb | 5 -- vendor/dust-0.1.6/lib/test_case_extension.rb | 76 ------------------- vendor/dust-0.1.6/rakefile.rb | 50 ------------ vendor/dust-0.1.6/test/all_tests.rb | 1 - .../test/failing_with_helper_unit_test.rb | 16 ---- .../test/failing_with_setup_unit_test.rb | 16 ---- vendor/dust-0.1.6/test/functional_test.rb | 12 --- vendor/dust-0.1.6/test/passing_unit_test.rb | 11 --- .../test/passing_with_helper_unit_test.rb | 10 --- .../test/passing_with_helpers_unit_test.rb | 13 ---- .../test/passing_with_setup_unit_test.rb | 10 --- vendor/dust-0.1.6/test/test_helper.rb | 1 - 21 files changed, 2 insertions(+), 363 deletions(-) delete mode 100644 vendor/dust-0.1.6/README delete mode 100644 vendor/dust-0.1.6/lib/array_extension.rb delete mode 100644 vendor/dust-0.1.6/lib/definition_error.rb delete mode 100644 vendor/dust-0.1.6/lib/dust.rb delete mode 100644 vendor/dust-0.1.6/lib/nil_extension.rb delete mode 100644 vendor/dust-0.1.6/lib/object_extension.rb delete mode 100644 vendor/dust-0.1.6/lib/string_extension.rb delete mode 100644 vendor/dust-0.1.6/lib/symbol_extension.rb delete mode 100644 vendor/dust-0.1.6/lib/test_case_extension.rb delete mode 100644 vendor/dust-0.1.6/rakefile.rb delete mode 100644 vendor/dust-0.1.6/test/all_tests.rb delete mode 100644 vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb delete mode 100644 vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb delete mode 100644 vendor/dust-0.1.6/test/functional_test.rb delete mode 100644 vendor/dust-0.1.6/test/passing_unit_test.rb delete mode 100644 vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb delete mode 100644 vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb delete mode 100644 vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb delete mode 100644 vendor/dust-0.1.6/test/test_helper.rb diff --git a/test/test_helper.rb b/test/test_helper.rb index 8f79cc4..480718a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -24,7 +24,6 @@ def root require "action_controller/test_case" if Rails::VERSION::MAJOR == 2 -$LOAD_PATH << File.dirname(__FILE__) + "/../vendor/dust-0.1.6/lib" require 'dust' Test::Unit::TestCase.disallow_setup! diff --git a/unit-record.gemspec b/unit-record.gemspec index 920c964..a5d7341 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.date = %q{2015-05-29} s.description = %q{UnitRecord enables unit testing without hitting the database.} s.email = %q{daniel.manges@gmail.com} - s.files = ["lib/active_record/connection_adapters/unit_record_adapter.rb", "lib/unit_record.rb", "lib/unit_record/disconnected_active_record.rb", "lib/unit_record/disconnected_test_case.rb", "lib/unit_record/column_extension.rb", "lib/unit_record/disconnected_fixtures.rb", "lib/unit_record/association_stubbing.rb", "test/test_helper.rb", "test/active_record/connection_adapters/unit_record_adapter_test.rb", "test/db/schema.rb", "test/sample_spec.rb", "test/unit_record/disconnected_test_case_test.rb", "test/unit_record/column_test.rb", "test/unit_record/column_cacher_test.rb", "test/unit_record/unit_record_test.rb", "test/unit_record/disconnected_active_record_test.rb", "test/unit_record/association_stubbing_test.rb", "test/unit_record/controller_test.rb", "test/unit_record/column_extension_test.rb", "test/unit_record/disconnected_fixtures_test.rb", "vendor/dust-0.1.6/rakefile.rb", "vendor/dust-0.1.6/test/test_helper.rb", "vendor/dust-0.1.6/test/passing_unit_test.rb", "vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb", "vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb", "vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb", "vendor/dust-0.1.6/test/all_tests.rb", "vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb", "vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb", "vendor/dust-0.1.6/test/functional_test.rb", "vendor/dust-0.1.6/lib/dust.rb", "vendor/dust-0.1.6/lib/definition_error.rb", "vendor/dust-0.1.6/lib/object_extension.rb", "vendor/dust-0.1.6/lib/string_extension.rb", "vendor/dust-0.1.6/lib/array_extension.rb", "vendor/dust-0.1.6/lib/symbol_extension.rb", "vendor/dust-0.1.6/lib/test_case_extension.rb", "vendor/dust-0.1.6/lib/nil_extension.rb", "CHANGELOG", "LICENSE", "README.markdown", "Rakefile"] + s.files = ["lib/active_record/connection_adapters/unit_record_adapter.rb", "lib/unit_record.rb", "lib/unit_record/disconnected_active_record.rb", "lib/unit_record/disconnected_test_case.rb", "lib/unit_record/column_extension.rb", "lib/unit_record/disconnected_fixtures.rb", "lib/unit_record/association_stubbing.rb", "test/test_helper.rb", "test/active_record/connection_adapters/unit_record_adapter_test.rb", "test/db/schema.rb", "test/sample_spec.rb", "test/unit_record/disconnected_test_case_test.rb", "test/unit_record/column_test.rb", "test/unit_record/column_cacher_test.rb", "test/unit_record/unit_record_test.rb", "test/unit_record/disconnected_active_record_test.rb", "test/unit_record/association_stubbing_test.rb", "test/unit_record/controller_test.rb", "test/unit_record/column_extension_test.rb", "test/unit_record/disconnected_fixtures_test.rb", "CHANGELOG", "LICENSE", "README.markdown", "Rakefile"] s.homepage = %q{http://unit-test-ar.rubyforge.org} s.require_paths = ["lib"] s.rubyforge_project = %q{unit-test-ar} @@ -19,6 +19,7 @@ Gem::Specification.new do |s| s.summary = %q{UnitRecord enables unit testing without hitting the database.} s.add_development_dependency 'rake', '> 0' s.add_development_dependency 'rails', '3.2.22' + s.add_development_dependency 'dust', '0.1.6' s.add_development_dependency 'test-unit', '3.1.1' s.add_development_dependency 'mocha', '1.1.0' diff --git a/vendor/dust-0.1.6/README b/vendor/dust-0.1.6/README deleted file mode 100644 index 17c9365..0000000 --- a/vendor/dust-0.1.6/README +++ /dev/null @@ -1,35 +0,0 @@ -= Dust - -Dust adds descriptive block syntax test definition. - -by Jay[http://blog.jayfields.com] Fields[http://blog.jayfields.com] - -== Download and Installation - -You can download Dust from here[http://rubyforge.org/projects/dust] or install it with the following command. - - $ gem install dust - -== License - -You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt). - -== Examples - - unit_tests do - - test "assert true" do - assert_equal true, true - end - - end - -See the tests for more examples - -== IDE specific files - -You can download TextMate and JEdit Run Focused Test Commands from http://rubyforge.org/projects/dust/ (in Files) - -== Contributors - -Dan Manges, David Vollbracht, Shane Harvie \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/array_extension.rb b/vendor/dust-0.1.6/lib/array_extension.rb deleted file mode 100644 index 3f60304..0000000 --- a/vendor/dust-0.1.6/lib/array_extension.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Array #:nodoc: - def arrayize - self - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/definition_error.rb b/vendor/dust-0.1.6/lib/definition_error.rb deleted file mode 100644 index cea0715..0000000 --- a/vendor/dust-0.1.6/lib/definition_error.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Dust #:nodoc: - # Dust::DefinitionError is raised when you attempt to define a disallowed method within a test file. - # - # Test::Unit::TestCase.disallow_setup! - # - # unit_tests do - # def setup - # ... - # end - # - # test "name" do - # ... - # end - # end - # - # The above code will generate the following error - # Dust::DefinitionError: setup is not allowed on class Units::[TestClassName] - class DefinitionError < StandardError - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/dust.rb b/vendor/dust-0.1.6/lib/dust.rb deleted file mode 100644 index 94e6863..0000000 --- a/vendor/dust-0.1.6/lib/dust.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'test/unit' -require File.expand_path(File.dirname(__FILE__) + '/object_extension') -require File.expand_path(File.dirname(__FILE__) + '/array_extension') -require File.expand_path(File.dirname(__FILE__) + '/nil_extension') -require File.expand_path(File.dirname(__FILE__) + '/string_extension') -require File.expand_path(File.dirname(__FILE__) + '/symbol_extension') -require File.expand_path(File.dirname(__FILE__) + '/test_case_extension') -require File.expand_path(File.dirname(__FILE__) + '/definition_error') \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/nil_extension.rb b/vendor/dust-0.1.6/lib/nil_extension.rb deleted file mode 100644 index 11b519b..0000000 --- a/vendor/dust-0.1.6/lib/nil_extension.rb +++ /dev/null @@ -1,5 +0,0 @@ -class NilClass #:nodoc: - def arrayize - [] - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/object_extension.rb b/vendor/dust-0.1.6/lib/object_extension.rb deleted file mode 100644 index de1d587..0000000 --- a/vendor/dust-0.1.6/lib/object_extension.rb +++ /dev/null @@ -1,62 +0,0 @@ -class Object - # call-seq: unit_tests(options={}, &block) - # - # Used to define a block of unit tests. - # - # unit_tests do - # test "verify something" do - # ... - # end - # end - # - # Configuration Options: - # * allow - Allows you to specify the methods that are allowed despite being disallowed. - # See Test::Unit::TestCase.disallow_helpers! or Test::Unit::TestCase.disallow_setup! for more info - def unit_tests(options={}, &block) - do_tests("Units", options, &block) - end - - # call-seq: functional_tests(options={}, &block) - # - # Used to define a block of functional tests. - # - # functional_tests do - # test "verify something" do - # ... - # end - # end - # - # Configuration Options: - # * allow - Allows you to specify the methods that are allowed despite being disallowed. - # See Test::Unit::TestCase.disallow_helpers! or Test::Unit::TestCase.disallow_setup! for more info - def functional_tests(options={}, &block) - do_tests("Functionals", options, &block) - end - - protected - def do_tests(type, options, &block) #:nodoc: - options[:allow] = options[:allow].arrayize - full_path_file_name = eval "__FILE__", block.binding - test_name = File.basename(full_path_file_name, ".rb") - test_class = eval "module #{type}; class #{test_name.to_class_name} < Test::Unit::TestCase; self; end; end" - test_class.class_eval &block - check_for_setup(test_class, options) - check_for_helpers(test_class, options) - end - - def check_for_setup(test_class, options) #:nodoc: - if test_class.instance_methods(false).include?("setup") && Test::Unit::TestCase.disallow_setup? && - !options[:allow].include?(:setup) - raise Dust::DefinitionError.new("setup is not allowed on class #{test_class.name}") - end - end - - def check_for_helpers(test_class, options) #:nodoc: - test_class.instance_methods(false).each do |method_name| - if method_name !~ /^test_/ && Test::Unit::TestCase.disallow_helpers? && !options[:allow].include?(method_name.to_sym) - p method_name.to_sym - raise Dust::DefinitionError.new("helper methods are not allowed on class #{test_class.name}") - end - end - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/string_extension.rb b/vendor/dust-0.1.6/lib/string_extension.rb deleted file mode 100644 index 8756ec0..0000000 --- a/vendor/dust-0.1.6/lib/string_extension.rb +++ /dev/null @@ -1,5 +0,0 @@ -class String #:nodoc: - def to_class_name - gsub(/(^|_)(.)/) { $2.upcase } - end -end diff --git a/vendor/dust-0.1.6/lib/symbol_extension.rb b/vendor/dust-0.1.6/lib/symbol_extension.rb deleted file mode 100644 index 1ff9448..0000000 --- a/vendor/dust-0.1.6/lib/symbol_extension.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Symbol #:nodoc: - def arrayize - [self] - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/lib/test_case_extension.rb b/vendor/dust-0.1.6/lib/test_case_extension.rb deleted file mode 100644 index 9c6f4b3..0000000 --- a/vendor/dust-0.1.6/lib/test_case_extension.rb +++ /dev/null @@ -1,76 +0,0 @@ -module Test #:nodoc: - module Unit #:nodoc: - class TestCase - # call-seq: disallow_setup! - # - # Used to disallow setup methods in test specifications. - # - # Test::Unit::TestCase.disallow_setup! - # - # A test specification can override this behavior by passing :setup in the :allow options. - # - # unit_tests :allow => :setup do - # def setup - # ... - # end - # - # test "verify something" do - # ... - # end - # end - def self.disallow_setup! - @disallow_setup = true - end - - def self.disallow_setup? #:nodoc: - @disallow_setup - end - - # call-seq: disallow_helpers! - # - # Used to disallow helper methods in test specifications. - # - # Test::Unit::TestCase.disallow_helper! - # - # A test specification can override this behavior by passing the helper name (as a symbol) in the :allow options. - # - # unit_tests :allow => [:create_something, :destroy_something] do - # test "verify something" do - # ... - # end - # - # def create_something - # ... - # end - # - # def destroy_something - # ... - # end - # end - def self.disallow_helpers! - @disallow_helpers = true - end - - def self.disallow_helpers? #:nodoc: - @disallow_helpers - end - - # call-seq: test(name, &block) - # - # Used to define a test and assign it a descriptive name. - # - # unit_tests do - # test "verify something" do - # ... - # end - # end - def self.test(name, &block) - test_name = "test_#{name.gsub(/[\s]/,'_')}".to_sym - raise "#{test_name} is already defined in #{self}" if self.instance_methods.include? test_name.to_s - define_method test_name do - instance_eval &block - end - end - end - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/rakefile.rb b/vendor/dust-0.1.6/rakefile.rb deleted file mode 100644 index 81cd5f3..0000000 --- a/vendor/dust-0.1.6/rakefile.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'rubygems' -require 'rake/gempackagetask' -require 'rake/rdoctask' -require 'rake/contrib/sshpublisher' - -task :default => :test - -task :test do - require File.dirname(__FILE__) + '/test/all_tests.rb' -end - -desc 'Generate RDoc' -Rake::RDocTask.new do |task| - task.main = 'README' - task.title = 'Dust' - task.rdoc_dir = 'doc' - task.options << "--line-numbers" << "--inline-source" - task.rdoc_files.include('README', 'lib/**/*.rb') -end - -desc "Upload RDoc to RubyForge" -task :publish_rdoc => [:rdoc] do - Rake::SshDirPublisher.new("jaycfields@rubyforge.org", "/var/www/gforge-projects/dust", "doc").upload -end - -Gem::manage_gems - -specification = Gem::Specification.new do |s| - s.name = "dust" - s.summary = "Dust is an add on for Test::Unit that allows an alternative test definintion syntax." - s.version = "0.1.6" - s.author = 'Jay Fields' - s.description = "Dust is an add on for Test::Unit that allows an alternative test definintion syntax." - s.email = 'dust-developer@rubyforge.org' - s.homepage = 'http://dust.rubyforge.org' - s.rubyforge_project = 'dust' - - s.has_rdoc = true - s.extra_rdoc_files = ['README'] - s.rdoc_options << '--title' << 'Dust' << '--main' << 'README' << '--line-numbers' - - s.autorequire = 'dust' - s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*$', 'rakefile.rb'].to_a - s.test_file = "test/all_tests.rb" -end - -Rake::GemPackageTask.new(specification) do |package| - package.need_zip = false - package.need_tar = false -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/all_tests.rb b/vendor/dust-0.1.6/test/all_tests.rb deleted file mode 100644 index 3697ba5..0000000 --- a/vendor/dust-0.1.6/test/all_tests.rb +++ /dev/null @@ -1 +0,0 @@ -Dir['**/*_test.rb'].each { |test_case| require test_case } \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb b/vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb deleted file mode 100644 index f8363e9..0000000 --- a/vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -begin - unit_tests do - Test::Unit::TestCase.disallow_helpers! - def helper_method - end - - test("true"){} - end - raise "shouldn't be here" -rescue Dust::DefinitionError => ex - raise unless ex.message == "helper methods are not allowed on class Units::FailingWithHelperUnitTest" -ensure - Test::Unit::TestCase.class_eval { @disallow_helpers = nil } -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb b/vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb deleted file mode 100644 index 1935856..0000000 --- a/vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb +++ /dev/null @@ -1,16 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -begin - unit_tests do - Test::Unit::TestCase.disallow_setup! - def setup - end - - test("true"){} - end - raise "shouldn't be here" -rescue Dust::DefinitionError => ex - raise unless ex.message == "setup is not allowed on class Units::FailingWithSetupUnitTest" -ensure - Test::Unit::TestCase.class_eval { @disallow_setup = nil } -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/functional_test.rb b/vendor/dust-0.1.6/test/functional_test.rb deleted file mode 100644 index 64fedd2..0000000 --- a/vendor/dust-0.1.6/test/functional_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -functional_tests do - test "assert true" do - assert_equal true, true - end - - test "class name is Functionals::FunctionalTest" do - assert_equal "Functionals::FunctionalTest", self.class.name - end - -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/passing_unit_test.rb b/vendor/dust-0.1.6/test/passing_unit_test.rb deleted file mode 100644 index 7362bb7..0000000 --- a/vendor/dust-0.1.6/test/passing_unit_test.rb +++ /dev/null @@ -1,11 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -unit_tests do - test "assert true" do - assert_equal true, true - end - - test "class name is Units::PassingUnitTest" do - assert_equal "Units::PassingUnitTest", self.class.name - end -end \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb b/vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb deleted file mode 100644 index d6208df..0000000 --- a/vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -Test::Unit::TestCase.disallow_helpers! -unit_tests :allow => :helper do - def helper - end - - test("true"){} -end -Test::Unit::TestCase.class_eval { @disallow_helpers = nil } \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb b/vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb deleted file mode 100644 index eaaafb1..0000000 --- a/vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -Test::Unit::TestCase.disallow_helpers! -unit_tests :allow => [:helper, :helper2] do - def helper - end - - def helper2 - end - - test("true"){} -end -Test::Unit::TestCase.class_eval { @disallow_helpers = nil } \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb b/vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb deleted file mode 100644 index 5f2de9c..0000000 --- a/vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + "/test_helper") - -Test::Unit::TestCase.disallow_setup! -unit_tests :allow => :setup do - def setup - end - - test("true"){} -end -Test::Unit::TestCase.class_eval { @disallow_setup = nil } \ No newline at end of file diff --git a/vendor/dust-0.1.6/test/test_helper.rb b/vendor/dust-0.1.6/test/test_helper.rb deleted file mode 100644 index 30b300c..0000000 --- a/vendor/dust-0.1.6/test/test_helper.rb +++ /dev/null @@ -1 +0,0 @@ -require File.dirname(__FILE__) + '/../lib/dust' From c223bb006edddc5a37fc93709650a07dc8e55fc4 Mon Sep 17 00:00:00 2001 From: Ryan/Sang Date: Fri, 11 Sep 2015 15:41:44 -0400 Subject: [PATCH 09/20] Reduce the number of files that gem will build --- unit-record.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit-record.gemspec b/unit-record.gemspec index a5d7341..fc46e9e 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.date = %q{2015-05-29} s.description = %q{UnitRecord enables unit testing without hitting the database.} s.email = %q{daniel.manges@gmail.com} - s.files = ["lib/active_record/connection_adapters/unit_record_adapter.rb", "lib/unit_record.rb", "lib/unit_record/disconnected_active_record.rb", "lib/unit_record/disconnected_test_case.rb", "lib/unit_record/column_extension.rb", "lib/unit_record/disconnected_fixtures.rb", "lib/unit_record/association_stubbing.rb", "test/test_helper.rb", "test/active_record/connection_adapters/unit_record_adapter_test.rb", "test/db/schema.rb", "test/sample_spec.rb", "test/unit_record/disconnected_test_case_test.rb", "test/unit_record/column_test.rb", "test/unit_record/column_cacher_test.rb", "test/unit_record/unit_record_test.rb", "test/unit_record/disconnected_active_record_test.rb", "test/unit_record/association_stubbing_test.rb", "test/unit_record/controller_test.rb", "test/unit_record/column_extension_test.rb", "test/unit_record/disconnected_fixtures_test.rb", "CHANGELOG", "LICENSE", "README.markdown", "Rakefile"] + s.files = Dir['lib/**/*rb'].concat(['CHANGELOG', 'LICENSE', 'README.md']) s.homepage = %q{http://unit-test-ar.rubyforge.org} s.require_paths = ["lib"] s.rubyforge_project = %q{unit-test-ar} From 10dfe6a24faab07f2f714a5dae2d2c9460b810e9 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 15:29:07 -0400 Subject: [PATCH 10/20] Add the ability to easily test against multiple versions of Rails --- .gitignore | 2 +- Gemfile | 2 ++ Gemfile.rails-3.0 | 5 +++++ Gemfile.rails-3.1 | 5 +++++ Gemfile.rails-3.2 | 5 +++++ Rakefile | 44 +------------------------------------------- test.sh | 4 ++++ unit-record.gemspec | 1 - update.sh | 4 ++++ 9 files changed, 27 insertions(+), 45 deletions(-) create mode 100644 Gemfile.rails-3.0 create mode 100644 Gemfile.rails-3.1 create mode 100644 Gemfile.rails-3.2 create mode 100755 test.sh create mode 100755 update.sh diff --git a/.gitignore b/.gitignore index 945873c..8332caf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ pkg *.gem .treasure_map.rb .bundle -Gemfile.lock +Gemfile*.lock vendor/bundle diff --git a/Gemfile b/Gemfile index fa75df1..32fd11c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' gemspec + +gem 'rails', '~> 3.2.0' diff --git a/Gemfile.rails-3.0 b/Gemfile.rails-3.0 new file mode 100644 index 0000000..c062a3c --- /dev/null +++ b/Gemfile.rails-3.0 @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gemspec + +gem 'rails', '~> 3.0.0' diff --git a/Gemfile.rails-3.1 b/Gemfile.rails-3.1 new file mode 100644 index 0000000..5636d10 --- /dev/null +++ b/Gemfile.rails-3.1 @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gemspec + +gem 'rails', '~> 3.1.0' diff --git a/Gemfile.rails-3.2 b/Gemfile.rails-3.2 new file mode 100644 index 0000000..32fd11c --- /dev/null +++ b/Gemfile.rails-3.2 @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gemspec + +gem 'rails', '~> 3.2.0' diff --git a/Rakefile b/Rakefile index 14016b3..191d1ae 100644 --- a/Rakefile +++ b/Rakefile @@ -2,7 +2,7 @@ require 'rake' require 'rake/testtask' desc "Default: run tests" -task :default => %w[test:multi_verbose spec] +task :default => %w[test spec] Rake::TestTask.new("test") do |t| t.pattern = "test/**/*_test.rb" @@ -20,28 +20,6 @@ begin rescue LoadError end -require "date" - -gem_spec = Gem::Specification.new do |s| - s.name = "unit_record" - s.summary = "UnitRecord enables unit testing without hitting the database." - s.version = "0.9.1" - s.author = "Dan Manges" - s.description = "UnitRecord enables unit testing without hitting the database." - s.email = "daniel.manges@gmail.com" - s.homepage = "http://unit-test-ar.rubyforge.org" - s.rubyforge_project = "unit-test-ar" - - s.has_rdoc = false - - s.autorequire = "unit_record" - s.files = FileList['{lib,test,vendor}/**/*.rb', 'CHANGELOG', 'LICENSE', 'README.markdown', 'Rakefile'].to_a -end - -task :gem => %w[test:multi] do - Gem::Builder.new(gem_spec).build -end - namespace :gemspec do desc "generates unit-record.gemspec" task :generate do @@ -59,26 +37,6 @@ task :readme do sh "open #{file}" end -RAILS_VERSIONS = %w[3.2.21] - -namespace :test do - desc "test with multiple versions of rails" - task :multi do - RAILS_VERSIONS.each do |rails_version| - puts "Testing with Rails #{rails_version}" - sh "RAILS_VERSION='#{rails_version}' rake test > /dev/null 2>&1" - end - end - - task :multi_verbose do - (RAILS_VERSIONS - %w[]).each do |rails_version| - task = defined?(Rcov) ? "rcov" : "test" - puts "Testing with Rails #{rails_version}" - sh "RAILS_VERSION='#{rails_version}' rake #{task}" - end - end -end - begin gem "rspec" require "spec/rake/spectask" diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..768c083 --- /dev/null +++ b/test.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +BUNDLE_GEMFILE=Gemfile.rails-3.2 bundle && BUNDLE_GEMFILE=Gemfile.rails-3.2 bundle exec rake +BUNDLE_GEMFILE=Gemfile.rails-3.1 bundle && BUNDLE_GEMFILE=Gemfile.rails-3.1 bundle exec rake +BUNDLE_GEMFILE=Gemfile.rails-3.0 bundle && BUNDLE_GEMFILE=Gemfile.rails-3.0 bundle exec rake diff --git a/unit-record.gemspec b/unit-record.gemspec index fc46e9e..42375d7 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -18,7 +18,6 @@ Gem::Specification.new do |s| s.rubygems_version = %q{1.3.1} s.summary = %q{UnitRecord enables unit testing without hitting the database.} s.add_development_dependency 'rake', '> 0' - s.add_development_dependency 'rails', '3.2.22' s.add_development_dependency 'dust', '0.1.6' s.add_development_dependency 'test-unit', '3.1.1' s.add_development_dependency 'mocha', '1.1.0' diff --git a/update.sh b/update.sh new file mode 100755 index 0000000..6450466 --- /dev/null +++ b/update.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +BUNDLE_GEMFILE=Gemfile.rails-3.2 bundle update +BUNDLE_GEMFILE=Gemfile.rails-3.1 bundle update +BUNDLE_GEMFILE=Gemfile.rails-3.0 bundle update From 322d8b39fd0c9fac3361a5ad68e95b439bdf98d1 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 15:38:19 -0400 Subject: [PATCH 11/20] Fix rspec example --- Rakefile | 17 +++++------------ test/sample_spec.rb | 17 ++++++++--------- unit-record.gemspec | 1 + 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/Rakefile b/Rakefile index 191d1ae..ae1f846 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require 'rake' require 'rake/testtask' +require "spec/rake/spectask" desc "Default: run tests" task :default => %w[test spec] @@ -9,6 +10,10 @@ Rake::TestTask.new("test") do |t| t.verbose = true end +Spec::Rake::SpecTask.new(:spec) do |t| + t.spec_files = %w[test/sample_spec.rb] +end + begin require "rcov/rcovtask" desc "run tests with rcov" @@ -37,17 +42,5 @@ task :readme do sh "open #{file}" end -begin - gem "rspec" - require "spec/rake/spectask" - Spec::Rake::SpecTask.new(:spec) do |t| - t.spec_files = %w[test/sample_spec.rb] - end -rescue LoadError - task :spec do - puts "== RSpec failed to load" - end -end - desc "pre-commit task" task :pc => %w[test:multi spec gemspec:generate] diff --git a/test/sample_spec.rb b/test/sample_spec.rb index 9a92089..c053587 100644 --- a/test/sample_spec.rb +++ b/test/sample_spec.rb @@ -1,19 +1,18 @@ require "rubygems" -gem "rspec", "1.1.11" -require "test/unit" require "spec" $:.unshift(File.dirname(__FILE__) + '/../lib') -if rails_version = ENV['RAILS_VERSION'] - gem "rails", rails_version +require "rails/all" +module Rails + class << self + def root + @root ||= Pathname.new(File.dirname(__FILE__)) + end + end end -require "rails/version" + puts "==== Testing with Rails #{Rails::VERSION::STRING} ====" -require 'active_record' -require 'active_record/fixtures' -require "action_controller" -require "action_controller/test_process" require "unit_record" diff --git a/unit-record.gemspec b/unit-record.gemspec index 42375d7..f587636 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'rake', '> 0' s.add_development_dependency 'dust', '0.1.6' s.add_development_dependency 'test-unit', '3.1.1' + s.add_development_dependency 'rspec', '1.1.11' s.add_development_dependency 'mocha', '1.1.0' if s.respond_to? :specification_version then From 70f13ac45041a605efded3c97c7b546cd870b5fe Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 16:14:54 -0400 Subject: [PATCH 12/20] Remove spec from default rake task. Rspec and TestUnit do not play nicely together. --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index ae1f846..eed5a98 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ require 'rake/testtask' require "spec/rake/spectask" desc "Default: run tests" -task :default => %w[test spec] +task :default => %w[test] Rake::TestTask.new("test") do |t| t.pattern = "test/**/*_test.rb" From 3472946a4b55e84b23f40299dc74dd0cc2eafe50 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 16:21:47 -0400 Subject: [PATCH 13/20] Wire up RuboCop --- .rubocop.yml | 268 ++++++++++++++++++++++++++++++++++++++++++++ Rakefile | 5 +- unit-record.gemspec | 1 + 3 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..980be7c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,268 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2015-09-14 16:20:12 -0400 using RuboCop version 0.34.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 5 +Lint/HandleExceptions: + Exclude: + - 'Rakefile' + - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' + - 'test/unit_record/column_test.rb' + - 'test/unit_record/disconnected_active_record_test.rb' + - 'test/unit_record/disconnected_test_case_test.rb' + +# Offense count: 2 +Lint/NestedMethodDefinition: + Exclude: + - 'lib/unit_record/disconnected_fixtures.rb' + +# Offense count: 1 +Lint/RescueException: + Exclude: + - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' + +# Offense count: 16 +# Cop supports --auto-correct. +Lint/UnusedMethodArgument: + Exclude: + - 'lib/active_record/connection_adapters/unit_record_adapter.rb' + - 'lib/unit_record.rb' + - 'lib/unit_record/disconnected_fixtures.rb' + - 'lib/unit_record/disconnected_test_case.rb' + - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' + +# Offense count: 1 +Lint/UselessAssignment: + Exclude: + - 'unit-record.gemspec' + +# Offense count: 1 +Metrics/AbcSize: + Max: 16 + +# Offense count: 55 +# Configuration parameters: AllowURI, URISchemes. +Metrics/LineLength: + Max: 154 + +# Offense count: 2 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 14 + +# Offense count: 20 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/AlignParameters: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods. +Style/BlockDelimiters: + Enabled: false + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/ClassAndModuleChildren: + Exclude: + - 'lib/active_record/connection_adapters/unit_record_adapter.rb' + +# Offense count: 17 +Style/Documentation: + Exclude: + - 'lib/active_record/connection_adapters/unit_record_adapter.rb' + - 'lib/unit_record.rb' + - 'lib/unit_record/association_stubbing.rb' + - 'lib/unit_record/column_extension.rb' + - 'lib/unit_record/disconnected_active_record.rb' + - 'lib/unit_record/disconnected_fixtures.rb' + - 'lib/unit_record/disconnected_test_case.rb' + - 'test/sample_spec.rb' + - 'test/test_helper.rb' + - 'test/unit_record/controller_test.rb' + +# Offense count: 1 +Style/EachWithObject: + Exclude: + - 'lib/unit_record/association_stubbing.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyElse: + Exclude: + - 'lib/unit_record.rb' + - 'unit-record.gemspec' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowAdjacentOneLineDefs. +Style/EmptyLineBetweenDefs: + Exclude: + - 'lib/unit_record/disconnected_fixtures.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/EmptyLines: + Exclude: + - 'test/unit_record/controller_test.rb' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyLinesAroundBlockBody: + Exclude: + - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' + - 'test/unit_record/association_stubbing_test.rb' + - 'test/unit_record/unit_record_test.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/EmptyLinesAroundModuleBody: + Exclude: + - 'lib/unit_record/association_stubbing.rb' + +# Offense count: 2 +# Configuration parameters: Exclude. +Style/FileName: + Exclude: + - 'bin/ruby-parse' + - 'bin/ruby-rewrite' + +# Offense count: 62 +# Cop supports --auto-correct. +# Configuration parameters: SupportedStyles, UseHashRocketsWithSymbolValues. +Style/HashSyntax: + EnforcedStyle: hash_rockets + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: MaxLineLength. +Style/IfUnlessModifier: + Exclude: + - 'test/unit_record/disconnected_fixtures_test.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: Width. +Style/IndentationWidth: + Exclude: + - 'test/test_helper.rb' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/MultilineIfThen: + Exclude: + - 'unit-record.gemspec' + +# Offense count: 1 +# Cop supports --auto-correct. +Style/NilComparison: + Exclude: + - 'test/sample_spec.rb' + +# Offense count: 12 +# Cop supports --auto-correct. +# Configuration parameters: PreferredDelimiters. +Style/PercentLiteralDelimiters: + Exclude: + - 'Rakefile' + - 'unit-record.gemspec' + +# Offense count: 2 +# Cop supports --auto-correct. +Style/RedundantSelf: + Exclude: + - 'lib/unit_record/association_stubbing.rb' + - 'lib/unit_record/disconnected_test_case.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowAsExpressionSeparator. +Style/Semicolon: + Exclude: + - 'Rakefile' + +# Offense count: 4 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/SignalException: + Exclude: + - 'lib/active_record/connection_adapters/unit_record_adapter.rb' + - 'lib/unit_record/disconnected_test_case.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: AllowIfMethodIsEmpty. +Style/SingleLineMethods: + Exclude: + - 'test/unit_record/association_stubbing_test.rb' + +# Offense count: 3 +# Cop supports --auto-correct. +Style/SpaceAfterComma: + Exclude: + - 'lib/unit_record/association_stubbing.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/SpaceAroundEqualsInParameterDefault: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +Style/SpaceBeforeComment: + Exclude: + - 'lib/active_record/connection_adapters/unit_record_adapter.rb' + +# Offense count: 10 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. +Style/SpaceInsideHashLiteralBraces: + Enabled: false + +# Offense count: 2 +# Cop supports --auto-correct. +Style/SpecialGlobalVars: + Exclude: + - 'test/sample_spec.rb' + - 'test/test_helper.rb' + +# Offense count: 221 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/StringLiterals: + Enabled: false + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles. +Style/TrailingBlankLines: + Exclude: + - 'test/unit_record/unit_record_test.rb' + +# Offense count: 33 +# Cop supports --auto-correct. +Style/TrailingWhitespace: + Exclude: + - 'lib/active_record/connection_adapters/unit_record_adapter.rb' + - 'lib/unit_record/column_extension.rb' + - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' + - 'test/db/schema.rb' + - 'test/unit_record/column_cacher_test.rb' + - 'test/unit_record/column_extension_test.rb' + - 'test/unit_record/column_test.rb' + - 'test/unit_record/disconnected_test_case_test.rb' + +# Offense count: 9 +# Cop supports --auto-correct. +Style/UnneededPercentQ: + Exclude: + - 'unit-record.gemspec' diff --git a/Rakefile b/Rakefile index eed5a98..fad0465 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,12 @@ require 'rake' require 'rake/testtask' require "spec/rake/spectask" +require 'rubocop/rake_task' desc "Default: run tests" -task :default => %w[test] +task :default => %w[rubocop test] + +RuboCop::RakeTask.new(:rubocop) Rake::TestTask.new("test") do |t| t.pattern = "test/**/*_test.rb" diff --git a/unit-record.gemspec b/unit-record.gemspec index f587636..3c71ac4 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'dust', '0.1.6' s.add_development_dependency 'test-unit', '3.1.1' s.add_development_dependency 'rspec', '1.1.11' + s.add_development_dependency 'rubocop', '~> 0.34.1' s.add_development_dependency 'mocha', '1.1.0' if s.respond_to? :specification_version then From 6259eb5ee64e3de85313a34eb77281df4e89e6dd Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 16:23:14 -0400 Subject: [PATCH 14/20] Fix RuboCop auto-correctable offenses --- .rubocop.yml | 203 +----------------- Rakefile | 37 ++-- .../unit_record_adapter.rb | 82 +++---- lib/unit_record.rb | 24 +-- lib/unit_record/association_stubbing.rb | 7 +- lib/unit_record/column_extension.rb | 4 +- lib/unit_record/disconnected_active_record.rb | 2 +- lib/unit_record/disconnected_fixtures.rb | 3 +- lib/unit_record/disconnected_test_case.rb | 6 +- .../unit_record_adapter_test.rb | 87 ++++---- test/db/schema.rb | 26 +-- test/sample_spec.rb | 24 +-- test/test_helper.rb | 100 ++++----- test/unit_record/association_stubbing_test.rb | 36 ++-- test/unit_record/column_cacher_test.rb | 26 +-- test/unit_record/column_extension_test.rb | 40 ++-- test/unit_record/column_test.rb | 36 ++-- test/unit_record/controller_test.rb | 7 +- .../disconnected_active_record_test.rb | 28 +-- .../unit_record/disconnected_fixtures_test.rb | 8 +- .../disconnected_test_case_test.rb | 10 +- test/unit_record/unit_record_test.rb | 11 +- unit-record.gemspec | 32 ++- 23 files changed, 319 insertions(+), 520 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 980be7c..d515266 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,11 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +AllCops: + Exclude: + - 'bin/**/*' + - 'vendor/**/*' + # Offense count: 5 Lint/HandleExceptions: Exclude: @@ -25,16 +30,6 @@ Lint/RescueException: Exclude: - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' -# Offense count: 16 -# Cop supports --auto-correct. -Lint/UnusedMethodArgument: - Exclude: - - 'lib/active_record/connection_adapters/unit_record_adapter.rb' - - 'lib/unit_record.rb' - - 'lib/unit_record/disconnected_fixtures.rb' - - 'lib/unit_record/disconnected_test_case.rb' - - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' - # Offense count: 1 Lint/UselessAssignment: Exclude: @@ -54,18 +49,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 14 -# Offense count: 20 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/AlignParameters: - Enabled: false - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods. -Style/BlockDelimiters: - Enabled: false - # Offense count: 1 # Configuration parameters: EnforcedStyle, SupportedStyles. Style/ClassAndModuleChildren: @@ -90,179 +73,3 @@ Style/Documentation: Style/EachWithObject: Exclude: - 'lib/unit_record/association_stubbing.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyElse: - Exclude: - - 'lib/unit_record.rb' - - 'unit-record.gemspec' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowAdjacentOneLineDefs. -Style/EmptyLineBetweenDefs: - Exclude: - - 'lib/unit_record/disconnected_fixtures.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/EmptyLines: - Exclude: - - 'test/unit_record/controller_test.rb' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyLinesAroundBlockBody: - Exclude: - - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' - - 'test/unit_record/association_stubbing_test.rb' - - 'test/unit_record/unit_record_test.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/EmptyLinesAroundModuleBody: - Exclude: - - 'lib/unit_record/association_stubbing.rb' - -# Offense count: 2 -# Configuration parameters: Exclude. -Style/FileName: - Exclude: - - 'bin/ruby-parse' - - 'bin/ruby-rewrite' - -# Offense count: 62 -# Cop supports --auto-correct. -# Configuration parameters: SupportedStyles, UseHashRocketsWithSymbolValues. -Style/HashSyntax: - EnforcedStyle: hash_rockets - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: MaxLineLength. -Style/IfUnlessModifier: - Exclude: - - 'test/unit_record/disconnected_fixtures_test.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: Width. -Style/IndentationWidth: - Exclude: - - 'test/test_helper.rb' - -# Offense count: 2 -# Cop supports --auto-correct. -Style/MultilineIfThen: - Exclude: - - 'unit-record.gemspec' - -# Offense count: 1 -# Cop supports --auto-correct. -Style/NilComparison: - Exclude: - - 'test/sample_spec.rb' - -# Offense count: 12 -# Cop supports --auto-correct. -# Configuration parameters: PreferredDelimiters. -Style/PercentLiteralDelimiters: - Exclude: - - 'Rakefile' - - 'unit-record.gemspec' - -# Offense count: 2 -# Cop supports --auto-correct. -Style/RedundantSelf: - Exclude: - - 'lib/unit_record/association_stubbing.rb' - - 'lib/unit_record/disconnected_test_case.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowAsExpressionSeparator. -Style/Semicolon: - Exclude: - - 'Rakefile' - -# Offense count: 4 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/SignalException: - Exclude: - - 'lib/active_record/connection_adapters/unit_record_adapter.rb' - - 'lib/unit_record/disconnected_test_case.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: AllowIfMethodIsEmpty. -Style/SingleLineMethods: - Exclude: - - 'test/unit_record/association_stubbing_test.rb' - -# Offense count: 3 -# Cop supports --auto-correct. -Style/SpaceAfterComma: - Exclude: - - 'lib/unit_record/association_stubbing.rb' - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/SpaceAroundEqualsInParameterDefault: - Enabled: false - -# Offense count: 1 -# Cop supports --auto-correct. -Style/SpaceBeforeComment: - Exclude: - - 'lib/active_record/connection_adapters/unit_record_adapter.rb' - -# Offense count: 10 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. -Style/SpaceInsideHashLiteralBraces: - Enabled: false - -# Offense count: 2 -# Cop supports --auto-correct. -Style/SpecialGlobalVars: - Exclude: - - 'test/sample_spec.rb' - - 'test/test_helper.rb' - -# Offense count: 221 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/StringLiterals: - Enabled: false - -# Offense count: 1 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/TrailingBlankLines: - Exclude: - - 'test/unit_record/unit_record_test.rb' - -# Offense count: 33 -# Cop supports --auto-correct. -Style/TrailingWhitespace: - Exclude: - - 'lib/active_record/connection_adapters/unit_record_adapter.rb' - - 'lib/unit_record/column_extension.rb' - - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' - - 'test/db/schema.rb' - - 'test/unit_record/column_cacher_test.rb' - - 'test/unit_record/column_extension_test.rb' - - 'test/unit_record/column_test.rb' - - 'test/unit_record/disconnected_test_case_test.rb' - -# Offense count: 9 -# Cop supports --auto-correct. -Style/UnneededPercentQ: - Exclude: - - 'unit-record.gemspec' diff --git a/Rakefile b/Rakefile index fad0465..041a64f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,49 +1,52 @@ require 'rake' require 'rake/testtask' -require "spec/rake/spectask" +require 'spec/rake/spectask' require 'rubocop/rake_task' -desc "Default: run tests" -task :default => %w[rubocop test] +desc 'Default: run tests' +task default: %w(rubocop test) RuboCop::RakeTask.new(:rubocop) -Rake::TestTask.new("test") do |t| - t.pattern = "test/**/*_test.rb" +Rake::TestTask.new('test') do |t| + t.pattern = 'test/**/*_test.rb' t.verbose = true end Spec::Rake::SpecTask.new(:spec) do |t| - t.spec_files = %w[test/sample_spec.rb] + t.spec_files = %w(test/sample_spec.rb) end begin - require "rcov/rcovtask" - desc "run tests with rcov" + require 'rcov/rcovtask' + desc 'run tests with rcov' Rcov::RcovTask.new do |t| - t.pattern = "test/**/*_test.rb" - t.rcov_opts << ["--no-html", "--exclude 'Library,#{Gem.path.join(',')}'"] + t.pattern = 'test/**/*_test.rb' + t.rcov_opts << ['--no-html', "--exclude 'Library,#{Gem.path.join(',')}'"] t.verbose = true end rescue LoadError end namespace :gemspec do - desc "generates unit-record.gemspec" + desc 'generates unit-record.gemspec' task :generate do - File.open("unit-record.gemspec", "w") do |f| - f.puts "# this file is generated by rake gemspec:generate for github" + File.open('unit-record.gemspec', 'w') do |f| + f.puts '# this file is generated by rake gemspec:generate for github' f.write gem_spec.to_ruby end end end task :readme do - require "rubygems"; gem "BlueCloth"; require "BlueCloth"; require 'tmpdir' + require 'rubygems' + gem 'BlueCloth' + require 'BlueCloth' + require 'tmpdir' file = "#{Dir.tmpdir}/readme.html" - File.open(file, "w") { |f| f.write BlueCloth.new(File.read("README.markdown")).to_html } + File.open(file, 'w') { |f| f.write BlueCloth.new(File.read('README.markdown')).to_html } sh "open #{file}" end -desc "pre-commit task" -task :pc => %w[test:multi spec gemspec:generate] +desc 'pre-commit task' +task pc: %w(test:multi spec gemspec:generate) diff --git a/lib/active_record/connection_adapters/unit_record_adapter.rb b/lib/active_record/connection_adapters/unit_record_adapter.rb index f99cb02..ed92ea7 100644 --- a/lib/active_record/connection_adapters/unit_record_adapter.rb +++ b/lib/active_record/connection_adapters/unit_record_adapter.rb @@ -1,25 +1,25 @@ class Visitor - def accept(ast) + def accept(_ast) '' end end class ActiveRecord::ConnectionAdapters::UnitRecordAdapter < ::ActiveRecord::ConnectionAdapters::AbstractAdapter - EXCEPTION_MESSAGE = "ActiveRecord is disconnected; database access is unavailable in unit tests." + EXCEPTION_MESSAGE = 'ActiveRecord is disconnected; database access is unavailable in unit tests.' def initialize(config = {}) super @strategy = config[:strategy] || :raise - @cached_columns = {"schema_info" => []} + @cached_columns = { 'schema_info' => [] } end - - def columns(table_name, name = nil)#:nodoc: + + def columns(table_name, _name = nil) #:nodoc: @cached_columns[table_name.to_s] || - raise("Columns are not cached for '#{table_name}' - check schema.rb") + fail("Columns are not cached for '#{table_name}' - check schema.rb") end - def create_table(table_name, options={}) + def create_table(table_name, options = {}) table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self) - table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false + table_definition.primary_key(options[:primary_key] || 'id') unless options[:id] == false yield table_definition @cached_columns[table_name.to_s] = table_definition.columns.map do |c| @@ -27,31 +27,31 @@ def create_table(table_name, options={}) end end - def primary_key(*args) + def primary_key(*_args) 'id' end - + def native_database_types # Copied from the MysqlAdapter so ColumnDefinition#sql_type will work { - :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", - :string => { :name => "varchar", :limit => 255 }, - :text => { :name => "text" }, - :integer => { :name => "int", :limit => 11 }, - :float => { :name => "float" }, - :decimal => { :name => "decimal" }, - :datetime => { :name => "datetime" }, - :timestamp => { :name => "datetime" }, - :time => { :name => "time" }, - :date => { :name => "date" }, - :binary => { :name => "blob" }, - :boolean => { :name => "tinyint", :limit => 1 } + primary_key: 'int(11) DEFAULT NULL auto_increment PRIMARY KEY', + string: { name: 'varchar', limit: 255 }, + text: { name: 'text' }, + integer: { name: 'int', limit: 11 }, + float: { name: 'float' }, + decimal: { name: 'decimal' }, + datetime: { name: 'datetime' }, + timestamp: { name: 'datetime' }, + time: { name: 'time' }, + date: { name: 'date' }, + binary: { name: 'blob' }, + boolean: { name: 'tinyint', limit: 1 } } end - - def change_strategy(new_strategy, &block) + + def change_strategy(new_strategy, &_block) unless [:noop, :raise].include?(new_strategy.to_sym) - raise ArgumentError, "#{new_strategy.inspect} is not a valid strategy - valid values are :noop and :raise" + fail ArgumentError, "#{new_strategy.inspect} is not a valid strategy - valid values are :noop and :raise" end begin old_strategy = @strategy @@ -61,32 +61,32 @@ def change_strategy(new_strategy, &block) @strategy = old_strategy end end - - def execute(*args) + + def execute(*_args) raise_or_noop end - - def select_rows(*args) + + def select_rows(*_args) raise_or_noop [] end - - def rename_table(*args) + + def rename_table(*_args) raise_or_noop end - - def change_column(*args) + + def change_column(*_args) raise_or_noop end - - def change_column_default(*args) + + def change_column_default(*_args) raise_or_noop end - def rename_column(*args) + def rename_column(*_args) raise_or_noop end - def add_foreign_key(*args) + def add_foreign_key(*_args) raise_or_noop end @@ -103,12 +103,12 @@ def quote_column_name(column_name) end protected - + def raise_or_noop(noop_return_value = nil) - @strategy == :raise ? raise(EXCEPTION_MESSAGE) : noop_return_value + @strategy == :raise ? fail(EXCEPTION_MESSAGE) : noop_return_value end - - def select(*args) + + def select(*_args) raise_or_noop [] end end diff --git a/lib/unit_record.rb b/lib/unit_record.rb index 6f2f279..944d1ca 100644 --- a/lib/unit_record.rb +++ b/lib/unit_record.rb @@ -1,33 +1,29 @@ module UnitRecord def self.rails_version - if defined?(Rails::VERSION::STRING) - Rails::VERSION::STRING - else - nil - end + Rails::VERSION::STRING if defined?(Rails::VERSION::STRING) end def self.base_rails_test_class - if rails_version && rails_version >= "2.3.1" + if rails_version && rails_version >= '2.3.1' ActiveSupport::TestCase elsif defined?(Test::Unit::TestCase) Test::Unit::TestCase else Class.new do - def self.disconnect!(*args) + def self.disconnect!(*_args) end end end end end -require "unit_record/association_stubbing" -require "unit_record/column_extension" -require "unit_record/disconnected_active_record" -require "unit_record/disconnected_test_case" -require "unit_record/disconnected_fixtures" -require "active_record/connection_adapters/unit_record_adapter" +require 'unit_record/association_stubbing' +require 'unit_record/column_extension' +require 'unit_record/disconnected_active_record' +require 'unit_record/disconnected_test_case' +require 'unit_record/disconnected_fixtures' +require 'active_record/connection_adapters/unit_record_adapter' -require "active_record/fixtures" +require 'active_record/fixtures' ActiveRecord::ConnectionAdapters::Column.send :include, UnitRecord::ColumnExtension ActiveRecord::Base.extend UnitRecord::DisconnectedActiveRecord diff --git a/lib/unit_record/association_stubbing.rb b/lib/unit_record/association_stubbing.rb index e938703..5c77fb7 100644 --- a/lib/unit_record/association_stubbing.rb +++ b/lib/unit_record/association_stubbing.rb @@ -1,6 +1,5 @@ module UnitRecord module AssociationStubbing - private def initialize_with_association_stubbing(attributes = {}) @@ -12,7 +11,7 @@ def initialize_with_association_stubbing(attributes = {}) protected def extract_associations(attributes = {}) - attributes.inject({}) do |associations,(attr,value)| + attributes.inject({}) do |associations, (attr, value)| next associations unless self.class.reflections.keys.include? attr unless value.is_a?(self.class.reflections[attr].klass) associations[attr] = attributes.delete attr @@ -22,8 +21,8 @@ def extract_associations(attributes = {}) end def stub_associations(associations = {}) - associations.each do |attr,value| - self.stubs(attr).returns(value) + associations.each do |attr, value| + stubs(attr).returns(value) end end diff --git a/lib/unit_record/column_extension.rb b/lib/unit_record/column_extension.rb index fb166c5..bc90868 100644 --- a/lib/unit_record/column_extension.rb +++ b/lib/unit_record/column_extension.rb @@ -5,9 +5,9 @@ def self.included(base) alias_method_chain :simplified_type, :boolean end end - + def simplified_type_with_boolean(field_type) - return :boolean if field_type.to_s.downcase.index("tinyint(1)") + return :boolean if field_type.to_s.downcase.index('tinyint(1)') simplified_type_without_boolean field_type end diff --git a/lib/unit_record/disconnected_active_record.rb b/lib/unit_record/disconnected_active_record.rb index 14dcc13..166aac1 100644 --- a/lib/unit_record/disconnected_active_record.rb +++ b/lib/unit_record/disconnected_active_record.rb @@ -6,7 +6,7 @@ def disconnected? def disconnect!(options = {}) return if disconnected? - establish_connection options.merge(:adapter => "unit_record") + establish_connection options.merge(adapter: 'unit_record') if options[:stub_associations] ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing end diff --git a/lib/unit_record/disconnected_fixtures.rb b/lib/unit_record/disconnected_fixtures.rb index 4b95acc..296f400 100644 --- a/lib/unit_record/disconnected_fixtures.rb +++ b/lib/unit_record/disconnected_fixtures.rb @@ -2,7 +2,8 @@ module UnitRecord module DisconnectedFixtures def disconnect! (class << self; self; end).class_eval do - def create_fixtures(*args); end + def create_fixtures(*_args); end + def reset_cache; end end end diff --git a/lib/unit_record/disconnected_test_case.rb b/lib/unit_record/disconnected_test_case.rb index 9807073..283faba 100644 --- a/lib/unit_record/disconnected_test_case.rb +++ b/lib/unit_record/disconnected_test_case.rb @@ -1,11 +1,11 @@ module UnitRecord module DisconnectedTestCase def disconnect! - self.use_transactional_fixtures = false if defined?(self.use_transactional_fixtures) + self.use_transactional_fixtures = false if defined?(use_transactional_fixtures) class_eval do - def self.fixtures(*args) - raise "Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests." + def self.fixtures(*_args) + fail 'Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests.' end end end diff --git a/test/active_record/connection_adapters/unit_record_adapter_test.rb b/test/active_record/connection_adapters/unit_record_adapter_test.rb index 9600a09..38f1f70 100644 --- a/test/active_record/connection_adapters/unit_record_adapter_test.rb +++ b/test/active_record/connection_adapters/unit_record_adapter_test.rb @@ -1,104 +1,103 @@ -require File.dirname(__FILE__) + "/../../test_helper" +require File.dirname(__FILE__) + '/../../test_helper' functional_tests do - - test "reconnect works" do + test 'reconnect works' do ActiveRecord::Base.connection.reconnect! assert_kind_of ActiveRecord::ConnectionAdapters::UnitRecordAdapter, - ActiveRecord::Base.connection + ActiveRecord::Base.connection end - test "all arel" do + test 'all arel' do ActiveRecord::Base.connection.change_strategy(:raise) do - assert_raises(RuntimeError) { + assert_raises(RuntimeError) do Person.all - } + end end ActiveRecord::Base.connection.change_strategy(:noop) do assert_equal [], Person.all end end - - test "execute raises an exception" do + + test 'execute raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.execute "SELECT 1" + ActiveRecord::Base.connection.execute 'SELECT 1' end end - - test "select_rows raises an exception" do + + test 'select_rows raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.select_rows "SELECT * FROM people" + ActiveRecord::Base.connection.select_rows 'SELECT * FROM people' end end - test "select raises an exception" do + test 'select raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.send :select, "SELECT * FROM people" + ActiveRecord::Base.connection.send :select, 'SELECT * FROM people' end end - - test "rename_table raises an exception" do + + test 'rename_table raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.rename_table "people", "persons" + ActiveRecord::Base.connection.rename_table 'people', 'persons' end end - test "change_column raises an exception" do + test 'change_column raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.change_column "people", "first_name", :string, :null => false + ActiveRecord::Base.connection.change_column 'people', 'first_name', :string, null: false end end - test "change_column_default raises an exception" do + test 'change_column_default raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.change_column_default "people", "first_person", "george" + ActiveRecord::Base.connection.change_column_default 'people', 'first_person', 'george' end end - test "rename_column raises an exception" do + test 'rename_column raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.rename_column "people", "first_name", "name_first" + ActiveRecord::Base.connection.rename_column 'people', 'first_name', 'name_first' end end - test "insert raises an exception" do + test 'insert raises an exception' do assert_raises_disconnected_exception do - ActiveRecord::Base.connection.rename_column "people", "first_name", "name_first" + ActiveRecord::Base.connection.rename_column 'people', 'first_name', 'name_first' end end - - test "initialize can set strategy" do - ActiveRecord::Base.establish_connection :adapter => "unit_record", :strategy => :noop - assert_nil ActiveRecord::Base.connection.execute("SELECT 1") - ActiveRecord::Base.establish_connection :adapter => "unit_record", :strategy => :raise - assert_raises(RuntimeError) { ActiveRecord::Base.connection.execute("SELECT 1") } + + test 'initialize can set strategy' do + ActiveRecord::Base.establish_connection adapter: 'unit_record', strategy: :noop + assert_nil ActiveRecord::Base.connection.execute('SELECT 1') + ActiveRecord::Base.establish_connection adapter: 'unit_record', strategy: :raise + assert_raises(RuntimeError) { ActiveRecord::Base.connection.execute('SELECT 1') } end - test "noop" do + test 'noop' do ActiveRecord::Base.connection.change_strategy(:noop) do - assert_nil ActiveRecord::Base.connection.execute("SELECT 1") - assert_equal [], ActiveRecord::Base.connection.select_rows("SELECT * FROM people") - assert_equal [], ActiveRecord::Base.connection.send(:select, "SELECT * FROM people") - assert_nil ActiveRecord::Base.connection.rename_table("people", "persons") - assert_nil ActiveRecord::Base.connection.change_column("people", "first_name", :string, :null => false) - assert_nil ActiveRecord::Base.connection.change_column_default("people", "first_person", "george") - assert_nil ActiveRecord::Base.connection.rename_column("people", "first_name", "name_first") + assert_nil ActiveRecord::Base.connection.execute('SELECT 1') + assert_equal [], ActiveRecord::Base.connection.select_rows('SELECT * FROM people') + assert_equal [], ActiveRecord::Base.connection.send(:select, 'SELECT * FROM people') + assert_nil ActiveRecord::Base.connection.rename_table('people', 'persons') + assert_nil ActiveRecord::Base.connection.change_column('people', 'first_name', :string, null: false) + assert_nil ActiveRecord::Base.connection.change_column_default('people', 'first_person', 'george') + assert_nil ActiveRecord::Base.connection.rename_column('people', 'first_name', 'name_first') end end - - test "change_strategy raises if invalid strategy" do + + test 'change_strategy raises if invalid strategy' do assert_nothing_raised { ActiveRecord::Base.connection.change_strategy(:noop) {} } assert_nothing_raised { ActiveRecord::Base.connection.change_strategy(:raise) {} } assert_raises(ArgumentError) { ActiveRecord::Base.connection.change_strategy(:bogus) {} } end - def assert_raises_disconnected_exception(&block) + def assert_raises_disconnected_exception(&_block) exception = nil begin yield rescue Exception => exception end assert_not_nil exception - assert_equal "ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message + assert_equal 'ActiveRecord is disconnected; database access is unavailable in unit tests.', exception.message end end diff --git a/test/db/schema.rb b/test/db/schema.rb index ba36174..c28535a 100644 --- a/test/db/schema.rb +++ b/test/db/schema.rb @@ -1,26 +1,26 @@ ActiveRecord::Schema.define do - create_table :preferences, :force => true do |t| + create_table :preferences, force: true do |t| t.column :some_count, :integer - t.column :show_help, :boolean, :default => true + t.column :show_help, :boolean, default: true end - create_table :people, :force => true do |t| + create_table :people, force: true do |t| t.column :first_name, :string t.column :last_name, :string end - - create_table "profiles", :force => true do |t| - t.column "description", :string - t.column "person_id", :integer + + create_table 'profiles', force: true do |t| + t.column 'description', :string + t.column 'person_id', :integer end - - create_table :pets, :force => true do |t| + + create_table :pets, force: true do |t| t.column :name, :string end - - create_table :foofoo, :force => true do |t| + + create_table :foofoo, force: true do |t| t.column :bar, :string end - - add_index "people", ["first_name"] + + add_index 'people', ['first_name'] end diff --git a/test/sample_spec.rb b/test/sample_spec.rb index c053587..08b8541 100644 --- a/test/sample_spec.rb +++ b/test/sample_spec.rb @@ -1,9 +1,9 @@ -require "rubygems" -require "spec" +require 'rubygems' +require 'spec' -$:.unshift(File.dirname(__FILE__) + '/../lib') +$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') -require "rails/all" +require 'rails/all' module Rails class << self def root @@ -14,28 +14,28 @@ def root puts "==== Testing with Rails #{Rails::VERSION::STRING} ====" -require "unit_record" +require 'unit_record' -if UnitRecord.rails_version >= "2.3" +if UnitRecord.rails_version >= '2.3' ActiveSupport::TestCase.class_eval { include ActiveRecord::TestFixtures } end # Needed because of this line in setup_with_fixtures and teardown_with_fixtures: # return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? -ActiveRecord::Base.configurations = {"irrelevant" => {:adapter => "stub"}} +ActiveRecord::Base.configurations = { 'irrelevant' => { adapter: 'stub' } } -ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true +ActiveRecord::Base.disconnect! strategy: :raise, stub_associations: true describe UnitRecord do - it "disconnects tests from the database" do + it 'disconnects tests from the database' do lambda do - ActiveRecord::Base.connection.select_value("SELECT 1") + ActiveRecord::Base.connection.select_value('SELECT 1') end.should raise_error end - it "can change strategy to noop" do + it 'can change strategy to noop' do ActiveRecord::Base.connection.change_strategy(:noop) do - ActiveRecord::Base.connection.select_value("SELECT 1").should == nil + ActiveRecord::Base.connection.select_value('SELECT 1').should.nil? end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 480718a..b1ed9ee 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,71 +1,71 @@ unless defined?(TEST_HELPER_LOADED) -TEST_HELPER_LOADED = true -$:.unshift(File.dirname(__FILE__) + '/../lib') + TEST_HELPER_LOADED = true + $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') -require 'rubygems' -require 'rails/all' + require 'rubygems' + require 'rails/all' -module Rails - class << self - def root - @root ||= Pathname.new(File.dirname(__FILE__)) + module Rails + class << self + def root + @root ||= Pathname.new(File.dirname(__FILE__)) + end end end -end -if Rails::VERSION::MAJOR > 3 && Rails::VERSION::MINOR > 0 - if defined?(Bundler) - # If you precompile assets before deploying to production, use this line - Bundler.require(*Rails.groups(:assets => %w(development test))) - # If you want your assets lazily compiled in production, use this line - # Bundler.require(:default, :assets, Rails.env) + if Rails::VERSION::MAJOR > 3 && Rails::VERSION::MINOR > 0 + if defined?(Bundler) + # If you precompile assets before deploying to production, use this line + Bundler.require(*Rails.groups(assets: %w(development test))) + # If you want your assets lazily compiled in production, use this line + # Bundler.require(:default, :assets, Rails.env) + end end -end -require "action_controller/test_case" if Rails::VERSION::MAJOR == 2 + require 'action_controller/test_case' if Rails::VERSION::MAJOR == 2 -require 'dust' -Test::Unit::TestCase.disallow_setup! + require 'dust' + Test::Unit::TestCase.disallow_setup! -$LOAD_PATH << File.dirname(__FILE__) + "/../lib" -require "unit_record" + $LOAD_PATH << File.dirname(__FILE__) + '/../lib' + require 'unit_record' -if UnitRecord.rails_version >= "2.3" - require "active_support/test_case" - ActiveSupport::TestCase.class_eval { include ActiveRecord::TestFixtures } -end + if UnitRecord.rails_version >= '2.3' + require 'active_support/test_case' + ActiveSupport::TestCase.class_eval { include ActiveRecord::TestFixtures } + end -UnitRecord.base_rails_test_class.use_transactional_fixtures = true + UnitRecord.base_rails_test_class.use_transactional_fixtures = true -# Needed because of this line in setup_with_fixtures and teardown_with_fixtures: -# return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? -ActiveRecord::Base.configurations = {"irrelevant" => {:adapter => "stub"}} + # Needed because of this line in setup_with_fixtures and teardown_with_fixtures: + # return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? + ActiveRecord::Base.configurations = { 'irrelevant' => { adapter: 'stub' } } -class Preference < ActiveRecord::Base -end + class Preference < ActiveRecord::Base + end -class Person < ActiveRecord::Base - has_many :pets - has_one :profile -end + class Person < ActiveRecord::Base + has_many :pets + has_one :profile + end -class Profile < ActiveRecord::Base - belongs_to :person -end + class Profile < ActiveRecord::Base + belongs_to :person + end -class Pet < ActiveRecord::Base - belongs_to :person -end + class Pet < ActiveRecord::Base + belongs_to :person + end -class Foo < ActiveRecord::Base - self.table_name = 'foofoo' -end + class Foo < ActiveRecord::Base + self.table_name = 'foofoo' + end -class DoesNotExist < ActiveRecord::Base - self.table_name = 'table_does_not_exist' -end + class DoesNotExist < ActiveRecord::Base + self.table_name = 'table_does_not_exist' + end -ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true -# make sure calling disconnect multiple times does not cause problems -ActiveRecord::Base.disconnect! :strategy => :raise, :stub_associations => true + ActiveRecord::Base.disconnect! strategy: :raise, stub_associations: true + # make sure calling disconnect multiple times does not cause problems + ActiveRecord::Base.disconnect! strategy: :raise, stub_associations: true end diff --git a/test/unit_record/association_stubbing_test.rb b/test/unit_record/association_stubbing_test.rb index 63ab55f..69fc7dc 100644 --- a/test/unit_record/association_stubbing_test.rb +++ b/test/unit_record/association_stubbing_test.rb @@ -1,37 +1,37 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' functional_tests do - - test "stubbing a has_many" do + test 'stubbing a has_many' do pets = [stub, stub] - person = Person.new :pets => pets + person = Person.new pets: pets assert_equal pets, person.pets end - test "stubbing a belongs_to" do + test 'stubbing a belongs_to' do person = stub - pet = Pet.new :person => person + pet = Pet.new person: person assert_equal person, pet.person end - test "using correct classes does not stub" do - person = Person.new(:first_name => "Dan") - pet = Pet.new :person => person - pet.person = Person.new(:first_name => "Tom") - assert_equal "Tom", pet.person.first_name + test 'using correct classes does not stub' do + person = Person.new(first_name: 'Dan') + pet = Pet.new person: person + pet.person = Person.new(first_name: 'Tom') + assert_equal 'Tom', pet.person.first_name end - test "using other than correct classes does stub" do + test 'using other than correct classes does stub' do person = Object.new - def person.first_name; "Dan"; end - pet = Pet.new :person => person - pet.person = Person.new(:first_name => "Tom") - assert_equal "Dan", pet.person.first_name + def person.first_name + 'Dan' + end + pet = Pet.new person: person + pet.person = Person.new(first_name: 'Tom') + assert_equal 'Dan', pet.person.first_name end - test "multiple includes does not hurt" do + test 'multiple includes does not hurt' do ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing Person.new end - end diff --git a/test/unit_record/column_cacher_test.rb b/test/unit_record/column_cacher_test.rb index 1943747..d683b96 100644 --- a/test/unit_record/column_cacher_test.rb +++ b/test/unit_record/column_cacher_test.rb @@ -1,26 +1,26 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' functional_tests do - test "caching columns with no defaults or not nulls" do + test 'caching columns with no defaults or not nulls' do expected = [ - ActiveRecord::ConnectionAdapters::Column.new("id", nil, "int(11) DEFAULT NULL auto_increment PRIMARY KEY", nil), - ActiveRecord::ConnectionAdapters::Column.new("first_name", nil, "varchar(255)", nil), - ActiveRecord::ConnectionAdapters::Column.new("last_name", nil, "varchar(255)", nil) + ActiveRecord::ConnectionAdapters::Column.new('id', nil, 'int(11) DEFAULT NULL auto_increment PRIMARY KEY', nil), + ActiveRecord::ConnectionAdapters::Column.new('first_name', nil, 'varchar(255)', nil), + ActiveRecord::ConnectionAdapters::Column.new('last_name', nil, 'varchar(255)', nil) ] expected[0].primary = true expected[1..-1].each { |column| column.primary = false } assert_equal expected, Person.columns end - - test "caching column with default" do - expected = ActiveRecord::ConnectionAdapters::Column.new("show_help", true, "tinyint(1)", nil) + + test 'caching column with default' do + expected = ActiveRecord::ConnectionAdapters::Column.new('show_help', true, 'tinyint(1)', nil) expected.primary = false - assert_equal expected, Preference.columns.detect { |c| c.name == "show_help" } + assert_equal expected, Preference.columns.detect { |c| c.name == 'show_help' } end - - test "boolean columns" do - expected = ActiveRecord::ConnectionAdapters::Column.new("show_help", true, "tinyint(1)", nil) + + test 'boolean columns' do + expected = ActiveRecord::ConnectionAdapters::Column.new('show_help', true, 'tinyint(1)', nil) expected.primary = false - assert_equal :boolean, Preference.columns.detect { |c| c.name == "show_help" }.type + assert_equal :boolean, Preference.columns.detect { |c| c.name == 'show_help' }.type end end diff --git a/test/unit_record/column_extension_test.rb b/test/unit_record/column_extension_test.rb index 2c67b4f..23c4cbf 100644 --- a/test/unit_record/column_extension_test.rb +++ b/test/unit_record/column_extension_test.rb @@ -1,33 +1,33 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' unit_tests do - test "equality" do - column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil) - column2 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil) + test 'equality' do + column1 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, nil) + column2 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, nil) assert_equal column1, column2 end - - test "non-equality on name" do - column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil) - column2 = ActiveRecord::ConnectionAdapters::Column.new("different name", nil, :string, nil) + + test 'non-equality on name' do + column1 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, nil) + column2 = ActiveRecord::ConnectionAdapters::Column.new('different name', nil, :string, nil) assert column1 != column2 end - - test "non-equality on sql_type" do - column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil) - column2 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :text, nil) + + test 'non-equality on sql_type' do + column1 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, nil) + column2 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :text, nil) assert column1 != column2 end - - test "non-equality on default" do - column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil) - column2 = ActiveRecord::ConnectionAdapters::Column.new("name", "Dan", :string, nil) + + test 'non-equality on default' do + column1 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, nil) + column2 = ActiveRecord::ConnectionAdapters::Column.new('name', 'Dan', :string, nil) assert column1 != column2 end - - test "non-equality on null" do - column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil) - column2 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, true) + + test 'non-equality on null' do + column1 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, nil) + column2 = ActiveRecord::ConnectionAdapters::Column.new('name', nil, :string, true) assert column1 != column2 end end diff --git a/test/unit_record/column_test.rb b/test/unit_record/column_test.rb index d91ef78..523706b 100644 --- a/test/unit_record/column_test.rb +++ b/test/unit_record/column_test.rb @@ -1,34 +1,34 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' functional_tests do - test "instantiating" do - person = Person.new :first_name => "Dan", :last_name => "Manges" - assert_equal "Dan", person.first_name - assert_equal "Manges", person.last_name + test 'instantiating' do + person = Person.new first_name: 'Dan', last_name: 'Manges' + assert_equal 'Dan', person.first_name + assert_equal 'Manges', person.last_name end - - test "using model with column with a default" do + + test 'using model with column with a default' do record = Preference.new assert_equal true, record.show_help? end - - test "typecasting happens for integer attributes" do + + test 'typecasting happens for integer attributes' do record = Preference.new - record.some_count = "42" + record.some_count = '42' assert_equal 42, record.some_count end - - test "a model with a non-convential table name does not blow up" do + + test 'a model with a non-convential table name does not blow up' do assert_nothing_raised { Foo.columns } end - - test "using attribute on a model with a non-conventional table name" do + + test 'using attribute on a model with a non-conventional table name' do foo = Foo.new - foo.bar = "baz" - assert_equal "baz", foo.bar + foo.bar = 'baz' + assert_equal 'baz', foo.bar end - - test "should get a descriptive error message if no cached columns" do + + test 'should get a descriptive error message if no cached columns' do exception = nil begin DoesNotExist.columns diff --git a/test/unit_record/controller_test.rb b/test/unit_record/controller_test.rb index 59f75b1..66f6a1b 100644 --- a/test/unit_record/controller_test.rb +++ b/test/unit_record/controller_test.rb @@ -3,17 +3,16 @@ class SampleApplication < Rails::Application end SampleApplication.routes.draw do - match '/sample/sample_action', :to => 'sample#sample_action' + match '/sample/sample_action', to: 'sample#sample_action' end - SampleApplication.routes.draw do - match '/sample/sample_action', :to => 'sample#sample_action' + match '/sample/sample_action', to: 'sample#sample_action' end class SampleController < ActionController::Base include SampleApplication.routes.url_helpers def sample_action - render :text => 'OK' + render text: 'OK' end end diff --git a/test/unit_record/disconnected_active_record_test.rb b/test/unit_record/disconnected_active_record_test.rb index 81ee778..0645394 100644 --- a/test/unit_record/disconnected_active_record_test.rb +++ b/test/unit_record/disconnected_active_record_test.rb @@ -1,52 +1,52 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' functional_tests do - test "find_by_sql gives disconnected exception message" do + test 'find_by_sql gives disconnected exception message' do exception = nil begin - Person.find_by_sql "SELECT * FROM people" + Person.find_by_sql 'SELECT * FROM people' rescue => exception end assert_not_nil exception - assert_equal "ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message + assert_equal 'ActiveRecord is disconnected; database access is unavailable in unit tests.', exception.message end - test "connected? is true" do + test 'connected? is true' do assert_equal true, ActiveRecord::Base.connected? end - test "disconnected? is true" do + test 'disconnected? is true' do assert_equal true, ActiveRecord::Base.disconnected? end - test "inspect does not blow up" do + test 'inspect does not blow up' do assert_nothing_raised { Person.inspect } end - test "table_exists?" do + test 'table_exists?' do assert_equal true, Person.table_exists? if ActiveRecord::Base.connection.respond_to?(:table_exists?) - assert_equal false, ActiveRecord::Base.connection.table_exists?("bogus_table") + assert_equal false, ActiveRecord::Base.connection.table_exists?('bogus_table') end end - test "setting a has_one association" do + test 'setting a has_one association' do person = Person.new person.profile = Profile.new end - test "boolean columns do type casting" do + test 'boolean columns do type casting' do pref = Preference.new - pref.show_help = "0" + pref.show_help = '0' assert_equal false, pref.send(:read_attribute, :show_help) assert_equal false, pref.show_help assert_equal false, pref.show_help? - pref.show_help = "1" + pref.show_help = '1' assert_equal true, pref.show_help assert_equal true, pref.show_help? end - test "migrations are not verbose" do + test 'migrations are not verbose' do assert_equal false, ActiveRecord::Migration.verbose end end diff --git a/test/unit_record/disconnected_fixtures_test.rb b/test/unit_record/disconnected_fixtures_test.rb index 52c182f..a73792c 100644 --- a/test/unit_record/disconnected_fixtures_test.rb +++ b/test/unit_record/disconnected_fixtures_test.rb @@ -1,9 +1,7 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' functional_tests do - test "create_fixtures does nothing" do - if defined?(Fixtures) - assert_nothing_raised { Fixtures.create_fixtures } - end + test 'create_fixtures does nothing' do + assert_nothing_raised { Fixtures.create_fixtures } if defined?(Fixtures) end end diff --git a/test/unit_record/disconnected_test_case_test.rb b/test/unit_record/disconnected_test_case_test.rb index ba6551c..e12ea3a 100644 --- a/test/unit_record/disconnected_test_case_test.rb +++ b/test/unit_record/disconnected_test_case_test.rb @@ -1,11 +1,11 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' functional_tests do - test "use_transactional_fixtures is false" do + test 'use_transactional_fixtures is false' do assert_equal false, UnitRecord.base_rails_test_class.use_transactional_fixtures end - - test "trying to use fixtures gives useful message" do + + test 'trying to use fixtures gives useful message' do exception = nil begin Class.new(UnitRecord.base_rails_test_class) do @@ -14,6 +14,6 @@ rescue => exception end assert_not_nil exception - assert_equal "Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message + assert_equal 'Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests.', exception.message end end diff --git a/test/unit_record/unit_record_test.rb b/test/unit_record/unit_record_test.rb index f6ca5b8..0fd8a75 100644 --- a/test/unit_record/unit_record_test.rb +++ b/test/unit_record/unit_record_test.rb @@ -1,14 +1,13 @@ -require File.dirname(__FILE__) + "/../test_helper" +require File.dirname(__FILE__) + '/../test_helper' unit_tests do - if UnitRecord.rails_version > "2.3" - test "test case base class" do + if UnitRecord.rails_version > '2.3' + test 'test case base class' do assert_equal ActiveSupport::TestCase, UnitRecord.base_rails_test_class end else - test "test case base class" do + test 'test case base class' do assert_equal Test::Unit::TestCase, UnitRecord.base_rails_test_class end end - -end \ No newline at end of file +end diff --git a/unit-record.gemspec b/unit-record.gemspec index 3c71ac4..193eb44 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -2,21 +2,21 @@ # -*- encoding: utf-8 -*- Gem::Specification.new do |s| - s.name = %q{unit_record} - s.version = "0.9.2" + s.name = 'unit_record' + s.version = '0.9.2' - s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["Dan Manges"] - s.autorequire = %q{unit_record} - s.date = %q{2015-05-29} - s.description = %q{UnitRecord enables unit testing without hitting the database.} - s.email = %q{daniel.manges@gmail.com} + s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version= + s.authors = ['Dan Manges'] + s.autorequire = 'unit_record' + s.date = '2015-05-29' + s.description = 'UnitRecord enables unit testing without hitting the database.' + s.email = 'daniel.manges@gmail.com' s.files = Dir['lib/**/*rb'].concat(['CHANGELOG', 'LICENSE', 'README.md']) - s.homepage = %q{http://unit-test-ar.rubyforge.org} - s.require_paths = ["lib"] - s.rubyforge_project = %q{unit-test-ar} - s.rubygems_version = %q{1.3.1} - s.summary = %q{UnitRecord enables unit testing without hitting the database.} + s.homepage = 'http://unit-test-ar.rubyforge.org' + s.require_paths = ['lib'] + s.rubyforge_project = 'unit-test-ar' + s.rubygems_version = '1.3.1' + s.summary = 'UnitRecord enables unit testing without hitting the database.' s.add_development_dependency 'rake', '> 0' s.add_development_dependency 'dust', '0.1.6' s.add_development_dependency 'test-unit', '3.1.1' @@ -24,13 +24,11 @@ Gem::Specification.new do |s| s.add_development_dependency 'rubocop', '~> 0.34.1' s.add_development_dependency 'mocha', '1.1.0' - if s.respond_to? :specification_version then + if s.respond_to? :specification_version current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 2 - if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then - else + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') end - else end end From 6fcc705a76d87433772998f526bb351ad92489b4 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 16:45:39 -0400 Subject: [PATCH 15/20] Fix RuboCop offenses --- .rubocop.yml | 18 +- .../unit_record_adapter.rb | 180 +++++++++--------- lib/unit_record/association_stubbing.rb | 3 +- lib/unit_record/disconnected_test_case.rb | 3 +- .../disconnected_test_case_test.rb | 4 +- unit-record.gemspec | 1 - 6 files changed, 99 insertions(+), 110 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d515266..853b4cd 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -30,11 +30,6 @@ Lint/RescueException: Exclude: - 'test/active_record/connection_adapters/unit_record_adapter_test.rb' -# Offense count: 1 -Lint/UselessAssignment: - Exclude: - - 'unit-record.gemspec' - # Offense count: 1 Metrics/AbcSize: Max: 16 @@ -42,19 +37,13 @@ Metrics/AbcSize: # Offense count: 55 # Configuration parameters: AllowURI, URISchemes. Metrics/LineLength: - Max: 154 + Max: 120 # Offense count: 2 # Configuration parameters: CountComments. Metrics/MethodLength: Max: 14 -# Offense count: 1 -# Configuration parameters: EnforcedStyle, SupportedStyles. -Style/ClassAndModuleChildren: - Exclude: - - 'lib/active_record/connection_adapters/unit_record_adapter.rb' - # Offense count: 17 Style/Documentation: Exclude: @@ -68,8 +57,3 @@ Style/Documentation: - 'test/sample_spec.rb' - 'test/test_helper.rb' - 'test/unit_record/controller_test.rb' - -# Offense count: 1 -Style/EachWithObject: - Exclude: - - 'lib/unit_record/association_stubbing.rb' diff --git a/lib/active_record/connection_adapters/unit_record_adapter.rb b/lib/active_record/connection_adapters/unit_record_adapter.rb index ed92ea7..77954fe 100644 --- a/lib/active_record/connection_adapters/unit_record_adapter.rb +++ b/lib/active_record/connection_adapters/unit_record_adapter.rb @@ -3,112 +3,116 @@ def accept(_ast) '' end end -class ActiveRecord::ConnectionAdapters::UnitRecordAdapter < ::ActiveRecord::ConnectionAdapters::AbstractAdapter - EXCEPTION_MESSAGE = 'ActiveRecord is disconnected; database access is unavailable in unit tests.' - - def initialize(config = {}) - super - @strategy = config[:strategy] || :raise - @cached_columns = { 'schema_info' => [] } - end +module ActiveRecord + module ConnectionAdapters + class UnitRecordAdapter < ::ActiveRecord::ConnectionAdapters::AbstractAdapter + EXCEPTION_MESSAGE = 'ActiveRecord is disconnected; database access is unavailable in unit tests.' + + def initialize(config = {}) + super + @strategy = config[:strategy] || :raise + @cached_columns = { 'schema_info' => [] } + end - def columns(table_name, _name = nil) #:nodoc: - @cached_columns[table_name.to_s] || - fail("Columns are not cached for '#{table_name}' - check schema.rb") - end + def columns(table_name, _name = nil) #:nodoc: + @cached_columns[table_name.to_s] || + fail("Columns are not cached for '#{table_name}' - check schema.rb") + end - def create_table(table_name, options = {}) - table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self) - table_definition.primary_key(options[:primary_key] || 'id') unless options[:id] == false - yield table_definition - @cached_columns[table_name.to_s] = - table_definition.columns.map do |c| - ActiveRecord::ConnectionAdapters::Column.new(c.name.to_s, c.default, c.sql_type, c.null) + def create_table(table_name, options = {}) + table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self) + table_definition.primary_key(options[:primary_key] || 'id') unless options[:id] == false + yield table_definition + @cached_columns[table_name.to_s] = + table_definition.columns.map do |c| + ActiveRecord::ConnectionAdapters::Column.new(c.name.to_s, c.default, c.sql_type, c.null) + end end - end - def primary_key(*_args) - 'id' - end + def primary_key(*_args) + 'id' + end - def native_database_types - # Copied from the MysqlAdapter so ColumnDefinition#sql_type will work - { - primary_key: 'int(11) DEFAULT NULL auto_increment PRIMARY KEY', - string: { name: 'varchar', limit: 255 }, - text: { name: 'text' }, - integer: { name: 'int', limit: 11 }, - float: { name: 'float' }, - decimal: { name: 'decimal' }, - datetime: { name: 'datetime' }, - timestamp: { name: 'datetime' }, - time: { name: 'time' }, - date: { name: 'date' }, - binary: { name: 'blob' }, - boolean: { name: 'tinyint', limit: 1 } - } - end + def native_database_types + # Copied from the MysqlAdapter so ColumnDefinition#sql_type will work + { + primary_key: 'int(11) DEFAULT NULL auto_increment PRIMARY KEY', + string: { name: 'varchar', limit: 255 }, + text: { name: 'text' }, + integer: { name: 'int', limit: 11 }, + float: { name: 'float' }, + decimal: { name: 'decimal' }, + datetime: { name: 'datetime' }, + timestamp: { name: 'datetime' }, + time: { name: 'time' }, + date: { name: 'date' }, + binary: { name: 'blob' }, + boolean: { name: 'tinyint', limit: 1 } + } + end - def change_strategy(new_strategy, &_block) - unless [:noop, :raise].include?(new_strategy.to_sym) - fail ArgumentError, "#{new_strategy.inspect} is not a valid strategy - valid values are :noop and :raise" - end - begin - old_strategy = @strategy - @strategy = new_strategy.to_sym - yield - ensure - @strategy = old_strategy - end - end + def change_strategy(new_strategy, &_block) + unless [:noop, :raise].include?(new_strategy.to_sym) + fail ArgumentError, "#{new_strategy.inspect} is not a valid strategy - valid values are :noop and :raise" + end + begin + old_strategy = @strategy + @strategy = new_strategy.to_sym + yield + ensure + @strategy = old_strategy + end + end - def execute(*_args) - raise_or_noop - end + def execute(*_args) + raise_or_noop + end - def select_rows(*_args) - raise_or_noop [] - end + def select_rows(*_args) + raise_or_noop [] + end - def rename_table(*_args) - raise_or_noop - end + def rename_table(*_args) + raise_or_noop + end - def change_column(*_args) - raise_or_noop - end + def change_column(*_args) + raise_or_noop + end - def change_column_default(*_args) - raise_or_noop - end + def change_column_default(*_args) + raise_or_noop + end - def rename_column(*_args) - raise_or_noop - end + def rename_column(*_args) + raise_or_noop + end - def add_foreign_key(*_args) - raise_or_noop - end + def add_foreign_key(*_args) + raise_or_noop + end - def tables - @cached_columns.keys - end + def tables + @cached_columns.keys + end - def visitor - Visitor.new - end + def visitor + Visitor.new + end - def quote_column_name(column_name) - column_name.to_s - end + def quote_column_name(column_name) + column_name.to_s + end - protected + protected - def raise_or_noop(noop_return_value = nil) - @strategy == :raise ? fail(EXCEPTION_MESSAGE) : noop_return_value - end + def raise_or_noop(noop_return_value = nil) + @strategy == :raise ? fail(EXCEPTION_MESSAGE) : noop_return_value + end - def select(*_args) - raise_or_noop [] + def select(*_args) + raise_or_noop [] + end + end end end diff --git a/lib/unit_record/association_stubbing.rb b/lib/unit_record/association_stubbing.rb index 5c77fb7..251b183 100644 --- a/lib/unit_record/association_stubbing.rb +++ b/lib/unit_record/association_stubbing.rb @@ -11,12 +11,11 @@ def initialize_with_association_stubbing(attributes = {}) protected def extract_associations(attributes = {}) - attributes.inject({}) do |associations, (attr, value)| + attributes.each_with_object({}) do |(attr, value), associations| next associations unless self.class.reflections.keys.include? attr unless value.is_a?(self.class.reflections[attr].klass) associations[attr] = attributes.delete attr end - associations end end diff --git a/lib/unit_record/disconnected_test_case.rb b/lib/unit_record/disconnected_test_case.rb index 283faba..0035f0f 100644 --- a/lib/unit_record/disconnected_test_case.rb +++ b/lib/unit_record/disconnected_test_case.rb @@ -5,7 +5,8 @@ def disconnect! class_eval do def self.fixtures(*_args) - fail 'Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests.' + fail 'Fixtures cannot be used with UnitRecord. ' \ + 'ActiveRecord is disconnected; database access is unavailable in unit tests.' end end end diff --git a/test/unit_record/disconnected_test_case_test.rb b/test/unit_record/disconnected_test_case_test.rb index e12ea3a..151767f 100644 --- a/test/unit_record/disconnected_test_case_test.rb +++ b/test/unit_record/disconnected_test_case_test.rb @@ -14,6 +14,8 @@ rescue => exception end assert_not_nil exception - assert_equal 'Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests.', exception.message + expected_message = 'Fixtures cannot be used with UnitRecord. ' \ + 'ActiveRecord is disconnected; database access is unavailable in unit tests.' + assert_equal expected_message, exception.message end end diff --git a/unit-record.gemspec b/unit-record.gemspec index 193eb44..bc14ec5 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -25,7 +25,6 @@ Gem::Specification.new do |s| s.add_development_dependency 'mocha', '1.1.0' if s.respond_to? :specification_version - current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 2 if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') From f9c28d23eabcd950970cc933a254bc36d565c3e0 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 16:56:18 -0400 Subject: [PATCH 16/20] Disable RuboCop Style/Documentation --- .rubocop.yml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 853b4cd..597fe64 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -46,14 +46,4 @@ Metrics/MethodLength: # Offense count: 17 Style/Documentation: - Exclude: - - 'lib/active_record/connection_adapters/unit_record_adapter.rb' - - 'lib/unit_record.rb' - - 'lib/unit_record/association_stubbing.rb' - - 'lib/unit_record/column_extension.rb' - - 'lib/unit_record/disconnected_active_record.rb' - - 'lib/unit_record/disconnected_fixtures.rb' - - 'lib/unit_record/disconnected_test_case.rb' - - 'test/sample_spec.rb' - - 'test/test_helper.rb' - - 'test/unit_record/controller_test.rb' + Enabled: false From 812ba6d75300278ca7c5b98851f1c5d0db31734a Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 17:02:42 -0400 Subject: [PATCH 17/20] Extract constants --- .../connection_adapters/unit_record_adapter.rb | 9 ++++++--- lib/unit_record/column_extension.rb | 4 +++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/active_record/connection_adapters/unit_record_adapter.rb b/lib/active_record/connection_adapters/unit_record_adapter.rb index 77954fe..73e1aaa 100644 --- a/lib/active_record/connection_adapters/unit_record_adapter.rb +++ b/lib/active_record/connection_adapters/unit_record_adapter.rb @@ -7,6 +7,8 @@ module ActiveRecord module ConnectionAdapters class UnitRecordAdapter < ::ActiveRecord::ConnectionAdapters::AbstractAdapter EXCEPTION_MESSAGE = 'ActiveRecord is disconnected; database access is unavailable in unit tests.' + ID = 'id'.freeze + STRATEGIES = [:noop, :raise].freeze def initialize(config = {}) super @@ -21,7 +23,7 @@ def columns(table_name, _name = nil) #:nodoc: def create_table(table_name, options = {}) table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self) - table_definition.primary_key(options[:primary_key] || 'id') unless options[:id] == false + table_definition.primary_key(options[:primary_key] || ID) unless options[:id] == false yield table_definition @cached_columns[table_name.to_s] = table_definition.columns.map do |c| @@ -30,7 +32,7 @@ def create_table(table_name, options = {}) end def primary_key(*_args) - 'id' + ID end def native_database_types @@ -52,9 +54,10 @@ def native_database_types end def change_strategy(new_strategy, &_block) - unless [:noop, :raise].include?(new_strategy.to_sym) + unless STRATEGIES.include?(new_strategy.to_sym) fail ArgumentError, "#{new_strategy.inspect} is not a valid strategy - valid values are :noop and :raise" end + begin old_strategy = @strategy @strategy = new_strategy.to_sym diff --git a/lib/unit_record/column_extension.rb b/lib/unit_record/column_extension.rb index bc90868..9d800fb 100644 --- a/lib/unit_record/column_extension.rb +++ b/lib/unit_record/column_extension.rb @@ -1,5 +1,7 @@ module UnitRecord module ColumnExtension + TINY_INT_1 = 'tinyint(1)'.freeze + def self.included(base) base.class_eval do alias_method_chain :simplified_type, :boolean @@ -7,7 +9,7 @@ def self.included(base) end def simplified_type_with_boolean(field_type) - return :boolean if field_type.to_s.downcase.index('tinyint(1)') + return :boolean if field_type.to_s.downcase.index(TINY_INT_1) simplified_type_without_boolean field_type end From 61295dbcc84d2361c2b9728317759563d6c9a5c1 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 14 Sep 2015 17:02:58 -0400 Subject: [PATCH 18/20] Order protected before private --- lib/unit_record/association_stubbing.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/unit_record/association_stubbing.rb b/lib/unit_record/association_stubbing.rb index 251b183..f80abc9 100644 --- a/lib/unit_record/association_stubbing.rb +++ b/lib/unit_record/association_stubbing.rb @@ -1,13 +1,5 @@ module UnitRecord module AssociationStubbing - private - - def initialize_with_association_stubbing(attributes = {}) - associations = extract_associations attributes - initialize_without_association_stubbing attributes - stub_associations associations - end - protected def extract_associations(attributes = {}) @@ -32,5 +24,13 @@ def self.included(klass) end end end + + private + + def initialize_with_association_stubbing(attributes = {}) + associations = extract_associations attributes + initialize_without_association_stubbing attributes + stub_associations associations + end end end From 9c4e9eaba7d3383a9ce685f255d74bcbfed34240 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Tue, 15 Sep 2015 08:36:28 -0400 Subject: [PATCH 19/20] Update README --- README.md | 105 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 4870852..e30211c 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,18 @@ Rationale: [http://www.dcmanges.com/blog/rails-unit-record-test-without-the-data One of the biggest benefits to disconnecting unit tests from the database is having a faster test suite. Here is the benchmark from one of my current projects: - Finished in 19.302702 seconds. - 4920 tests, 7878 assertions, 0 failures, 0 errors +``` +Finished in 19.302702 seconds. +4920 tests, 7878 assertions, 0 failures, 0 errors +``` 4 seconds per 1,000 tests is a good guideline. Installation ------------ - - gem install unit_record +``` +gem install unit_record +``` Usage ----- @@ -26,37 +29,43 @@ Usage Restructuring the Rails Test Directory -------------------------------------- -The Rails test directory typically places testing for models under test/unit and tests for controllers under test/functional. However, we need to change the definition of unit and functional. Controllers can be unit tested (mocking out models and not rendering the view). Models can be functionally tested (hitting the database). Also, each type of test needs its own test\_helper. I recommend restructuring your test directory like this: - - test - test_helper.rb - unit - unit_test_helper.rb - controllers - models - functional - functional_test_helper.rb - controllers - models +The Rails test directory typically places testing for models under `test/unit` and tests for controllers under `test/functional`. However, we need to change the definition of unit and functional. Controllers can be unit tested (mocking out models and not rendering the view). Models can be functionally tested (hitting the database). Also, each type of test needs its own test\_helper. I recommend restructuring your test directory like this: + +``` +test + test_helper.rb + unit + unit_test_helper.rb + controllers + models + functional + functional_test_helper.rb + controllers + models +``` You should move existing functional tests into functional/controllers. You will also need to change the require line at the top of those tests to require the functional\_test\_helper.rb file instead of the test\_helper.rb file. -The functional_test_helper.rb file only needs to require test_helper.rb: +The `functional_test_helper.rb` file only needs to `require test_helper.rb`: - require File.dirname(__FILE__) + "/../test_helper" +```ruby +require File.dirname(__FILE__) + "/../test_helper" +``` For moving unit tests, you have a few options. I recommend moving them to unit/models and then disconnecting your unit tests from the database. Any tests that fail should then be modified to not hit the database or moved to functional/models. Usage ----- -In the test/unit/unit\_test\_helper.rb file you created when restructuring your test directory, you should add these lines: +In the `test/unit/unit\_test\_helper.rb` file you created when restructuring your test directory, you should add these lines: - require File.dirname(__FILE__) + "/../test_helper" - require "unit_record" - ActiveRecord::Base.disconnect! +```ruby +require File.dirname(__FILE__) + "/../test_helper" +require "unit_record" +ActiveRecord::Base.disconnect! +``` -The disconnect! method will do everything necessary to run your unit tests without hitting the database. +The `disconnect!` method will do everything necessary to run your unit tests without hitting the database. Strategy -------- @@ -65,46 +74,58 @@ There are two options for what should happen if you hit the database. You can ei If you want to raise an exception: - ActiveRecord::Base.disconnect! :strategy => :raise +```ruby +ActiveRecord::Base.disconnect! :strategy => :raise - Person.find(:all) - #=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests. +Person.find(:all) +#=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests. +``` If you want to no-op: - ActiveRecord::Base.disconnect! :strategy => :noop +```ruby +ActiveRecord::Base.disconnect! :strategy => :noop - Person.find(:all) - #=> [] +Person.find(:all) +#=> [] +``` You can also change strategies within a block: - ActiveRecord::Base.connection.change_strategy(:raise) do - Person.find(:all) - #=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests. - end +```ruby +ActiveRecord::Base.connection.change_strategy(:raise) do + Person.find(:all) + #=> RuntimeError: ActiveRecord is disconnected; database access is unavailable in unit tests. +end - ActiveRecord::Base.connection.change_strategy(:noop) do - Person.find(:all) - #=> [] - end +ActiveRecord::Base.connection.change_strategy(:noop) do + Person.find(:all) + #=> [] +end +``` Association Stubbing -------------------- One painful aspect of unit testing ActiveRecord classes is setting associations. Because Rails does type checking when setting an association, you'll receive an exception if you try to use a stub in place of the expected class. - Pet.new :owner => stub("person") - #=> ActiveRecord::AssociationTypeMismatch: Person(#16620740) expected, got Mocha::Mock(#11567340) +```ruby +Pet.new :owner => stub("person") +#=> ActiveRecord::AssociationTypeMismatch: Person(#16620740) expected, got Mocha::Mock(#11567340) +``` If you're using mocha, you can have UnitRecord stub associations. To enable association stubbing: - ActiveRecord::Base.disconnect! :stub_associations => true +```ruby +ActiveRecord::Base.disconnect! :stub_associations => true +``` The above example would no longer raise an exception. It would be the equivalent of: - pet = Pet.new - pet.stubs(:owner).returns(stub("person")) +```ruby +pet = Pet.new +pet.stubs(:owner).returns(stub("person")) +``` Note that using this approach, the setter for the association will not work for that instance. From a8aff822b90829f4bbc285e56d7d304f9d3f1db7 Mon Sep 17 00:00:00 2001 From: Ryan Rosenblum Date: Mon, 12 Oct 2015 12:00:11 -0400 Subject: [PATCH 20/20] Increment version to 0.10.0 --- CHANGELOG | 6 +++++- unit-record.gemspec | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e443975..a8e41da 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +*0.10.0 + +* Rails 3.0 and Rails 3.2 compatibility + *0.9.1* (April 22, 2009) * Rails 2.3 compatibility @@ -13,7 +17,7 @@ ActiveRecord::Base.disconnect! :strategy => :noop or ActiveRecord::Base.disconnect! :strategy => :raise - + * Implemented as a connection adapter *0.4.1* (December 10th, 2007) diff --git a/unit-record.gemspec b/unit-record.gemspec index bc14ec5..a460fce 100644 --- a/unit-record.gemspec +++ b/unit-record.gemspec @@ -3,7 +3,7 @@ Gem::Specification.new do |s| s.name = 'unit_record' - s.version = '0.9.2' + s.version = '0.10.0' s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version= s.authors = ['Dan Manges']