activerecord 1.14.4 → 1.15.0

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

Potentially problematic release.


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

Files changed (159) hide show
  1. data/CHANGELOG +400 -1
  2. data/README +2 -2
  3. data/RUNNING_UNIT_TESTS +21 -3
  4. data/Rakefile +55 -10
  5. data/lib/active_record.rb +10 -4
  6. data/lib/active_record/acts/list.rb +15 -4
  7. data/lib/active_record/acts/nested_set.rb +11 -12
  8. data/lib/active_record/acts/tree.rb +13 -14
  9. data/lib/active_record/aggregations.rb +46 -22
  10. data/lib/active_record/associations.rb +213 -162
  11. data/lib/active_record/associations/association_collection.rb +45 -15
  12. data/lib/active_record/associations/association_proxy.rb +32 -13
  13. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +18 -18
  14. data/lib/active_record/associations/has_many_association.rb +37 -17
  15. data/lib/active_record/associations/has_many_through_association.rb +120 -30
  16. data/lib/active_record/associations/has_one_association.rb +1 -1
  17. data/lib/active_record/attribute_methods.rb +75 -0
  18. data/lib/active_record/base.rb +282 -203
  19. data/lib/active_record/calculations.rb +95 -54
  20. data/lib/active_record/callbacks.rb +13 -24
  21. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +12 -1
  22. data/lib/active_record/connection_adapters/abstract/connection_specification.rb.rej +21 -0
  23. data/lib/active_record/connection_adapters/abstract/database_statements.rb +30 -4
  24. data/lib/active_record/connection_adapters/abstract/quoting.rb +16 -9
  25. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +121 -37
  26. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +55 -23
  27. data/lib/active_record/connection_adapters/abstract_adapter.rb +8 -0
  28. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -11
  29. data/lib/active_record/connection_adapters/firebird_adapter.rb +364 -50
  30. data/lib/active_record/connection_adapters/frontbase_adapter.rb +861 -0
  31. data/lib/active_record/connection_adapters/mysql_adapter.rb +86 -33
  32. data/lib/active_record/connection_adapters/openbase_adapter.rb +4 -3
  33. data/lib/active_record/connection_adapters/oracle_adapter.rb +151 -127
  34. data/lib/active_record/connection_adapters/postgresql_adapter.rb +125 -48
  35. data/lib/active_record/connection_adapters/sqlite_adapter.rb +38 -10
  36. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +183 -155
  37. data/lib/active_record/connection_adapters/sybase_adapter.rb +190 -212
  38. data/lib/active_record/deprecated_associations.rb +24 -10
  39. data/lib/active_record/deprecated_finders.rb +4 -1
  40. data/lib/active_record/fixtures.rb +37 -23
  41. data/lib/active_record/locking/optimistic.rb +106 -0
  42. data/lib/active_record/locking/pessimistic.rb +77 -0
  43. data/lib/active_record/migration.rb +8 -5
  44. data/lib/active_record/observer.rb +73 -34
  45. data/lib/active_record/reflection.rb +21 -7
  46. data/lib/active_record/schema_dumper.rb +33 -5
  47. data/lib/active_record/timestamp.rb +23 -34
  48. data/lib/active_record/transactions.rb +37 -30
  49. data/lib/active_record/validations.rb +46 -30
  50. data/lib/active_record/vendor/mysql.rb +20 -5
  51. data/lib/active_record/version.rb +2 -2
  52. data/lib/active_record/wrappings.rb +1 -2
  53. data/lib/active_record/xml_serialization.rb +308 -0
  54. data/test/aaa_create_tables_test.rb +5 -1
  55. data/test/abstract_unit.rb +18 -8
  56. data/test/{active_schema_mysql.rb → active_schema_test_mysql.rb} +2 -2
  57. data/test/adapter_test.rb +9 -7
  58. data/test/adapter_test_sqlserver.rb +81 -0
  59. data/test/aggregations_test.rb +29 -0
  60. data/test/{association_callbacks_test.rb → associations/callbacks_test.rb} +10 -8
  61. data/test/{associations_cascaded_eager_loading_test.rb → associations/cascaded_eager_loading_test.rb} +35 -3
  62. data/test/{associations_go_eager_test.rb → associations/eager_test.rb} +36 -2
  63. data/test/{associations_extensions_test.rb → associations/extension_test.rb} +5 -0
  64. data/test/{associations_join_model_test.rb → associations/join_model_test.rb} +118 -8
  65. data/test/associations_test.rb +339 -45
  66. data/test/attribute_methods_test.rb +49 -0
  67. data/test/base_test.rb +321 -67
  68. data/test/calculations_test.rb +48 -10
  69. data/test/callbacks_test.rb +13 -0
  70. data/test/connection_test_firebird.rb +8 -0
  71. data/test/connections/native_db2/connection.rb +18 -17
  72. data/test/connections/native_firebird/connection.rb +19 -17
  73. data/test/connections/native_frontbase/connection.rb +27 -0
  74. data/test/connections/native_mysql/connection.rb +18 -15
  75. data/test/connections/native_openbase/connection.rb +14 -15
  76. data/test/connections/native_oracle/connection.rb +16 -12
  77. data/test/connections/native_postgresql/connection.rb +16 -17
  78. data/test/connections/native_sqlite/connection.rb +3 -6
  79. data/test/connections/native_sqlite3/connection.rb +3 -6
  80. data/test/connections/native_sqlserver/connection.rb +16 -17
  81. data/test/connections/native_sqlserver_odbc/connection.rb +18 -19
  82. data/test/connections/native_sybase/connection.rb +16 -17
  83. data/test/datatype_test_postgresql.rb +52 -0
  84. data/test/defaults_test.rb +52 -10
  85. data/test/deprecated_associations_test.rb +151 -107
  86. data/test/deprecated_finder_test.rb +83 -66
  87. data/test/empty_date_time_test.rb +25 -0
  88. data/test/finder_test.rb +118 -11
  89. data/test/fixtures/accounts.yml +6 -1
  90. data/test/fixtures/author.rb +27 -4
  91. data/test/fixtures/categorizations.yml +8 -2
  92. data/test/fixtures/category.rb +1 -2
  93. data/test/fixtures/comments.yml +0 -6
  94. data/test/fixtures/companies.yml +6 -1
  95. data/test/fixtures/company.rb +23 -1
  96. data/test/fixtures/company_in_module.rb +8 -10
  97. data/test/fixtures/customer.rb +2 -2
  98. data/test/fixtures/customers.yml +9 -0
  99. data/test/fixtures/db_definitions/db2.drop.sql +1 -0
  100. data/test/fixtures/db_definitions/db2.sql +9 -0
  101. data/test/fixtures/db_definitions/firebird.drop.sql +3 -0
  102. data/test/fixtures/db_definitions/firebird.sql +13 -1
  103. data/test/fixtures/db_definitions/frontbase.drop.sql +31 -0
  104. data/test/fixtures/db_definitions/frontbase.sql +262 -0
  105. data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
  106. data/test/fixtures/db_definitions/frontbase2.sql +4 -0
  107. data/test/fixtures/db_definitions/mysql.drop.sql +1 -0
  108. data/test/fixtures/db_definitions/mysql.sql +23 -14
  109. data/test/fixtures/db_definitions/openbase.sql +13 -1
  110. data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
  111. data/test/fixtures/db_definitions/oracle.sql +29 -2
  112. data/test/fixtures/db_definitions/postgresql.drop.sql +3 -1
  113. data/test/fixtures/db_definitions/postgresql.sql +13 -3
  114. data/test/fixtures/db_definitions/schema.rb +29 -1
  115. data/test/fixtures/db_definitions/sqlite.drop.sql +1 -0
  116. data/test/fixtures/db_definitions/sqlite.sql +12 -3
  117. data/test/fixtures/db_definitions/sqlserver.drop.sql +3 -0
  118. data/test/fixtures/db_definitions/sqlserver.sql +35 -0
  119. data/test/fixtures/db_definitions/sybase.drop.sql +2 -0
  120. data/test/fixtures/db_definitions/sybase.sql +13 -4
  121. data/test/fixtures/developer.rb +12 -0
  122. data/test/fixtures/edge.rb +5 -0
  123. data/test/fixtures/edges.yml +6 -0
  124. data/test/fixtures/funny_jokes.yml +3 -7
  125. data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
  126. data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
  127. data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
  128. data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
  129. data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
  130. data/test/fixtures/mixin.rb +15 -0
  131. data/test/fixtures/mixins.yml +38 -0
  132. data/test/fixtures/post.rb +3 -2
  133. data/test/fixtures/project.rb +3 -1
  134. data/test/fixtures/topic.rb +6 -1
  135. data/test/fixtures/topics.yml +4 -4
  136. data/test/fixtures/vertex.rb +9 -0
  137. data/test/fixtures/vertices.yml +4 -0
  138. data/test/fixtures_test.rb +45 -0
  139. data/test/inheritance_test.rb +67 -6
  140. data/test/lifecycle_test.rb +40 -19
  141. data/test/locking_test.rb +170 -26
  142. data/test/method_scoping_test.rb +2 -2
  143. data/test/migration_test.rb +387 -110
  144. data/test/migration_test_firebird.rb +124 -0
  145. data/test/mixin_nested_set_test.rb +14 -2
  146. data/test/mixin_test.rb +56 -18
  147. data/test/modules_test.rb +8 -2
  148. data/test/multiple_db_test.rb +2 -2
  149. data/test/pk_test.rb +1 -0
  150. data/test/reflection_test.rb +8 -2
  151. data/test/schema_authorization_test_postgresql.rb +75 -0
  152. data/test/schema_dumper_test.rb +40 -4
  153. data/test/table_name_test_sqlserver.rb +23 -0
  154. data/test/threaded_connections_test.rb +19 -16
  155. data/test/transactions_test.rb +86 -72
  156. data/test/validations_test.rb +126 -56
  157. data/test/xml_serialization_test.rb +125 -0
  158. metadata +45 -11
  159. data/lib/active_record/locking.rb +0 -79
@@ -4,21 +4,20 @@ require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
6
6
 
7
- db1 = 'activerecord_unittest'
8
- db2 = 'activerecord_unittest2'
7
+ ActiveRecord::Base.configurations = {
8
+ 'arunit' => {
9
+ :adapter => 'sqlserver',
10
+ :host => 'localhost',
11
+ :username => 'sa',
12
+ :database => 'activerecord_unittest'
13
+ },
14
+ 'arunit2' => {
15
+ :adapter => 'sqlserver',
16
+ :host => 'localhost',
17
+ :username => 'sa',
18
+ :database => 'activerecord_unittest2'
19
+ }
20
+ }
9
21
 
10
- ActiveRecord::Base.establish_connection(
11
- :adapter => "sqlserver",
12
- :host => "localhost",
13
- :username => "sa",
14
- :password => "",
15
- :database => db1
16
- )
17
-
18
- Course.establish_connection(
19
- :adapter => "sqlserver",
20
- :host => "localhost",
21
- :username => "sa",
22
- :password => "",
23
- :database => db2
24
- )
22
+ ActiveRecord::Base.establish_connection 'arunit'
23
+ Course.establish_connection 'arunit2'
@@ -4,23 +4,22 @@ require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
6
6
 
7
- dsn1 = 'activerecord_unittest'
8
- dsn2 = 'activerecord_unittest2'
7
+ ActiveRecord::Base.configurations = {
8
+ 'arunit' => {
9
+ :adapter => 'sqlserver',
10
+ :mode => 'ODBC',
11
+ :host => 'localhost',
12
+ :username => 'sa',
13
+ :dsn => 'activerecord_unittest'
14
+ },
15
+ 'arunit2' => {
16
+ :adapter => 'sqlserver',
17
+ :mode => 'ODBC',
18
+ :host => 'localhost',
19
+ :username => 'sa',
20
+ :dsn => 'activerecord_unittest2'
21
+ }
22
+ }
9
23
 
10
- ActiveRecord::Base.establish_connection(
11
- :adapter => "sqlserver",
12
- :mode => "ODBC",
13
- :host => "localhost",
14
- :username => "sa",
15
- :password => "",
16
- :dsn => dsn1
17
- )
18
-
19
- Course.establish_connection(
20
- :adapter => "sqlserver",
21
- :mode => "ODBC",
22
- :host => "localhost",
23
- :username => "sa",
24
- :password => "",
25
- :dsn => dsn2
26
- )
24
+ ActiveRecord::Base.establish_connection 'arunit'
25
+ Course.establish_connection 'arunit2'
@@ -4,21 +4,20 @@ require 'logger'
4
4
 
5
5
  ActiveRecord::Base.logger = Logger.new("debug.log")
6
6
 
7
- db1 = 'activerecord_unittest'
8
- db2 = 'activerecord_unittest2'
7
+ ActiveRecord::Base.configurations = {
8
+ 'arunit' => {
9
+ :adapter => 'sybase',
10
+ :host => 'database_ASE',
11
+ :username => 'sa',
12
+ :database => 'activerecord_unittest'
13
+ },
14
+ 'arunit2' => {
15
+ :adapter => 'sybase',
16
+ :host => 'database_ASE',
17
+ :username => 'sa',
18
+ :database => 'activerecord_unittest2'
19
+ }
20
+ }
9
21
 
10
- ActiveRecord::Base.establish_connection(
11
- :adapter => "sybase",
12
- :host => "database_ASE",
13
- :username => "sa",
14
- :password => "",
15
- :database => db1
16
- )
17
-
18
- Course.establish_connection(
19
- :adapter => "sybase",
20
- :host => "database_ASE",
21
- :username => "sa",
22
- :password => "",
23
- :database => db2
24
- )
22
+ ActiveRecord::Base.establish_connection 'arunit'
23
+ Course.establish_connection 'arunit2'
@@ -0,0 +1,52 @@
1
+ require 'abstract_unit'
2
+
3
+ class PostgresqlDatatype < ActiveRecord::Base
4
+ end
5
+
6
+ class PGDataTypeTest < Test::Unit::TestCase
7
+ self.use_transactional_fixtures = false
8
+
9
+ TABLE_NAME = 'postgresql_datatypes'
10
+ COLUMNS = [
11
+ 'id SERIAL PRIMARY KEY',
12
+ 'commission_by_quarter INTEGER[]',
13
+ 'nicknames TEXT[]'
14
+ ]
15
+
16
+ def setup
17
+ @connection = ActiveRecord::Base.connection
18
+ @connection.execute "CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
19
+ @connection.execute "INSERT INTO #{TABLE_NAME} (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )"
20
+ @first = PostgresqlDatatype.find( 1 )
21
+ end
22
+
23
+ def teardown
24
+ @connection.execute "DROP TABLE #{TABLE_NAME}"
25
+ end
26
+
27
+ def test_data_type_of_array_types
28
+ assert_equal :string, @first.column_for_attribute("commission_by_quarter").type
29
+ assert_equal :string, @first.column_for_attribute("nicknames").type
30
+ end
31
+
32
+ def test_array_values
33
+ assert_equal '{35000,21000,18000,17000}', @first.commission_by_quarter
34
+ assert_equal '{foo,bar,baz}', @first.nicknames
35
+ end
36
+
37
+ def test_update_integer_array
38
+ new_value = '{32800,95000,29350,17000}'
39
+ assert @first.commission_by_quarter = new_value
40
+ assert @first.save
41
+ assert @first.reload
42
+ assert_equal @first.commission_by_quarter, new_value
43
+ end
44
+
45
+ def test_update_text_array
46
+ new_value = '{robby,robert,rob,robbie}'
47
+ assert @first.nicknames = new_value
48
+ assert @first.save
49
+ assert @first.reload
50
+ assert_equal @first.nicknames, new_value
51
+ end
52
+ end
@@ -1,18 +1,60 @@
1
1
  require 'abstract_unit'
2
2
  require 'fixtures/default'
3
+ require 'fixtures/entrant'
3
4
 
4
- class DefaultsTest < Test::Unit::TestCase
5
- if %w(PostgreSQL).include? ActiveRecord::Base.connection.adapter_name
5
+ class DefaultTest < Test::Unit::TestCase
6
+ def test_nil_defaults_for_not_null_columns
7
+ column_defaults =
8
+ if current_adapter?(:MysqlAdapter)
9
+ { 'id' => nil, 'name' => '', 'course_id' => nil }
10
+ else
11
+ { 'id' => nil, 'name' => nil, 'course_id' => nil }
12
+ end
13
+
14
+ column_defaults.each do |name, default|
15
+ column = Entrant.columns_hash[name]
16
+ assert !column.null, "#{name} column should be NOT NULL"
17
+ assert_equal default, column.default, "#{name} column should be DEFAULT #{default.inspect}"
18
+ end
19
+ end
20
+
21
+ if current_adapter?(:MysqlAdapter)
22
+ # MySQL uses an implicit default 0 rather than NULL unless in strict mode.
23
+ # We use an implicit NULL so schema.rb is compatible with other databases.
24
+ def test_mysql_integer_not_null_defaults
25
+ klass = Class.new(ActiveRecord::Base)
26
+ klass.table_name = 'test_integer_not_null_default_zero'
27
+ klass.connection.create_table klass.table_name do |t|
28
+ t.column :zero, :integer, :null => false, :default => 0
29
+ t.column :omit, :integer, :null => false
30
+ end
31
+
32
+ assert_equal 0, klass.columns_hash['zero'].default
33
+ assert !klass.columns_hash['zero'].null
34
+ assert_equal nil, klass.columns_hash['omit'].default
35
+ assert !klass.columns_hash['omit'].null
36
+
37
+ assert_raise(ActiveRecord::StatementInvalid) { klass.create! }
38
+
39
+ assert_nothing_raised do
40
+ instance = klass.create!(:omit => 1)
41
+ assert_equal 0, instance.zero
42
+ assert_equal 1, instance.omit
43
+ end
44
+ ensure
45
+ klass.connection.drop_table(klass.table_name) rescue nil
46
+ end
47
+ end
48
+
49
+ if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
6
50
  def test_default_integers
7
51
  default = Default.new
8
- assert_instance_of(Fixnum, default.positive_integer)
9
- assert_equal(default.positive_integer, 1)
10
- assert_instance_of(Fixnum, default.negative_integer)
11
- assert_equal(default.negative_integer, -1)
12
- end
13
- else
14
- def test_dummy
15
- assert true
52
+ assert_instance_of Fixnum, default.positive_integer
53
+ assert_equal 1, default.positive_integer
54
+ assert_instance_of Fixnum, default.negative_integer
55
+ assert_equal -1, default.negative_integer
56
+ assert_instance_of BigDecimal, default.decimal_number
57
+ assert_equal BigDecimal.new("2.78"), default.decimal_number
16
58
  end
17
59
  end
18
60
  end
@@ -15,173 +15,213 @@ end
15
15
  raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
16
16
 
17
17
 
18
+ class DeprecatedAssociationWarningsTest < Test::Unit::TestCase
19
+ def test_deprecation_warnings
20
+ assert_deprecated('find_first') { Firm.find_first }
21
+ assert_deprecated('find_all') { Firm.find_all }
22
+ assert_deprecated('has_account?') { Firm.find(:first).has_account? }
23
+ assert_deprecated('has_clients?') { Firm.find(:first).has_clients? }
24
+ end
25
+ end
26
+
18
27
  class DeprecatedAssociationsTest < Test::Unit::TestCase
19
28
  fixtures :accounts, :companies, :developers, :projects, :topics,
20
29
  :developers_projects
21
30
 
31
+ def setup
32
+ @firm = companies(:first_firm)
33
+ end
34
+
22
35
  def test_has_many_find
23
- assert_equal 2, Firm.find_first.clients.length
36
+ assert_equal 2, @firm.clients.length
24
37
  end
25
38
 
26
39
  def test_has_many_orders
27
- assert_equal "Summit", Firm.find_first.clients.first.name
40
+ assert_equal "Summit", @firm.clients.first.name
28
41
  end
29
42
 
30
43
  def test_has_many_class_name
31
- assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
44
+ assert_equal "Microsoft", @firm.clients_sorted_desc.first.name
32
45
  end
33
46
 
34
47
  def test_has_many_foreign_key
35
- assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
48
+ assert_equal "Microsoft", @firm.clients_of_firm.first.name
36
49
  end
37
50
 
38
51
  def test_has_many_conditions
39
- assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
52
+ assert_equal "Microsoft", @firm.clients_like_ms.first.name
40
53
  end
41
54
 
42
55
  def test_has_many_sql
43
- firm = Firm.find_first
44
- assert_equal "Microsoft", firm.clients_using_sql.first.name
45
- assert_equal 1, firm.clients_using_sql_count
46
- assert_equal 1, Firm.find_first.clients_using_sql_count
56
+ assert_equal "Microsoft", @firm.clients_using_sql.first.name
57
+ assert_equal 1, @firm.clients_using_sql.count
58
+ assert_equal 1, @firm.clients_using_sql.count
47
59
  end
48
60
 
49
61
  def test_has_many_counter_sql
50
- assert_equal 1, Firm.find_first.clients_using_counter_sql_count
62
+ assert_equal 1, @firm.clients_using_counter_sql.count
51
63
  end
52
64
 
53
65
  def test_has_many_queries
54
- assert Firm.find_first.has_clients?
55
- firm = Firm.find_first
56
- assert_equal 2, firm.clients_count # tests using class count
57
- firm.clients
58
- assert firm.has_clients?
59
- assert_equal 2, firm.clients_count # tests using collection length
66
+ assert !@firm.clients.loaded?
67
+ assert_deprecated 'has_clients?' do
68
+ assert_queries(1) { assert @firm.has_clients? }
69
+ end
70
+ assert !@firm.clients.loaded?
71
+ assert_deprecated 'clients_count' do
72
+ assert_queries(1) { assert_equal 2, @firm.clients_count }
73
+ end
74
+ assert !@firm.clients.loaded?
75
+ assert_queries(1) { @firm.clients.size }
76
+ assert !@firm.clients.loaded?
77
+ assert_queries(0) { @firm.clients }
78
+ assert !@firm.clients.loaded?
79
+ assert_queries(1) { @firm.clients.reload }
80
+ assert @firm.clients.loaded?
81
+ assert_queries(0) { @firm.clients.size }
82
+ assert_queries(1) { @firm.clients.count }
60
83
  end
61
84
 
62
85
  def test_has_many_dependence
63
- assert_equal 3, Client.find_all.length
64
- Firm.find_first.destroy
65
- assert_equal 1, Client.find_all.length
86
+ count = Client.count
87
+ Firm.find(:first).destroy
88
+ assert_equal count - 2, Client.count
66
89
  end
67
90
 
68
91
  uses_transaction :test_has_many_dependence_with_transaction_support_on_failure
69
92
  def test_has_many_dependence_with_transaction_support_on_failure
70
- assert_equal 3, Client.find_all.length
93
+ count = Client.count
71
94
 
72
- firm = Firm.find_first
73
- clients = firm.clients
95
+ clients = @firm.clients
74
96
  clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
75
97
 
76
- firm.destroy rescue "do nothing"
98
+ @firm.destroy rescue "do nothing"
77
99
 
78
- assert_equal 3, Client.find_all.length
100
+ assert_equal count, Client.count
79
101
  end
80
102
 
81
103
  def test_has_one_dependence
82
104
  num_accounts = Account.count
83
- firm = Firm.find(1)
84
- assert firm.has_account?
85
- firm.destroy
105
+ assert_not_nil @firm.account
106
+ @firm.destroy
86
107
  assert_equal num_accounts - 1, Account.count
87
108
  end
88
109
 
89
110
  def test_has_one_dependence_with_missing_association
90
111
  Account.destroy_all
91
- firm = Firm.find(1)
92
- assert !firm.has_account?
93
- firm.destroy
112
+ assert_nil @firm.account
113
+ @firm.destroy
94
114
  end
95
115
 
96
116
  def test_belongs_to
97
- assert_equal companies(:first_firm).name, Client.find(3).firm.name
98
- assert Client.find(3).has_firm?, "Microsoft should have a firm"
99
- # assert !Company.find(1).has_firm?, "37signals shouldn't have a firm"
117
+ client = companies(:second_client)
118
+ assert_deprecated('has_firm?') do
119
+ assert companies(:second_client).has_firm?, "Microsoft should have a firm"
120
+ end
121
+ assert_equal companies(:first_firm), client.firm, "Microsoft should have a firm"
100
122
  end
101
123
 
102
124
  def test_belongs_to_with_different_class_name
103
- assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
104
- assert Company.find(3).has_firm_with_other_name?, "Microsoft should have a firm"
125
+ assert_equal @firm, companies(:second_client).firm_with_other_name
105
126
  end
106
127
 
107
128
  def test_belongs_to_with_condition
108
- assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
109
- assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm"
129
+ assert_equal @firm, companies(:second_client).firm_with_condition
110
130
  end
111
131
 
112
132
  def test_belongs_to_equality
113
- assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm"
114
- assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm"
133
+ assert_equal @firm, companies(:second_client).firm, 'Microsoft should have 37signals as firm'
115
134
  end
116
135
 
117
136
  def test_has_one
118
- assert companies(:first_firm).account?(Account.find(1))
119
- assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
120
- assert companies(:first_firm).has_account?, "37signals should have an account"
121
- assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack"
122
- assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
137
+ assert_equal accounts(:signals37), @firm.account
138
+ assert_deprecated 'has_account?' do
139
+ assert @firm.has_account?, "37signals should have an account"
140
+ end
141
+ assert_deprecated 'firm?' do
142
+ assert accounts(:signals37).firm?(@firm), "37signals account should be able to backtrack"
143
+ end
144
+ assert_deprecated 'has_firm?' do
145
+ assert accounts(:signals37).has_firm?, "37signals account should be able to backtrack"
146
+ end
123
147
 
124
- assert !Account.find(2).has_firm?, "Unknown isn't linked"
125
- assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
148
+ assert_nil accounts(:unknown).firm, "Unknown isn't linked"
126
149
  end
127
150
 
128
- def test_has_many_dependence_on_account
151
+ def test_has_many_dependence_on_account
129
152
  num_accounts = Account.count
130
- companies(:first_firm).destroy
153
+ @firm.destroy
131
154
  assert_equal num_accounts - 1, Account.count
132
155
  end
133
156
 
134
157
  def test_find_in
135
- assert_equal Client.find(2).name, companies(:first_firm).find_in_clients(2).name
136
- assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).find_in_clients(6) }
158
+ assert_deprecated 'find_in_clients' do
159
+ assert_equal companies(:first_client), @firm.find_in_clients(2)
160
+ assert_raises(ActiveRecord::RecordNotFound) { @firm.find_in_clients(6) }
161
+ end
137
162
  end
138
163
 
139
164
  def test_force_reload
140
- firm = Firm.new("name" => "A New Firm, Inc")
141
- firm.save
142
- firm.clients.each {|c|} # forcing to load all clients
143
- assert firm.clients.empty?, "New firm shouldn't have client objects"
144
- assert !firm.has_clients?, "New firm shouldn't have clients"
145
- assert_equal 0, firm.clients_count, "New firm should have 0 clients"
146
-
147
- client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
148
- client.save
149
-
150
- assert firm.clients.empty?, "New firm should have cached no client objects"
151
- assert !firm.has_clients?, "New firm should have cached a no-clients response"
152
- assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
153
-
154
- assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
155
- assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
156
- assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
165
+ ActiveSupport::Deprecation.silence do
166
+ firm = Firm.new("name" => "A New Firm, Inc")
167
+ firm.save
168
+ firm.clients.each {|c|} # forcing to load all clients
169
+ assert firm.clients.empty?, "New firm shouldn't have client objects"
170
+ assert !firm.has_clients?, "New firm shouldn't have clients"
171
+ assert_equal 0, firm.clients_count, "New firm should have 0 clients"
172
+
173
+ client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
174
+ client.save
175
+
176
+ assert firm.clients.empty?, "New firm should have cached no client objects"
177
+ assert !firm.has_clients?, "New firm should have cached a no-clients response"
178
+ assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
179
+
180
+ assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
181
+ assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
182
+ assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
183
+ end
157
184
  end
158
185
 
159
186
  def test_included_in_collection
160
- assert companies(:first_firm).clients.include?(Client.find(2))
187
+ assert @firm.clients.include?(Client.find(2))
161
188
  end
162
189
 
163
190
  def test_build_to_collection
164
- assert_equal 1, companies(:first_firm).clients_of_firm_count
165
- new_client = companies(:first_firm).build_to_clients_of_firm("name" => "Another Client")
191
+ count = @firm.clients_of_firm.count
192
+ new_client = nil
193
+ assert_deprecated 'build_to_clients_of_firm' do
194
+ new_client = @firm.build_to_clients_of_firm("name" => "Another Client")
195
+ end
166
196
  assert_equal "Another Client", new_client.name
167
197
  assert new_client.save
168
198
 
169
- assert new_client.firm?(companies(:first_firm))
170
- assert_equal 2, companies(:first_firm).clients_of_firm_count(true)
199
+ assert_equal @firm, new_client.firm
200
+ assert_equal count + 1, @firm.clients_of_firm.count
171
201
  end
172
202
 
173
203
  def test_create_in_collection
174
- assert_equal companies(:first_firm).create_in_clients_of_firm("name" => "Another Client"), companies(:first_firm).clients_of_firm(true).last
204
+ assert_deprecated 'create_in_clients_of_firm' do
205
+ assert_equal @firm.create_in_clients_of_firm("name" => "Another Client"), @firm.clients_of_firm(true).last
206
+ end
175
207
  end
176
208
 
177
209
  def test_has_and_belongs_to_many
178
210
  david = Developer.find(1)
179
- assert david.has_projects?
180
- assert_equal 2, david.projects_count
211
+ assert_deprecated 'has_projects?' do
212
+ assert david.has_projects?
213
+ end
214
+ assert_deprecated 'projects_count' do
215
+ assert_equal 2, david.projects_count
216
+ end
181
217
 
182
218
  active_record = Project.find(1)
183
- assert active_record.has_developers?
184
- assert_equal 3, active_record.developers_count
219
+ assert_deprecated 'has_developers?' do
220
+ assert active_record.has_developers?
221
+ end
222
+ assert_deprecated 'developers_count' do
223
+ assert_equal 3, active_record.developers_count
224
+ end
185
225
  assert active_record.developers.include?(david)
186
226
  end
187
227
 
@@ -189,53 +229,59 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
189
229
  david = Developer.find(1)
190
230
  active_record = Project.find(1)
191
231
 
192
- david.remove_projects(active_record)
193
-
194
- assert_equal 1, david.projects_count
195
- assert_equal 2, active_record.developers_count
232
+ assert_deprecated do
233
+ david.remove_projects(active_record)
234
+ assert_equal 1, david.projects_count
235
+ assert_equal 2, active_record.developers_count
236
+ end
196
237
  end
197
238
 
198
239
  def test_has_and_belongs_to_many_zero
199
240
  david = Developer.find(1)
200
- david.remove_projects(Project.find_all)
201
-
202
- assert_equal 0, david.projects_count
203
- assert !david.has_projects?
241
+ assert_deprecated do
242
+ david.remove_projects Project.find_all
243
+ assert_equal 0, david.projects_count
244
+ assert !david.has_projects?
245
+ end
204
246
  end
205
247
 
206
248
  def test_has_and_belongs_to_many_adding
207
249
  jamis = Developer.find(2)
208
250
  action_controller = Project.find(2)
209
251
 
210
- jamis.add_projects(action_controller)
211
-
212
- assert_equal 2, jamis.projects_count
213
- assert_equal 2, action_controller.developers_count
252
+ assert_deprecated do
253
+ jamis.add_projects(action_controller)
254
+ assert_equal 2, jamis.projects_count
255
+ assert_equal 2, action_controller.developers_count
256
+ end
214
257
  end
215
258
 
216
259
  def test_has_and_belongs_to_many_adding_from_the_project
217
260
  jamis = Developer.find(2)
218
261
  action_controller = Project.find(2)
219
262
 
220
- action_controller.add_developers(jamis)
221
-
222
- assert_equal 2, jamis.projects_count
223
- assert_equal 2, action_controller.developers_count
263
+ assert_deprecated do
264
+ action_controller.add_developers(jamis)
265
+ assert_equal 2, jamis.projects_count
266
+ assert_equal 2, action_controller.developers_count
267
+ end
224
268
  end
225
269
 
226
270
  def test_has_and_belongs_to_many_adding_a_collection
227
271
  aredridel = Developer.new("name" => "Aredridel")
228
272
  aredridel.save
229
273
 
230
- aredridel.add_projects([ Project.find(1), Project.find(2) ])
231
- assert_equal 2, aredridel.projects_count
274
+ assert_deprecated do
275
+ aredridel.add_projects([ Project.find(1), Project.find(2) ])
276
+ assert_equal 2, aredridel.projects_count
277
+ end
232
278
  end
233
279
 
234
280
  def test_belongs_to_counter
235
281
  topic = Topic.create("title" => "Apple", "content" => "hello world")
236
282
  assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
237
283
 
238
- reply = topic.create_in_replies("title" => "I'm saying no!", "content" => "over here")
284
+ reply = assert_deprecated { topic.create_in_replies("title" => "I'm saying no!", "content" => "over here") }
239
285
  assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created"
240
286
 
241
287
  reply.destroy
@@ -312,25 +358,23 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
312
358
  end
313
359
 
314
360
  def test_has_many_find_all
315
- assert_equal 2, Firm.find_first.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length
316
- assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length
361
+ assert_deprecated 'find_all_in_clients' do
362
+ assert_equal 2, @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length
363
+ assert_equal 1, @firm.find_all_in_clients("name = 'Summit'").length
364
+ end
317
365
  end
318
366
 
319
367
  def test_has_one
320
- assert companies(:first_firm).account?(Account.find(1))
321
- assert companies(:first_firm).has_account?, "37signals should have an account"
322
- assert Account.find(1).firm?(companies(:first_firm)), "37signals account should be able to backtrack"
323
- assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
324
-
325
- assert !Account.find(2).has_firm?, "Unknown isn't linked"
326
- assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
368
+ assert_equal Account.find(1), @firm.account, "37signals should have an account"
369
+ assert_equal @firm, Account.find(1).firm, "37signals account should be able to backtrack"
370
+ assert_nil Account.find(2).firm, "Unknown isn't linked"
327
371
  end
328
372
 
329
373
  def test_has_one_build
330
374
  firm = Firm.new("name" => "GlobalMegaCorp")
331
375
  assert firm.save
332
376
 
333
- account = firm.build_account("credit_limit" => 1000)
377
+ account = firm.build_account(:credit_limit => 1000)
334
378
  assert account.save
335
379
  assert_equal account, firm.account
336
380
  end
@@ -338,7 +382,7 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
338
382
  def test_has_one_failing_build_association
339
383
  firm = Firm.new("name" => "GlobalMegaCorp")
340
384
  firm.save
341
-
385
+
342
386
  account = firm.build_account
343
387
  assert !account.save
344
388
  assert_equal "can't be empty", account.errors.on("credit_limit")