2
require 'models/contact'
4
require 'models/author'
5
require 'models/tagging'
7
require 'models/comment'
9
class JsonSerializationTest < ActiveRecord::TestCase
10
class NamespacedContact < Contact
15
@contact = Contact.new(
16
:name => 'Konata Izumi',
18
:avatar => 'binarydata',
19
:created_at => Time.utc(2006, 8, 1),
21
:preferences => { :shows => 'anime' }
25
def test_should_demodulize_root_in_json
26
NamespacedContact.include_root_in_json = true
27
@contact = NamespacedContact.new :name => 'whatever'
28
json = @contact.to_json
29
assert_match %r{^\{"namespaced_contact":\{}, json
32
def test_should_include_root_in_json
33
Contact.include_root_in_json = true
34
json = @contact.to_json
36
assert_match %r{^\{"contact":\{}, json
37
assert_match %r{"name":"Konata Izumi"}, json
38
assert_match %r{"age":16}, json
39
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
40
assert_match %r{"awesome":true}, json
41
assert_match %r{"preferences":\{"shows":"anime"\}}, json
43
Contact.include_root_in_json = false
46
def test_should_encode_all_encodable_attributes
47
json = @contact.to_json
49
assert_match %r{"name":"Konata Izumi"}, json
50
assert_match %r{"age":16}, json
51
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
52
assert_match %r{"awesome":true}, json
53
assert_match %r{"preferences":\{"shows":"anime"\}}, json
56
def test_should_allow_attribute_filtering_with_only
57
json = @contact.to_json(:only => [:name, :age])
59
assert_match %r{"name":"Konata Izumi"}, json
60
assert_match %r{"age":16}, json
61
assert_no_match %r{"awesome":true}, json
62
assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
63
assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
66
def test_should_allow_attribute_filtering_with_except
67
json = @contact.to_json(:except => [:name, :age])
69
assert_no_match %r{"name":"Konata Izumi"}, json
70
assert_no_match %r{"age":16}, json
71
assert_match %r{"awesome":true}, json
72
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
73
assert_match %r{"preferences":\{"shows":"anime"\}}, json
76
def test_methods_are_called_on_object
77
# Define methods on fixture.
78
def @contact.label; "Has cheezburger"; end
79
def @contact.favorite_quote; "Constraints are liberating"; end
82
assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
85
methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
86
assert_match %r{"label":"Has cheezburger"}, methods_json
87
assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
91
class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
92
fixtures :authors, :posts, :comments, :tags, :taggings
95
@david = authors(:david)
96
@mary = authors(:mary)
99
def test_includes_uses_association_name
100
json = @david.to_json(:include => :posts)
102
assert_match %r{"posts":\[}, json
104
assert_match %r{"id":1}, json
105
assert_match %r{"name":"David"}, json
107
assert_match %r{"author_id":1}, json
108
assert_match %r{"title":"Welcome to the weblog"}, json
109
assert_match %r{"body":"Such a lovely day"}, json
111
assert_match %r{"title":"So I was thinking"}, json
112
assert_match %r{"body":"Like I hopefully always am"}, json
115
def test_includes_uses_association_name_and_applies_attribute_filters
116
json = @david.to_json(:include => { :posts => { :only => :title } })
118
assert_match %r{"name":"David"}, json
119
assert_match %r{"posts":\[}, json
121
assert_match %r{"title":"Welcome to the weblog"}, json
122
assert_no_match %r{"body":"Such a lovely day"}, json
124
assert_match %r{"title":"So I was thinking"}, json
125
assert_no_match %r{"body":"Like I hopefully always am"}, json
128
def test_includes_fetches_second_level_associations
129
json = @david.to_json(:include => { :posts => { :include => { :comments => { :only => :body } } } })
131
assert_match %r{"name":"David"}, json
132
assert_match %r{"posts":\[}, json
134
assert_match %r{"comments":\[}, json
135
assert_match %r{\{"body":"Thank you again for the welcome"\}}, json
136
assert_match %r{\{"body":"Don't think too hard"\}}, json
137
assert_no_match %r{"post_id":}, json
140
def test_includes_fetches_nth_level_associations
141
json = @david.to_json(
147
:tag => { :only => :name }
154
assert_match %r{"name":"David"}, json
155
assert_match %r{"posts":\[}, json
157
assert_match %r{"taggings":\[}, json
158
assert_match %r{"tag":\{"name":"General"\}}, json
161
def test_should_not_call_methods_on_associations_that_dont_respond
162
def @david.favorite_quote; "Constraints are liberating"; end
163
json = @david.to_json(:include => :posts, :methods => :favorite_quote)
165
assert !@david.posts.first.respond_to?(:favorite_quote)
166
assert_match %r{"favorite_quote":"Constraints are liberating"}, json
167
assert_equal %r{"favorite_quote":}.match(json).size, 1
170
def test_should_allow_only_option_for_list_of_authors
171
authors = [@david, @mary]
173
assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
176
def test_should_allow_except_option_for_list_of_authors
177
authors = [@david, @mary]
179
assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
182
def test_should_allow_includes_for_list_of_authors
183
authors = [@david, @mary]
184
json = ActiveSupport::JSON.encode(authors,
187
:posts => { :only => :id }
191
['"name":"David"', '"posts":[', '{"id":1}', '{"id":2}', '{"id":4}',
192
'{"id":5}', '{"id":6}', '"name":"Mary"', '"posts":[{"id":7}]'].each do |fragment|
193
assert json.include?(fragment), json
197
def test_should_allow_options_for_hash_of_authors
203
assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])