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.
- data/CHANGELOG +400 -1
- data/README +2 -2
- data/RUNNING_UNIT_TESTS +21 -3
- data/Rakefile +55 -10
- data/lib/active_record.rb +10 -4
- data/lib/active_record/acts/list.rb +15 -4
- data/lib/active_record/acts/nested_set.rb +11 -12
- data/lib/active_record/acts/tree.rb +13 -14
- data/lib/active_record/aggregations.rb +46 -22
- data/lib/active_record/associations.rb +213 -162
- data/lib/active_record/associations/association_collection.rb +45 -15
- data/lib/active_record/associations/association_proxy.rb +32 -13
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +18 -18
- data/lib/active_record/associations/has_many_association.rb +37 -17
- data/lib/active_record/associations/has_many_through_association.rb +120 -30
- data/lib/active_record/associations/has_one_association.rb +1 -1
- data/lib/active_record/attribute_methods.rb +75 -0
- data/lib/active_record/base.rb +282 -203
- data/lib/active_record/calculations.rb +95 -54
- data/lib/active_record/callbacks.rb +13 -24
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +12 -1
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb.rej +21 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +30 -4
- data/lib/active_record/connection_adapters/abstract/quoting.rb +16 -9
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +121 -37
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +55 -23
- data/lib/active_record/connection_adapters/abstract_adapter.rb +8 -0
- data/lib/active_record/connection_adapters/db2_adapter.rb +1 -11
- data/lib/active_record/connection_adapters/firebird_adapter.rb +364 -50
- data/lib/active_record/connection_adapters/frontbase_adapter.rb +861 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +86 -33
- data/lib/active_record/connection_adapters/openbase_adapter.rb +4 -3
- data/lib/active_record/connection_adapters/oracle_adapter.rb +151 -127
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +125 -48
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +38 -10
- data/lib/active_record/connection_adapters/sqlserver_adapter.rb +183 -155
- data/lib/active_record/connection_adapters/sybase_adapter.rb +190 -212
- data/lib/active_record/deprecated_associations.rb +24 -10
- data/lib/active_record/deprecated_finders.rb +4 -1
- data/lib/active_record/fixtures.rb +37 -23
- data/lib/active_record/locking/optimistic.rb +106 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/migration.rb +8 -5
- data/lib/active_record/observer.rb +73 -34
- data/lib/active_record/reflection.rb +21 -7
- data/lib/active_record/schema_dumper.rb +33 -5
- data/lib/active_record/timestamp.rb +23 -34
- data/lib/active_record/transactions.rb +37 -30
- data/lib/active_record/validations.rb +46 -30
- data/lib/active_record/vendor/mysql.rb +20 -5
- data/lib/active_record/version.rb +2 -2
- data/lib/active_record/wrappings.rb +1 -2
- data/lib/active_record/xml_serialization.rb +308 -0
- data/test/aaa_create_tables_test.rb +5 -1
- data/test/abstract_unit.rb +18 -8
- data/test/{active_schema_mysql.rb → active_schema_test_mysql.rb} +2 -2
- data/test/adapter_test.rb +9 -7
- data/test/adapter_test_sqlserver.rb +81 -0
- data/test/aggregations_test.rb +29 -0
- data/test/{association_callbacks_test.rb → associations/callbacks_test.rb} +10 -8
- data/test/{associations_cascaded_eager_loading_test.rb → associations/cascaded_eager_loading_test.rb} +35 -3
- data/test/{associations_go_eager_test.rb → associations/eager_test.rb} +36 -2
- data/test/{associations_extensions_test.rb → associations/extension_test.rb} +5 -0
- data/test/{associations_join_model_test.rb → associations/join_model_test.rb} +118 -8
- data/test/associations_test.rb +339 -45
- data/test/attribute_methods_test.rb +49 -0
- data/test/base_test.rb +321 -67
- data/test/calculations_test.rb +48 -10
- data/test/callbacks_test.rb +13 -0
- data/test/connection_test_firebird.rb +8 -0
- data/test/connections/native_db2/connection.rb +18 -17
- data/test/connections/native_firebird/connection.rb +19 -17
- data/test/connections/native_frontbase/connection.rb +27 -0
- data/test/connections/native_mysql/connection.rb +18 -15
- data/test/connections/native_openbase/connection.rb +14 -15
- data/test/connections/native_oracle/connection.rb +16 -12
- data/test/connections/native_postgresql/connection.rb +16 -17
- data/test/connections/native_sqlite/connection.rb +3 -6
- data/test/connections/native_sqlite3/connection.rb +3 -6
- data/test/connections/native_sqlserver/connection.rb +16 -17
- data/test/connections/native_sqlserver_odbc/connection.rb +18 -19
- data/test/connections/native_sybase/connection.rb +16 -17
- data/test/datatype_test_postgresql.rb +52 -0
- data/test/defaults_test.rb +52 -10
- data/test/deprecated_associations_test.rb +151 -107
- data/test/deprecated_finder_test.rb +83 -66
- data/test/empty_date_time_test.rb +25 -0
- data/test/finder_test.rb +118 -11
- data/test/fixtures/accounts.yml +6 -1
- data/test/fixtures/author.rb +27 -4
- data/test/fixtures/categorizations.yml +8 -2
- data/test/fixtures/category.rb +1 -2
- data/test/fixtures/comments.yml +0 -6
- data/test/fixtures/companies.yml +6 -1
- data/test/fixtures/company.rb +23 -1
- data/test/fixtures/company_in_module.rb +8 -10
- data/test/fixtures/customer.rb +2 -2
- data/test/fixtures/customers.yml +9 -0
- data/test/fixtures/db_definitions/db2.drop.sql +1 -0
- data/test/fixtures/db_definitions/db2.sql +9 -0
- data/test/fixtures/db_definitions/firebird.drop.sql +3 -0
- data/test/fixtures/db_definitions/firebird.sql +13 -1
- data/test/fixtures/db_definitions/frontbase.drop.sql +31 -0
- data/test/fixtures/db_definitions/frontbase.sql +262 -0
- data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
- data/test/fixtures/db_definitions/frontbase2.sql +4 -0
- data/test/fixtures/db_definitions/mysql.drop.sql +1 -0
- data/test/fixtures/db_definitions/mysql.sql +23 -14
- data/test/fixtures/db_definitions/openbase.sql +13 -1
- data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
- data/test/fixtures/db_definitions/oracle.sql +29 -2
- data/test/fixtures/db_definitions/postgresql.drop.sql +3 -1
- data/test/fixtures/db_definitions/postgresql.sql +13 -3
- data/test/fixtures/db_definitions/schema.rb +29 -1
- data/test/fixtures/db_definitions/sqlite.drop.sql +1 -0
- data/test/fixtures/db_definitions/sqlite.sql +12 -3
- data/test/fixtures/db_definitions/sqlserver.drop.sql +3 -0
- data/test/fixtures/db_definitions/sqlserver.sql +35 -0
- data/test/fixtures/db_definitions/sybase.drop.sql +2 -0
- data/test/fixtures/db_definitions/sybase.sql +13 -4
- data/test/fixtures/developer.rb +12 -0
- data/test/fixtures/edge.rb +5 -0
- data/test/fixtures/edges.yml +6 -0
- data/test/fixtures/funny_jokes.yml +3 -7
- data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
- data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
- data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
- data/test/fixtures/mixin.rb +15 -0
- data/test/fixtures/mixins.yml +38 -0
- data/test/fixtures/post.rb +3 -2
- data/test/fixtures/project.rb +3 -1
- data/test/fixtures/topic.rb +6 -1
- data/test/fixtures/topics.yml +4 -4
- data/test/fixtures/vertex.rb +9 -0
- data/test/fixtures/vertices.yml +4 -0
- data/test/fixtures_test.rb +45 -0
- data/test/inheritance_test.rb +67 -6
- data/test/lifecycle_test.rb +40 -19
- data/test/locking_test.rb +170 -26
- data/test/method_scoping_test.rb +2 -2
- data/test/migration_test.rb +387 -110
- data/test/migration_test_firebird.rb +124 -0
- data/test/mixin_nested_set_test.rb +14 -2
- data/test/mixin_test.rb +56 -18
- data/test/modules_test.rb +8 -2
- data/test/multiple_db_test.rb +2 -2
- data/test/pk_test.rb +1 -0
- data/test/reflection_test.rb +8 -2
- data/test/schema_authorization_test_postgresql.rb +75 -0
- data/test/schema_dumper_test.rb +40 -4
- data/test/table_name_test_sqlserver.rb +23 -0
- data/test/threaded_connections_test.rb +19 -16
- data/test/transactions_test.rb +86 -72
- data/test/validations_test.rb +126 -56
- data/test/xml_serialization_test.rb +125 -0
- metadata +45 -11
- 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
|
-
|
8
|
-
|
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
|
-
|
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
|
-
|
8
|
-
|
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
|
-
|
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
|
-
|
8
|
-
|
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
|
-
|
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
|
data/test/defaults_test.rb
CHANGED
@@ -1,18 +1,60 @@
|
|
1
1
|
require 'abstract_unit'
|
2
2
|
require 'fixtures/default'
|
3
|
+
require 'fixtures/entrant'
|
3
4
|
|
4
|
-
class
|
5
|
-
|
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
|
9
|
-
assert_equal
|
10
|
-
assert_instance_of
|
11
|
-
assert_equal
|
12
|
-
|
13
|
-
|
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,
|
36
|
+
assert_equal 2, @firm.clients.length
|
24
37
|
end
|
25
38
|
|
26
39
|
def test_has_many_orders
|
27
|
-
assert_equal "Summit",
|
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",
|
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",
|
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",
|
52
|
+
assert_equal "Microsoft", @firm.clients_like_ms.first.name
|
40
53
|
end
|
41
54
|
|
42
55
|
def test_has_many_sql
|
43
|
-
|
44
|
-
assert_equal
|
45
|
-
assert_equal 1, firm.
|
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,
|
62
|
+
assert_equal 1, @firm.clients_using_counter_sql.count
|
51
63
|
end
|
52
64
|
|
53
65
|
def test_has_many_queries
|
54
|
-
assert
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
assert firm.
|
59
|
-
|
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
|
-
|
64
|
-
Firm.
|
65
|
-
assert_equal
|
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
|
-
|
93
|
+
count = Client.count
|
71
94
|
|
72
|
-
|
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
|
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
|
84
|
-
|
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
|
92
|
-
|
93
|
-
firm.destroy
|
112
|
+
assert_nil @firm.account
|
113
|
+
@firm.destroy
|
94
114
|
end
|
95
115
|
|
96
116
|
def test_belongs_to
|
97
|
-
|
98
|
-
|
99
|
-
|
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
|
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
|
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
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
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
|
-
|
153
|
+
@firm.destroy
|
131
154
|
assert_equal num_accounts - 1, Account.count
|
132
155
|
end
|
133
156
|
|
134
157
|
def test_find_in
|
135
|
-
|
136
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
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
|
187
|
+
assert @firm.clients.include?(Client.find(2))
|
161
188
|
end
|
162
189
|
|
163
190
|
def test_build_to_collection
|
164
|
-
|
165
|
-
new_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
|
-
|
170
|
-
assert_equal
|
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
|
-
|
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
|
-
|
180
|
-
|
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
|
-
|
184
|
-
|
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
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
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
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
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
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
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
|
-
|
231
|
-
|
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
|
-
|
316
|
-
|
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
|
-
|
321
|
-
|
322
|
-
|
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(
|
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")
|