Skip to content

Commit

Permalink
Moved to non dynamic class constant creation. Using assert_contains i…
Browse files Browse the repository at this point in the history
…n a few places.
  • Loading branch information
metaskills committed Sep 24, 2008
1 parent 96d0858 commit e82bb3c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion test/grouped_scope/association_reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setup

end

should_eventually 'TEST COLLECTION READER METHOD GENERATION' do
should_eventually 'TEST COLLECTION READER METHOD GENERATION FOR ALL ASSOC TYPES' do

end

Expand Down
2 changes: 1 addition & 1 deletion test/grouped_scope/class_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup
end

should 'add to inheritable attributes with new grouped_scope' do
assert_nil Employee.grouped_scopes[:foobars]
Employee.grouped_scopes[:foobars] = nil
Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) }
assert Employee.grouped_scopes[:foobars]
end
Expand Down
40 changes: 21 additions & 19 deletions test/grouped_scope/self_grouping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setup
end

should 'raise a GroupedScope::NoGroupIdError exception for objects with no group_id schema' do
assert !FooBar.column_names.include?('group_id')
assert_does_not_contain FooBar.column_names, 'group_id'
assert_raise(GroupedScope::NoGroupIdError) { GroupedScope::SelfGroupping.new(FooBar.new) }
end

Expand All @@ -53,20 +53,24 @@ def setup

end

context 'Created via calling #group' do
context 'Calling #group' do

should 'return an array' do
assert_instance_of Array, Factory(:employee).group
end

context 'with a NIL group_id' do

setup do
@employee = Factory(:employee)
end

should 'return an array of one' do
should 'return a collection of one' do
assert_equal 1, @employee.group.size
end

should 'include self in group' do
assert @employee.group.include?(@employee)
assert_contains @employee.group, @employee
end

end
Expand All @@ -77,33 +81,31 @@ def setup
@employee = Factory(:employee, :group_id => 1)
end

should 'return an array of one' do
should 'return a collection of one' do
assert_equal 1, @employee.group.size
end

should 'include self in group' do
assert @employee.group.include?(@employee)
assert_contains @employee.group, @employee
end

end

context 'with different groups' do
context 'with different groups available' do

setup do
@e1_g1 = Factory(:employee_with_reports, :group_id => 1)
@e2_g1 = Factory(:employee, :group_id => 1)
@e2_g2 = Factory(:employee_with_reports, :group_id => 2)
@e2_g2 = Factory(:employee, :group_id => 2)
@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)
end

should 'return a collection of group members' do
assert_equal 2, @e1.group.size
end

should 'have created factories in a sane manner' do
# raise Employee.reflections[:reports].inspect
# raise Employee.reflections[:grouped_scope_reports].inspect
# assert_equal @e1_g1.reports.size, @e1_g1.group.reports.size

# @e1_g1.reports
# @e1_g1.group.reports
# @e1_g1.grouped_scope_reports
should 'include all group members' do
assert_same_elements @e1.group, [@e1,@e2]
end

end
Expand Down
30 changes: 1 addition & 29 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
require 'factory_girl'
require 'lib/test_case'
require 'grouped_scope'

require 'models'

class GroupedScope::TestCase

def setup_environment(options={})
options.reverse_merge! :group_column => :group_id
setup_database(options)
setup_models(options)
end

protected
Expand Down Expand Up @@ -50,32 +49,5 @@ def setup_database(options)
end
end

def setup_models(options)
['Employee','Report','LegacyEmployee','LegacyReport','FooBar'].each do |klass|
Object.send(:remove_const,klass) rescue nil
Object.const_set klass, Class.new(ActiveRecord::Base)
end
Employee.class_eval do
has_many :reports
grouped_scope :reports
end
Report.class_eval do
belongs_to :employee
end
LegacyEmployee.class_eval do
set_primary_key :email
has_many :reports, :class_name => 'LegacyReport', :foreign_key => 'email'
grouped_scope :reports
end
LegacyReport.class_eval do
belongs_to :employee, :class_name => 'LegacyEmployee'
end
FooBar.class_eval do
has_many :reports
grouped_scope :reports
end
end


end

26 changes: 26 additions & 0 deletions test/models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

class Employee < ActiveRecord::Base
has_many :reports
grouped_scope :reports
end

class Report < ActiveRecord::Base
belongs_to :employee
end

class LegacyEmployee < ActiveRecord::Base
set_primary_key :email
has_many :reports, :class_name => 'LegacyReport', :foreign_key => 'email'
grouped_scope :reports
end

class LegacyReport < ActiveRecord::Base
belongs_to :employee, :class_name => 'LegacyEmployee'
end

class FooBar < ActiveRecord::Base
has_many :reports
grouped_scope :reports
end


0 comments on commit e82bb3c

Please sign in to comment.