diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-12-07 04:32:05 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-07 04:32:05 +0900 |
commit | 19d2fab6ff11222757a06bc1bd24f3c5df2d10d3 (patch) | |
tree | 2b08963ed001886df485bb9a424bd1a532b3a846 /activerecord/test/cases | |
parent | 5d337fe386d37918246886e2600b586471067cf6 (diff) | |
parent | b34838cbe29434212eb81ca1e31bb0ab0765e32a (diff) | |
download | rails-19d2fab6ff11222757a06bc1bd24f3c5df2d10d3.tar.gz rails-19d2fab6ff11222757a06bc1bd24f3c5df2d10d3.tar.bz2 rails-19d2fab6ff11222757a06bc1bd24f3c5df2d10d3.zip |
Merge pull request #31344 from kamipo/use_string_instead_of_text
Use `:string` instead of `:text` for `JsonAttributeTest`
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/json_attribute_test.rb | 46 | ||||
-rw-r--r-- | activerecord/test/cases/json_shared_test_cases.rb | 29 |
2 files changed, 43 insertions, 32 deletions
diff --git a/activerecord/test/cases/json_attribute_test.rb b/activerecord/test/cases/json_attribute_test.rb index a6fd4f34dc..afc39d0420 100644 --- a/activerecord/test/cases/json_attribute_test.rb +++ b/activerecord/test/cases/json_attribute_test.rb @@ -3,35 +3,33 @@ require "cases/helper" require "cases/json_shared_test_cases" -if ActiveRecord::Base.connection.supports_json? - class JsonAttributeTest < ActiveRecord::TestCase - include JSONSharedTestCases - self.use_transactional_tests = false +class JsonAttributeTest < ActiveRecord::TestCase + include JSONSharedTestCases + self.use_transactional_tests = false - class JsonDataTypeOnText < ActiveRecord::Base - self.table_name = "json_data_type" + class JsonDataTypeOnText < ActiveRecord::Base + self.table_name = "json_data_type" - attribute :payload, :json - attribute :settings, :json + attribute :payload, :json + attribute :settings, :json - store_accessor :settings, :resolution - end + store_accessor :settings, :resolution + end - def setup - super - @connection.create_table("json_data_type") do |t| - t.text "payload" - t.text "settings" - end + def setup + super + @connection.create_table("json_data_type") do |t| + t.string "payload" + t.string "settings" end + end - private - def column_type - :text - end + private + def column_type + :string + end - def klass - JsonDataTypeOnText - end - 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 56ec8c8a82..b0c0f2c283 100644 --- a/activerecord/test/cases/json_shared_test_cases.rb +++ b/activerecord/test/cases/json_shared_test_cases.rb @@ -23,7 +23,7 @@ module JSONSharedTestCases def test_column column = klass.columns_hash["payload"] assert_equal column_type, column.type - assert_equal column_type.to_s, column.sql_type + assert_type_match column_type, column.sql_type type = klass.type_for_attribute("payload") assert_not type.binary? @@ -36,7 +36,7 @@ module JSONSharedTestCases klass.reset_column_information column = klass.columns_hash["users"] assert_equal column_type, column.type - assert_equal column_type.to_s, column.sql_type + assert_type_match column_type, column.sql_type end def test_schema_dumping @@ -66,26 +66,26 @@ module JSONSharedTestCases end def test_rewrite - @connection.execute(%q|insert into json_data_type (payload) VALUES ('{"k":"v"}')|) + @connection.execute(insert_statement_per_database('{"k":"v"}')) 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"}')|) + @connection.execute(insert_statement_per_database('{"k":"v"}')) 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]}')|) + @connection.execute(insert_statement_per_database('{"k1":"v1", "k2":"v2", "k3":[1,2,3]}')) 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)") + @connection.execute(insert_statement_per_database("null")) x = klass.first assert_nil(x.payload) end @@ -107,13 +107,13 @@ module JSONSharedTestCases end def test_select_array_json_value - @connection.execute(%q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')|) + @connection.execute(insert_statement_per_database('["v0",{"k1":"v1"}]')) 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"}]')|) + @connection.execute(insert_statement_per_database('["v0",{"k1":"v1"}]')) x = klass.first x.payload = ["v1", { "k2" => "v2" }, "v3"] assert x.save! @@ -253,4 +253,17 @@ module JSONSharedTestCases def klass JsonDataType end + + def assert_type_match(type, sql_type) + native_type = ActiveRecord::Base.connection.native_database_types[type][:name] + assert_match %r(\A#{native_type}\b), sql_type + end + + def insert_statement_per_database(values) + if current_adapter?(:OracleAdapter) + "insert into json_data_type (id, payload) VALUES (json_data_type_seq.nextval, '#{values}')" + else + "insert into json_data_type (payload) VALUES ('#{values}')" + end + end end |