diff --git a/.gitignore b/.gitignore index 02edc45..2ef238e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .DS_Store -test/debug.log +debug.log autotest -.rbenv-version \ No newline at end of file +.rbenv-version +Gemfile.lock +debug.log \ No newline at end of file diff --git a/CHANGELOG b/CHANGELOG index fcf9719..5457776 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,33 +1,42 @@ -*master* += master -* += 0.6.0 (May 06, 2009) +* ActiveRecord 2.3.14 compatibility. -*0.5.1* (January 7th 2009) +* Refactored to use MiniTest::Spec and Bundler for development/testing. + + += 0.6.0 (May 06, 2009) + +* ActiveRecord 2.3.2 compatibility. + + += 0.5.1 (January 7th 2009) * Add rails 2.2.2 to test task and remove quite backtrace usage so 1.2.6 testing can run. [Ken Collins] -*0.4* (October 3rd 2008) += 0.4 (October 3rd 2008) * Use the #737f2bdabb of NamedScope plugin from http://github.com/metaskills/named_scope/tree/master -*0.3* (October 2nd 2008) += 0.3 (October 2nd 2008) * Add additional NamedScope patches for attribute_condition. Also added GroupedScope::CoreExt to follow suite for GroupedScope::SelfGrouping attribute_conditions. -*0.2* (September 29th 2008) += 0.2 (September 29th 2008) * Add WillPaginate test and confirm grouped scope, named scope, and will paginate all play together. [Ken Collins] * Stronger back port for named_scope in 1.2.6 and 2.0.4. Fixed bug where association proxy owner scope was after named_scope calls. [Ken Collins] -*0.1* (September 26th, 2008) += 0.1 (September 26th, 2008) * Initial public release. [Ken Collins] diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..570a7e4 --- /dev/null +++ b/Gemfile @@ -0,0 +1,22 @@ + +source :rubygems +source 'http://gems.github.com/' + +spec = eval(File.read('grouped_scope.gemspec')) +ar_version = spec.dependencies.detect{ |d|d.name == 'activerecord' }.requirement.to_s + +gem 'sqlite3', '1.3.4' +gem 'activerecord', ar_version, :require => 'active_record' +gem 'mislav-will_paginate', '2.3.4', :require => 'will_paginate' + +group :development do + gem 'rake', '0.8.7' +end + +group :test do + gem 'minitest', '2.5.1' + gem 'mini_shoulda', '0.4.0' + gem 'factory_girl', '2.1.0' + gem 'mocha', '0.10.0' +end + diff --git a/MIT-LICENSE b/MIT-LICENSE index 2a20c02..f85fec3 100644 --- a/MIT-LICENSE +++ b/MIT-LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2008 Ken Collins, Decisiv Inc. +Copyright (c) 2008-2011 Ken Collins Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Rakefile b/Rakefile index 7261442..c1b299d 100644 --- a/Rakefile +++ b/Rakefile @@ -1,43 +1,14 @@ require 'rake' require 'rake/testtask' -require 'rake/rdoctask' - -def reset_invoked - ['test_rails','test'].each do |name| - Rake::Task[name].instance_variable_set '@already_invoked', false - end -end - desc 'Default: run unit tests.' -task :default => :test_rails +task :default => :test desc 'Test the GroupedScope plugin.' -Rake::TestTask.new(:test) do |t| +Rake::TestTask.new do |t| t.libs << 'lib' t.libs << 'test' t.pattern = 'test/**/*_test.rb' t.verbose = true end -desc 'Test the GroupedScope plugin with Rails 2.3.2, 2.2.2, and 2.1.1 gems' -task :test_rails do - test = Rake::Task['test'] - versions = ['2.3.2','2.2.2','2.1.1'] - versions.each do |version| - ENV['RAILS_VERSION'] = "#{version}" - test.invoke - reset_invoked unless version == versions.last - end -end - -desc 'Generate documentation for the GroupedScope plugin.' -Rake::RDocTask.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'GroupedScope' - rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README') - rdoc.rdoc_files.include('lib/**/*.rb') -end - - diff --git a/grouped_scope.gemspec b/grouped_scope.gemspec index 26bd353..a2bd4d5 100644 --- a/grouped_scope.gemspec +++ b/grouped_scope.gemspec @@ -1,39 +1,20 @@ +$:.push File.expand_path("../lib", __FILE__) +require "grouped_scope/version" + Gem::Specification.new do |s| - s.name = "grouped_scope" - s.version = "0.6.0" - s.date = "2009-05-06" - s.summary = "Extends has_many associations to group scope." - s.email = "ken@metaskills.net" - s.homepage = "http://github.com/metaskills/grouped_scope/" - s.description = "Extends has_many associations to group scope." - s.has_rdoc = true - s.authors = ["Ken Collins"] - s.files = [ - "CHANGELOG", - "MIT-LICENSE", - "Rakefile", - "README.rdoc", - "init.rb", - "lib/grouped_scope.rb", - "lib/grouped_scope/association_reflection.rb", - "lib/grouped_scope/class_methods.rb", - "lib/grouped_scope/core_ext.rb", - "lib/grouped_scope/errors.rb", - "lib/grouped_scope/grouping.rb", - "lib/grouped_scope/has_many_association.rb", - "lib/grouped_scope/has_many_through_association.rb", - "lib/grouped_scope/instance_methods.rb", - "lib/grouped_scope/self_grouping.rb" ] - s.test_files = [ - "test/factories.rb", - "test/grouped_scope/association_reflection_test.rb", - "test/grouped_scope/class_methods_test.rb", - "test/grouped_scope/has_many_association_test.rb", - "test/grouped_scope/has_many_through_association_test.rb", - "test/grouped_scope/self_grouping_test.rb", - "test/helper.rb", - "test/lib/boot.rb", - "test/lib/test_case.rb" ] - s.rdoc_options = ["--main", "README.rdoc"] - s.extra_rdoc_files = ["README.rdoc","CHANGELOG","MIT-LICENSE"] + s.name = 'grouped_scope' + s.version = GroupedScope::VERSION + s.platform = Gem::Platform::RUBY + s.authors = ['Ken Collins'] + s.email = ['ken@metaskills.net'] + s.homepage = 'http://github.com/metaskills/grouped_scope/' + s.summary = 'Extends has_many associations to group scope.' + s.description = 'Extends has_many associations to group scope. For ActiveRecord 2.3.x!' + s.files = `git ls-files`.split("\n") - ["grouped_scope.gemspec"] + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ['lib'] + s.rdoc_options = ['--charset=UTF-8'] + s.add_dependency 'activerecord', '~> 2.3.14' end + diff --git a/init.rb b/init.rb deleted file mode 100644 index 2694de6..0000000 --- a/init.rb +++ /dev/null @@ -1,2 +0,0 @@ -require 'grouped_scope' - diff --git a/lib/grouped_scope.rb b/lib/grouped_scope.rb index ec4ce18..1c9efc9 100644 --- a/lib/grouped_scope.rb +++ b/lib/grouped_scope.rb @@ -7,10 +7,5 @@ require 'grouped_scope/has_many_association' require 'grouped_scope/has_many_through_association' require 'grouped_scope/core_ext' - -module GroupedScope - - VERSION = '0.6.0' - -end +require 'grouped_scope/version' diff --git a/lib/grouped_scope/version.rb b/lib/grouped_scope/version.rb new file mode 100644 index 0000000..6ca021f --- /dev/null +++ b/lib/grouped_scope/version.rb @@ -0,0 +1,5 @@ +module GroupedScope + + VERSION = '0.6.1' + +end \ No newline at end of file diff --git a/test/factories.rb b/test/factories.rb index dfab939..6ce2529 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -1,46 +1,41 @@ +FactoryGirl.define do + + sequence(:id) { |n| n } + sequence(:email) { |n| "test_#{n}@domain.com" } + sequence(:title) { |n| "Report Title ##{n}" } + + factory :report do + title { FactoryGirl.generate(:title) } + body 'Bla bla bla. Bla. Bla bla.' + end + + factory :employee do + name { "Factory Employee ##{FactoryGirl.generate(:id)}" } + email { FactoryGirl.generate(:email) } + end + + factory :employee_with_reports, :parent => :employee do + reports { |e| [e.association(:report), e.association(:report)] } + end + + factory :employee_with_urgent_reports, :parent => :employee do + reports { |e| [e.association(:report), e.association(:report, :title=>'URGENT'), + e.association(:report), e.association(:report, :body=>'This is URGENT.')] } + end + + factory :legacy_employee do + name { "Legacy Factory Employee ##{FactoryGirl.generate(:id)}" } + email { FactoryGirl.generate(:email) } + end + + factory :legacy_report do |r| + r.title { FactoryGirl.generate(:title) } + r.body 'Legacy bla bla. Legacy. Legacy bla.' + end + + factory :legacy_employee_with_reports, :parent => :legacy_employee do + reports { |e| [e.association(:legacy_report), e.association(:legacy_report)] } + end -Factory.sequence(:id) { |n| n } -Factory.sequence(:email) { |n| "test_#{n}@domain.com" } -Factory.sequence(:title) { |n| "Report Title ##{n}" } - -Factory.define :employee do |e| - e.name { "Factory Employee ##{Factory.next(:id)}" } - e.email { Factory.next(:email) } -end - -Factory.define :report do |r| - r.title { Factory.next(:title) } - r.body 'Bla bla bla. Bla. Bla bla.' -end - -Factory.define :employee_with_reports, :class => 'Employee' do |e| - e.name { "Factory Employee ##{Factory.next(:id)}" } - e.email { Factory.next(:email) } - e.reports { |employee| [employee.association(:report),employee.association(:report)] } -end - -Factory.define :employee_with_urgent_reports, :class => 'Employee' do |e| - e.name { "Factory Employee ##{Factory.next(:id)}" } - e.email { Factory.next(:email) } - e.reports { |employee| [employee.association(:report), employee.association(:report,:title=>'URGENT'), - employee.association(:report), employee.association(:report,:body=>'This is URGENT.')] } end - -Factory.define :legacy_employee do |e| - e.name { "Legacy Factory Employee ##{Factory.next(:id)}" } - e.email { Factory.next(:email) } -end - -Factory.define :legacy_report do |r| - r.title { Factory.next(:title) } - r.body 'Legacy bla bla. Legacy. Legacy bla.' -end - -Factory.define :legacy_employee_with_reports, :class => 'LegacyEmployee' do |e| - e.name { "Legacy Factory Employee ##{Factory.next(:id)}" } - e.email { Factory.next(:email) } - e.reports { |employee| [employee.association(:legacy_report),employee.association(:legacy_report)] } -end - - diff --git a/test/grouped_scope/association_reflection_test.rb b/test/grouped_scope/association_reflection_test.rb index bc4227b..75cea2c 100644 --- a/test/grouped_scope/association_reflection_test.rb +++ b/test/grouped_scope/association_reflection_test.rb @@ -1,8 +1,8 @@ -require File.dirname(__FILE__) + '/../helper' +require 'helper' class GroupedScope::AssociationReflectionTest < GroupedScope::TestCase - def setup + setup do setup_environment end @@ -13,19 +13,18 @@ def setup setup { @reflection_klass = GroupedScope::AssociationReflection } should 'when a association does not exist' do - assert_raise(ArgumentError) { @reflection_klass.new(Employee,:foobars) } + lambda{ @reflection_klass.new(Employee,:doesnotexist) }.must_raise(ArgumentError) end should 'when the association is not a has_many or a has_and_belongs_to_many' do Employee.class_eval { belongs_to(:foo) } - assert_raise(ArgumentError) { @reflection_klass.new(Employee,:foo) } + lambda{ @reflection_klass.new(Employee,:foo) }.must_raise(ArgumentError) end end end - context 'For #ungrouped_reflection' do setup do @@ -47,8 +46,8 @@ def setup end should 'not delegate to #ungrouped_reflection for #options and #name' do - assert_not_equal @ungrouped_reflection.name, @grouped_reflection.name - assert_not_equal @ungrouped_reflection.options, @grouped_reflection.options + @ungrouped_reflection.name.wont_equal @grouped_reflection.name + @ungrouped_reflection.options.wont_equal @grouped_reflection.options end should 'derive class name to same as ungrouped reflection' do @@ -71,5 +70,4 @@ def setup end - end diff --git a/test/grouped_scope/class_methods_test.rb b/test/grouped_scope/class_methods_test.rb index b1b59b7..b3c68ac 100644 --- a/test/grouped_scope/class_methods_test.rb +++ b/test/grouped_scope/class_methods_test.rb @@ -1,8 +1,8 @@ -require File.dirname(__FILE__) + '/../helper' +require 'helper' class GroupedScope::ClassMethodsTest < GroupedScope::TestCase - def setup + setup do setup_environment end @@ -34,7 +34,7 @@ def setup should 'create a has_many assoc named :grouped_scope_* using existing association as a suffix' do grouped_reports_assoc = Employee.reflections[:grouped_scope_reports] assert_instance_of GroupedScope::AssociationReflection, grouped_reports_assoc - assert Factory(:employee).respond_to?(:grouped_scope_reports) + assert FactoryGirl.create(:employee).respond_to?(:grouped_scope_reports) end should 'not add the :grouped_scope option to existing reflection' do diff --git a/test/grouped_scope/has_many_association_test.rb b/test/grouped_scope/has_many_association_test.rb index 8504472..305f1f9 100644 --- a/test/grouped_scope/has_many_association_test.rb +++ b/test/grouped_scope/has_many_association_test.rb @@ -1,16 +1,15 @@ -require File.dirname(__FILE__) + '/../helper' +require 'helper' class GroupedScope::HasManyAssociationTest < GroupedScope::TestCase - def setup + setup do setup_environment end - context 'For an Employee' do setup do - @employee = Factory(:employee) + @employee = FactoryGirl.create(:employee) end should 'scope existing association to owner' do @@ -28,8 +27,8 @@ def setup context 'for counting sql' do setup do - @e1 = Factory(:employee_with_reports, :group_id => 1) - @e2 = Factory(:employee_with_reports, :group_id => 1) + @e1 = FactoryGirl.create(:employee_with_reports, :group_id => 1) + @e2 = FactoryGirl.create(:employee_with_reports, :group_id => 1) end should 'scope count sql to owner' do @@ -53,8 +52,8 @@ def setup context 'training association extensions' do setup do - @e1 = Factory(:employee_with_urgent_reports, :group_id => 1) - @e2 = Factory(:employee, :group_id => 1) + @e1 = FactoryGirl.create(:employee_with_urgent_reports, :group_id => 1) + @e2 = FactoryGirl.create(:employee, :group_id => 1) @urgent_reports = @e1.reports.select(&:urgent_title?) end @@ -77,8 +76,8 @@ def setup context 'training named scopes' do setup do - @e1 = Factory(:employee_with_urgent_reports, :group_id => 1) - @e2 = Factory(:employee, :group_id => 1) + @e1 = FactoryGirl.create(:employee_with_urgent_reports, :group_id => 1) + @e2 = FactoryGirl.create(:employee, :group_id => 1) @urgent_titles = @e1.reports.select(&:urgent_title?) @urgent_bodys = @e1.reports.select(&:urgent_body?) end @@ -117,7 +116,7 @@ def setup context 'For a LegacyEmployee' do setup do - @employee = Factory(:legacy_employee) + @employee = FactoryGirl.create(:legacy_employee) end should 'scope existing association to owner' do diff --git a/test/grouped_scope/has_many_through_association_test.rb b/test/grouped_scope/has_many_through_association_test.rb index 1e1cb1b..c33547d 100644 --- a/test/grouped_scope/has_many_through_association_test.rb +++ b/test/grouped_scope/has_many_through_association_test.rb @@ -1,17 +1,16 @@ -require File.dirname(__FILE__) + '/../helper' +require 'helper' class GroupedScope::HasManyThroughAssociationTest < GroupedScope::TestCase - def setup + setup do setup_environment - @e1 = Factory(:employee, :group_id => 1) + @e1 = FactoryGirl.create(:employee, :group_id => 1) @e1.departments << Department.hr << Department.finance - @e2 = Factory(:employee, :group_id => 1) + @e2 = FactoryGirl.create(:employee, :group_id => 1) @e2.departments << Department.it @all_group_departments = [Department.hr, Department.it, Department.finance] end - context 'For default association' do should 'scope to owner' do @@ -49,5 +48,4 @@ def setup end - end diff --git a/test/grouped_scope/self_grouping_test.rb b/test/grouped_scope/self_grouping_test.rb index 9ff7d15..c2e2c85 100644 --- a/test/grouped_scope/self_grouping_test.rb +++ b/test/grouped_scope/self_grouping_test.rb @@ -1,15 +1,15 @@ -require File.dirname(__FILE__) + '/../helper' +require 'helper' class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase - def setup + setup do setup_environment end context 'General behavior' do setup do - @employee = Factory(:employee) + @employee = FactoryGirl.create(:employee) end should 'return an array' do @@ -29,8 +29,8 @@ def setup end should 'raise a GroupedScope::NoGroupIdError exception for objects with no group_id schema' do - assert_does_not_contain FooBar.column_names, 'group_id' - assert_raise(GroupedScope::NoGroupIdError) { GroupedScope::SelfGroupping.new(FooBar.new) } + FooBar.column_names.wont_include 'group_id' + lambda{ GroupedScope::SelfGroupping.new(FooBar.new) }.must_raise(GroupedScope::NoGroupIdError) end should 'return correct attribute_condition for GroupedScope::SelfGroupping object' do @@ -61,13 +61,13 @@ def setup context 'Calling #group' do should 'return an array' do - assert_instance_of Array, Factory(:employee).group + assert_instance_of Array, FactoryGirl.create(:employee).group end context 'with a NIL group_id' do setup do - @employee = Factory(:employee) + @employee = FactoryGirl.create(:employee) end should 'return a collection of one' do @@ -75,7 +75,7 @@ def setup end should 'include self in group' do - assert_contains @employee.group, @employee + assert @employee.group.include?(@employee) end end @@ -83,7 +83,7 @@ def setup context 'with a set group_id' do setup do - @employee = Factory(:employee, :group_id => 1) + @employee = FactoryGirl.create(:employee, :group_id => 1) end should 'return a collection of one' do @@ -91,7 +91,7 @@ def setup end should 'include self in group' do - assert_contains @employee.group, @employee + assert @employee.group.include?(@employee) end end @@ -99,10 +99,10 @@ def setup context 'with different groups available' do setup do - @e1 = Factory(:employee_with_reports, :group_id => 1) - @e2 = Factory(:employee, :group_id => 1) - @e3 = Factory(:employee_with_reports, :group_id => 2) - @e4 = Factory(:employee, :group_id => 2) + @e1 = FactoryGirl.create(:employee_with_reports, :group_id => 1) + @e2 = FactoryGirl.create(:employee, :group_id => 1) + @e3 = FactoryGirl.create(:employee_with_reports, :group_id => 2) + @e4 = FactoryGirl.create(:employee, :group_id => 2) end should 'return a collection of group members' do @@ -128,10 +128,10 @@ def setup context 'with different groups in legacy schema' do setup do - @e1 = Factory(:legacy_employee_with_reports, :group_id => 1) - @e2 = Factory(:legacy_employee, :group_id => 1) - @e3 = Factory(:legacy_employee_with_reports, :group_id => 2) - @e4 = Factory(:legacy_employee, :group_id => 2) + @e1 = FactoryGirl.create(:legacy_employee_with_reports, :group_id => 1) + @e2 = FactoryGirl.create(:legacy_employee, :group_id => 1) + @e3 = FactoryGirl.create(:legacy_employee_with_reports, :group_id => 2) + @e4 = FactoryGirl.create(:legacy_employee, :group_id => 2) end should 'honor legacy reports association options like class_name and foreign_key' do @@ -140,7 +140,6 @@ def setup end - end diff --git a/test/helper.rb b/test/helper.rb index 91863fa..5929e12 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,62 +1,111 @@ -require File.join(File.dirname(__FILE__),'lib/boot') unless defined?(ActiveRecord) -require 'test/unit' -require 'shoulda' -require 'mocha' -require 'factory_girl' -require 'lib/test_case' +require 'rubygems' +require 'bundler' +require "bundler/setup" +Bundler.require(:default, :development, :test) require 'grouped_scope' +require 'mini_shoulda' +require 'minitest/autorun' +require 'factories' -class GroupedScope::TestCase - - def setup_environment(options={}) - options.reverse_merge! :group_column => :group_id - setup_database(options) - Department.create! :name => 'IT' - Department.create! :name => 'Human Resources' - Department.create! :name => 'Finance' +WillPaginate.enable_activerecord + +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)+'/debug.log') +ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:' +ActiveRecord::Base.connection.class.class_eval do + IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/] + def execute_with_query_record(sql, name = nil, &block) + $queries_executed ||= [] + $queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r } + execute_without_query_record(sql, name, &block) end - - protected - - def setup_database(options) - ActiveRecord::Base.class_eval do - silence do - connection.create_table :employees, :force => true do |t| - t.column :name, :string - t.column :email, :string - t.column options[:group_column], :integer - end - connection.create_table :reports, :force => true do |t| - t.column :title, :string - t.column :body, :string - t.column :employee_id, :integer - end - connection.create_table :departments, :force => true do |t| - t.column :name, :string - end - connection.create_table :department_memberships, :force => true do |t| - t.column :employee_id, :integer - t.column :department_id, :integer - t.column :meta_info, :string - end - connection.create_table :legacy_employees, :force => true, :id => false do |t| - t.column :name, :string - t.column :email, :string - t.column options[:group_column], :integer - end - connection.create_table :legacy_reports, :force => true do |t| - t.column :title, :string - t.column :body, :string - t.column :email, :string - end - connection.create_table :foo_bars, :force => true do |t| - t.column :foo, :string - t.column :bar, :string + alias_method_chain :execute, :query_record +end + + +module GroupedScope + class TestCase < MiniTest::Spec + + def setup_environment(options={}) + options.reverse_merge! :group_column => :group_id + setup_database(options) + Department.create! :name => 'IT' + Department.create! :name => 'Human Resources' + Department.create! :name => 'Finance' + end + + protected + + def assert_same_elements(a1, a2, msg = nil) + [:select, :inject, :size].each do |m| + [a1, a2].each { |a| a.must_respond_to m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}." } + end + assert a1h = a1.inject({}) { |h,e| h[e] ||= a1.select { |i| i == e }.size; h } + assert a2h = a2.inject({}) { |h,e| h[e] ||= a2.select { |i| i == e }.size; h } + a1h.must_equal a2h, msg + end + + def assert_sql(*patterns_to_match) + $queries_executed = [] + yield + ensure + failed_patterns = [] + patterns_to_match.each do |pattern| + failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql } + end + assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found in:\n#{$queries_executed.inspect}" + end + + def assert_queries(num = 1) + $queries_executed = [] + yield + ensure + assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed." + end + + def assert_no_queries(&block) + assert_queries(0, &block) + end + + def setup_database(options) + ActiveRecord::Base.class_eval do + silence do + connection.create_table :employees, :force => true do |t| + t.column :name, :string + t.column :email, :string + t.column options[:group_column], :integer + end + connection.create_table :reports, :force => true do |t| + t.column :title, :string + t.column :body, :string + t.column :employee_id, :integer + end + connection.create_table :departments, :force => true do |t| + t.column :name, :string + end + connection.create_table :department_memberships, :force => true do |t| + t.column :employee_id, :integer + t.column :department_id, :integer + t.column :meta_info, :string + end + connection.create_table :legacy_employees, :force => true, :id => false do |t| + t.column :name, :string + t.column :email, :string + t.column options[:group_column], :integer + end + connection.create_table :legacy_reports, :force => true do |t| + t.column :title, :string + t.column :body, :string + t.column :email, :string + end + connection.create_table :foo_bars, :force => true do |t| + t.column :foo, :string + t.column :bar, :string + end end end end + end - end class Employee < ActiveRecord::Base diff --git a/test/lib/boot.rb b/test/lib/boot.rb deleted file mode 100644 index 689ab96..0000000 --- a/test/lib/boot.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'rubygems' - -plugin_root = File.expand_path(File.join(File.dirname(__FILE__),'..')) -framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].detect { |p| File.directory? p } -rails_version = ENV['RAILS_VERSION'] -rails_version = nil if rails_version && rails_version == '' - -['.','lib','test'].each do |plugin_lib| - load_path = File.expand_path("#{plugin_root}/#{plugin_lib}") - $LOAD_PATH.unshift(load_path) unless $LOAD_PATH.include?(load_path) -end - -if rails_version.nil? && framework_root - puts "Found framework root: #{framework_root}" - $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib" -else - puts "Using rails#{" #{rails_version}" if rails_version} from gems" - if rails_version - gem 'rails', rails_version - else - gem 'activerecord' - end -end - -require 'active_record' -require 'active_support' - -gem 'mislav-will_paginate', '2.3.4' -require 'will_paginate' -WillPaginate.enable_activerecord - diff --git a/test/lib/test_case.rb b/test/lib/test_case.rb deleted file mode 100644 index 16e4b08..0000000 --- a/test/lib/test_case.rb +++ /dev/null @@ -1,44 +0,0 @@ - -ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)+'/../debug.log') -ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:' -ActiveRecord::Base.connection.class.class_eval do - IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/] - def execute_with_query_record(sql, name = nil, &block) - $queries_executed ||= [] - $queries_executed << sql unless IGNORED_SQL.any? { |r| sql =~ r } - execute_without_query_record(sql, name, &block) - end - alias_method_chain :execute, :query_record -end - -module GroupedScope - class TestCase < Test::Unit::TestCase - - def test_truth ; end - - protected - - def assert_sql(*patterns_to_match) - $queries_executed = [] - yield - ensure - failed_patterns = [] - patterns_to_match.each do |pattern| - failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql } - end - assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found in:\n#{$queries_executed.inspect}" - end - - def assert_queries(num = 1) - $queries_executed = [] - yield - ensure - assert_equal num, $queries_executed.size, "#{$queries_executed.size} instead of #{num} queries were executed." - end - - def assert_no_queries(&block) - assert_queries(0, &block) - end - - end -end