Skip to content

Commit

Permalink
New factory to generate :employee_with_reports. Turn on color logging…
Browse files Browse the repository at this point in the history
…. Add a few tests for HasManyAssociation.
  • Loading branch information
metaskills committed Sep 21, 2008
1 parent 7ead06e commit bdcc1b9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
5 changes: 3 additions & 2 deletions lib/grouped_scope/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def grouped_scope(*args)
raise ArgumentError, "Cannot create a group scope for :#{association} because it is not a has_many " +
"association. Make sure to call grouped_scope after the has_many associations."
end
grouped_scopes[association] = true
has_many :"grouped_scope_#{association}", existing_assoc.options
grouped_scope_method = :"grouped_scope_#{association}"
grouped_scopes[grouped_scope_method] = true
has_many grouped_scope_method, existing_assoc.options
end
include InstanceMethods
end
Expand Down
7 changes: 4 additions & 3 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
end

Factory.define :employee_with_reports, :class => 'Employee' do |e|
e.reports do |reports|
[reports.association(:report), reports.associations(:report)]
end
e.name { "Factory Employee ##{Factory.next(:id)}" }
e.email { Factory.next(:email) }
e.reports { |employee| [employee.association(:report),employee.association(:report)] }
end


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 @@ -15,7 +15,7 @@ def setup
should 'add to inheritable attributes with new grouped_scope' do
assert_nil Employee.grouped_scopes[:foobars]
Employee.class_eval { has_many(:foobars) ; grouped_scope(:foobars) }
assert Employee.grouped_scopes[:foobars]
assert Employee.grouped_scopes[:grouped_scope_foobars]
end

end
Expand Down
34 changes: 18 additions & 16 deletions test/grouped_scope/has_many_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ def setup

context 'For existing ungrouped has_many associations' do

setup do
@con = ActiveRecord::Base.connection
end

should 'be able to call it' do
@employee = Factory(:employee)
assert_instance_of Array, @employee.reports
context 'for an Employee' do

setup do
@con = ActiveRecord::Base.connection
end

should 'simply work' do
@employee = Factory(:employee)
assert_instance_of Array, @employee.reports
end

should 'scope existing associations to owner' do
@employee = Factory(:employee_with_reports)
assert_sql(/"reports".employee_id = 1/) do
@employee.reports.reload
end
end

end

# should 'description' do
# @employee = Factory(:employee)
# assert_instance_of Array, e.reports
# # assert_sql(/\(#{@con.quote_table_name('employees')}.#{@con.quote_column_name('id')} = dddd/) do
# #
# # end
# end


end


Expand Down
3 changes: 1 addition & 2 deletions test/lib/test_case.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__)+'/../debug.log')
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
ActiveRecord::Base.connection.class.class_eval do
IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/]
Expand Down Expand Up @@ -31,7 +30,7 @@ def assert_sql(*patterns_to_match)
patterns_to_match.each do |pattern|
failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql }
end
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found."
assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found in:\n#{$queries_executed.inspect}"
end

def assert_queries(num = 1)
Expand Down

0 comments on commit bdcc1b9

Please sign in to comment.