aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/json_serialization_test.rb
blob: a2226759182f5866dc6f9120418217a06755d6a8 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require "cases/helper"
require 'models/contact'
require 'models/post'
require 'models/author'
require 'models/tagging'
require 'models/tag'
require 'models/comment'

module JsonSerializationHelpers
  private

  def set_include_root_in_json(value)
    original_root_in_json = ActiveRecord::Base.include_root_in_json
    ActiveRecord::Base.include_root_in_json = value
    yield
  ensure
    ActiveRecord::Base.include_root_in_json = original_root_in_json
  end
end

class JsonSerializationTest < ActiveRecord::TestCase
  include JsonSerializationHelpers

  class NamespacedContact < Contact
    column :name, :string
  end

  def setup
    @contact = Contact.new(
      :name        => 'Konata Izumi',
      :age         => 16,
      :avatar      => 'binarydata',
      :created_at  => Time.utc(2006, 8, 1),
      :awesome     => true,
      :preferences => { :shows => 'anime' }
    )
  end

  def test_should_demodulize_root_in_json
    set_include_root_in_json(true) do
      @contact = NamespacedContact.new name: 'whatever'
      json = @contact.to_json
      assert_match %r{^\{"namespaced_contact":\{}, json
    end
  end

  def test_should_include_root_in_json
    set_include_root_in_json(true) do
      json = @contact.to_json

      assert_match %r{^\{"contact":\{}, json
      assert_match %r{"name":"Konata Izumi"}, json
      assert_match %r{"age":16}, json
      assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
      assert_match %r{"awesome":true}, json
      assert_match %r{"preferences":\{"shows":"anime"\}}, json
    end
  end

  def test_should_encode_all_encodable_attributes
    json = @contact.to_json

    assert_match %r{"name":"Konata Izumi"}, json
    assert_match %r{"age":16}, json
    assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
    assert_match %r{"awesome":true}, json
    assert_match %r{"preferences":\{"shows":"anime"\}}, json
  end

  def test_should_allow_attribute_filtering_with_only
    json = @contact.to_json(:only => [:name, :age])

    assert_match %r{"name":"Konata Izumi"}, json
    assert_match %r{"age":16}, json
    assert_no_match %r{"awesome":true}, json
    assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
    assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
  end

  def test_should_allow_attribute_filtering_with_except
    json = @contact.to_json(:except => [:name, :age])

    assert_no_match %r{"name":"Konata Izumi"}, json
    assert_no_match %r{"age":16}, json
    assert_match %r{"awesome":true}, json
    assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
    assert_match %r{"preferences":\{"shows":"anime"\}}, json
  end

  def test_methods_are_called_on_object
    # Define methods on fixture.
    def @contact.label; "Has cheezburger"; end
    def @contact.favorite_quote; "Constraints are liberating"; end

    # Single method.
    assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)

    # Both methods.
    methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
    assert_match %r{"label":"Has cheezburger"}, methods_json
    assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
  end

  def test_uses_serializable_hash_with_only_option
    def @contact.serializable_hash(options=nil)
      super(only: %w(name))
    end

    json = @contact.to_json
    assert_match %r{"name":"Konata Izumi"}, json
    assert_no_match %r{awesome}, json
    assert_no_match %r{age}, json
  end

  def test_uses_serializable_hash_with_except_option
    def @contact.serializable_hash(options=nil)
      super(except: %w(age))
    end

    json = @contact.to_json
    assert_match %r{"name":"Konata Izumi"}, json
    assert_match %r{"awesome":true}, json
    assert_no_match %r{age}, json
  end

  def test_does_not_include_inheritance_column_from_sti
    @contact = ContactSti.new(@contact.attributes)
    assert_equal 'ContactSti', @contact.type

    json = @contact.to_json
    assert_match %r{"name":"Konata Izumi"}, json
    assert_no_match %r{type}, json
    assert_no_match %r{ContactSti}, json
  end

  def test_serializable_hash_with_default_except_option_and_excluding_inheritance_column_from_sti
    @contact = ContactSti.new(@contact.attributes)
    assert_equal 'ContactSti', @contact.type

    def @contact.serializable_hash(options={})
      super({ except: %w(age) }.merge!(options))
    end

    json = @contact.to_json
    assert_match %r{"name":"Konata Izumi"}, json
    assert_no_match %r{age}, json
    assert_no_match %r{type}, json
    assert_no_match %r{ContactSti}, json
  end

  def test_serializable_hash_should_not_modify_options_in_argument
    options = { :only => :name }
    @contact.serializable_hash(options)

    assert_nil options[:except]
  end
end

class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
  fixtures :authors, :posts, :comments, :tags, :taggings

  include JsonSerializationHelpers

  def setup
    @david = authors(:david)
    @mary = authors(:mary)
  end

  def test_includes_uses_association_name
    json = @david.to_json(:include => :posts)

    assert_match %r{"posts":\[}, json

    assert_match %r{"id":1}, json
    assert_match %r{"name":"David"}, json

    assert_match %r{"author_id":1}, json
    assert_match %r{"title":"Welcome to the weblog"}, json
    assert_match %r{"body":"Such a lovely day"}, json

    assert_match %r{"title":"So I was thinking"}, json
    assert_match %r{"body":"Like I hopefully always am"}, json
  end

  def test_includes_uses_association_name_and_applies_attribute_filters
    json = @david.to_json(:include => { :posts => { :only => :title } })

    assert_match %r{"name":"David"}, json
    assert_match %r{"posts":\[}, json

    assert_match %r{"title":"Welcome to the weblog"}, json
    assert_no_match %r{"body":"Such a lovely day"}, json

    assert_match %r{"title":"So I was thinking"}, json
    assert_no_match %r{"body":"Like I hopefully always am"}, json
  end

  def test_includes_fetches_second_level_associations
    json = @david.to_json(:include => { :posts => { :include => { :comments => { :only => :body } } } })

    assert_match %r{"name":"David"}, json
    assert_match %r{"posts":\[}, json

    assert_match %r{"comments":\[}, json
    assert_match %r{\{"body":"Thank you again for the welcome"\}}, json
    assert_match %r{\{"body":"Don't think too hard"\}}, json
    assert_no_match %r{"post_id":}, json
  end

  def test_includes_fetches_nth_level_associations
    json = @david.to_json(
      :include => {
        :posts => {
          :include => {
            :taggings => {
              :include => {
                :tag => { :only => :name }
              }
            }
          }
        }
    })

    assert_match %r{"name":"David"}, json
    assert_match %r{"posts":\[}, json

    assert_match %r{"taggings":\[}, json
    assert_match %r{"tag":\{"name":"General"\}}, json
  end

  def test_includes_doesnt_merge_opts_from_base
    json = @david.to_json(
      :only => :id,
      :include => :posts
    )

    assert_match %{"title":"Welcome to the weblog"}, json
  end

  def test_should_not_call_methods_on_associations_that_dont_respond
    def @david.favorite_quote; "Constraints are liberating"; end
    json = @david.to_json(:include => :posts, :methods => :favorite_quote)

    assert !@david.posts.first.respond_to?(:favorite_quote)
    assert_match %r{"favorite_quote":"Constraints are liberating"}, json
    assert_equal %r{"favorite_quote":}.match(json).size, 1
  end

  def test_should_allow_only_option_for_list_of_authors
    set_include_root_in_json(false) do
      authors = [@david, @mary]
      assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, only: :name)
    end
  end

  def test_should_allow_except_option_for_list_of_authors
    set_include_root_in_json(false) do
      authors = [@david, @mary]
      encoded = ActiveSupport::JSON.encode(authors, except: [
        :name, :author_address_id, :author_address_extra_id,
        :organization_id, :owned_essay_id
      ])
      assert_equal %([{"id":1},{"id":2}]), encoded
    end
  end

  def test_should_allow_includes_for_list_of_authors
    authors = [@david, @mary]
    json = ActiveSupport::JSON.encode(authors,
      :only => :name,
      :include => {
        :posts => { :only => :id }
      }
    )

    ['"name":"David"', '"posts":[', '{"id":1}', '{"id":2}', '{"id":4}',
     '{"id":5}', '{"id":6}', '"name":"Mary"', '"posts":[', '{"id":7}', '{"id":9}'].each do |fragment|
      assert json.include?(fragment), json
     end
  end

  def test_should_allow_options_for_hash_of_authors
    set_include_root_in_json(true) do
      authors_hash = {
        1 => @david,
        2 => @mary
      }
      assert_equal %({"1":{"author":{"name":"David"}}}), ActiveSupport::JSON.encode(authors_hash, only: [1, :name])
    end
  end

  def test_should_be_able_to_encode_relation
    set_include_root_in_json(true) do
      authors_relation = Author.where(id: [@david.id, @mary.id])

      json = ActiveSupport::JSON.encode authors_relation, only: :name
      assert_equal '[{"author":{"name":"David"}},{"author":{"name":"Mary"}}]', json
    end
  end
end