|
2 | 2 |
|
3 | 3 | require "cases/helper"
|
4 | 4 | require "models/contact"
|
| 5 | +require "models/address" |
5 | 6 | require "active_support/core_ext/object/instance_variables"
|
6 | 7 |
|
7 | 8 | class JsonSerializationTest < ActiveModel::TestCase
|
8 | 9 | def setup
|
9 | 10 | @contact = Contact.new
|
10 | 11 | @contact.name = "Konata Izumi"
|
| 12 | + @contact.address = Address.new(address_line: "Cantonment Road", city: "Trichy", state: "Tamil Nadu", country: "India") |
11 | 13 | @contact.age = 16
|
12 | 14 | @contact.created_at = Time.utc(2006, 8, 1)
|
13 | 15 | @contact.awesome = true
|
@@ -145,6 +147,82 @@ def @contact.favorite_quote; "Constraints are liberating"; end
|
145 | 147 | Contact.include_root_in_json = original_include_root_in_json
|
146 | 148 | end
|
147 | 149 |
|
| 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 | + |
148 | 226 | test "from_json should work without a root (class attribute)" do
|
149 | 227 | json = @contact.to_json
|
150 | 228 | result = Contact.new.from_json(json)
|
|
0 commit comments