Skip to content

Commit

Permalink
Associations test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
methodmissing committed Mar 19, 2009
1 parent 9cafcfe commit 10cc592
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/optimizations/associations/macro.rb
Expand Up @@ -107,17 +107,19 @@ def association_instance_set(name, association)
instance_variable_set("@#{name}", association)
end

# Register an association with Scrooge
#
def scrooge_seen_association!( association )
if scrooged? && !scrooge_seen_association?( association )
@attributes.scrooge_associations << association
self.class.scrooge_callsite( @attributes.callsite_signature ).association!( association )
end
end

private

# Register an association with Scrooge
#
def scrooge_seen_association!( association )
if scrooged? && !scrooge_seen_association?( association )
@attributes.scrooge_associations << association
self.class.scrooge_callsite( @attributes.callsite_signature ).association!( association )
end
end

# Has this association already been flagged for the callsite ?
#
def scrooge_seen_association?( association )
@attributes.scrooge_associations.include?( association )
end
Expand Down
6 changes: 6 additions & 0 deletions lib/optimizations/columns/macro.rb
Expand Up @@ -168,6 +168,12 @@ def respond_to_with_scrooge?(symbol, include_private=false)
respond_to_without_scrooge?(symbol, include_private)
end
end

# Expose this record's callsite signature
#
def callsite_signature
scrooged? ? @attributes.callsite_signature : nil
end

private

Expand Down
2 changes: 1 addition & 1 deletion lib/scrooge.rb
Expand Up @@ -79,4 +79,4 @@ def callsite_signature( call_stack, supplementary )
end

Scrooge::Optimizations::Columns::Macro.install!
Scrooge::Optimizations::Associations::Macro.install!
Scrooge::Optimizations::Associations::Macro.install!
2 changes: 1 addition & 1 deletion test/helper.rb
Expand Up @@ -31,7 +31,7 @@ def active_record_test_files
end

def test_files
glob( "#{File.dirname(__FILE__)}/*_test.rb" )
glob( "#{File.dirname(__FILE__)}/**/*_test.rb" )
end

private
Expand Down
5 changes: 5 additions & 0 deletions test/models/mysql_host.rb
@@ -0,0 +1,5 @@
class MysqlHost < ActiveRecord::Base
set_table_name 'host'
set_primary_key 'Host'

end
2 changes: 2 additions & 0 deletions test/models/mysql_user.rb
Expand Up @@ -3,6 +3,8 @@ class MysqlUser < ActiveRecord::Base
set_primary_key 'User'

has_many :table_privileges, :class_name => 'MysqlTablePrivilege', :foreign_key => 'User'
has_many :column_privileges, :class_name => 'MysqlColumnPrivilege', :foreign_key => 'User'
belongs_to :host, :class_name => 'MysqlHost', :foreign_key => 'Host'

def after_initialize
max_connections if @attributes.has_key?("max_user_connections")
Expand Down
31 changes: 31 additions & 0 deletions test/optimizations/associations/macro_test.rb
@@ -0,0 +1,31 @@
require "#{File.dirname(__FILE__)}/../../helper"

Scrooge::Test.prepare!

class OptimizationsAssociationsMacroTest < ActiveSupport::TestCase

test "should flag a record as being scrooged when found through a supported SQL query" do
assert MysqlUser.find(:first).scrooged?
end

test "should be able to flag any associations instantiated from a record" do
@user = MysqlUser.find(:first)
@user.host
assert_equal MysqlUser.scrooge_callsite( @user.callsite_signature ).associations, Set[:host]
end

test "should only flag preloadable associations" do
Scrooge::Callsite.any_instance.expects(:association!).once
@user = MysqlUser.find(:first)
@user.table_privileges
assert_equal MysqlUser.scrooge_callsite( @user.callsite_signature ).associations, Set.new
end

test "should be able to identify all preloadable associations for a given Model" do
assert_equal MysqlUser.preloadable_associations, [:host]
assert_equal MysqlHost.preloadable_associations, []
assert_equal MysqlColumnPrivilege.preloadable_associations, [:mysql_user]
assert_equal MysqlTablePrivilege.preloadable_associations, [:mysql_user, :column_privilege]
end

end

0 comments on commit 10cc592

Please sign in to comment.