activerecord 1.10.1 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (84) hide show
  1. data/CHANGELOG +187 -19
  2. data/RUNNING_UNIT_TESTS +11 -0
  3. data/lib/active_record.rb +3 -1
  4. data/lib/active_record/acts/list.rb +25 -14
  5. data/lib/active_record/acts/nested_set.rb +4 -4
  6. data/lib/active_record/acts/tree.rb +18 -1
  7. data/lib/active_record/associations.rb +90 -17
  8. data/lib/active_record/associations/association_collection.rb +44 -5
  9. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +17 -4
  10. data/lib/active_record/associations/has_many_association.rb +13 -3
  11. data/lib/active_record/associations/has_one_association.rb +19 -0
  12. data/lib/active_record/base.rb +292 -268
  13. data/lib/active_record/callbacks.rb +14 -14
  14. data/lib/active_record/connection_adapters/abstract_adapter.rb +137 -75
  15. data/lib/active_record/connection_adapters/db2_adapter.rb +10 -8
  16. data/lib/active_record/connection_adapters/mysql_adapter.rb +91 -64
  17. data/lib/active_record/connection_adapters/oci_adapter.rb +6 -6
  18. data/lib/active_record/connection_adapters/postgresql_adapter.rb +113 -60
  19. data/lib/active_record/connection_adapters/sqlite_adapter.rb +15 -12
  20. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +159 -132
  21. data/lib/active_record/fixtures.rb +59 -12
  22. data/lib/active_record/locking.rb +10 -9
  23. data/lib/active_record/migration.rb +112 -5
  24. data/lib/active_record/query_cache.rb +64 -0
  25. data/lib/active_record/timestamp.rb +10 -8
  26. data/lib/active_record/validations.rb +121 -26
  27. data/rakefile +16 -10
  28. data/test/aaa_create_tables_test.rb +26 -48
  29. data/test/abstract_unit.rb +3 -0
  30. data/test/aggregations_test.rb +19 -19
  31. data/test/association_callbacks_test.rb +110 -0
  32. data/test/associations_go_eager_test.rb +48 -14
  33. data/test/associations_test.rb +344 -142
  34. data/test/base_test.rb +150 -31
  35. data/test/binary_test.rb +7 -0
  36. data/test/callbacks_test.rb +24 -5
  37. data/test/column_alias_test.rb +2 -2
  38. data/test/connections/native_sqlserver_odbc/connection.rb +26 -0
  39. data/test/deprecated_associations_test.rb +27 -28
  40. data/test/deprecated_finder_test.rb +8 -9
  41. data/test/finder_test.rb +52 -17
  42. data/test/fixtures/author.rb +39 -0
  43. data/test/fixtures/categories.yml +7 -0
  44. data/test/fixtures/categories_posts.yml +8 -0
  45. data/test/fixtures/category.rb +2 -0
  46. data/test/fixtures/comment.rb +3 -1
  47. data/test/fixtures/comments.yml +43 -1
  48. data/test/fixtures/companies.yml +14 -0
  49. data/test/fixtures/company.rb +1 -1
  50. data/test/fixtures/computers.yml +2 -1
  51. data/test/fixtures/db_definitions/db2.sql +7 -2
  52. data/test/fixtures/db_definitions/mysql.drop.sql +2 -0
  53. data/test/fixtures/db_definitions/mysql.sql +11 -6
  54. data/test/fixtures/db_definitions/oci.sql +7 -2
  55. data/test/fixtures/db_definitions/postgresql.drop.sql +3 -1
  56. data/test/fixtures/db_definitions/postgresql.sql +8 -5
  57. data/test/fixtures/db_definitions/sqlite.drop.sql +2 -0
  58. data/test/fixtures/db_definitions/sqlite.sql +9 -4
  59. data/test/fixtures/db_definitions/sqlserver.drop.sql +2 -0
  60. data/test/fixtures/db_definitions/sqlserver.sql +12 -7
  61. data/test/fixtures/developer.rb +8 -1
  62. data/test/fixtures/migrations/3_innocent_jointable.rb +12 -0
  63. data/test/fixtures/post.rb +8 -2
  64. data/test/fixtures/posts.yml +21 -0
  65. data/test/fixtures/project.rb +14 -1
  66. data/test/fixtures/subscriber.rb +3 -0
  67. data/test/fixtures_test.rb +14 -0
  68. data/test/inheritance_test.rb +30 -22
  69. data/test/lifecycle_test.rb +3 -4
  70. data/test/locking_test.rb +2 -4
  71. data/test/migration_test.rb +186 -0
  72. data/test/mixin_nested_set_test.rb +19 -19
  73. data/test/mixin_test.rb +88 -88
  74. data/test/modules_test.rb +5 -10
  75. data/test/multiple_db_test.rb +2 -0
  76. data/test/pk_test.rb +8 -12
  77. data/test/reflection_test.rb +8 -4
  78. data/test/schema_test_postgresql.rb +63 -0
  79. data/test/thread_safety_test.rb +4 -1
  80. data/test/transactions_test.rb +9 -2
  81. data/test/unconnected_test.rb +1 -0
  82. data/test/validations_test.rb +151 -8
  83. metadata +11 -5
  84. data/test/migration_mysql.rb +0 -104
data/rakefile CHANGED
@@ -8,7 +8,7 @@ require 'rake/contrib/rubyforgepublisher'
8
8
 
9
9
  PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
10
10
  PKG_NAME = 'activerecord'
11
- PKG_VERSION = '1.10.1' + PKG_BUILD
11
+ PKG_VERSION = '1.11.0' + PKG_BUILD
12
12
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
13
13
 
14
14
  RELEASE_NAME = "REL #{PKG_VERSION}"
@@ -28,20 +28,20 @@ task :default => [ :test_ruby_mysql, :test_mysql_ruby, :test_sqlite, :test_sqlit
28
28
 
29
29
  Rake::TestTask.new("test_ruby_mysql") { |t|
30
30
  t.libs << "test" << "test/connections/native_mysql"
31
- t.pattern = 'test/*_test.rb'
31
+ t.pattern = 'test/*_test{,_mysql}.rb'
32
32
  t.verbose = true
33
33
  }
34
34
 
35
35
  Rake::TestTask.new("test_mysql_ruby") { |t|
36
36
  t.libs << "test" << "test/connections/native_mysql"
37
- t.pattern = 'test/*_test.rb'
37
+ t.pattern = 'test/*_test{,_mysql}.rb'
38
38
  t.verbose = true
39
39
  }
40
40
 
41
- for adapter in %w( postgresql sqlite sqlite3 sqlserver db2 oci )
41
+ for adapter in %w( postgresql sqlite sqlite3 sqlserver sqlserver_odbc db2 oci )
42
42
  Rake::TestTask.new("test_#{adapter}") { |t|
43
43
  t.libs << "test" << "test/connections/native_#{adapter}"
44
- t.pattern = 'test/*_test.rb'
44
+ t.pattern = "test/*_test{,_#{adapter}}.rb"
45
45
  t.verbose = true
46
46
  }
47
47
  end
@@ -60,6 +60,12 @@ Rake::RDocTask.new { |rdoc|
60
60
  rdoc.rdoc_files.include('dev-utils/*.rb')
61
61
  }
62
62
 
63
+ # Enhance rdoc task to copy referenced images also
64
+ task :rdoc do
65
+ FileUtils.mkdir_p "doc/files/examples/"
66
+ FileUtils.copy "examples/associations.png", "doc/files/examples/associations.png"
67
+ end
68
+
63
69
 
64
70
  # Create compressed packages
65
71
 
@@ -76,7 +82,7 @@ spec = Gem::Specification.new do |s|
76
82
  s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
77
83
  end
78
84
 
79
- s.add_dependency('activesupport', '= 1.0.4' + PKG_BUILD)
85
+ s.add_dependency('activesupport', '= 1.1.0' + PKG_BUILD)
80
86
 
81
87
  s.files.delete "test/fixtures/fixture_database.sqlite"
82
88
  s.files.delete "test/fixtures/fixture_database_2.sqlite"
@@ -130,13 +136,13 @@ end
130
136
 
131
137
  desc "Publish the beta gem"
132
138
  task :pgem => [:package] do
133
- Rake::SshFilePublisher.new("davidhh@wrath.rubyonrails.com", "public_html/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
134
- `ssh davidhh@wrath.rubyonrails.com './gemupdate.sh'`
139
+ Rake::SshFilePublisher.new("davidhh@wrath.rubyonrails.org", "public_html/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
140
+ `ssh davidhh@wrath.rubyonrails.org './gemupdate.sh'`
135
141
  end
136
142
 
137
143
  desc "Publish the API documentation"
138
144
  task :pdoc => [:rdoc] do
139
- Rake::SshDirPublisher.new("davidhh@wrath.rubyonrails.com", "public_html/ar", "doc").upload
145
+ Rake::SshDirPublisher.new("davidhh@wrath.rubyonrails.org", "public_html/ar", "doc").upload
140
146
  end
141
147
 
142
148
  desc "Publish the release files to RubyForge."
@@ -252,4 +258,4 @@ task :release => [:package] do
252
258
  first_file = false
253
259
  end
254
260
  end
255
- end
261
+ end
@@ -1,58 +1,36 @@
1
+ # The filename begins with "aaa" to ensure this is the first test.
1
2
  require 'abstract_unit'
2
3
 
3
- # The filename for this test begins with "aaa" so that
4
- # it will be the first test.
5
-
6
- class SqlFile < File
7
- #Define an iterator that iterates over the statements in a .sql file.
8
- #statements are separated by a semicolon.
9
- def initialize(path)
10
- super(path)
11
- end
12
-
13
- def each_statement()
14
- statement = ''
15
- each_line { |line|
16
- #The last character of each line is a line-feed, so we will check the next-to-last character
17
- #to see if it is a semicolon. A better way of doing this would be to look for a semicolon anywhere
18
- #within the line in case multiple statements have been put on a single line.
19
- #The last statement in the file must be followed by a line-feed.
20
- if line.slice(-2,1)==';' then
21
- statement = statement + line.slice(0,line.length-2) + "\n"
22
- yield statement
23
- statement = ''
24
- else
25
- statement = statement + line
26
- end
27
- }
28
- end
29
- end
30
-
31
4
  class CreateTablesTest < Test::Unit::TestCase
32
5
  def setup
33
- # This method is required by rake.
6
+ @base_path = "#{File.dirname(__FILE__)}/fixtures/db_definitions"
34
7
  end
35
8
 
36
- def run_sql_file(connection, path)
37
- sql_file = SqlFile.new(path)
38
- sql_file.each_statement { |statement|
39
- begin
40
- #Skip errors. If there is a problem creating the tables then it will show up in other tests.
41
- connection.execute(statement)
42
- rescue ActiveRecord::StatementInvalid
43
- end }
9
+ def test_drop_and_create_main_tables
10
+ recreate ActiveRecord::Base
11
+ assert true
12
+ end
13
+
14
+ def test_drop_and_create_courses_table
15
+ recreate Course, '2'
16
+ assert true
44
17
  end
45
18
 
46
- def test_table_creation
47
- adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
48
- run_sql_file ActiveRecord::Base.connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + ".drop.sql"
49
- run_sql_file ActiveRecord::Base.connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + ".sql"
19
+ private
20
+ def recreate(base, suffix = nil)
21
+ connection = base.connection
22
+ adapter_name = connection.adapter_name.downcase + suffix.to_s
23
+ execute_sql_file "#{@base_path}/#{adapter_name}.drop.sql", connection
24
+ execute_sql_file "#{@base_path}/#{adapter_name}.sql", connection
25
+ end
50
26
 
51
- # Now do the same thing with the connection used by multiple_db_test.rb
52
- adapter_name = Course.retrieve_connection.adapter_name.downcase
53
- run_sql_file Course.retrieve_connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + "2.drop.sql"
54
- run_sql_file Course.retrieve_connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + "2.sql"
55
-
56
- assert_equal 1,1
57
- end
27
+ def execute_sql_file(path, connection)
28
+ File.read(path).split(';').each_with_index do |sql, i|
29
+ begin
30
+ connection.execute("\n\n-- statement ##{i}\n#{sql}\n") unless sql.blank?
31
+ rescue ActiveRecord::StatementInvalid
32
+ #$stderr.puts "warning: #{$!}"
33
+ end
34
+ end
35
+ end
58
36
  end
@@ -19,3 +19,6 @@ class Test::Unit::TestCase #:nodoc:
19
19
  end
20
20
 
21
21
  Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
22
+ Test::Unit::TestCase.use_instantiated_fixtures = false
23
+ Test::Unit::TestCase.use_transactional_fixtures = (ENV['AR_TX_FIXTURES'] == "yes")
24
+
@@ -5,43 +5,43 @@ class AggregationsTest < Test::Unit::TestCase
5
5
  fixtures :customers
6
6
 
7
7
  def test_find_single_value_object
8
- assert_equal 50, @david.balance.amount
9
- assert_kind_of Money, @david.balance
10
- assert_equal 300, @david.balance.exchange_to("DKK").amount
8
+ assert_equal 50, customers(:david).balance.amount
9
+ assert_kind_of Money, customers(:david).balance
10
+ assert_equal 300, customers(:david).balance.exchange_to("DKK").amount
11
11
  end
12
12
 
13
13
  def test_find_multiple_value_object
14
- assert_equal @customers["david"]["address_street"], @david.address.street
14
+ assert_equal customers(:david).address_street, customers(:david).address.street
15
15
  assert(
16
- @david.address.close_to?(Address.new("Different Street", @customers["david"]["address_city"], @customers["david"]["address_country"]))
16
+ customers(:david).address.close_to?(Address.new("Different Street", customers(:david).address_city, customers(:david).address_country))
17
17
  )
18
18
  end
19
19
 
20
20
  def test_change_single_value_object
21
- @david.balance = Money.new(100)
22
- @david.save
21
+ customers(:david).balance = Money.new(100)
22
+ customers(:david).save
23
23
  assert_equal 100, Customer.find(1).balance.amount
24
24
  end
25
25
 
26
26
  def test_immutable_value_objects
27
- @david.balance = Money.new(100)
28
- assert_raises(TypeError) { @david.balance.instance_eval { @amount = 20 } }
27
+ customers(:david).balance = Money.new(100)
28
+ assert_raises(TypeError) { customers(:david).balance.instance_eval { @amount = 20 } }
29
29
  end
30
30
 
31
31
  def test_inferred_mapping
32
- assert_equal "35.544623640962634", @david.gps_location.latitude
33
- assert_equal "-105.9309951055148", @david.gps_location.longitude
32
+ assert_equal "35.544623640962634", customers(:david).gps_location.latitude
33
+ assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
34
34
 
35
- @david.gps_location = GpsLocation.new("39x-110")
35
+ customers(:david).gps_location = GpsLocation.new("39x-110")
36
36
 
37
- assert_equal "39", @david.gps_location.latitude
38
- assert_equal "-110", @david.gps_location.longitude
37
+ assert_equal "39", customers(:david).gps_location.latitude
38
+ assert_equal "-110", customers(:david).gps_location.longitude
39
39
 
40
- @david.save
40
+ customers(:david).save
41
41
 
42
- @david.reload
42
+ customers(:david).reload
43
43
 
44
- assert_equal "39", @david.gps_location.latitude
45
- assert_equal "-110", @david.gps_location.longitude
44
+ assert_equal "39", customers(:david).gps_location.latitude
45
+ assert_equal "-110", customers(:david).gps_location.longitude
46
46
  end
47
- end
47
+ end
@@ -0,0 +1,110 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/post'
3
+ require 'fixtures/comment'
4
+ require 'fixtures/author'
5
+ require 'fixtures/category'
6
+ require 'fixtures/project'
7
+ require 'fixtures/developer'
8
+
9
+ class AssociationCallbacksTest < Test::Unit::TestCase
10
+ fixtures :posts, :authors, :projects, :developers
11
+
12
+ def setup
13
+ @david = authors(:david)
14
+ @thinking = posts(:thinking)
15
+ @authorless = posts(:authorless)
16
+ assert @david.post_log.empty?
17
+ end
18
+
19
+ def test_adding_macro_callbacks
20
+ @david.posts_with_callbacks << @thinking
21
+ assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
22
+ @david.posts_with_callbacks << @thinking
23
+ assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
24
+ "after_adding#{@thinking.id}"], @david.post_log
25
+ end
26
+
27
+ def test_adding_with_proc_callbacks
28
+ @david.posts_with_proc_callbacks << @thinking
29
+ assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
30
+ @david.posts_with_proc_callbacks << @thinking
31
+ assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
32
+ "after_adding#{@thinking.id}"], @david.post_log
33
+ end
34
+
35
+ def test_removing_with_macro_callbacks
36
+ first_post, second_post = @david.posts_with_callbacks[0, 2]
37
+ @david.posts_with_callbacks.delete(first_post)
38
+ assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
39
+ @david.posts_with_callbacks.delete(second_post)
40
+ assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
41
+ "after_removing#{second_post.id}"], @david.post_log
42
+ end
43
+
44
+ def test_removing_with_proc_callbacks
45
+ first_post, second_post = @david.posts_with_callbacks[0, 2]
46
+ @david.posts_with_proc_callbacks.delete(first_post)
47
+ assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
48
+ @david.posts_with_proc_callbacks.delete(second_post)
49
+ assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
50
+ "after_removing#{second_post.id}"], @david.post_log
51
+ end
52
+
53
+ def test_multiple_callbacks
54
+ @david.posts_with_multiple_callbacks << @thinking
55
+ assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
56
+ "after_adding_proc#{@thinking.id}"], @david.post_log
57
+ @david.posts_with_multiple_callbacks << @thinking
58
+ assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
59
+ "after_adding_proc#{@thinking.id}", "before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}",
60
+ "after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log
61
+ end
62
+
63
+ def test_has_and_belongs_to_many_add_callback
64
+ david = developers(:david)
65
+ ar = projects(:active_record)
66
+ assert ar.developers_log.empty?
67
+ ar.developers_with_callbacks << david
68
+ assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], ar.developers_log
69
+ ar.developers_with_callbacks << david
70
+ assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
71
+ "after_adding#{david.id}"], ar.developers_log
72
+ end
73
+
74
+ def test_has_and_belongs_to_many_remove_callback
75
+ david = developers(:david)
76
+ jamis = developers(:jamis)
77
+ activerecord = projects(:active_record)
78
+ assert activerecord.developers_log.empty?
79
+ activerecord.developers_with_callbacks.delete(david)
80
+ assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log
81
+
82
+ activerecord.developers_with_callbacks.delete(jamis)
83
+ assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}",
84
+ "after_removing#{jamis.id}"], activerecord.developers_log
85
+ end
86
+
87
+ def test_dont_add_if_before_callback_raises_exception
88
+ assert !@david.unchangable_posts.include?(@authorless)
89
+ begin
90
+ @david.unchangable_posts << @authorless
91
+ rescue Exception => e
92
+ end
93
+ assert @david.post_log.empty?
94
+ assert !@david.unchangable_posts.include?(@authorless)
95
+ @david.reload
96
+ assert !@david.unchangable_posts.include?(@authorless)
97
+ end
98
+
99
+ def test_push_with_attributes
100
+ david = developers(:david)
101
+ activerecord = projects(:active_record)
102
+ assert activerecord.developers_log.empty?
103
+ activerecord.developers_with_callbacks.push_with_attributes(david, {})
104
+ assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], activerecord.developers_log
105
+ activerecord.developers_with_callbacks.push_with_attributes(david, {})
106
+ assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
107
+ "after_adding#{david.id}"], activerecord.developers_log
108
+ end
109
+ end
110
+
@@ -3,47 +3,54 @@ require 'fixtures/post'
3
3
  require 'fixtures/comment'
4
4
  require 'fixtures/author'
5
5
  require 'fixtures/category'
6
+ require 'fixtures/company'
6
7
 
7
8
  class EagerAssociationTest < Test::Unit::TestCase
8
- fixtures :posts, :comments, :authors, :categories, :categories_posts
9
+ fixtures :posts, :comments, :authors, :categories, :categories_posts,
10
+ :companies, :accounts
9
11
 
10
12
  def test_loading_with_one_association
11
13
  posts = Post.find(:all, :include => :comments)
12
- assert_equal 2, posts.first.comments.size
13
- assert posts.first.comments.include?(@greetings)
14
+ post = posts.find { |p| p.id == 1 }
15
+ assert_equal 2, post.comments.size
16
+ assert post.comments.include?(comments(:greetings))
14
17
 
15
18
  post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
16
19
  assert_equal 2, post.comments.size
17
- assert post.comments.include?(@greetings)
20
+ assert post.comments.include?(comments(:greetings))
18
21
  end
19
22
 
20
23
  def test_with_ordering
21
24
  posts = Post.find(:all, :include => :comments, :order => "posts.id DESC")
22
- assert_equal @authorless, posts[0]
23
- assert_equal @thinking, posts[1]
24
- assert_equal @welcome, posts[2]
25
+ assert_equal posts(:sti_habtm), posts[0]
26
+ assert_equal posts(:sti_post_and_comments), posts[1]
27
+ assert_equal posts(:sti_comments), posts[2]
28
+ assert_equal posts(:authorless), posts[3]
29
+ assert_equal posts(:thinking), posts[4]
30
+ assert_equal posts(:welcome), posts[5]
25
31
  end
26
32
 
27
33
  def test_loading_with_multiple_associations
28
34
  posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
29
35
  assert_equal 2, posts.first.comments.size
30
36
  assert_equal 2, posts.first.categories.size
31
- assert posts.first.comments.include?(@greetings)
37
+ assert posts.first.comments.include?(comments(:greetings))
32
38
  end
33
39
 
34
40
  def test_loading_from_an_association
35
- posts = @david.posts.find(:all, :include => :comments, :order => "posts.id")
41
+ posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
36
42
  assert_equal 2, posts.first.comments.size
37
43
  end
38
44
 
39
45
  def test_loading_with_no_associations
40
- assert_nil Post.find(@authorless.id, :include => :author).author
46
+ assert_nil Post.find(posts(:authorless).id, :include => :author).author
41
47
  end
42
48
 
43
49
  def test_eager_association_loading_with_belongs_to
44
50
  comments = Comment.find(:all, :include => :post)
45
- assert_equal @welcome.title, comments.first.post.title
46
- assert_equal @thinking.title, comments.last.post.title
51
+ titles = comments.map { |c| c.post.title }
52
+ assert titles.include?(posts(:welcome).title)
53
+ assert titles.include?(posts(:sti_post_and_comments).title)
47
54
  end
48
55
 
49
56
  def test_eager_association_loading_with_habtm
@@ -51,12 +58,39 @@ class EagerAssociationTest < Test::Unit::TestCase
51
58
  assert_equal 2, posts[0].categories.size
52
59
  assert_equal 1, posts[1].categories.size
53
60
  assert_equal 0, posts[2].categories.size
54
- assert posts[0].categories.include?(@technology)
55
- assert posts[1].categories.include?(@general)
61
+ assert posts[0].categories.include?(categories(:technology))
62
+ assert posts[1].categories.include?(categories(:general))
56
63
  end
57
64
 
58
65
  def test_eager_with_inheritance
59
66
  posts = SpecialPost.find(:all, :include => [ :comments ])
60
67
  end
68
+
69
+ def test_eager_has_one_with_association_inheritance
70
+ post = Post.find(4, :include => [ :very_special_comment ])
71
+ assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
72
+ end
73
+
74
+ def test_eager_has_many_with_association_inheritance
75
+ post = Post.find(4, :include => [ :special_comments ])
76
+ post.special_comments.each do |special_comment|
77
+ assert_equal "SpecialComment", special_comment.class.to_s
78
+ end
79
+ end
80
+
81
+ def test_eager_habtm_with_association_inheritance
82
+ post = Post.find(6, :include => [ :special_categories ])
83
+ assert_equal 1, post.special_categories.size
84
+ post.special_categories.each do |special_category|
85
+ assert_equal "SpecialCategory", special_category.class.to_s
86
+ end
87
+ end
88
+
89
+ def test_eager_with_has_one_dependent_does_not_destroy_dependent
90
+ assert_not_nil companies(:first_firm).account
91
+ f = Firm.find(:first, :include => :account,
92
+ :conditions => ["companies.name = ?", "37signals"])
93
+ assert_not_nil companies(:first_firm, :reload).account
94
+ end
61
95
  end
62
96