From d04a21165503cd1fd32501231a3bbcc5074b5af8 Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Thu, 1 Dec 2011 13:01:52 -0500 Subject: [PATCH] Change to MiniTest::Spec syntax and remove MiniShoulda. --- Gemfile | 7 +- .../association_reflection_test.rb | 30 ++++----- test/grouped_scope/class_methods_test.rb | 24 +++---- .../has_many_association_test.rb | 54 +++++++-------- .../has_many_through_association_test.rb | 17 +++-- test/grouped_scope/self_grouping_test.rb | 66 +++++++++---------- test/helper.rb | 6 +- 7 files changed, 95 insertions(+), 109 deletions(-) diff --git a/Gemfile b/Gemfile index 2ac3260..4783e3b 100644 --- a/Gemfile +++ b/Gemfile @@ -14,9 +14,8 @@ group :development do 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' + gem 'minitest', '~> 2.8.1' + gem 'factory_girl', '~> 2.3.2' + gem 'mocha', '~> 0.10.0' end diff --git a/test/grouped_scope/association_reflection_test.rb b/test/grouped_scope/association_reflection_test.rb index 75cea2c..232a7bf 100644 --- a/test/grouped_scope/association_reflection_test.rb +++ b/test/grouped_scope/association_reflection_test.rb @@ -2,21 +2,17 @@ class GroupedScope::AssociationReflectionTest < GroupedScope::TestCase - setup do - setup_environment - end - - context 'For initialization' do + describe 'For initialization' do - context 'Raise and exception' do + describe 'Raise and exception' do - setup { @reflection_klass = GroupedScope::AssociationReflection } + before { @reflection_klass = GroupedScope::AssociationReflection } - should 'when a association does not exist' do + it 'when a association does not exist' do 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 + it 'when the association is not a has_many or a has_and_belongs_to_many' do Employee.class_eval { belongs_to(:foo) } lambda{ @reflection_klass.new(Employee,:foo) }.must_raise(ArgumentError) end @@ -25,18 +21,18 @@ class GroupedScope::AssociationReflectionTest < GroupedScope::TestCase end - context 'For #ungrouped_reflection' do + describe 'For #ungrouped_reflection' do - setup do + before do @ungrouped_reflection = Employee.reflections[:reports] @grouped_reflection = Employee.reflections[:grouped_scope_reports] end - should 'access ungrouped reflection' do + it 'access ungrouped reflection' do assert_equal @ungrouped_reflection, @grouped_reflection.ungrouped_reflection end - should 'delegate instance methods to #ungrouped_reflection' do + it 'delegate instance methods to #ungrouped_reflection' do methods = [:class_name,:klass,:table_name,:primary_key_name,:active_record, :association_foreign_key,:counter_cache_column,:source_reflection] methods.each do |m| @@ -45,20 +41,20 @@ class GroupedScope::AssociationReflectionTest < GroupedScope::TestCase end end - should 'not delegate to #ungrouped_reflection for #options and #name' do + it 'not delegate to #ungrouped_reflection for #options and #name' do @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 + it 'derive class name to same as ungrouped reflection' do assert_equal @ungrouped_reflection.send(:derive_class_name), @grouped_reflection.send(:derive_class_name) end - should 'derive primary key name to same as ungrouped reflection' do + it 'derive primary key name to same as ungrouped reflection' do assert_equal @ungrouped_reflection.send(:derive_primary_key_name), @grouped_reflection.send(:derive_primary_key_name) end - should 'honor explicit legacy reports association options like class_name and foreign_key' do + it 'honor explicit legacy reports association options like class_name and foreign_key' do @ungrouped_reflection = LegacyEmployee.reflections[:reports] @grouped_reflection = LegacyEmployee.reflections[:grouped_scope_reports] [:class_name,:primary_key_name].each do |m| diff --git a/test/grouped_scope/class_methods_test.rb b/test/grouped_scope/class_methods_test.rb index b3c68ac..e0b0147 100644 --- a/test/grouped_scope/class_methods_test.rb +++ b/test/grouped_scope/class_methods_test.rb @@ -2,17 +2,13 @@ class GroupedScope::ClassMethodsTest < GroupedScope::TestCase - setup do - setup_environment - end - - context 'For .grouped_scopes' do + describe 'For .grouped_scopes' do - should 'have a inheritable attribute hash' do + it 'have a inheritable attribute hash' do assert_instance_of Hash, Employee.grouped_scopes end - should 'add to inheritable attributes with new grouped_scope' do + it 'add to inheritable attributes with new grouped_scope' do Employee.grouped_scopes[:foobars] = nil Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) } assert Employee.grouped_scopes[:foobars] @@ -20,28 +16,28 @@ class GroupedScope::ClassMethodsTest < GroupedScope::TestCase end - context 'For .grouped_scope' do + describe 'For .grouped_scope' do - should 'create a belongs_to :grouping association' do + it 'create a belongs_to :grouping association' do assert Employee.reflections[:grouping] end - should 'not recreate belongs_to :grouping on additional calls' do - Employee.stubs(:belongs_to).never + it 'not recreate belongs_to :grouping on additional calls' do + Employee.expects(:belongs_to).never Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) } end - should 'create a has_many assoc named :grouped_scope_* using existing association as a suffix' do + it '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 FactoryGirl.create(:employee).respond_to?(:grouped_scope_reports) end - should 'not add the :grouped_scope option to existing reflection' do + it 'not add the :grouped_scope option to existing reflection' do assert_nil Employee.reflections[:reports].options[:grouped_scope] end - should 'have added the :grouped_scope option to new grouped reflection' do + it 'have added the :grouped_scope option to new grouped reflection' do assert Employee.reflections[:grouped_scope_reports].options[:grouped_scope] end diff --git a/test/grouped_scope/has_many_association_test.rb b/test/grouped_scope/has_many_association_test.rb index 305f1f9..e526420 100644 --- a/test/grouped_scope/has_many_association_test.rb +++ b/test/grouped_scope/has_many_association_test.rb @@ -2,70 +2,66 @@ class GroupedScope::HasManyAssociationTest < GroupedScope::TestCase - setup do - setup_environment - end - - context 'For an Employee' do + describe 'For an Employee' do - setup do + before do @employee = FactoryGirl.create(:employee) end - should 'scope existing association to owner' do + it 'scope existing association to owner' do assert_sql(/employee_id = #{@employee.id}/) do @employee.reports(true) end end - should 'scope group association to group' do + it 'scope group association to group' do assert_sql(/employee_id IN \(#{@employee.id}\)/) do @employee.group.reports(true) end end - context 'for counting sql' do + describe 'for counting sql' do - setup do + before do @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 + it 'scope count sql to owner' do assert_sql(/SELECT count\(\*\)/,/employee_id = #{@e1.id}/) do @e1.reports(true).count end end - should 'scope count sql to group' do + it 'scope count sql to group' do assert_sql(/SELECT count\(\*\)/,/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do @e1.group.reports(true).count end end - should 'have a group count equal to sum of seperate owner counts' do + it 'have a group count equal to sum of seperate owner counts' do assert_equal @e1.reports(true).count + @e2.reports(true).count, @e2.group.reports(true).count end end - context 'training association extensions' do + describe 'training association extensions' do - setup do + before do @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 - should 'find urgent report via normal ungrouped association' do + it 'find urgent report via normal ungrouped association' do assert_same_elements @urgent_reports, @e1.reports(true).urgent end - should 'find urgent report via grouped reflection' do + it 'find urgent report via grouped reflection' do assert_same_elements @urgent_reports, @e2.group.reports(true).urgent end - should 'use assoc extension SQL along with group reflection' do + it 'use assoc extension SQL along with group reflection' do assert_sql(select_from_reports, where_for_groups, where_for_urgent_title) do @e2.group.reports.urgent end @@ -73,34 +69,34 @@ class GroupedScope::HasManyAssociationTest < GroupedScope::TestCase end - context 'training named scopes' do + describe 'training named scopes' do - setup do + before do @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 - should 'find urgent reports via normal named scopes by normal owner' do + it 'find urgent reports via normal named scopes by normal owner' do assert_same_elements @urgent_titles, @e1.reports(true).with_urgent_title assert_same_elements @urgent_bodys, @e1.reports(true).with_urgent_body end - should 'find urgent reports via group reflection by group member' do + it 'find urgent reports via group reflection by group member' do assert_same_elements @urgent_titles, @e2.group.reports(true).with_urgent_title assert_same_elements @urgent_bodys, @e2.group.reports(true).with_urgent_body end - should 'use named scope SQL along with group reflection' do + it 'use named scope SQL along with group reflection' do assert_sql(select_from_reports, where_for_groups, where_for_urgent_body, where_for_urgent_title) do @e2.group.reports.with_urgent_title.with_urgent_body.inspect end end - context 'with will paginate' do + describe 'with will paginate' do - should 'use group reflection, named scope, and paginate SQL' do + it 'use group reflection, named scope, and paginate SQL' do where_ends_with_limits = /WHERE.*LIMIT 2 OFFSET 0/ assert_sql(select_from_reports, where_for_groups, where_for_urgent_body, where_for_urgent_title, where_ends_with_limits) do @e2.group.reports.with_urgent_title.with_urgent_body.paginate(:page=>1,:per_page=>2) @@ -113,19 +109,19 @@ class GroupedScope::HasManyAssociationTest < GroupedScope::TestCase end - context 'For a LegacyEmployee' do + describe 'For a LegacyEmployee' do - setup do + before do @employee = FactoryGirl.create(:legacy_employee) end - should 'scope existing association to owner' do + it 'scope existing association to owner' do assert_sql(/"?legacy_reports"?.email = '#{@employee.id}'/) do @employee.reports(true) end end - should 'scope group association to group' do + it 'scope group association to group' do assert_sql(/"?legacy_reports"?.email IN \('#{@employee.id}'\)/) do @employee.group.reports(true) end diff --git a/test/grouped_scope/has_many_through_association_test.rb b/test/grouped_scope/has_many_through_association_test.rb index c33547d..98372e3 100644 --- a/test/grouped_scope/has_many_through_association_test.rb +++ b/test/grouped_scope/has_many_through_association_test.rb @@ -2,8 +2,7 @@ class GroupedScope::HasManyThroughAssociationTest < GroupedScope::TestCase - setup do - setup_environment + before do @e1 = FactoryGirl.create(:employee, :group_id => 1) @e1.departments << Department.hr << Department.finance @e2 = FactoryGirl.create(:employee, :group_id => 1) @@ -11,15 +10,15 @@ class GroupedScope::HasManyThroughAssociationTest < GroupedScope::TestCase @all_group_departments = [Department.hr, Department.it, Department.finance] end - context 'For default association' do + describe 'For default association' do - should 'scope to owner' do + it 'scope to owner' do assert_sql(/employee_id = #{@e1.id}/) do @e1.departments(true) end end - should 'scope count to owner' do + it 'scope count to owner' do assert_sql(/employee_id = #{@e1.id}/) do @e1.departments(true).count end @@ -27,21 +26,21 @@ class GroupedScope::HasManyThroughAssociationTest < GroupedScope::TestCase end - context 'For grouped association' do + describe 'For grouped association' do - should 'scope to group' do + it 'scope to group' do assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do @e2.group.departments(true) end end - should 'scope count to group' do + it 'scope count to group' do assert_sql(/employee_id IN \(#{@e1.id},#{@e2.id}\)/) do @e1.group.departments(true).count end end - should 'have a group count equal to sum of seperate owner counts' do + it 'have a group count equal to sum of seperate owner counts' do assert_equal @e1.departments(true).count + @e2.departments(true).count, @e2.group.departments(true).count end diff --git a/test/grouped_scope/self_grouping_test.rb b/test/grouped_scope/self_grouping_test.rb index c2e2c85..a4c9c01 100644 --- a/test/grouped_scope/self_grouping_test.rb +++ b/test/grouped_scope/self_grouping_test.rb @@ -2,52 +2,48 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase - setup do - setup_environment - end - - context 'General behavior' do + describe 'General behavior' do - setup do + before do @employee = FactoryGirl.create(:employee) end - should 'return an array' do + it 'return an array' do assert_instance_of Array, @employee.group end - should 'return #ids array' do + it 'return #ids array' do assert_equal [@employee.id], @employee.group.ids end - should 'return #quoted_ids string for use in sql statments' do + it 'return #quoted_ids string for use in sql statments' do assert_equal "#{@employee.id}", @employee.group.quoted_ids end - should 'respond true to grouped associations' do + it 'respond true to grouped associations' do assert @employee.group.respond_to?(:reports) end - should 'raise a GroupedScope::NoGroupIdError exception for objects with no group_id schema' do + it 'raise a GroupedScope::NoGroupIdError exception for objects with no group_id schema' do 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 + it 'return correct attribute_condition for GroupedScope::SelfGroupping object' do assert_sql(/"?group_id"? IN \(#{@employee.id}\)/) do Employee.find :all, :conditions => {:group_id => @employee.group} end end - context 'for Array delegates' do + describe 'for Array delegates' do - should 'respond to first/last' do + it 'respond to first/last' do [:first,:last].each do |method| assert @employee.group.respond_to?(method), "Should respond to #{method.inspect}" end end - should 'respond to each' do + it 'respond to each' do assert @employee.group.respond_to?(:each) @employee.group.each do |employee| assert_instance_of Employee, employee @@ -58,83 +54,83 @@ class GroupedScope::SelfGrouppingTest < GroupedScope::TestCase end - context 'Calling #group' do + describe 'Calling #group' do - should 'return an array' do + it 'return an array' do assert_instance_of Array, FactoryGirl.create(:employee).group end - context 'with a NIL group_id' do + describe 'with a NIL group_id' do - setup do + before do @employee = FactoryGirl.create(:employee) end - should 'return a collection of one' do + it 'return a collection of one' do assert_equal 1, @employee.group.size end - should 'include self in group' do + it 'include self in group' do assert @employee.group.include?(@employee) end end - context 'with a set group_id' do + describe 'with a set group_id' do - setup do + before do @employee = FactoryGirl.create(:employee, :group_id => 1) end - should 'return a collection of one' do + it 'return a collection of one' do assert_equal 1, @employee.group.size end - should 'include self in group' do + it 'include self in group' do assert @employee.group.include?(@employee) end end - context 'with different groups available' do + describe 'with different groups available' do - setup do + before do @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 + it 'return a collection of group members' do assert_equal 2, @e1.group.size end - should 'include all group members' do + it 'include all group members' do assert_same_elements [@e1,@e2], @e1.group end - should 'allow member to find grouped associations of other member' do + it 'allow member to find grouped associations of other member' do assert_same_elements @e1.reports, @e2.group.reports end - should 'allow proxy owner to define all grouped which ignores group_id schema' do - @e2.stubs(:all_grouped?).returns(true) + it 'allow proxy owner to define all grouped which ignores group_id schema' do + @e2.stubs :all_grouped? => true assert_same_elements [@e1,@e2,@e3,@e4], @e2.group assert_same_elements @e1.reports + @e3.reports, @e2.group.reports end end - context 'with different groups in legacy schema' do + describe 'with different groups in legacy schema' do - setup do + before do @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 + it 'honor legacy reports association options like class_name and foreign_key' do @e2.group.reports.all? { |r| r.is_a?(LegacyReport) } end diff --git a/test/helper.rb b/test/helper.rb index 5929e12..dd0add1 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -3,7 +3,6 @@ require "bundler/setup" Bundler.require(:default, :development, :test) require 'grouped_scope' -require 'mini_shoulda' require 'minitest/autorun' require 'factories' @@ -25,6 +24,11 @@ def execute_with_query_record(sql, name = nil, &block) module GroupedScope class TestCase < MiniTest::Spec + include Mocha::API + + before { setup_environment } + after { mocha_teardown } + def setup_environment(options={}) options.reverse_merge! :group_column => :group_id setup_database(options)