diff options
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/adapters/mysql2/json_test.rb | 15 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/json_test.rb | 27 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/inheritance_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/json_attribute_test.rb | 33 | ||||
-rw-r--r-- | activerecord/test/cases/json_shared_test_cases.rb | 68 | ||||
-rw-r--r-- | activerecord/test/cases/schema_dumper_test.rb | 18 | ||||
-rw-r--r-- | activerecord/test/models/company.rb | 1 |
8 files changed, 99 insertions, 72 deletions
diff --git a/activerecord/test/cases/adapters/mysql2/json_test.rb b/activerecord/test/cases/adapters/mysql2/json_test.rb index d311ffb703..26c69edc7b 100644 --- a/activerecord/test/cases/adapters/mysql2/json_test.rb +++ b/activerecord/test/cases/adapters/mysql2/json_test.rb @@ -7,20 +7,13 @@ if ActiveRecord::Base.connection.supports_json? self.use_transactional_tests = false def setup - @connection = ActiveRecord::Base.connection - begin - @connection.create_table("json_data_type") do |t| - t.json "payload" - t.json "settings" - end + super + @connection.create_table("json_data_type") do |t| + t.json "payload" + t.json "settings" end end - def teardown - @connection.drop_table :json_data_type, if_exists: true - JsonDataType.reset_column_information - end - private def column_type :json diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb index 4eeb563781..6aa60630c2 100644 --- a/activerecord/test/cases/adapters/postgresql/json_test.rb +++ b/activerecord/test/cases/adapters/postgresql/json_test.rb @@ -5,35 +5,26 @@ module PostgresqlJSONSharedTestCases include JSONSharedTestCases def setup - @connection = ActiveRecord::Base.connection - begin - @connection.create_table("json_data_type") do |t| - t.public_send column_type, "payload", default: {} # t.json 'payload', default: {} - t.public_send column_type, "settings" # t.json 'settings' - t.public_send column_type, "objects", array: true # t.json 'objects', array: true - end - rescue ActiveRecord::StatementInvalid - skip "do not test on PostgreSQL without #{column_type} type." + super + @connection.create_table("json_data_type") do |t| + t.public_send column_type, "payload", default: {} # t.json 'payload', default: {} + t.public_send column_type, "settings" # t.json 'settings' + t.public_send column_type, "objects", array: true # t.json 'objects', array: true end - end - - def teardown - @connection.drop_table :json_data_type, if_exists: true - JsonDataType.reset_column_information + rescue ActiveRecord::StatementInvalid + skip "do not test on PostgreSQL without #{column_type} type." end def test_default @connection.add_column "json_data_type", "permissions", column_type, default: { "users": "read", "posts": ["read", "write"] } - JsonDataType.reset_column_information + klass.reset_column_information assert_equal({ "users" => "read", "posts" => ["read", "write"] }, JsonDataType.column_defaults["permissions"]) assert_equal({ "users" => "read", "posts" => ["read", "write"] }, JsonDataType.new.permissions) - ensure - JsonDataType.reset_column_information end def test_deserialize_with_array - x = JsonDataType.new(objects: ["foo" => "bar"]) + x = klass.new(objects: ["foo" => "bar"]) assert_equal ["foo" => "bar"], x.objects x.save! assert_equal ["foo" => "bar"], x.objects diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a794eba691..590d3642bd 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1355,7 +1355,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase Client.create(client_of: firm.id, name: "SmallTime Inc.") # only one of two clients is included in the association due to the :conditions key assert_equal 2, Client.where(client_of: firm.id).size - assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size + assert_equal 1, firm.dependent_hash_conditional_clients_of_firm.size firm.destroy # only the correctly associated client should have been deleted assert_equal 1, Client.where(client_of: firm.id).size diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index b4bbdc6dad..fb5a7bcc31 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -1,6 +1,7 @@ require "cases/helper" require "models/author" require "models/company" +require "models/membership" require "models/person" require "models/post" require "models/project" @@ -29,7 +30,7 @@ end class InheritanceTest < ActiveRecord::TestCase include InheritanceTestHelper - fixtures :companies, :projects, :subscribers, :accounts, :vegetables + fixtures :companies, :projects, :subscribers, :accounts, :vegetables, :memberships def test_class_with_store_full_sti_class_returns_full_name with_store_full_sti_class do @@ -435,6 +436,10 @@ class InheritanceTest < ActiveRecord::TestCase assert_nothing_raised { Company.of_first_firm } assert_nothing_raised { Client.of_first_firm } end + + def test_inheritance_with_default_scope + assert_equal 1, SelectedMembership.count(:all) + end end class InheritanceComputeTypeTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/json_attribute_test.rb b/activerecord/test/cases/json_attribute_test.rb new file mode 100644 index 0000000000..e5848b45f8 --- /dev/null +++ b/activerecord/test/cases/json_attribute_test.rb @@ -0,0 +1,33 @@ +require "cases/helper" +require "cases/json_shared_test_cases" + +class JsonAttributeTest < ActiveRecord::TestCase + include JSONSharedTestCases + self.use_transactional_tests = false + + class JsonDataTypeOnText < ActiveRecord::Base + self.table_name = "json_data_type" + + attribute :payload, :json + attribute :settings, :json + + store_accessor :settings, :resolution + end + + def setup + super + @connection.create_table("json_data_type") do |t| + t.text "payload" + t.text "settings" + end + end + + private + def column_type + :text + end + + def klass + JsonDataTypeOnText + end +end diff --git a/activerecord/test/cases/json_shared_test_cases.rb b/activerecord/test/cases/json_shared_test_cases.rb index ef5ca86874..5aa41b9d6d 100644 --- a/activerecord/test/cases/json_shared_test_cases.rb +++ b/activerecord/test/cases/json_shared_test_cases.rb @@ -9,12 +9,21 @@ module JSONSharedTestCases store_accessor :settings, :resolution end + def setup + @connection = ActiveRecord::Base.connection + end + + def teardown + @connection.drop_table :json_data_type, if_exists: true + klass.reset_column_information + end + def test_column - column = JsonDataType.columns_hash["payload"] + column = klass.columns_hash["payload"] assert_equal column_type, column.type assert_equal column_type.to_s, column.sql_type - type = JsonDataType.type_for_attribute("payload") + type = klass.type_for_attribute("payload") assert_not type.binary? end @@ -22,8 +31,8 @@ module JSONSharedTestCases @connection.change_table("json_data_type") do |t| t.public_send column_type, "users" end - JsonDataType.reset_column_information - column = JsonDataType.columns_hash["users"] + klass.reset_column_information + column = klass.columns_hash["users"] assert_equal column_type, column.type assert_equal column_type.to_s, column.sql_type end @@ -34,7 +43,7 @@ module JSONSharedTestCases end def test_cast_value_on_write - x = JsonDataType.new(payload: { "string" => "foo", :symbol => :bar }) + x = klass.new(payload: { "string" => "foo", :symbol => :bar }) assert_equal({ "string" => "foo", :symbol => :bar }, x.payload_before_type_cast) assert_equal({ "string" => "foo", "symbol" => "bar" }, x.payload) x.save! @@ -42,7 +51,7 @@ module JSONSharedTestCases end def test_type_cast_json - type = JsonDataType.type_for_attribute("payload") + type = klass.type_for_attribute("payload") data = '{"a_key":"a_value"}' hash = type.deserialize(data) @@ -56,75 +65,75 @@ module JSONSharedTestCases def test_rewrite @connection.execute(%q|insert into json_data_type (payload) VALUES ('{"k":"v"}')|) - x = JsonDataType.first + x = klass.first x.payload = { '"a\'' => "b" } assert x.save! end def test_select @connection.execute(%q|insert into json_data_type (payload) VALUES ('{"k":"v"}')|) - x = JsonDataType.first + x = klass.first assert_equal({ "k" => "v" }, x.payload) end def test_select_multikey @connection.execute(%q|insert into json_data_type (payload) VALUES ('{"k1":"v1", "k2":"v2", "k3":[1,2,3]}')|) - x = JsonDataType.first + x = klass.first assert_equal({ "k1" => "v1", "k2" => "v2", "k3" => [1, 2, 3] }, x.payload) end def test_null_json @connection.execute("insert into json_data_type (payload) VALUES(null)") - x = JsonDataType.first + x = klass.first assert_nil(x.payload) end def test_select_nil_json_after_create - json = JsonDataType.create!(payload: nil) - x = JsonDataType.where(payload: nil).first + json = klass.create!(payload: nil) + x = klass.where(payload: nil).first assert_equal(json, x) end def test_select_nil_json_after_update - json = JsonDataType.create!(payload: "foo") - x = JsonDataType.where(payload: nil).first + json = klass.create!(payload: "foo") + x = klass.where(payload: nil).first assert_nil(x) json.update_attributes(payload: nil) - x = JsonDataType.where(payload: nil).first + x = klass.where(payload: nil).first assert_equal(json.reload, x) end def test_select_array_json_value @connection.execute(%q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|) - x = JsonDataType.first + x = klass.first assert_equal(["v0", { "k1" => "v1" }], x.payload) end def test_rewrite_array_json_value @connection.execute(%q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|) - x = JsonDataType.first + x = klass.first x.payload = ["v1", { "k2" => "v2" }, "v3"] assert x.save! end def test_with_store_accessors - x = JsonDataType.new(resolution: "320×480") + x = klass.new(resolution: "320×480") assert_equal "320×480", x.resolution x.save! - x = JsonDataType.first + x = klass.first assert_equal "320×480", x.resolution x.resolution = "640×1136" x.save! - x = JsonDataType.first + x = klass.first assert_equal "640×1136", x.resolution end def test_duplication_with_store_accessors - x = JsonDataType.new(resolution: "320×480") + x = klass.new(resolution: "320×480") assert_equal "320×480", x.resolution y = x.dup @@ -132,7 +141,7 @@ module JSONSharedTestCases end def test_yaml_round_trip_with_store_accessors - x = JsonDataType.new(resolution: "320×480") + x = klass.new(resolution: "320×480") assert_equal "320×480", x.resolution y = YAML.load(YAML.dump(x)) @@ -140,7 +149,7 @@ module JSONSharedTestCases end def test_changes_in_place - json = JsonDataType.new + json = klass.new assert_not json.changed? json.payload = { "one" => "two" } @@ -162,7 +171,7 @@ module JSONSharedTestCases def test_changes_in_place_with_ruby_object time = Time.now.utc - json = JsonDataType.create!(payload: time) + json = klass.create!(payload: time) json.reload assert_not json.changed? @@ -172,17 +181,22 @@ module JSONSharedTestCases end def test_assigning_string_literal - json = JsonDataType.create!(payload: "foo") + json = klass.create!(payload: "foo") assert_equal "foo", json.payload end def test_assigning_number - json = JsonDataType.create!(payload: 1.234) + json = klass.create!(payload: 1.234) assert_equal 1.234, json.payload end def test_assigning_boolean - json = JsonDataType.create!(payload: true) + json = klass.create!(payload: true) assert_equal true, json.payload end + + private + def klass + JsonDataType + end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index cb8d449ba9..417c6f4832 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -116,32 +116,22 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_schema_dump_includes_limit_constraint_for_integer_columns output = dump_all_table_schema([/^(?!integer_limits)/]) - assert_match %r{c_int_without_limit}, output + assert_match %r{"c_int_without_limit"(?!.*limit)}, output if current_adapter?(:PostgreSQLAdapter) - assert_no_match %r{c_int_without_limit.*limit:}, output - assert_match %r{c_int_1.*limit: 2}, output assert_match %r{c_int_2.*limit: 2}, output # int 3 is 4 bytes in postgresql - assert_match %r{c_int_3.*}, output - assert_no_match %r{c_int_3.*limit:}, output - - assert_match %r{c_int_4.*}, output - assert_no_match %r{c_int_4.*limit:}, output + assert_match %r{"c_int_3"(?!.*limit)}, output + assert_match %r{"c_int_4"(?!.*limit)}, output elsif current_adapter?(:Mysql2Adapter) - assert_match %r{c_int_without_limit"$}, output - assert_match %r{c_int_1.*limit: 1}, output assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_3.*limit: 3}, output - assert_match %r{c_int_4.*}, output - assert_no_match %r{c_int_4.*:limit}, output + assert_match %r{"c_int_4"(?!.*limit)}, output elsif current_adapter?(:SQLite3Adapter) - assert_no_match %r{c_int_without_limit.*limit:}, output - assert_match %r{c_int_1.*limit: 1}, output assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_3.*limit: 3}, output diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index d269a95e8c..c6a5bf1c92 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -175,6 +175,7 @@ end class ExclusivelyDependentFirm < Company has_one :account, foreign_key: "firm_id", dependent: :delete has_many :dependent_sanitized_conditional_clients_of_firm, -> { order("id").where("name = 'BigShot Inc.'") }, foreign_key: "client_of", class_name: "Client", dependent: :delete_all + has_many :dependent_hash_conditional_clients_of_firm, -> { order("id").where(name: "BigShot Inc.") }, foreign_key: "client_of", class_name: "Client", dependent: :delete_all has_many :dependent_conditional_clients_of_firm, -> { order("id").where("name = ?", "BigShot Inc.") }, foreign_key: "client_of", class_name: "Client", dependent: :delete_all end |