aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/json_shared_test_cases.rb
blob: 9b7980350305241f99e832b2e7342e1b992df748 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# frozen_string_literal: true

require "support/schema_dumping_helper"

module JSONSharedTestCases
  include SchemaDumpingHelper

  class JsonDataType < ActiveRecord::Base
    self.table_name = "json_data_type"

    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 = klass.columns_hash["payload"]
    assert_equal column_type, column.type
    assert_type_match column_type, column.sql_type

    type = klass.type_for_attribute("payload")
    assert_not_predicate type, :binary?
  end

  def test_change_table_supports_json
    @connection.change_table("json_data_type") do |t|
      t.public_send column_type, "users"
    end
    klass.reset_column_information
    column = klass.columns_hash["users"]
    assert_equal column_type, column.type
    assert_type_match column_type, column.sql_type
  end

  def test_schema_dumping
    output = dump_table_schema("json_data_type")
    assert_match(/t\.#{column_type}\s+"settings"/, output)
  end

  def test_cast_value_on_write
    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!
    assert_equal({ "string" => "foo", "symbol" => "bar" }, x.reload.payload)
  end

  def test_type_cast_json
    type = klass.type_for_attribute("payload")

    data = '{"a_key":"a_value"}'
    hash = type.deserialize(data)
    assert_equal({ "a_key" => "a_value" }, hash)
    assert_equal({ "a_key" => "a_value" }, type.deserialize(data))

    assert_equal({}, type.deserialize("{}"))
    assert_equal({ "key" => nil }, type.deserialize('{"key": null}'))
    assert_equal({ "c" => "}", '"a"' => 'b "a b' }, type.deserialize(%q({"c":"}", "\"a\"":"b \"a b"})))
  end

  def test_rewrite
    @connection.execute(insert_statement_per_database('{"k":"v"}'))
    x = klass.first
    x.payload = { '"a\'' => "b" }
    assert x.save!
  end

  def test_select
    @connection.execute(insert_statement_per_database('{"k":"v"}'))
    x = klass.first
    assert_equal({ "k" => "v" }, x.payload)
  end

  def test_select_multikey
    @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_statement_per_database("null"))
    x = klass.first
    assert_nil(x.payload)
  end

  def test_select_nil_json_after_create
    json = klass.create!(payload: nil)
    x = klass.where(payload: nil).first
    assert_equal(json, x)
  end

  def test_select_nil_json_after_update
    json = klass.create!(payload: "foo")
    x = klass.where(payload: nil).first
    assert_nil(x)

    json.update(payload: nil)
    x = klass.where(payload: nil).first
    assert_equal(json.reload, x)
  end

  def test_select_array_json_value
    @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(insert_statement_per_database('["v0",{"k1":"v1"}]'))
    x = klass.first
    x.payload = ["v1", { "k2" => "v2" }, "v3"]
    assert x.save!
  end

  def test_with_store_accessors
    x = klass.new(resolution: "320×480")
    assert_equal "320×480", x.resolution

    x.save!
    x = klass.first
    assert_equal "320×480", x.resolution

    x.resolution = "640×1136"
    x.save!

    x = klass.first
    assert_equal "640×1136", x.resolution
  end

  def test_duplication_with_store_accessors
    x = klass.new(resolution: "320×480")
    assert_equal "320×480", x.resolution

    y = x.dup
    assert_equal "320×480", y.resolution
  end

  def test_yaml_round_trip_with_store_accessors
    x = klass.new(resolution: "320×480")
    assert_equal "320×480", x.resolution

    y = YAML.load(YAML.dump(x))
    assert_equal "320×480", y.resolution
  end

  def test_changes_in_place
    json = klass.new
    assert_not_predicate json, :changed?

    json.payload = { "one" => "two" }
    assert_predicate json, :changed?
    assert_predicate json, :payload_changed?

    json.save!
    assert_not_predicate json, :changed?

    json.payload["three"] = "four"
    assert_predicate json, :payload_changed?

    json.save!
    json.reload

    assert_equal({ "one" => "two", "three" => "four" }, json.payload)
    assert_not_predicate json, :changed?
  end

  def test_changes_in_place_ignores_key_order
    json = klass.new
    assert_not_predicate json, :changed?

    json.payload = { "three" => "four", "one" => "two" }
    json.save!
    json.reload

    json.payload = { "three" => "four", "one" => "two" }
    assert_not_predicate json, :changed?

    json.payload = [{ "three" => "four", "one" => "two" }, { "seven" => "eight", "five" => "six" }]
    json.save!
    json.reload

    json.payload = [{ "three" => "four", "one" => "two" }, { "seven" => "eight", "five" => "six" }]
    assert_not_predicate json, :changed?
  end

  def test_changes_in_place_with_ruby_object
    time = Time.now.utc
    json = klass.create!(payload: time)

    json.reload
    assert_not_predicate json, :changed?

    json.payload = time
    assert_not_predicate json, :changed?
  end

  def test_assigning_string_literal
    json = klass.create!(payload: "foo")
    assert_equal "foo", json.payload
  end

  def test_assigning_number
    json = klass.create!(payload: 1.234)
    assert_equal 1.234, json.payload
  end

  def test_assigning_boolean
    json = klass.create!(payload: true)
    assert_equal true, json.payload
  end

  def test_not_compatible_with_serialize_json
    new_klass = Class.new(klass) do
      serialize :payload, JSON
    end
    assert_raises(ActiveRecord::AttributeMethods::Serialization::ColumnNotSerializableError) do
      new_klass.new
    end
  end

  class MySettings
    def initialize(hash); @hash = hash end
    def to_hash; @hash end
    def self.load(hash); new(hash) end
    def self.dump(object); object.to_hash end
  end

  def test_json_with_serialized_attributes
    new_klass = Class.new(klass) do
      serialize :settings, MySettings
    end

    new_klass.create!(settings: MySettings.new("one" => "two"))
    record = new_klass.first

    assert_instance_of MySettings, record.settings
    assert_equal({ "one" => "two" }, record.settings.to_hash)

    record.settings = MySettings.new("three" => "four")
    record.save!

    assert_equal({ "three" => "four" }, record.reload.settings.to_hash)
  end

  private
    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