Skip to content

Commit

Permalink
Adding some HasManyAssociation count tests. Setting up :through model…
Browse files Browse the repository at this point in the history
…s for next implementation.
  • Loading branch information
metaskills committed Sep 25, 2008
1 parent 76f75a2 commit ab6c3bb
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 78 deletions.
4 changes: 0 additions & 4 deletions test/grouped_scope/association_reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ def setup
end

end

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

end

end

Expand Down
155 changes: 87 additions & 68 deletions test/grouped_scope/has_many_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,122 @@ def setup
setup_environment
end

context 'For existing ungrouped has_many associations' do

context 'For an Employee' do

context 'for an Employee' do

setup do
@employee = Factory(:employee)
setup do
@employee = Factory(:employee)
end

should 'scope existing association to owner' do
assert_sql(/"reports".employee_id = #{@employee.id}/) do
@employee.reports(true)
end
end

should 'scope group association to group' do
assert_sql(/"reports".employee_id IN \(#{@employee.id}\)/) do
@employee.group.reports(true)
end
end

context 'for counting sql' do

should 'scope existing association to owner' do
assert_sql(/"reports".employee_id = #{@employee.id}/) do
@employee.reports(true)
end
setup do
@e1 = Factory(:employee_with_reports, :group_id => 1)
@e2 = Factory(:employee_with_reports, :group_id => 1)
end

should 'scope group association to group' do
assert_sql(/"reports".employee_id IN \(#{@employee.id}\)/) do
@employee.group.reports(true)
should 'scope count sql to owner' do
assert_sql(/SELECT count\(\*\)/,/employee_id = #{@e1.id}/) do
@e1.reports(true).count
end
end

context 'training association extensions' do

setup do
@e1 = Factory(:employee_with_urgent_reports, :group_id => 1)
@e2 = Factory(:employee, :group_id => 1)
@urgent_reports = @e1.reports.select(&:urgent_title?)
end

should '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
assert_same_elements @urgent_reports, @e2.group.reports(true).urgent
end

should 'use assoc extension SQL along with group reflection' do
assert_sql(/'URGENT'/,/"reports".employee_id IN/) do
@e2.group.reports(true).urgent
end
should '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
assert_equal @e1.reports(true).count + @e2.reports(true).count, @e2.group.reports(true).count
end

context 'training named scopes' do
end

setup do
@e1 = Factory(:employee_with_urgent_reports, :group_id => 1)
@e2 = Factory(: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
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
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
assert_sql(/body LIKE '%URGENT%'/,/"title" = 'URGENT'/,/employee_id IN/) do
@e2.group.reports(true).with_urgent_title.with_urgent_body.inspect
end
end
context 'training association extensions' do

setup do
@e1 = Factory(:employee_with_urgent_reports, :group_id => 1)
@e2 = Factory(:employee, :group_id => 1)
@urgent_reports = @e1.reports.select(&:urgent_title?)
end

should 'find urgent report via normal ungrouped association' do
assert_same_elements @urgent_reports, @e1.reports(true).urgent
end

end
should '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
assert_sql(/'URGENT'/,/"reports".employee_id IN/) do
@e2.group.reports(true).urgent
end
end

context 'for a LegacyEmployee' do
end

context 'training named scopes' do

setup do
@employee = Factory(:legacy_employee)
@e1 = Factory(:employee_with_urgent_reports, :group_id => 1)
@e2 = Factory(:employee, :group_id => 1)
@urgent_titles = @e1.reports.select(&:urgent_title?)
@urgent_bodys = @e1.reports.select(&:urgent_body?)
end

should 'scope existing association to owner' do
assert_sql(/"legacy_reports".email = '#{@employee.id}'/) do
@employee.reports(true)
end

should '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 'scope group association to group' do
assert_sql(/"legacy_reports".email IN \('#{@employee.id}'\)/) do
@employee.group.reports(true)
end
should '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
assert_sql(/body LIKE '%URGENT%'/,/"title" = 'URGENT'/,/employee_id IN/) do
@e2.group.reports(true).with_urgent_title.with_urgent_body.inspect
end
end

end


end

context 'For a LegacyEmployee' do

setup do
@employee = Factory(:legacy_employee)
end

should '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
assert_sql(/"legacy_reports".email IN \('#{@employee.id}'\)/) do
@employee.group.reports(true)
end
end

end


end
35 changes: 29 additions & 6 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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'
end

protected
Expand All @@ -30,6 +33,14 @@ def setup_database(options)
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
Expand All @@ -51,12 +62,11 @@ def setup_database(options)
end

class Employee < ActiveRecord::Base
has_many :reports do
def urgent
all(:conditions => {:title => 'URGENT'})
end
end
grouped_scope :reports
has_many :reports do ; def urgent ; all(:conditions => {:title => 'URGENT'}) ; end ; end
has_many :taxonomies, :as => :classable
has_many :department_memberships
has_many :departments, :through => :department_memberships
grouped_scope :reports, :departments
end

class Report < ActiveRecord::Base
Expand All @@ -67,6 +77,19 @@ def urgent_title? ; self[:title] == 'URGENT' ; end
def urgent_body? ; self[:body] =~ /URGENT/ ; end
end

class Department < ActiveRecord::Base
named_scope :it, :conditions => {:name => 'IT'}
named_scope :hr, :conditions => {:name => 'Human Resources'}
named_scope :finance, :conditions => {:name => 'Finance'}
has_many :department_memberships
has_many :employees, :through => :department_memberships
end

class DepartmentMembership < ActiveRecord::Base
belongs_to :employee
belongs_to :department
end

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

0 comments on commit ab6c3bb

Please sign in to comment.