Skip to content

Commit

Permalink
Stress testing GroupedScope::SelfGrouping class.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Sep 22, 2008
1 parent 223fa43 commit a78b28d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ TODO
* Raise error if piggy back assoc uses :finder_sql option
* Change these to account for :as option:
GroupedScope::HasManyAssociation
* Turn on some mocha options/warnings.


Running Tests
Expand Down
5 changes: 3 additions & 2 deletions lib/grouped_scope/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def grouped_scope(*args)
include InstanceMethods
end

private

def grouped_scope_for(association)
:"grouped_scope_#{association}"
end


private

def create_belongs_to_grouped_scope_grouping
grouping_class_name = 'GroupedScope::Grouping'
existing_grouping = reflect_on_association(:grouping)
Expand Down
2 changes: 1 addition & 1 deletion lib/grouped_scope/self_grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SelfGroupping
delegate :primary_key, :quote_value, :columns_hash, :to => :proxy_class

[].methods.each do |m|
unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|respond_to\?)/
delegate m, :to => :group_proxy
end
end
Expand Down
90 changes: 86 additions & 4 deletions test/grouped_scope/self_grouping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,51 @@ def setup

context 'Calling #group' do

should 'return an array' do
assert_instance_of Array, Factory(:employee).group
context 'in general behavior' do

setup do
@employee = Factory(:employee)
end

should 'return an array' do
assert_instance_of Array, @employee.group
end

should 'respond true to grouped associations' do
assert @employee.group.respond_to?(:reports)
end

context 'for Array delegates' do

should '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
assert @employee.group.respond_to?(:each)
@employee.group.each do |employee|
# FIXME: Figure why this does not work.
# assert_instance_of Employee, employee
assert_equal Employee.name, employee.class.name
end
end

end

end

context 'with a NIL group' do
context 'with no group_id schema' do

should 'raise a GroupedScope::NoGroupIdError exception' do
assert_raise(GroupedScope::NoGroupIdError) { FooBar.new.group }
end

end

context 'with a NIL group_id' do

setup do
@employee = Factory(:employee)
end
Expand All @@ -26,10 +65,53 @@ def setup
should 'include self in group' do
assert @employee.group.include?(@employee)
end


should 'return #ids array' do
assert_equal [@employee.id], @employee.group.ids
end

should 'return #quoted_ids string for use in sql statments' do
assert_equal "#{@employee.id}", @employee.group.quoted_ids
end

end

context 'with a set group_id' do

setup do
@employee = Factory(:employee, :group_id => 1)
end

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

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

end

# context 'with different employees in different groups' 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)
# end
#
# should 'have creaed factories in a sane manner' do
# # raise Employee.reflect_on_association(:reports).inspect
# # raise Employee.reflect_on_association(:grouped_scope_reports).inspect
# # assert_equal @e1_g1.reports.size, @e1_g1.group.reports.size
# end
#
# end


end

# Make sure legacy quoted_ids are quoted

end
10 changes: 9 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ def setup_database(options)
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

def setup_models(options)
['Employee','Report','LegacyEmployee','LegacyReport'].each do |klass|
['Employee','Report','LegacyEmployee','LegacyReport','FooBar'].each do |klass|
Object.send(:remove_const,klass) rescue nil
Object.const_set klass, Class.new(ActiveRecord::Base)
end
Expand All @@ -66,6 +70,10 @@ def setup_models(options)
LegacyReport.class_eval do
belongs_to :employee, :class_name => 'LegacyEmployee'
end
FooBar.class_eval do
has_many :reports
grouped_scope :reports
end
end


Expand Down

0 comments on commit a78b28d

Please sign in to comment.