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.
- data/CHANGELOG +187 -19
- data/RUNNING_UNIT_TESTS +11 -0
- data/lib/active_record.rb +3 -1
- data/lib/active_record/acts/list.rb +25 -14
- data/lib/active_record/acts/nested_set.rb +4 -4
- data/lib/active_record/acts/tree.rb +18 -1
- data/lib/active_record/associations.rb +90 -17
- data/lib/active_record/associations/association_collection.rb +44 -5
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +17 -4
- data/lib/active_record/associations/has_many_association.rb +13 -3
- data/lib/active_record/associations/has_one_association.rb +19 -0
- data/lib/active_record/base.rb +292 -268
- data/lib/active_record/callbacks.rb +14 -14
- data/lib/active_record/connection_adapters/abstract_adapter.rb +137 -75
- data/lib/active_record/connection_adapters/db2_adapter.rb +10 -8
- data/lib/active_record/connection_adapters/mysql_adapter.rb +91 -64
- data/lib/active_record/connection_adapters/oci_adapter.rb +6 -6
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +113 -60
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +15 -12
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +159 -132
- data/lib/active_record/fixtures.rb +59 -12
- data/lib/active_record/locking.rb +10 -9
- data/lib/active_record/migration.rb +112 -5
- data/lib/active_record/query_cache.rb +64 -0
- data/lib/active_record/timestamp.rb +10 -8
- data/lib/active_record/validations.rb +121 -26
- data/rakefile +16 -10
- data/test/aaa_create_tables_test.rb +26 -48
- data/test/abstract_unit.rb +3 -0
- data/test/aggregations_test.rb +19 -19
- data/test/association_callbacks_test.rb +110 -0
- data/test/associations_go_eager_test.rb +48 -14
- data/test/associations_test.rb +344 -142
- data/test/base_test.rb +150 -31
- data/test/binary_test.rb +7 -0
- data/test/callbacks_test.rb +24 -5
- data/test/column_alias_test.rb +2 -2
- data/test/connections/native_sqlserver_odbc/connection.rb +26 -0
- data/test/deprecated_associations_test.rb +27 -28
- data/test/deprecated_finder_test.rb +8 -9
- data/test/finder_test.rb +52 -17
- data/test/fixtures/author.rb +39 -0
- data/test/fixtures/categories.yml +7 -0
- data/test/fixtures/categories_posts.yml +8 -0
- data/test/fixtures/category.rb +2 -0
- data/test/fixtures/comment.rb +3 -1
- data/test/fixtures/comments.yml +43 -1
- data/test/fixtures/companies.yml +14 -0
- data/test/fixtures/company.rb +1 -1
- data/test/fixtures/computers.yml +2 -1
- data/test/fixtures/db_definitions/db2.sql +7 -2
- data/test/fixtures/db_definitions/mysql.drop.sql +2 -0
- data/test/fixtures/db_definitions/mysql.sql +11 -6
- data/test/fixtures/db_definitions/oci.sql +7 -2
- data/test/fixtures/db_definitions/postgresql.drop.sql +3 -1
- data/test/fixtures/db_definitions/postgresql.sql +8 -5
- data/test/fixtures/db_definitions/sqlite.drop.sql +2 -0
- data/test/fixtures/db_definitions/sqlite.sql +9 -4
- data/test/fixtures/db_definitions/sqlserver.drop.sql +2 -0
- data/test/fixtures/db_definitions/sqlserver.sql +12 -7
- data/test/fixtures/developer.rb +8 -1
- data/test/fixtures/migrations/3_innocent_jointable.rb +12 -0
- data/test/fixtures/post.rb +8 -2
- data/test/fixtures/posts.yml +21 -0
- data/test/fixtures/project.rb +14 -1
- data/test/fixtures/subscriber.rb +3 -0
- data/test/fixtures_test.rb +14 -0
- data/test/inheritance_test.rb +30 -22
- data/test/lifecycle_test.rb +3 -4
- data/test/locking_test.rb +2 -4
- data/test/migration_test.rb +186 -0
- data/test/mixin_nested_set_test.rb +19 -19
- data/test/mixin_test.rb +88 -88
- data/test/modules_test.rb +5 -10
- data/test/multiple_db_test.rb +2 -0
- data/test/pk_test.rb +8 -12
- data/test/reflection_test.rb +8 -4
- data/test/schema_test_postgresql.rb +63 -0
- data/test/thread_safety_test.rb +4 -1
- data/test/transactions_test.rb +9 -2
- data/test/unconnected_test.rb +1 -0
- data/test/validations_test.rb +151 -8
- metadata +11 -5
- 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.
|
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 =
|
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
|
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.
|
134
|
-
`ssh davidhh@wrath.rubyonrails.
|
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.
|
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
|
-
|
6
|
+
@base_path = "#{File.dirname(__FILE__)}/fixtures/db_definitions"
|
34
7
|
end
|
35
8
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
data/test/abstract_unit.rb
CHANGED
@@ -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
|
+
|
data/test/aggregations_test.rb
CHANGED
@@ -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,
|
9
|
-
assert_kind_of Money,
|
10
|
-
assert_equal 300,
|
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
|
14
|
+
assert_equal customers(:david).address_street, customers(:david).address.street
|
15
15
|
assert(
|
16
|
-
|
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
|
-
|
22
|
-
|
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
|
-
|
28
|
-
assert_raises(TypeError) {
|
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",
|
33
|
-
assert_equal "-105.9309951055148",
|
32
|
+
assert_equal "35.544623640962634", customers(:david).gps_location.latitude
|
33
|
+
assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
|
34
34
|
|
35
|
-
|
35
|
+
customers(:david).gps_location = GpsLocation.new("39x-110")
|
36
36
|
|
37
|
-
assert_equal "39",
|
38
|
-
assert_equal "-110",
|
37
|
+
assert_equal "39", customers(:david).gps_location.latitude
|
38
|
+
assert_equal "-110", customers(:david).gps_location.longitude
|
39
39
|
|
40
|
-
|
40
|
+
customers(:david).save
|
41
41
|
|
42
|
-
|
42
|
+
customers(:david).reload
|
43
43
|
|
44
|
-
assert_equal "39",
|
45
|
-
assert_equal "-110",
|
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
|
-
|
13
|
-
|
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?(
|
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
|
23
|
-
assert_equal
|
24
|
-
assert_equal
|
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?(
|
37
|
+
assert posts.first.comments.include?(comments(:greetings))
|
32
38
|
end
|
33
39
|
|
34
40
|
def test_loading_from_an_association
|
35
|
-
posts =
|
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(
|
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
|
-
|
46
|
-
|
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?(
|
55
|
-
assert posts[1].categories.include?(
|
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
|
|