ibm_db 2.5.26-universal-darwin-14 → 2.6.1-universal-darwin-14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +11 -0
- data/MANIFEST +14 -14
- data/README +225 -225
- data/ext/Makefile.nt32 +181 -181
- data/ext/Makefile.nt32.191 +212 -212
- data/ext/extconf.rb +264 -261
- data/ext/extconf_MacOS.rb +269 -0
- data/ext/ibm_db.c +11879 -11793
- data/ext/ruby_ibm_db.h +241 -240
- data/ext/ruby_ibm_db_cli.c +851 -845
- data/ext/ruby_ibm_db_cli.h +500 -489
- data/init.rb +41 -41
- data/lib/IBM_DB.rb +27 -19
- data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3339 -3289
- data/lib/active_record/connection_adapters/ibmdb_adapter.rb +1 -1
- data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -328
- data/test/cases/adapter_test.rb +207 -207
- data/test/cases/associations/belongs_to_associations_test.rb +711 -711
- data/test/cases/associations/cascaded_eager_loading_test.rb +181 -181
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +851 -851
- data/test/cases/associations/join_model_test.rb +743 -743
- data/test/cases/attribute_methods_test.rb +822 -822
- data/test/cases/base_test.rb +2133 -2133
- data/test/cases/calculations_test.rb +482 -482
- data/test/cases/migration_test.rb +2408 -2408
- data/test/cases/persistence_test.rb +642 -642
- data/test/cases/query_cache_test.rb +257 -257
- data/test/cases/relations_test.rb +1182 -1182
- data/test/cases/schema_dumper_test.rb +256 -256
- data/test/cases/transaction_callbacks_test.rb +300 -300
- data/test/cases/validations/uniqueness_validation_test.rb +299 -299
- data/test/cases/xml_serialization_test.rb +408 -408
- data/test/config.yml +154 -154
- data/test/connections/native_ibm_db/connection.rb +43 -43
- data/test/ibm_db_test.rb +24 -24
- data/test/models/warehouse_thing.rb +4 -4
- data/test/schema/schema.rb +751 -751
- metadata +6 -8
- data/lib/linux/rb18x/ibm_db.bundle +0 -0
- data/lib/linux/rb19x/ibm_db.bundle +0 -0
- data/lib/linux/rb20x/ibm_db.bundle +0 -0
- data/lib/linux/rb21x/ibm_db.bundle +0 -0
data/test/cases/adapter_test.rb
CHANGED
@@ -1,207 +1,207 @@
|
|
1
|
-
require "cases/helper"
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
class AdapterTest < ActiveRecord::TestCase
|
5
|
-
def setup
|
6
|
-
@connection = ActiveRecord::Base.connection
|
7
|
-
end
|
8
|
-
|
9
|
-
if current_adapter?(:IBM_DBAdapter)
|
10
|
-
def test_a_connection_attributes
|
11
|
-
if @connection.servertype.class.name.include?('::IBM_IDS')
|
12
|
-
return
|
13
|
-
end
|
14
|
-
if @connection.respond_to?(:schema)
|
15
|
-
previous_schema = ActiveRecord::Base.connection.schema
|
16
|
-
ActiveRecord::Base.connection.schema = 'SYSCAT'
|
17
|
-
assert_equal 'SYSCAT', ActiveRecord::Base.connection.schema
|
18
|
-
ActiveRecord::Base.connection.schema = previous_schema
|
19
|
-
else
|
20
|
-
warn "#{@connection.class} does not support client connection attribute schema_name"
|
21
|
-
end
|
22
|
-
|
23
|
-
if @connection.respond_to?(:app_user)
|
24
|
-
ActiveRecord::Base.connection.app_user = 'new_user'
|
25
|
-
assert_equal 'new_user', ActiveRecord::Base.connection.app_user
|
26
|
-
else
|
27
|
-
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_USER"
|
28
|
-
end
|
29
|
-
|
30
|
-
if @connection.respond_to?(:account)
|
31
|
-
ActiveRecord::Base.connection.account = 'new_acct'
|
32
|
-
assert_equal 'new_acct', ActiveRecord::Base.connection.account
|
33
|
-
else
|
34
|
-
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_ACCTSTR"
|
35
|
-
end
|
36
|
-
|
37
|
-
if @connection.respond_to?(:application)
|
38
|
-
ActiveRecord::Base.connection.application = 'new_app'
|
39
|
-
assert_equal 'new_app', ActiveRecord::Base.connection.application
|
40
|
-
else
|
41
|
-
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_APPLNAME"
|
42
|
-
end
|
43
|
-
|
44
|
-
if @connection.respond_to?(:workstation)
|
45
|
-
ActiveRecord::Base.connection.workstation = 'new_wrkst'
|
46
|
-
assert_equal 'new_wrkst', ActiveRecord::Base.connection.workstation
|
47
|
-
else
|
48
|
-
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_WRKSTNNAME"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_tables
|
54
|
-
tables = @connection.tables
|
55
|
-
assert tables.include?("accounts")
|
56
|
-
assert tables.include?("authors")
|
57
|
-
assert tables.include?("tasks")
|
58
|
-
assert tables.include?("topics")
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_table_exists?
|
62
|
-
assert @connection.table_exists?("accounts")
|
63
|
-
assert !@connection.table_exists?("nonexistingtable")
|
64
|
-
assert !@connection.table_exists?(nil)
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_indexes
|
68
|
-
idx_name = "accounts_idx"
|
69
|
-
|
70
|
-
if @connection.respond_to?(:indexes)
|
71
|
-
indexes = @connection.indexes("accounts")
|
72
|
-
assert indexes.empty?
|
73
|
-
|
74
|
-
@connection.add_index :accounts, :firm_id, :name => idx_name
|
75
|
-
indexes = @connection.indexes("accounts")
|
76
|
-
assert_equal "accounts", indexes.first.table
|
77
|
-
# OpenBase does not have the concept of a named index
|
78
|
-
# Indexes are merely properties of columns.
|
79
|
-
assert_equal idx_name, indexes.first.name unless current_adapter?(:OpenBaseAdapter)
|
80
|
-
assert !indexes.first.unique
|
81
|
-
assert_equal ["firm_id"], indexes.first.columns
|
82
|
-
else
|
83
|
-
warn "#{@connection.class} does not respond to #indexes"
|
84
|
-
end
|
85
|
-
|
86
|
-
ensure
|
87
|
-
@connection.remove_index(:accounts, :name => idx_name) rescue nil
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_current_database
|
91
|
-
if @connection.respond_to?(:current_database)
|
92
|
-
assert_equal ARTest.connection_config['arunit']['database'], @connection.current_database
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
if current_adapter?(:MysqlAdapter)
|
97
|
-
def test_charset
|
98
|
-
assert_not_nil @connection.charset
|
99
|
-
assert_not_equal 'character_set_database', @connection.charset
|
100
|
-
assert_equal @connection.show_variable('character_set_database'), @connection.charset
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_collation
|
104
|
-
assert_not_nil @connection.collation
|
105
|
-
assert_not_equal 'collation_database', @connection.collation
|
106
|
-
assert_equal @connection.show_variable('collation_database'), @connection.collation
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_show_nonexistent_variable_returns_nil
|
110
|
-
assert_nil @connection.show_variable('foo_bar_baz')
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_not_specifying_database_name_for_cross_database_selects
|
114
|
-
begin
|
115
|
-
assert_nothing_raised do
|
116
|
-
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['arunit'].except(:database))
|
117
|
-
|
118
|
-
config = ARTest.connection_config
|
119
|
-
ActiveRecord::Base.connection.execute(
|
120
|
-
"SELECT #{config['arunit']['database']}.pirates.*, #{config['arunit2']['database']}.courses.* " \
|
121
|
-
"FROM #{config['arunit']['database']}.pirates, #{config['arunit2']['database']}.courses"
|
122
|
-
)
|
123
|
-
end
|
124
|
-
ensure
|
125
|
-
ActiveRecord::Base.establish_connection 'arunit'
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_table_alias
|
131
|
-
def @connection.test_table_alias_length() 10; end
|
132
|
-
class << @connection
|
133
|
-
alias_method :old_table_alias_length, :table_alias_length
|
134
|
-
alias_method :table_alias_length, :test_table_alias_length
|
135
|
-
end
|
136
|
-
|
137
|
-
assert_equal 'posts', @connection.table_alias_for('posts')
|
138
|
-
assert_equal 'posts_comm', @connection.table_alias_for('posts_comments')
|
139
|
-
assert_equal 'dbo_posts', @connection.table_alias_for('dbo.posts')
|
140
|
-
|
141
|
-
class << @connection
|
142
|
-
remove_method :table_alias_length
|
143
|
-
alias_method :table_alias_length, :old_table_alias_length
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
# test resetting sequences in odd tables in postgreSQL
|
148
|
-
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
|
149
|
-
require 'models/movie'
|
150
|
-
require 'models/subscriber'
|
151
|
-
|
152
|
-
def test_reset_empty_table_with_custom_pk
|
153
|
-
Movie.delete_all
|
154
|
-
Movie.connection.reset_pk_sequence! 'movies'
|
155
|
-
assert_equal 1, Movie.create(:name => 'fight club').id
|
156
|
-
end
|
157
|
-
|
158
|
-
if ActiveRecord::Base.connection.adapter_name != "FrontBase"
|
159
|
-
def test_reset_table_with_non_integer_pk
|
160
|
-
Subscriber.delete_all
|
161
|
-
Subscriber.connection.reset_pk_sequence! 'subscribers'
|
162
|
-
sub = Subscriber.new(:name => 'robert drake')
|
163
|
-
sub.id = 'bob drake'
|
164
|
-
assert_nothing_raised { sub.save! }
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_uniqueness_violations_are_translated_to_specific_exception
|
170
|
-
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
171
|
-
assert_raises(ActiveRecord::RecordNotUnique) do
|
172
|
-
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_foreign_key_violations_are_translated_to_specific_exception
|
177
|
-
unless @connection.adapter_name == 'SQLite'
|
178
|
-
assert_raises(ActiveRecord::InvalidForeignKey) do
|
179
|
-
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
|
180
|
-
if @connection.prefetch_primary_key?
|
181
|
-
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
|
182
|
-
@connection.execute "INSERT INTO fk_test_has_fk (id, fk_id) VALUES (#{id_value},0)"
|
183
|
-
else
|
184
|
-
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_disable_referential_integrity
|
191
|
-
assert_nothing_raised do
|
192
|
-
@connection.disable_referential_integrity do
|
193
|
-
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
|
194
|
-
if @connection.prefetch_primary_key?
|
195
|
-
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
|
196
|
-
@connection.execute "INSERT INTO fk_test_has_fk (id, fk_id) VALUES (#{id_value},0)"
|
197
|
-
else
|
198
|
-
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
|
199
|
-
end
|
200
|
-
# should deleted created record as otherwise disable_referential_integrity will try to enable contraints after executed block
|
201
|
-
# and will fail (at least on Oracle)
|
202
|
-
@connection.execute "DELETE FROM fk_test_has_fk"
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
1
|
+
require "cases/helper"
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class AdapterTest < ActiveRecord::TestCase
|
5
|
+
def setup
|
6
|
+
@connection = ActiveRecord::Base.connection
|
7
|
+
end
|
8
|
+
|
9
|
+
if current_adapter?(:IBM_DBAdapter)
|
10
|
+
def test_a_connection_attributes
|
11
|
+
if @connection.servertype.class.name.include?('::IBM_IDS')
|
12
|
+
return
|
13
|
+
end
|
14
|
+
if @connection.respond_to?(:schema)
|
15
|
+
previous_schema = ActiveRecord::Base.connection.schema
|
16
|
+
ActiveRecord::Base.connection.schema = 'SYSCAT'
|
17
|
+
assert_equal 'SYSCAT', ActiveRecord::Base.connection.schema
|
18
|
+
ActiveRecord::Base.connection.schema = previous_schema
|
19
|
+
else
|
20
|
+
warn "#{@connection.class} does not support client connection attribute schema_name"
|
21
|
+
end
|
22
|
+
|
23
|
+
if @connection.respond_to?(:app_user)
|
24
|
+
ActiveRecord::Base.connection.app_user = 'new_user'
|
25
|
+
assert_equal 'new_user', ActiveRecord::Base.connection.app_user
|
26
|
+
else
|
27
|
+
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_USER"
|
28
|
+
end
|
29
|
+
|
30
|
+
if @connection.respond_to?(:account)
|
31
|
+
ActiveRecord::Base.connection.account = 'new_acct'
|
32
|
+
assert_equal 'new_acct', ActiveRecord::Base.connection.account
|
33
|
+
else
|
34
|
+
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_ACCTSTR"
|
35
|
+
end
|
36
|
+
|
37
|
+
if @connection.respond_to?(:application)
|
38
|
+
ActiveRecord::Base.connection.application = 'new_app'
|
39
|
+
assert_equal 'new_app', ActiveRecord::Base.connection.application
|
40
|
+
else
|
41
|
+
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_APPLNAME"
|
42
|
+
end
|
43
|
+
|
44
|
+
if @connection.respond_to?(:workstation)
|
45
|
+
ActiveRecord::Base.connection.workstation = 'new_wrkst'
|
46
|
+
assert_equal 'new_wrkst', ActiveRecord::Base.connection.workstation
|
47
|
+
else
|
48
|
+
warn "#{@connection.class} does not support client connection attribute SQL_ATTR_INFO_WRKSTNNAME"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_tables
|
54
|
+
tables = @connection.tables
|
55
|
+
assert tables.include?("accounts")
|
56
|
+
assert tables.include?("authors")
|
57
|
+
assert tables.include?("tasks")
|
58
|
+
assert tables.include?("topics")
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_table_exists?
|
62
|
+
assert @connection.table_exists?("accounts")
|
63
|
+
assert !@connection.table_exists?("nonexistingtable")
|
64
|
+
assert !@connection.table_exists?(nil)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_indexes
|
68
|
+
idx_name = "accounts_idx"
|
69
|
+
|
70
|
+
if @connection.respond_to?(:indexes)
|
71
|
+
indexes = @connection.indexes("accounts")
|
72
|
+
assert indexes.empty?
|
73
|
+
|
74
|
+
@connection.add_index :accounts, :firm_id, :name => idx_name
|
75
|
+
indexes = @connection.indexes("accounts")
|
76
|
+
assert_equal "accounts", indexes.first.table
|
77
|
+
# OpenBase does not have the concept of a named index
|
78
|
+
# Indexes are merely properties of columns.
|
79
|
+
assert_equal idx_name, indexes.first.name unless current_adapter?(:OpenBaseAdapter)
|
80
|
+
assert !indexes.first.unique
|
81
|
+
assert_equal ["firm_id"], indexes.first.columns
|
82
|
+
else
|
83
|
+
warn "#{@connection.class} does not respond to #indexes"
|
84
|
+
end
|
85
|
+
|
86
|
+
ensure
|
87
|
+
@connection.remove_index(:accounts, :name => idx_name) rescue nil
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_current_database
|
91
|
+
if @connection.respond_to?(:current_database)
|
92
|
+
assert_equal ARTest.connection_config['arunit']['database'], @connection.current_database
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
if current_adapter?(:MysqlAdapter)
|
97
|
+
def test_charset
|
98
|
+
assert_not_nil @connection.charset
|
99
|
+
assert_not_equal 'character_set_database', @connection.charset
|
100
|
+
assert_equal @connection.show_variable('character_set_database'), @connection.charset
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_collation
|
104
|
+
assert_not_nil @connection.collation
|
105
|
+
assert_not_equal 'collation_database', @connection.collation
|
106
|
+
assert_equal @connection.show_variable('collation_database'), @connection.collation
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_show_nonexistent_variable_returns_nil
|
110
|
+
assert_nil @connection.show_variable('foo_bar_baz')
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_not_specifying_database_name_for_cross_database_selects
|
114
|
+
begin
|
115
|
+
assert_nothing_raised do
|
116
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['arunit'].except(:database))
|
117
|
+
|
118
|
+
config = ARTest.connection_config
|
119
|
+
ActiveRecord::Base.connection.execute(
|
120
|
+
"SELECT #{config['arunit']['database']}.pirates.*, #{config['arunit2']['database']}.courses.* " \
|
121
|
+
"FROM #{config['arunit']['database']}.pirates, #{config['arunit2']['database']}.courses"
|
122
|
+
)
|
123
|
+
end
|
124
|
+
ensure
|
125
|
+
ActiveRecord::Base.establish_connection 'arunit'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_table_alias
|
131
|
+
def @connection.test_table_alias_length() 10; end
|
132
|
+
class << @connection
|
133
|
+
alias_method :old_table_alias_length, :table_alias_length
|
134
|
+
alias_method :table_alias_length, :test_table_alias_length
|
135
|
+
end
|
136
|
+
|
137
|
+
assert_equal 'posts', @connection.table_alias_for('posts')
|
138
|
+
assert_equal 'posts_comm', @connection.table_alias_for('posts_comments')
|
139
|
+
assert_equal 'dbo_posts', @connection.table_alias_for('dbo.posts')
|
140
|
+
|
141
|
+
class << @connection
|
142
|
+
remove_method :table_alias_length
|
143
|
+
alias_method :table_alias_length, :old_table_alias_length
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# test resetting sequences in odd tables in postgreSQL
|
148
|
+
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
|
149
|
+
require 'models/movie'
|
150
|
+
require 'models/subscriber'
|
151
|
+
|
152
|
+
def test_reset_empty_table_with_custom_pk
|
153
|
+
Movie.delete_all
|
154
|
+
Movie.connection.reset_pk_sequence! 'movies'
|
155
|
+
assert_equal 1, Movie.create(:name => 'fight club').id
|
156
|
+
end
|
157
|
+
|
158
|
+
if ActiveRecord::Base.connection.adapter_name != "FrontBase"
|
159
|
+
def test_reset_table_with_non_integer_pk
|
160
|
+
Subscriber.delete_all
|
161
|
+
Subscriber.connection.reset_pk_sequence! 'subscribers'
|
162
|
+
sub = Subscriber.new(:name => 'robert drake')
|
163
|
+
sub.id = 'bob drake'
|
164
|
+
assert_nothing_raised { sub.save! }
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_uniqueness_violations_are_translated_to_specific_exception
|
170
|
+
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
171
|
+
assert_raises(ActiveRecord::RecordNotUnique) do
|
172
|
+
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_foreign_key_violations_are_translated_to_specific_exception
|
177
|
+
unless @connection.adapter_name == 'SQLite'
|
178
|
+
assert_raises(ActiveRecord::InvalidForeignKey) do
|
179
|
+
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
|
180
|
+
if @connection.prefetch_primary_key?
|
181
|
+
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
|
182
|
+
@connection.execute "INSERT INTO fk_test_has_fk (id, fk_id) VALUES (#{id_value},0)"
|
183
|
+
else
|
184
|
+
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_disable_referential_integrity
|
191
|
+
assert_nothing_raised do
|
192
|
+
@connection.disable_referential_integrity do
|
193
|
+
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
|
194
|
+
if @connection.prefetch_primary_key?
|
195
|
+
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
|
196
|
+
@connection.execute "INSERT INTO fk_test_has_fk (id, fk_id) VALUES (#{id_value},0)"
|
197
|
+
else
|
198
|
+
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
|
199
|
+
end
|
200
|
+
# should deleted created record as otherwise disable_referential_integrity will try to enable contraints after executed block
|
201
|
+
# and will fail (at least on Oracle)
|
202
|
+
@connection.execute "DELETE FROM fk_test_has_fk"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
@@ -1,711 +1,711 @@
|
|
1
|
-
require "cases/helper"
|
2
|
-
require 'models/developer'
|
3
|
-
require 'models/project'
|
4
|
-
require 'models/company'
|
5
|
-
require 'models/topic'
|
6
|
-
require 'models/reply'
|
7
|
-
require 'models/computer'
|
8
|
-
require 'models/post'
|
9
|
-
require 'models/author'
|
10
|
-
require 'models/tag'
|
11
|
-
require 'models/tagging'
|
12
|
-
require 'models/comment'
|
13
|
-
require 'models/sponsor'
|
14
|
-
require 'models/member'
|
15
|
-
require 'models/essay'
|
16
|
-
require 'models/toy'
|
17
|
-
|
18
|
-
class BelongsToAssociationsTest < ActiveRecord::TestCase
|
19
|
-
fixtures :accounts, :companies, :developers, :projects, :topics,
|
20
|
-
:developers_projects, :computers, :authors, :author_addresses,
|
21
|
-
:posts, :tags, :taggings, :comments, :sponsors, :members
|
22
|
-
|
23
|
-
def test_belongs_to
|
24
|
-
Client.find(3).firm.name
|
25
|
-
assert_equal companies(:first_firm).name, Client.find(3).firm.name
|
26
|
-
assert_not_nil Client.find(3).firm, "Microsoft should have a firm"
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_belongs_to_with_primary_key
|
30
|
-
client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
|
31
|
-
assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_belongs_to_with_primary_key_joins_on_correct_column
|
35
|
-
sql = Client.joins(:firm_with_primary_key).to_sql
|
36
|
-
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
37
|
-
assert_no_match(/`firm_with_primary_keys_companies`\.`id`/, sql)
|
38
|
-
assert_match(/`firm_with_primary_keys_companies`\.`name`/, sql)
|
39
|
-
elsif current_adapter?(:OracleAdapter)
|
40
|
-
# on Oracle aliases are truncated to 30 characters and are quoted in uppercase
|
41
|
-
assert_no_match(/"firm_with_primary_keys_compani"\."id"/i, sql)
|
42
|
-
assert_match(/"firm_with_primary_keys_compani"\."name"/i, sql)
|
43
|
-
elsif current_adapter?(:IBM_DBAdapter)
|
44
|
-
# Quoting of column names is not necessary for IBM_DB
|
45
|
-
assert_no_match(/firm_with_primary_keys_companies\.id/i, sql)
|
46
|
-
assert_match(/firm_with_primary_keys_companies\.name/i, sql)
|
47
|
-
else
|
48
|
-
assert_no_match(/"firm_with_primary_keys_companies"\."id"/, sql)
|
49
|
-
assert_match(/"firm_with_primary_keys_companies"\."name"/, sql)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_proxy_assignment
|
54
|
-
account = Account.find(1)
|
55
|
-
assert_nothing_raised { account.firm = account.firm }
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_type_mismatch
|
59
|
-
assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
|
60
|
-
assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_natural_assignment
|
64
|
-
apple = Firm.create("name" => "Apple")
|
65
|
-
citibank = Account.create("credit_limit" => 10)
|
66
|
-
citibank.firm = apple
|
67
|
-
assert_equal apple.id, citibank.firm_id
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_natural_assignment_with_primary_key
|
71
|
-
apple = Firm.create("name" => "Apple")
|
72
|
-
citibank = Client.create("name" => "Primary key client")
|
73
|
-
citibank.firm_with_primary_key = apple
|
74
|
-
assert_equal apple.name, citibank.firm_name
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_eager_loading_with_primary_key
|
78
|
-
Firm.create("name" => "Apple")
|
79
|
-
Client.create("name" => "Citibank", :firm_name => "Apple")
|
80
|
-
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
|
81
|
-
assert citibank_result.association_cache.key?(:firm_with_primary_key)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_eager_loading_with_primary_key_as_symbol
|
85
|
-
Firm.create("name" => "Apple")
|
86
|
-
Client.create("name" => "Citibank", :firm_name => "Apple")
|
87
|
-
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
|
88
|
-
assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_creating_the_belonging_object
|
92
|
-
citibank = Account.create("credit_limit" => 10)
|
93
|
-
apple = citibank.create_firm("name" => "Apple")
|
94
|
-
assert_equal apple, citibank.firm
|
95
|
-
citibank.save
|
96
|
-
citibank.reload
|
97
|
-
assert_equal apple, citibank.firm
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_creating_the_belonging_object_with_primary_key
|
101
|
-
client = Client.create(:name => "Primary key client")
|
102
|
-
apple = client.create_firm_with_primary_key("name" => "Apple")
|
103
|
-
assert_equal apple, client.firm_with_primary_key
|
104
|
-
client.save
|
105
|
-
client.reload
|
106
|
-
assert_equal apple, client.firm_with_primary_key
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_building_the_belonging_object
|
110
|
-
citibank = Account.create("credit_limit" => 10)
|
111
|
-
apple = citibank.build_firm("name" => "Apple")
|
112
|
-
citibank.save
|
113
|
-
assert_equal apple.id, citibank.firm_id
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_building_the_belonging_object_with_primary_key
|
117
|
-
client = Client.create(:name => "Primary key client")
|
118
|
-
apple = client.build_firm_with_primary_key("name" => "Apple")
|
119
|
-
client.save
|
120
|
-
assert_equal apple.name, client.firm_name
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_create!
|
124
|
-
client = Client.create!(:name => "Jimmy")
|
125
|
-
account = client.create_account!(:credit_limit => 10)
|
126
|
-
assert_equal account, client.account
|
127
|
-
assert account.persisted?
|
128
|
-
client.save
|
129
|
-
client.reload
|
130
|
-
assert_equal account, client.account
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_failing_create!
|
134
|
-
client = Client.create!(:name => "Jimmy")
|
135
|
-
assert_raise(ActiveRecord::RecordInvalid) { client.create_account! }
|
136
|
-
assert_not_nil client.account
|
137
|
-
assert client.account.new_record?
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_natural_assignment_to_nil
|
141
|
-
client = Client.find(3)
|
142
|
-
client.firm = nil
|
143
|
-
client.save
|
144
|
-
assert_nil client.firm(true)
|
145
|
-
assert_nil client.client_of
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_natural_assignment_to_nil_with_primary_key
|
149
|
-
client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
|
150
|
-
client.firm_with_primary_key = nil
|
151
|
-
client.save
|
152
|
-
assert_nil client.firm_with_primary_key(true)
|
153
|
-
assert_nil client.client_of
|
154
|
-
end
|
155
|
-
|
156
|
-
def test_with_different_class_name
|
157
|
-
assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
|
158
|
-
assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_with_condition
|
162
|
-
assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
|
163
|
-
assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_polymorphic_association_class
|
167
|
-
sponsor = Sponsor.new
|
168
|
-
assert_nil sponsor.association(:sponsorable).send(:klass)
|
169
|
-
|
170
|
-
sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
|
171
|
-
assert_nil sponsor.association(:sponsorable).send(:klass)
|
172
|
-
|
173
|
-
sponsor.sponsorable = Member.new :name => "Bert"
|
174
|
-
assert_equal Member, sponsor.association(:sponsorable).send(:klass)
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_with_polymorphic_and_condition
|
178
|
-
sponsor = Sponsor.create
|
179
|
-
member = Member.create :name => "Bert"
|
180
|
-
sponsor.sponsorable = member
|
181
|
-
|
182
|
-
assert_equal member, sponsor.sponsorable
|
183
|
-
assert_nil sponsor.sponsorable_with_conditions
|
184
|
-
end
|
185
|
-
|
186
|
-
def test_with_select
|
187
|
-
assert_equal Company.find(2).firm_with_select.attributes.size, 1
|
188
|
-
assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_belongs_to_counter
|
192
|
-
debate = Topic.create("title" => "debate")
|
193
|
-
assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
|
194
|
-
|
195
|
-
trash = debate.replies.create("title" => "blah!", "content" => "world around!")
|
196
|
-
assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
|
197
|
-
|
198
|
-
trash.destroy
|
199
|
-
assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
|
200
|
-
end
|
201
|
-
|
202
|
-
def test_belongs_to_counter_with_assigning_nil
|
203
|
-
p = Post.find(1)
|
204
|
-
c = Comment.find(1)
|
205
|
-
|
206
|
-
assert_equal p.id, c.post_id
|
207
|
-
assert_equal 2, Post.find(p.id).comments.size
|
208
|
-
|
209
|
-
c.post = nil
|
210
|
-
|
211
|
-
assert_equal 1, Post.find(p.id).comments.size
|
212
|
-
end
|
213
|
-
|
214
|
-
def test_belongs_to_with_primary_key_counter
|
215
|
-
debate = Topic.create("title" => "debate")
|
216
|
-
debate2 = Topic.create("title" => "debate2")
|
217
|
-
reply = Reply.create("title" => "blah!", "content" => "world around!", "parent_title" => "debate")
|
218
|
-
|
219
|
-
assert_equal 1, debate.reload.replies_count
|
220
|
-
assert_equal 0, debate2.reload.replies_count
|
221
|
-
|
222
|
-
reply.topic_with_primary_key = debate2
|
223
|
-
|
224
|
-
assert_equal 0, debate.reload.replies_count
|
225
|
-
assert_equal 1, debate2.reload.replies_count
|
226
|
-
|
227
|
-
reply.topic_with_primary_key = nil
|
228
|
-
|
229
|
-
assert_equal 0, debate.reload.replies_count
|
230
|
-
assert_equal 0, debate2.reload.replies_count
|
231
|
-
end
|
232
|
-
|
233
|
-
def test_belongs_to_counter_with_reassigning
|
234
|
-
t1 = Topic.create("title" => "t1")
|
235
|
-
t2 = Topic.create("title" => "t2")
|
236
|
-
r1 = Reply.new("title" => "r1", "content" => "r1")
|
237
|
-
r1.topic = t1
|
238
|
-
|
239
|
-
assert r1.save
|
240
|
-
assert_equal 1, Topic.find(t1.id).replies.size
|
241
|
-
assert_equal 0, Topic.find(t2.id).replies.size
|
242
|
-
|
243
|
-
r1.topic = Topic.find(t2.id)
|
244
|
-
|
245
|
-
assert_no_queries do
|
246
|
-
r1.topic = t2
|
247
|
-
end
|
248
|
-
|
249
|
-
assert r1.save
|
250
|
-
assert_equal 0, Topic.find(t1.id).replies.size
|
251
|
-
assert_equal 1, Topic.find(t2.id).replies.size
|
252
|
-
|
253
|
-
r1.topic = nil
|
254
|
-
|
255
|
-
assert_equal 0, Topic.find(t1.id).replies.size
|
256
|
-
assert_equal 0, Topic.find(t2.id).replies.size
|
257
|
-
|
258
|
-
r1.topic = t1
|
259
|
-
|
260
|
-
assert_equal 1, Topic.find(t1.id).replies.size
|
261
|
-
assert_equal 0, Topic.find(t2.id).replies.size
|
262
|
-
|
263
|
-
r1.destroy
|
264
|
-
|
265
|
-
assert_equal 0, Topic.find(t1.id).replies.size
|
266
|
-
assert_equal 0, Topic.find(t2.id).replies.size
|
267
|
-
end
|
268
|
-
|
269
|
-
def test_belongs_to_reassign_with_namespaced_models_and_counters
|
270
|
-
t1 = Web::Topic.create("title" => "t1")
|
271
|
-
t2 = Web::Topic.create("title" => "t2")
|
272
|
-
r1 = Web::Reply.new("title" => "r1", "content" => "r1")
|
273
|
-
r1.topic = t1
|
274
|
-
|
275
|
-
assert r1.save
|
276
|
-
assert_equal 1, Web::Topic.find(t1.id).replies.size
|
277
|
-
assert_equal 0, Web::Topic.find(t2.id).replies.size
|
278
|
-
|
279
|
-
r1.topic = Web::Topic.find(t2.id)
|
280
|
-
|
281
|
-
assert r1.save
|
282
|
-
assert_equal 0, Web::Topic.find(t1.id).replies.size
|
283
|
-
assert_equal 1, Web::Topic.find(t2.id).replies.size
|
284
|
-
end
|
285
|
-
|
286
|
-
def test_belongs_to_counter_after_save
|
287
|
-
topic = Topic.create!(:title => "monday night")
|
288
|
-
topic.replies.create!(:title => "re: monday night", :content => "football")
|
289
|
-
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
290
|
-
|
291
|
-
topic.save!
|
292
|
-
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
293
|
-
end
|
294
|
-
|
295
|
-
def test_belongs_to_counter_after_update_attributes
|
296
|
-
topic = Topic.create!(:title => "37s")
|
297
|
-
topic.replies.create!(:title => "re: 37s", :content => "rails")
|
298
|
-
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
299
|
-
|
300
|
-
topic.update_attributes(:title => "37signals")
|
301
|
-
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
302
|
-
end
|
303
|
-
|
304
|
-
def test_belongs_to_counter_when_update_column
|
305
|
-
topic = Topic.create!(:title => "37s")
|
306
|
-
topic.replies.create!(:title => "re: 37s", :content => "rails")
|
307
|
-
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
308
|
-
|
309
|
-
topic.update_column(:content, "rails is wonderfull")
|
310
|
-
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
311
|
-
end
|
312
|
-
|
313
|
-
def test_assignment_before_child_saved
|
314
|
-
final_cut = Client.new("name" => "Final Cut")
|
315
|
-
firm = Firm.find(1)
|
316
|
-
final_cut.firm = firm
|
317
|
-
assert !final_cut.persisted?
|
318
|
-
assert final_cut.save
|
319
|
-
assert final_cut.persisted?
|
320
|
-
assert firm.persisted?
|
321
|
-
assert_equal firm, final_cut.firm
|
322
|
-
assert_equal firm, final_cut.firm(true)
|
323
|
-
end
|
324
|
-
|
325
|
-
def test_assignment_before_child_saved_with_primary_key
|
326
|
-
final_cut = Client.new("name" => "Final Cut")
|
327
|
-
firm = Firm.find(1)
|
328
|
-
final_cut.firm_with_primary_key = firm
|
329
|
-
assert !final_cut.persisted?
|
330
|
-
assert final_cut.save
|
331
|
-
assert final_cut.persisted?
|
332
|
-
assert firm.persisted?
|
333
|
-
assert_equal firm, final_cut.firm_with_primary_key
|
334
|
-
assert_equal firm, final_cut.firm_with_primary_key(true)
|
335
|
-
end
|
336
|
-
|
337
|
-
def test_new_record_with_foreign_key_but_no_object
|
338
|
-
c = Client.new("firm_id" => 1)
|
339
|
-
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
|
340
|
-
assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
|
341
|
-
end
|
342
|
-
|
343
|
-
def test_setting_foreign_key_after_nil_target_loaded
|
344
|
-
client = Client.new
|
345
|
-
client.firm_with_basic_id
|
346
|
-
client.firm_id = 1
|
347
|
-
|
348
|
-
assert_equal companies(:first_firm), client.firm_with_basic_id
|
349
|
-
end
|
350
|
-
|
351
|
-
def test_polymorphic_setting_foreign_key_after_nil_target_loaded
|
352
|
-
sponsor = Sponsor.new
|
353
|
-
sponsor.sponsorable
|
354
|
-
sponsor.sponsorable_id = 1
|
355
|
-
sponsor.sponsorable_type = "Member"
|
356
|
-
|
357
|
-
assert_equal members(:groucho), sponsor.sponsorable
|
358
|
-
end
|
359
|
-
|
360
|
-
def test_dont_find_target_when_foreign_key_is_null
|
361
|
-
tagging = taggings(:thinking_general)
|
362
|
-
queries = assert_sql { tagging.super_tag }
|
363
|
-
assert_equal 0, queries.length
|
364
|
-
end
|
365
|
-
|
366
|
-
def test_field_name_same_as_foreign_key
|
367
|
-
computer = Computer.find(1)
|
368
|
-
assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
|
369
|
-
end
|
370
|
-
|
371
|
-
def test_counter_cache
|
372
|
-
topic = Topic.create :title => "Zoom-zoom-zoom"
|
373
|
-
assert_equal 0, topic[:replies_count]
|
374
|
-
|
375
|
-
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
|
376
|
-
reply.topic = topic
|
377
|
-
|
378
|
-
assert_equal 1, topic.reload[:replies_count]
|
379
|
-
assert_equal 1, topic.replies.size
|
380
|
-
|
381
|
-
topic[:replies_count] = 15
|
382
|
-
assert_equal 15, topic.replies.size
|
383
|
-
end
|
384
|
-
|
385
|
-
def test_custom_counter_cache
|
386
|
-
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
|
387
|
-
assert_equal 0, reply[:replies_count]
|
388
|
-
|
389
|
-
silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
|
390
|
-
silly.reply = reply
|
391
|
-
|
392
|
-
assert_equal 1, reply.reload[:replies_count]
|
393
|
-
assert_equal 1, reply.replies.size
|
394
|
-
|
395
|
-
reply[:replies_count] = 17
|
396
|
-
assert_equal 17, reply.replies.size
|
397
|
-
end
|
398
|
-
|
399
|
-
def test_association_assignment_sticks
|
400
|
-
post = Post.find(:first)
|
401
|
-
|
402
|
-
author1, author2 = Author.find(:all, :limit => 2)
|
403
|
-
assert_not_nil author1
|
404
|
-
assert_not_nil author2
|
405
|
-
|
406
|
-
# make sure the association is loaded
|
407
|
-
post.author
|
408
|
-
|
409
|
-
# set the association by id, directly
|
410
|
-
post.author_id = author2.id
|
411
|
-
|
412
|
-
# save and reload
|
413
|
-
post.save!
|
414
|
-
post.reload
|
415
|
-
|
416
|
-
# the author id of the post should be the id we set
|
417
|
-
assert_equal post.author_id, author2.id
|
418
|
-
end
|
419
|
-
|
420
|
-
def test_cant_save_readonly_association
|
421
|
-
assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
|
422
|
-
assert companies(:first_client).readonly_firm.readonly?
|
423
|
-
end
|
424
|
-
|
425
|
-
def test_polymorphic_assignment_foreign_type_field_updating
|
426
|
-
# should update when assigning a saved record
|
427
|
-
sponsor = Sponsor.new
|
428
|
-
member = Member.create
|
429
|
-
sponsor.sponsorable = member
|
430
|
-
assert_equal "Member", sponsor.sponsorable_type
|
431
|
-
|
432
|
-
# should update when assigning a new record
|
433
|
-
sponsor = Sponsor.new
|
434
|
-
member = Member.new
|
435
|
-
sponsor.sponsorable = member
|
436
|
-
assert_equal "Member", sponsor.sponsorable_type
|
437
|
-
end
|
438
|
-
|
439
|
-
def test_polymorphic_assignment_with_primary_key_foreign_type_field_updating
|
440
|
-
# should update when assigning a saved record
|
441
|
-
essay = Essay.new
|
442
|
-
writer = Author.create(:name => "David")
|
443
|
-
essay.writer = writer
|
444
|
-
assert_equal "Author", essay.writer_type
|
445
|
-
|
446
|
-
# should update when assigning a new record
|
447
|
-
essay = Essay.new
|
448
|
-
writer = Author.new
|
449
|
-
essay.writer = writer
|
450
|
-
assert_equal "Author", essay.writer_type
|
451
|
-
end
|
452
|
-
|
453
|
-
def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
|
454
|
-
sponsor = Sponsor.new
|
455
|
-
saved_member = Member.create
|
456
|
-
new_member = Member.new
|
457
|
-
|
458
|
-
sponsor.sponsorable = saved_member
|
459
|
-
assert_equal saved_member.id, sponsor.sponsorable_id
|
460
|
-
|
461
|
-
sponsor.sponsorable = new_member
|
462
|
-
assert_nil sponsor.sponsorable_id
|
463
|
-
end
|
464
|
-
|
465
|
-
def test_assignment_updates_foreign_id_field_for_new_and_saved_records
|
466
|
-
client = Client.new
|
467
|
-
saved_firm = Firm.create :name => "Saved"
|
468
|
-
new_firm = Firm.new
|
469
|
-
|
470
|
-
client.firm = saved_firm
|
471
|
-
assert_equal saved_firm.id, client.client_of
|
472
|
-
|
473
|
-
client.firm = new_firm
|
474
|
-
assert_nil client.client_of
|
475
|
-
end
|
476
|
-
|
477
|
-
def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records
|
478
|
-
essay = Essay.new
|
479
|
-
saved_writer = Author.create(:name => "David")
|
480
|
-
new_writer = Author.new
|
481
|
-
|
482
|
-
essay.writer = saved_writer
|
483
|
-
assert_equal saved_writer.name, essay.writer_id
|
484
|
-
|
485
|
-
essay.writer = new_writer
|
486
|
-
assert_nil essay.writer_id
|
487
|
-
end
|
488
|
-
|
489
|
-
def test_belongs_to_proxy_should_not_respond_to_private_methods
|
490
|
-
assert_raise(NoMethodError) { companies(:first_firm).private_method }
|
491
|
-
assert_raise(NoMethodError) { companies(:second_client).firm.private_method }
|
492
|
-
end
|
493
|
-
|
494
|
-
def test_belongs_to_proxy_should_respond_to_private_methods_via_send
|
495
|
-
companies(:first_firm).send(:private_method)
|
496
|
-
companies(:second_client).firm.send(:private_method)
|
497
|
-
end
|
498
|
-
|
499
|
-
def test_save_of_record_with_loaded_belongs_to
|
500
|
-
@account = companies(:first_firm).account
|
501
|
-
|
502
|
-
assert_nothing_raised do
|
503
|
-
Account.find(@account.id).save!
|
504
|
-
Account.find(@account.id, :include => :firm).save!
|
505
|
-
end
|
506
|
-
|
507
|
-
@account.firm.delete
|
508
|
-
|
509
|
-
assert_nothing_raised do
|
510
|
-
Account.find(@account.id).save!
|
511
|
-
Account.find(@account.id, :include => :firm).save!
|
512
|
-
end
|
513
|
-
end
|
514
|
-
|
515
|
-
def test_dependent_delete_and_destroy_with_belongs_to
|
516
|
-
author_address = author_addresses(:david_address)
|
517
|
-
author_address_extra = author_addresses(:david_address_extra)
|
518
|
-
assert_equal [], AuthorAddress.destroyed_author_address_ids
|
519
|
-
|
520
|
-
assert_difference "AuthorAddress.count", -2 do
|
521
|
-
authors(:david).destroy
|
522
|
-
end
|
523
|
-
|
524
|
-
assert_equal [], AuthorAddress.find_all_by_id([author_address.id, author_address_extra.id])
|
525
|
-
assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids
|
526
|
-
end
|
527
|
-
|
528
|
-
def test_invalid_belongs_to_dependent_option_nullify_raises_exception
|
529
|
-
assert_raise ArgumentError do
|
530
|
-
Author.belongs_to :special_author_address, :dependent => :nullify
|
531
|
-
end
|
532
|
-
end
|
533
|
-
|
534
|
-
def test_invalid_belongs_to_dependent_option_restrict_raises_exception
|
535
|
-
assert_raise ArgumentError do
|
536
|
-
Author.belongs_to :special_author_address, :dependent => :restrict
|
537
|
-
end
|
538
|
-
end
|
539
|
-
|
540
|
-
def test_attributes_are_being_set_when_initialized_from_belongs_to_association_with_where_clause
|
541
|
-
new_firm = accounts(:signals37).build_firm(:name => 'Apple')
|
542
|
-
assert_equal new_firm.name, "Apple"
|
543
|
-
end
|
544
|
-
|
545
|
-
def test_reassigning_the_parent_id_updates_the_object
|
546
|
-
client = companies(:second_client)
|
547
|
-
|
548
|
-
client.firm
|
549
|
-
client.firm_with_condition
|
550
|
-
firm_proxy = client.send(:association_instance_get, :firm)
|
551
|
-
firm_with_condition_proxy = client.send(:association_instance_get, :firm_with_condition)
|
552
|
-
|
553
|
-
assert !firm_proxy.stale_target?
|
554
|
-
assert !firm_with_condition_proxy.stale_target?
|
555
|
-
assert_equal companies(:first_firm), client.firm
|
556
|
-
assert_equal companies(:first_firm), client.firm_with_condition
|
557
|
-
|
558
|
-
client.client_of = companies(:another_firm).id
|
559
|
-
|
560
|
-
assert firm_proxy.stale_target?
|
561
|
-
assert firm_with_condition_proxy.stale_target?
|
562
|
-
assert_equal companies(:another_firm), client.firm
|
563
|
-
assert_equal companies(:another_firm), client.firm_with_condition
|
564
|
-
end
|
565
|
-
|
566
|
-
def test_polymorphic_reassignment_of_associated_id_updates_the_object
|
567
|
-
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
|
568
|
-
|
569
|
-
sponsor.sponsorable
|
570
|
-
proxy = sponsor.send(:association_instance_get, :sponsorable)
|
571
|
-
|
572
|
-
assert !proxy.stale_target?
|
573
|
-
assert_equal members(:groucho), sponsor.sponsorable
|
574
|
-
|
575
|
-
sponsor.sponsorable_id = members(:some_other_guy).id
|
576
|
-
|
577
|
-
assert proxy.stale_target?
|
578
|
-
assert_equal members(:some_other_guy), sponsor.sponsorable
|
579
|
-
end
|
580
|
-
|
581
|
-
def test_polymorphic_reassignment_of_associated_type_updates_the_object
|
582
|
-
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
|
583
|
-
|
584
|
-
sponsor.sponsorable
|
585
|
-
proxy = sponsor.send(:association_instance_get, :sponsorable)
|
586
|
-
|
587
|
-
assert !proxy.stale_target?
|
588
|
-
assert_equal members(:groucho), sponsor.sponsorable
|
589
|
-
|
590
|
-
sponsor.sponsorable_type = 'Firm'
|
591
|
-
|
592
|
-
assert proxy.stale_target?
|
593
|
-
assert_equal companies(:first_firm), sponsor.sponsorable
|
594
|
-
end
|
595
|
-
|
596
|
-
def test_reloading_association_with_key_change
|
597
|
-
client = companies(:second_client)
|
598
|
-
firm = client.association(:firm)
|
599
|
-
|
600
|
-
client.firm = companies(:another_firm)
|
601
|
-
firm.reload
|
602
|
-
assert_equal companies(:another_firm), firm.target
|
603
|
-
|
604
|
-
client.client_of = companies(:first_firm).id
|
605
|
-
firm.reload
|
606
|
-
assert_equal companies(:first_firm), firm.target
|
607
|
-
end
|
608
|
-
|
609
|
-
def test_polymorphic_counter_cache
|
610
|
-
tagging = taggings(:welcome_general)
|
611
|
-
post = posts(:welcome)
|
612
|
-
comment = comments(:greetings)
|
613
|
-
|
614
|
-
assert_difference lambda { post.reload.taggings_count }, -1 do
|
615
|
-
assert_difference 'comment.reload.taggings_count', +1 do
|
616
|
-
tagging.taggable = comment
|
617
|
-
end
|
618
|
-
end
|
619
|
-
end
|
620
|
-
|
621
|
-
def test_polymorphic_with_custom_foreign_type
|
622
|
-
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
|
623
|
-
groucho = members(:groucho)
|
624
|
-
other = members(:some_other_guy)
|
625
|
-
|
626
|
-
assert_equal groucho, sponsor.sponsorable
|
627
|
-
assert_equal groucho, sponsor.thing
|
628
|
-
|
629
|
-
sponsor.thing = other
|
630
|
-
|
631
|
-
assert_equal other, sponsor.sponsorable
|
632
|
-
assert_equal other, sponsor.thing
|
633
|
-
|
634
|
-
sponsor.sponsorable = groucho
|
635
|
-
|
636
|
-
assert_equal groucho, sponsor.sponsorable
|
637
|
-
assert_equal groucho, sponsor.thing
|
638
|
-
end
|
639
|
-
|
640
|
-
def test_build_with_conditions
|
641
|
-
client = companies(:second_client)
|
642
|
-
firm = client.build_bob_firm
|
643
|
-
|
644
|
-
assert_equal "Bob", firm.name
|
645
|
-
end
|
646
|
-
|
647
|
-
def test_create_with_conditions
|
648
|
-
client = companies(:second_client)
|
649
|
-
firm = client.create_bob_firm
|
650
|
-
|
651
|
-
assert_equal "Bob", firm.name
|
652
|
-
end
|
653
|
-
|
654
|
-
def test_create_bang_with_conditions
|
655
|
-
client = companies(:second_client)
|
656
|
-
firm = client.create_bob_firm!
|
657
|
-
|
658
|
-
assert_equal "Bob", firm.name
|
659
|
-
end
|
660
|
-
|
661
|
-
def test_build_with_block
|
662
|
-
client = Client.create(:name => 'Client Company')
|
663
|
-
|
664
|
-
firm = client.build_firm{ |f| f.name = 'Agency Company' }
|
665
|
-
assert_equal 'Agency Company', firm.name
|
666
|
-
end
|
667
|
-
|
668
|
-
def test_create_with_block
|
669
|
-
client = Client.create(:name => 'Client Company')
|
670
|
-
|
671
|
-
firm = client.create_firm{ |f| f.name = 'Agency Company' }
|
672
|
-
assert_equal 'Agency Company', firm.name
|
673
|
-
end
|
674
|
-
|
675
|
-
def test_create_bang_with_block
|
676
|
-
client = Client.create(:name => 'Client Company')
|
677
|
-
|
678
|
-
firm = client.create_firm!{ |f| f.name = 'Agency Company' }
|
679
|
-
assert_equal 'Agency Company', firm.name
|
680
|
-
end
|
681
|
-
|
682
|
-
def test_should_set_foreign_key_on_create_association
|
683
|
-
client = Client.create! :name => "fuu"
|
684
|
-
|
685
|
-
firm = client.create_firm :name => "baa"
|
686
|
-
assert_equal firm.id, client.client_of
|
687
|
-
end
|
688
|
-
|
689
|
-
def test_should_set_foreign_key_on_create_association!
|
690
|
-
client = Client.create! :name => "fuu"
|
691
|
-
|
692
|
-
firm = client.create_firm! :name => "baa"
|
693
|
-
assert_equal firm.id, client.client_of
|
694
|
-
end
|
695
|
-
|
696
|
-
def test_self_referential_belongs_to_with_counter_cache_assigning_nil
|
697
|
-
comment = Comment.create! :post => posts(:thinking), :body => "fuu"
|
698
|
-
comment.parent = nil
|
699
|
-
comment.save!
|
700
|
-
|
701
|
-
assert_equal nil, comment.reload.parent
|
702
|
-
assert_equal 0, comments(:greetings).reload.children_count
|
703
|
-
end
|
704
|
-
|
705
|
-
def test_polymorphic_with_custom_primary_key
|
706
|
-
toy = Toy.create!
|
707
|
-
sponsor = Sponsor.create!(:sponsorable => toy)
|
708
|
-
|
709
|
-
assert_equal toy, sponsor.reload.sponsorable
|
710
|
-
end
|
711
|
-
end
|
1
|
+
require "cases/helper"
|
2
|
+
require 'models/developer'
|
3
|
+
require 'models/project'
|
4
|
+
require 'models/company'
|
5
|
+
require 'models/topic'
|
6
|
+
require 'models/reply'
|
7
|
+
require 'models/computer'
|
8
|
+
require 'models/post'
|
9
|
+
require 'models/author'
|
10
|
+
require 'models/tag'
|
11
|
+
require 'models/tagging'
|
12
|
+
require 'models/comment'
|
13
|
+
require 'models/sponsor'
|
14
|
+
require 'models/member'
|
15
|
+
require 'models/essay'
|
16
|
+
require 'models/toy'
|
17
|
+
|
18
|
+
class BelongsToAssociationsTest < ActiveRecord::TestCase
|
19
|
+
fixtures :accounts, :companies, :developers, :projects, :topics,
|
20
|
+
:developers_projects, :computers, :authors, :author_addresses,
|
21
|
+
:posts, :tags, :taggings, :comments, :sponsors, :members
|
22
|
+
|
23
|
+
def test_belongs_to
|
24
|
+
Client.find(3).firm.name
|
25
|
+
assert_equal companies(:first_firm).name, Client.find(3).firm.name
|
26
|
+
assert_not_nil Client.find(3).firm, "Microsoft should have a firm"
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_belongs_to_with_primary_key
|
30
|
+
client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
|
31
|
+
assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_belongs_to_with_primary_key_joins_on_correct_column
|
35
|
+
sql = Client.joins(:firm_with_primary_key).to_sql
|
36
|
+
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
37
|
+
assert_no_match(/`firm_with_primary_keys_companies`\.`id`/, sql)
|
38
|
+
assert_match(/`firm_with_primary_keys_companies`\.`name`/, sql)
|
39
|
+
elsif current_adapter?(:OracleAdapter)
|
40
|
+
# on Oracle aliases are truncated to 30 characters and are quoted in uppercase
|
41
|
+
assert_no_match(/"firm_with_primary_keys_compani"\."id"/i, sql)
|
42
|
+
assert_match(/"firm_with_primary_keys_compani"\."name"/i, sql)
|
43
|
+
elsif current_adapter?(:IBM_DBAdapter)
|
44
|
+
# Quoting of column names is not necessary for IBM_DB
|
45
|
+
assert_no_match(/firm_with_primary_keys_companies\.id/i, sql)
|
46
|
+
assert_match(/firm_with_primary_keys_companies\.name/i, sql)
|
47
|
+
else
|
48
|
+
assert_no_match(/"firm_with_primary_keys_companies"\."id"/, sql)
|
49
|
+
assert_match(/"firm_with_primary_keys_companies"\."name"/, sql)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_proxy_assignment
|
54
|
+
account = Account.find(1)
|
55
|
+
assert_nothing_raised { account.firm = account.firm }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_type_mismatch
|
59
|
+
assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
|
60
|
+
assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_natural_assignment
|
64
|
+
apple = Firm.create("name" => "Apple")
|
65
|
+
citibank = Account.create("credit_limit" => 10)
|
66
|
+
citibank.firm = apple
|
67
|
+
assert_equal apple.id, citibank.firm_id
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_natural_assignment_with_primary_key
|
71
|
+
apple = Firm.create("name" => "Apple")
|
72
|
+
citibank = Client.create("name" => "Primary key client")
|
73
|
+
citibank.firm_with_primary_key = apple
|
74
|
+
assert_equal apple.name, citibank.firm_name
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_eager_loading_with_primary_key
|
78
|
+
Firm.create("name" => "Apple")
|
79
|
+
Client.create("name" => "Citibank", :firm_name => "Apple")
|
80
|
+
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
|
81
|
+
assert citibank_result.association_cache.key?(:firm_with_primary_key)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_eager_loading_with_primary_key_as_symbol
|
85
|
+
Firm.create("name" => "Apple")
|
86
|
+
Client.create("name" => "Citibank", :firm_name => "Apple")
|
87
|
+
citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
|
88
|
+
assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_creating_the_belonging_object
|
92
|
+
citibank = Account.create("credit_limit" => 10)
|
93
|
+
apple = citibank.create_firm("name" => "Apple")
|
94
|
+
assert_equal apple, citibank.firm
|
95
|
+
citibank.save
|
96
|
+
citibank.reload
|
97
|
+
assert_equal apple, citibank.firm
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_creating_the_belonging_object_with_primary_key
|
101
|
+
client = Client.create(:name => "Primary key client")
|
102
|
+
apple = client.create_firm_with_primary_key("name" => "Apple")
|
103
|
+
assert_equal apple, client.firm_with_primary_key
|
104
|
+
client.save
|
105
|
+
client.reload
|
106
|
+
assert_equal apple, client.firm_with_primary_key
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_building_the_belonging_object
|
110
|
+
citibank = Account.create("credit_limit" => 10)
|
111
|
+
apple = citibank.build_firm("name" => "Apple")
|
112
|
+
citibank.save
|
113
|
+
assert_equal apple.id, citibank.firm_id
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_building_the_belonging_object_with_primary_key
|
117
|
+
client = Client.create(:name => "Primary key client")
|
118
|
+
apple = client.build_firm_with_primary_key("name" => "Apple")
|
119
|
+
client.save
|
120
|
+
assert_equal apple.name, client.firm_name
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_create!
|
124
|
+
client = Client.create!(:name => "Jimmy")
|
125
|
+
account = client.create_account!(:credit_limit => 10)
|
126
|
+
assert_equal account, client.account
|
127
|
+
assert account.persisted?
|
128
|
+
client.save
|
129
|
+
client.reload
|
130
|
+
assert_equal account, client.account
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_failing_create!
|
134
|
+
client = Client.create!(:name => "Jimmy")
|
135
|
+
assert_raise(ActiveRecord::RecordInvalid) { client.create_account! }
|
136
|
+
assert_not_nil client.account
|
137
|
+
assert client.account.new_record?
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_natural_assignment_to_nil
|
141
|
+
client = Client.find(3)
|
142
|
+
client.firm = nil
|
143
|
+
client.save
|
144
|
+
assert_nil client.firm(true)
|
145
|
+
assert_nil client.client_of
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_natural_assignment_to_nil_with_primary_key
|
149
|
+
client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
|
150
|
+
client.firm_with_primary_key = nil
|
151
|
+
client.save
|
152
|
+
assert_nil client.firm_with_primary_key(true)
|
153
|
+
assert_nil client.client_of
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_with_different_class_name
|
157
|
+
assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
|
158
|
+
assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_with_condition
|
162
|
+
assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
|
163
|
+
assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_polymorphic_association_class
|
167
|
+
sponsor = Sponsor.new
|
168
|
+
assert_nil sponsor.association(:sponsorable).send(:klass)
|
169
|
+
|
170
|
+
sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
|
171
|
+
assert_nil sponsor.association(:sponsorable).send(:klass)
|
172
|
+
|
173
|
+
sponsor.sponsorable = Member.new :name => "Bert"
|
174
|
+
assert_equal Member, sponsor.association(:sponsorable).send(:klass)
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_with_polymorphic_and_condition
|
178
|
+
sponsor = Sponsor.create
|
179
|
+
member = Member.create :name => "Bert"
|
180
|
+
sponsor.sponsorable = member
|
181
|
+
|
182
|
+
assert_equal member, sponsor.sponsorable
|
183
|
+
assert_nil sponsor.sponsorable_with_conditions
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_with_select
|
187
|
+
assert_equal Company.find(2).firm_with_select.attributes.size, 1
|
188
|
+
assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_belongs_to_counter
|
192
|
+
debate = Topic.create("title" => "debate")
|
193
|
+
assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
|
194
|
+
|
195
|
+
trash = debate.replies.create("title" => "blah!", "content" => "world around!")
|
196
|
+
assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
|
197
|
+
|
198
|
+
trash.destroy
|
199
|
+
assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_belongs_to_counter_with_assigning_nil
|
203
|
+
p = Post.find(1)
|
204
|
+
c = Comment.find(1)
|
205
|
+
|
206
|
+
assert_equal p.id, c.post_id
|
207
|
+
assert_equal 2, Post.find(p.id).comments.size
|
208
|
+
|
209
|
+
c.post = nil
|
210
|
+
|
211
|
+
assert_equal 1, Post.find(p.id).comments.size
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_belongs_to_with_primary_key_counter
|
215
|
+
debate = Topic.create("title" => "debate")
|
216
|
+
debate2 = Topic.create("title" => "debate2")
|
217
|
+
reply = Reply.create("title" => "blah!", "content" => "world around!", "parent_title" => "debate")
|
218
|
+
|
219
|
+
assert_equal 1, debate.reload.replies_count
|
220
|
+
assert_equal 0, debate2.reload.replies_count
|
221
|
+
|
222
|
+
reply.topic_with_primary_key = debate2
|
223
|
+
|
224
|
+
assert_equal 0, debate.reload.replies_count
|
225
|
+
assert_equal 1, debate2.reload.replies_count
|
226
|
+
|
227
|
+
reply.topic_with_primary_key = nil
|
228
|
+
|
229
|
+
assert_equal 0, debate.reload.replies_count
|
230
|
+
assert_equal 0, debate2.reload.replies_count
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_belongs_to_counter_with_reassigning
|
234
|
+
t1 = Topic.create("title" => "t1")
|
235
|
+
t2 = Topic.create("title" => "t2")
|
236
|
+
r1 = Reply.new("title" => "r1", "content" => "r1")
|
237
|
+
r1.topic = t1
|
238
|
+
|
239
|
+
assert r1.save
|
240
|
+
assert_equal 1, Topic.find(t1.id).replies.size
|
241
|
+
assert_equal 0, Topic.find(t2.id).replies.size
|
242
|
+
|
243
|
+
r1.topic = Topic.find(t2.id)
|
244
|
+
|
245
|
+
assert_no_queries do
|
246
|
+
r1.topic = t2
|
247
|
+
end
|
248
|
+
|
249
|
+
assert r1.save
|
250
|
+
assert_equal 0, Topic.find(t1.id).replies.size
|
251
|
+
assert_equal 1, Topic.find(t2.id).replies.size
|
252
|
+
|
253
|
+
r1.topic = nil
|
254
|
+
|
255
|
+
assert_equal 0, Topic.find(t1.id).replies.size
|
256
|
+
assert_equal 0, Topic.find(t2.id).replies.size
|
257
|
+
|
258
|
+
r1.topic = t1
|
259
|
+
|
260
|
+
assert_equal 1, Topic.find(t1.id).replies.size
|
261
|
+
assert_equal 0, Topic.find(t2.id).replies.size
|
262
|
+
|
263
|
+
r1.destroy
|
264
|
+
|
265
|
+
assert_equal 0, Topic.find(t1.id).replies.size
|
266
|
+
assert_equal 0, Topic.find(t2.id).replies.size
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_belongs_to_reassign_with_namespaced_models_and_counters
|
270
|
+
t1 = Web::Topic.create("title" => "t1")
|
271
|
+
t2 = Web::Topic.create("title" => "t2")
|
272
|
+
r1 = Web::Reply.new("title" => "r1", "content" => "r1")
|
273
|
+
r1.topic = t1
|
274
|
+
|
275
|
+
assert r1.save
|
276
|
+
assert_equal 1, Web::Topic.find(t1.id).replies.size
|
277
|
+
assert_equal 0, Web::Topic.find(t2.id).replies.size
|
278
|
+
|
279
|
+
r1.topic = Web::Topic.find(t2.id)
|
280
|
+
|
281
|
+
assert r1.save
|
282
|
+
assert_equal 0, Web::Topic.find(t1.id).replies.size
|
283
|
+
assert_equal 1, Web::Topic.find(t2.id).replies.size
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_belongs_to_counter_after_save
|
287
|
+
topic = Topic.create!(:title => "monday night")
|
288
|
+
topic.replies.create!(:title => "re: monday night", :content => "football")
|
289
|
+
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
290
|
+
|
291
|
+
topic.save!
|
292
|
+
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_belongs_to_counter_after_update_attributes
|
296
|
+
topic = Topic.create!(:title => "37s")
|
297
|
+
topic.replies.create!(:title => "re: 37s", :content => "rails")
|
298
|
+
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
299
|
+
|
300
|
+
topic.update_attributes(:title => "37signals")
|
301
|
+
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
302
|
+
end
|
303
|
+
|
304
|
+
def test_belongs_to_counter_when_update_column
|
305
|
+
topic = Topic.create!(:title => "37s")
|
306
|
+
topic.replies.create!(:title => "re: 37s", :content => "rails")
|
307
|
+
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
308
|
+
|
309
|
+
topic.update_column(:content, "rails is wonderfull")
|
310
|
+
assert_equal 1, Topic.find(topic.id)[:replies_count]
|
311
|
+
end
|
312
|
+
|
313
|
+
def test_assignment_before_child_saved
|
314
|
+
final_cut = Client.new("name" => "Final Cut")
|
315
|
+
firm = Firm.find(1)
|
316
|
+
final_cut.firm = firm
|
317
|
+
assert !final_cut.persisted?
|
318
|
+
assert final_cut.save
|
319
|
+
assert final_cut.persisted?
|
320
|
+
assert firm.persisted?
|
321
|
+
assert_equal firm, final_cut.firm
|
322
|
+
assert_equal firm, final_cut.firm(true)
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_assignment_before_child_saved_with_primary_key
|
326
|
+
final_cut = Client.new("name" => "Final Cut")
|
327
|
+
firm = Firm.find(1)
|
328
|
+
final_cut.firm_with_primary_key = firm
|
329
|
+
assert !final_cut.persisted?
|
330
|
+
assert final_cut.save
|
331
|
+
assert final_cut.persisted?
|
332
|
+
assert firm.persisted?
|
333
|
+
assert_equal firm, final_cut.firm_with_primary_key
|
334
|
+
assert_equal firm, final_cut.firm_with_primary_key(true)
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_new_record_with_foreign_key_but_no_object
|
338
|
+
c = Client.new("firm_id" => 1)
|
339
|
+
# sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
|
340
|
+
assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
|
341
|
+
end
|
342
|
+
|
343
|
+
def test_setting_foreign_key_after_nil_target_loaded
|
344
|
+
client = Client.new
|
345
|
+
client.firm_with_basic_id
|
346
|
+
client.firm_id = 1
|
347
|
+
|
348
|
+
assert_equal companies(:first_firm), client.firm_with_basic_id
|
349
|
+
end
|
350
|
+
|
351
|
+
def test_polymorphic_setting_foreign_key_after_nil_target_loaded
|
352
|
+
sponsor = Sponsor.new
|
353
|
+
sponsor.sponsorable
|
354
|
+
sponsor.sponsorable_id = 1
|
355
|
+
sponsor.sponsorable_type = "Member"
|
356
|
+
|
357
|
+
assert_equal members(:groucho), sponsor.sponsorable
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_dont_find_target_when_foreign_key_is_null
|
361
|
+
tagging = taggings(:thinking_general)
|
362
|
+
queries = assert_sql { tagging.super_tag }
|
363
|
+
assert_equal 0, queries.length
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_field_name_same_as_foreign_key
|
367
|
+
computer = Computer.find(1)
|
368
|
+
assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_counter_cache
|
372
|
+
topic = Topic.create :title => "Zoom-zoom-zoom"
|
373
|
+
assert_equal 0, topic[:replies_count]
|
374
|
+
|
375
|
+
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
|
376
|
+
reply.topic = topic
|
377
|
+
|
378
|
+
assert_equal 1, topic.reload[:replies_count]
|
379
|
+
assert_equal 1, topic.replies.size
|
380
|
+
|
381
|
+
topic[:replies_count] = 15
|
382
|
+
assert_equal 15, topic.replies.size
|
383
|
+
end
|
384
|
+
|
385
|
+
def test_custom_counter_cache
|
386
|
+
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
|
387
|
+
assert_equal 0, reply[:replies_count]
|
388
|
+
|
389
|
+
silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
|
390
|
+
silly.reply = reply
|
391
|
+
|
392
|
+
assert_equal 1, reply.reload[:replies_count]
|
393
|
+
assert_equal 1, reply.replies.size
|
394
|
+
|
395
|
+
reply[:replies_count] = 17
|
396
|
+
assert_equal 17, reply.replies.size
|
397
|
+
end
|
398
|
+
|
399
|
+
def test_association_assignment_sticks
|
400
|
+
post = Post.find(:first)
|
401
|
+
|
402
|
+
author1, author2 = Author.find(:all, :limit => 2)
|
403
|
+
assert_not_nil author1
|
404
|
+
assert_not_nil author2
|
405
|
+
|
406
|
+
# make sure the association is loaded
|
407
|
+
post.author
|
408
|
+
|
409
|
+
# set the association by id, directly
|
410
|
+
post.author_id = author2.id
|
411
|
+
|
412
|
+
# save and reload
|
413
|
+
post.save!
|
414
|
+
post.reload
|
415
|
+
|
416
|
+
# the author id of the post should be the id we set
|
417
|
+
assert_equal post.author_id, author2.id
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_cant_save_readonly_association
|
421
|
+
assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
|
422
|
+
assert companies(:first_client).readonly_firm.readonly?
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_polymorphic_assignment_foreign_type_field_updating
|
426
|
+
# should update when assigning a saved record
|
427
|
+
sponsor = Sponsor.new
|
428
|
+
member = Member.create
|
429
|
+
sponsor.sponsorable = member
|
430
|
+
assert_equal "Member", sponsor.sponsorable_type
|
431
|
+
|
432
|
+
# should update when assigning a new record
|
433
|
+
sponsor = Sponsor.new
|
434
|
+
member = Member.new
|
435
|
+
sponsor.sponsorable = member
|
436
|
+
assert_equal "Member", sponsor.sponsorable_type
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_polymorphic_assignment_with_primary_key_foreign_type_field_updating
|
440
|
+
# should update when assigning a saved record
|
441
|
+
essay = Essay.new
|
442
|
+
writer = Author.create(:name => "David")
|
443
|
+
essay.writer = writer
|
444
|
+
assert_equal "Author", essay.writer_type
|
445
|
+
|
446
|
+
# should update when assigning a new record
|
447
|
+
essay = Essay.new
|
448
|
+
writer = Author.new
|
449
|
+
essay.writer = writer
|
450
|
+
assert_equal "Author", essay.writer_type
|
451
|
+
end
|
452
|
+
|
453
|
+
def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
|
454
|
+
sponsor = Sponsor.new
|
455
|
+
saved_member = Member.create
|
456
|
+
new_member = Member.new
|
457
|
+
|
458
|
+
sponsor.sponsorable = saved_member
|
459
|
+
assert_equal saved_member.id, sponsor.sponsorable_id
|
460
|
+
|
461
|
+
sponsor.sponsorable = new_member
|
462
|
+
assert_nil sponsor.sponsorable_id
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_assignment_updates_foreign_id_field_for_new_and_saved_records
|
466
|
+
client = Client.new
|
467
|
+
saved_firm = Firm.create :name => "Saved"
|
468
|
+
new_firm = Firm.new
|
469
|
+
|
470
|
+
client.firm = saved_firm
|
471
|
+
assert_equal saved_firm.id, client.client_of
|
472
|
+
|
473
|
+
client.firm = new_firm
|
474
|
+
assert_nil client.client_of
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records
|
478
|
+
essay = Essay.new
|
479
|
+
saved_writer = Author.create(:name => "David")
|
480
|
+
new_writer = Author.new
|
481
|
+
|
482
|
+
essay.writer = saved_writer
|
483
|
+
assert_equal saved_writer.name, essay.writer_id
|
484
|
+
|
485
|
+
essay.writer = new_writer
|
486
|
+
assert_nil essay.writer_id
|
487
|
+
end
|
488
|
+
|
489
|
+
def test_belongs_to_proxy_should_not_respond_to_private_methods
|
490
|
+
assert_raise(NoMethodError) { companies(:first_firm).private_method }
|
491
|
+
assert_raise(NoMethodError) { companies(:second_client).firm.private_method }
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_belongs_to_proxy_should_respond_to_private_methods_via_send
|
495
|
+
companies(:first_firm).send(:private_method)
|
496
|
+
companies(:second_client).firm.send(:private_method)
|
497
|
+
end
|
498
|
+
|
499
|
+
def test_save_of_record_with_loaded_belongs_to
|
500
|
+
@account = companies(:first_firm).account
|
501
|
+
|
502
|
+
assert_nothing_raised do
|
503
|
+
Account.find(@account.id).save!
|
504
|
+
Account.find(@account.id, :include => :firm).save!
|
505
|
+
end
|
506
|
+
|
507
|
+
@account.firm.delete
|
508
|
+
|
509
|
+
assert_nothing_raised do
|
510
|
+
Account.find(@account.id).save!
|
511
|
+
Account.find(@account.id, :include => :firm).save!
|
512
|
+
end
|
513
|
+
end
|
514
|
+
|
515
|
+
def test_dependent_delete_and_destroy_with_belongs_to
|
516
|
+
author_address = author_addresses(:david_address)
|
517
|
+
author_address_extra = author_addresses(:david_address_extra)
|
518
|
+
assert_equal [], AuthorAddress.destroyed_author_address_ids
|
519
|
+
|
520
|
+
assert_difference "AuthorAddress.count", -2 do
|
521
|
+
authors(:david).destroy
|
522
|
+
end
|
523
|
+
|
524
|
+
assert_equal [], AuthorAddress.find_all_by_id([author_address.id, author_address_extra.id])
|
525
|
+
assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids
|
526
|
+
end
|
527
|
+
|
528
|
+
def test_invalid_belongs_to_dependent_option_nullify_raises_exception
|
529
|
+
assert_raise ArgumentError do
|
530
|
+
Author.belongs_to :special_author_address, :dependent => :nullify
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
def test_invalid_belongs_to_dependent_option_restrict_raises_exception
|
535
|
+
assert_raise ArgumentError do
|
536
|
+
Author.belongs_to :special_author_address, :dependent => :restrict
|
537
|
+
end
|
538
|
+
end
|
539
|
+
|
540
|
+
def test_attributes_are_being_set_when_initialized_from_belongs_to_association_with_where_clause
|
541
|
+
new_firm = accounts(:signals37).build_firm(:name => 'Apple')
|
542
|
+
assert_equal new_firm.name, "Apple"
|
543
|
+
end
|
544
|
+
|
545
|
+
def test_reassigning_the_parent_id_updates_the_object
|
546
|
+
client = companies(:second_client)
|
547
|
+
|
548
|
+
client.firm
|
549
|
+
client.firm_with_condition
|
550
|
+
firm_proxy = client.send(:association_instance_get, :firm)
|
551
|
+
firm_with_condition_proxy = client.send(:association_instance_get, :firm_with_condition)
|
552
|
+
|
553
|
+
assert !firm_proxy.stale_target?
|
554
|
+
assert !firm_with_condition_proxy.stale_target?
|
555
|
+
assert_equal companies(:first_firm), client.firm
|
556
|
+
assert_equal companies(:first_firm), client.firm_with_condition
|
557
|
+
|
558
|
+
client.client_of = companies(:another_firm).id
|
559
|
+
|
560
|
+
assert firm_proxy.stale_target?
|
561
|
+
assert firm_with_condition_proxy.stale_target?
|
562
|
+
assert_equal companies(:another_firm), client.firm
|
563
|
+
assert_equal companies(:another_firm), client.firm_with_condition
|
564
|
+
end
|
565
|
+
|
566
|
+
def test_polymorphic_reassignment_of_associated_id_updates_the_object
|
567
|
+
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
|
568
|
+
|
569
|
+
sponsor.sponsorable
|
570
|
+
proxy = sponsor.send(:association_instance_get, :sponsorable)
|
571
|
+
|
572
|
+
assert !proxy.stale_target?
|
573
|
+
assert_equal members(:groucho), sponsor.sponsorable
|
574
|
+
|
575
|
+
sponsor.sponsorable_id = members(:some_other_guy).id
|
576
|
+
|
577
|
+
assert proxy.stale_target?
|
578
|
+
assert_equal members(:some_other_guy), sponsor.sponsorable
|
579
|
+
end
|
580
|
+
|
581
|
+
def test_polymorphic_reassignment_of_associated_type_updates_the_object
|
582
|
+
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
|
583
|
+
|
584
|
+
sponsor.sponsorable
|
585
|
+
proxy = sponsor.send(:association_instance_get, :sponsorable)
|
586
|
+
|
587
|
+
assert !proxy.stale_target?
|
588
|
+
assert_equal members(:groucho), sponsor.sponsorable
|
589
|
+
|
590
|
+
sponsor.sponsorable_type = 'Firm'
|
591
|
+
|
592
|
+
assert proxy.stale_target?
|
593
|
+
assert_equal companies(:first_firm), sponsor.sponsorable
|
594
|
+
end
|
595
|
+
|
596
|
+
def test_reloading_association_with_key_change
|
597
|
+
client = companies(:second_client)
|
598
|
+
firm = client.association(:firm)
|
599
|
+
|
600
|
+
client.firm = companies(:another_firm)
|
601
|
+
firm.reload
|
602
|
+
assert_equal companies(:another_firm), firm.target
|
603
|
+
|
604
|
+
client.client_of = companies(:first_firm).id
|
605
|
+
firm.reload
|
606
|
+
assert_equal companies(:first_firm), firm.target
|
607
|
+
end
|
608
|
+
|
609
|
+
def test_polymorphic_counter_cache
|
610
|
+
tagging = taggings(:welcome_general)
|
611
|
+
post = posts(:welcome)
|
612
|
+
comment = comments(:greetings)
|
613
|
+
|
614
|
+
assert_difference lambda { post.reload.taggings_count }, -1 do
|
615
|
+
assert_difference 'comment.reload.taggings_count', +1 do
|
616
|
+
tagging.taggable = comment
|
617
|
+
end
|
618
|
+
end
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_polymorphic_with_custom_foreign_type
|
622
|
+
sponsor = sponsors(:moustache_club_sponsor_for_groucho)
|
623
|
+
groucho = members(:groucho)
|
624
|
+
other = members(:some_other_guy)
|
625
|
+
|
626
|
+
assert_equal groucho, sponsor.sponsorable
|
627
|
+
assert_equal groucho, sponsor.thing
|
628
|
+
|
629
|
+
sponsor.thing = other
|
630
|
+
|
631
|
+
assert_equal other, sponsor.sponsorable
|
632
|
+
assert_equal other, sponsor.thing
|
633
|
+
|
634
|
+
sponsor.sponsorable = groucho
|
635
|
+
|
636
|
+
assert_equal groucho, sponsor.sponsorable
|
637
|
+
assert_equal groucho, sponsor.thing
|
638
|
+
end
|
639
|
+
|
640
|
+
def test_build_with_conditions
|
641
|
+
client = companies(:second_client)
|
642
|
+
firm = client.build_bob_firm
|
643
|
+
|
644
|
+
assert_equal "Bob", firm.name
|
645
|
+
end
|
646
|
+
|
647
|
+
def test_create_with_conditions
|
648
|
+
client = companies(:second_client)
|
649
|
+
firm = client.create_bob_firm
|
650
|
+
|
651
|
+
assert_equal "Bob", firm.name
|
652
|
+
end
|
653
|
+
|
654
|
+
def test_create_bang_with_conditions
|
655
|
+
client = companies(:second_client)
|
656
|
+
firm = client.create_bob_firm!
|
657
|
+
|
658
|
+
assert_equal "Bob", firm.name
|
659
|
+
end
|
660
|
+
|
661
|
+
def test_build_with_block
|
662
|
+
client = Client.create(:name => 'Client Company')
|
663
|
+
|
664
|
+
firm = client.build_firm{ |f| f.name = 'Agency Company' }
|
665
|
+
assert_equal 'Agency Company', firm.name
|
666
|
+
end
|
667
|
+
|
668
|
+
def test_create_with_block
|
669
|
+
client = Client.create(:name => 'Client Company')
|
670
|
+
|
671
|
+
firm = client.create_firm{ |f| f.name = 'Agency Company' }
|
672
|
+
assert_equal 'Agency Company', firm.name
|
673
|
+
end
|
674
|
+
|
675
|
+
def test_create_bang_with_block
|
676
|
+
client = Client.create(:name => 'Client Company')
|
677
|
+
|
678
|
+
firm = client.create_firm!{ |f| f.name = 'Agency Company' }
|
679
|
+
assert_equal 'Agency Company', firm.name
|
680
|
+
end
|
681
|
+
|
682
|
+
def test_should_set_foreign_key_on_create_association
|
683
|
+
client = Client.create! :name => "fuu"
|
684
|
+
|
685
|
+
firm = client.create_firm :name => "baa"
|
686
|
+
assert_equal firm.id, client.client_of
|
687
|
+
end
|
688
|
+
|
689
|
+
def test_should_set_foreign_key_on_create_association!
|
690
|
+
client = Client.create! :name => "fuu"
|
691
|
+
|
692
|
+
firm = client.create_firm! :name => "baa"
|
693
|
+
assert_equal firm.id, client.client_of
|
694
|
+
end
|
695
|
+
|
696
|
+
def test_self_referential_belongs_to_with_counter_cache_assigning_nil
|
697
|
+
comment = Comment.create! :post => posts(:thinking), :body => "fuu"
|
698
|
+
comment.parent = nil
|
699
|
+
comment.save!
|
700
|
+
|
701
|
+
assert_equal nil, comment.reload.parent
|
702
|
+
assert_equal 0, comments(:greetings).reload.children_count
|
703
|
+
end
|
704
|
+
|
705
|
+
def test_polymorphic_with_custom_primary_key
|
706
|
+
toy = Toy.create!
|
707
|
+
sponsor = Sponsor.create!(:sponsorable => toy)
|
708
|
+
|
709
|
+
assert_equal toy, sponsor.reload.sponsorable
|
710
|
+
end
|
711
|
+
end
|