Skip to content

Commit

Permalink
Change to MiniTest::Spec syntax and remove MiniShoulda.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Dec 1, 2011
1 parent 9fd64a6 commit d04a211
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 109 deletions.
7 changes: 3 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

30 changes: 13 additions & 17 deletions test/grouped_scope/association_reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand All @@ -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|
Expand Down
24 changes: 10 additions & 14 deletions test/grouped_scope/class_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,42 @@

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]
end

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

Expand Down
54 changes: 25 additions & 29 deletions test/grouped_scope/has_many_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,101 @@

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
end

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)
Expand All @@ -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
Expand Down
17 changes: 8 additions & 9 deletions test/grouped_scope/has_many_through_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,45 @@

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)
@e2.departments << Department.it
@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
end

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

Expand Down
Loading

0 comments on commit d04a211

Please sign in to comment.