Skip to content

Commit d96f25e

Browse files
authored
Merge pull request rails#44859 from ghousemohamed/extend-as-json-serilization-test-for-activemodel
Extend json_serilization test in Active Model for the `as_json` method
2 parents 7ebd5f6 + 6e9bdb7 commit d96f25e

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

activemodel/test/cases/serializers/json_serialization_test.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
require "cases/helper"
44
require "models/contact"
5+
require "models/address"
56
require "active_support/core_ext/object/instance_variables"
67

78
class JsonSerializationTest < ActiveModel::TestCase
89
def setup
910
@contact = Contact.new
1011
@contact.name = "Konata Izumi"
12+
@contact.address = Address.new(address_line: "Cantonment Road", city: "Trichy", state: "Tamil Nadu", country: "India")
1113
@contact.age = 16
1214
@contact.created_at = Time.utc(2006, 8, 1)
1315
@contact.awesome = true
@@ -145,6 +147,82 @@ def @contact.favorite_quote; "Constraints are liberating"; end
145147
Contact.include_root_in_json = original_include_root_in_json
146148
end
147149

150+
test "as_json should work with root option set to true" do
151+
json = @contact.as_json(root: true)
152+
153+
assert_kind_of Hash, json
154+
assert_kind_of Hash, json["contact"]
155+
%w(name age created_at awesome preferences).each do |field|
156+
assert_equal @contact.public_send(field).as_json, json["contact"][field]
157+
end
158+
end
159+
160+
test "as_json should work with root option set to string" do
161+
json = @contact.as_json(root: "connection")
162+
163+
assert_kind_of Hash, json
164+
assert_kind_of Hash, json["connection"]
165+
%w(name age created_at awesome preferences).each do |field|
166+
assert_equal @contact.public_send(field).as_json, json["connection"][field]
167+
end
168+
end
169+
170+
test "as_json should allow attribute filtering with except" do
171+
json = @contact.as_json(except: [:age, :created_at, :awesome, :preferences])
172+
173+
assert_kind_of Hash, json
174+
assert_equal json, { "name" => "Konata Izumi" }
175+
end
176+
177+
test "as_json should allow attribute filtering with only" do
178+
json = @contact.as_json(only: :name)
179+
180+
assert_kind_of Hash, json
181+
assert_equal json, { "name" => "Konata Izumi" }
182+
end
183+
184+
test "as_json should work with methods options" do
185+
json = @contact.as_json(methods: :social)
186+
187+
assert_kind_of Hash, json
188+
%w(name age created_at awesome preferences social).each do |field|
189+
assert_equal @contact.public_send(field).as_json, json[field]
190+
end
191+
end
192+
193+
test "as_json should work with include option" do
194+
json = @contact.as_json(include: :address)
195+
196+
assert_kind_of Hash, json
197+
assert_kind_of Hash, json["address"]
198+
%w(name age created_at awesome preferences).each do |field|
199+
assert_equal @contact.public_send(field).as_json, json[field]
200+
end
201+
%w(address_line city state country).each do |field|
202+
assert_equal @contact.address.public_send(field).as_json, json["address"][field]
203+
end
204+
end
205+
206+
test "as_json should work with include option paired with only filter" do
207+
json = @contact.as_json(include: { address: { only: :city } })
208+
209+
assert_kind_of Hash, json
210+
%w(name age created_at awesome preferences).each do |field|
211+
assert_equal @contact.public_send(field).as_json, json[field]
212+
end
213+
assert_equal json["address"], { "city" => "Trichy" }
214+
end
215+
216+
test "as_json should work with include option paired with except filter" do
217+
json = @contact.as_json(include: { address: { except: [:address_line, :state, :country] } })
218+
219+
assert_kind_of Hash, json
220+
%w(name age created_at awesome preferences).each do |field|
221+
assert_equal @contact.public_send(field).as_json, json[field]
222+
end
223+
assert_equal json["address"], { "city" => "Trichy" }
224+
end
225+
148226
test "from_json should work without a root (class attribute)" do
149227
json = @contact.to_json
150228
result = Contact.new.from_json(json)

activemodel/test/models/address.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class Address
4+
include ActiveModel::Serializers::JSON
5+
6+
attr_accessor :address_line, :city, :state, :country
7+
8+
def initialize(options = {})
9+
options.each { |name, value| public_send("#{name}=", value) }
10+
end
11+
12+
def attributes
13+
instance_values
14+
end
15+
end

0 commit comments

Comments
 (0)