~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/test/cases/json_serialization_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require "cases/helper"
2
 
require 'models/contact'
3
 
require 'models/post'
4
 
require 'models/author'
5
 
require 'models/tagging'
6
 
require 'models/tag'
7
 
require 'models/comment'
8
 
 
9
 
class JsonSerializationTest < ActiveRecord::TestCase
10
 
  class NamespacedContact < Contact
11
 
    column :name,        :string
12
 
  end
13
 
 
14
 
  def setup
15
 
    @contact = Contact.new(
16
 
      :name        => 'Konata Izumi',
17
 
      :age         => 16,
18
 
      :avatar      => 'binarydata',
19
 
      :created_at  => Time.utc(2006, 8, 1),
20
 
      :awesome     => true,
21
 
      :preferences => { :shows => 'anime' }
22
 
    )
23
 
  end
24
 
 
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
30
 
  end
31
 
 
32
 
  def test_should_include_root_in_json
33
 
    Contact.include_root_in_json = true
34
 
    json = @contact.to_json
35
 
 
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
42
 
  ensure
43
 
    Contact.include_root_in_json = false
44
 
  end
45
 
 
46
 
  def test_should_encode_all_encodable_attributes
47
 
    json = @contact.to_json
48
 
 
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
54
 
  end
55
 
 
56
 
  def test_should_allow_attribute_filtering_with_only
57
 
    json = @contact.to_json(:only => [:name, :age])
58
 
 
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
64
 
  end
65
 
 
66
 
  def test_should_allow_attribute_filtering_with_except
67
 
    json = @contact.to_json(:except => [:name, :age])
68
 
 
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
74
 
  end
75
 
 
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
80
 
 
81
 
    # Single method.
82
 
    assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
83
 
 
84
 
    # Both methods.
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
88
 
  end
89
 
end
90
 
 
91
 
class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
92
 
  fixtures :authors, :posts, :comments, :tags, :taggings
93
 
 
94
 
  def setup
95
 
    @david = authors(:david)
96
 
    @mary = authors(:mary)
97
 
  end
98
 
 
99
 
  def test_includes_uses_association_name
100
 
    json = @david.to_json(:include => :posts)
101
 
 
102
 
    assert_match %r{"posts":\[}, json
103
 
 
104
 
    assert_match %r{"id":1}, json
105
 
    assert_match %r{"name":"David"}, json
106
 
 
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
110
 
 
111
 
    assert_match %r{"title":"So I was thinking"}, json
112
 
    assert_match %r{"body":"Like I hopefully always am"}, json
113
 
  end
114
 
 
115
 
  def test_includes_uses_association_name_and_applies_attribute_filters
116
 
    json = @david.to_json(:include => { :posts => { :only => :title } })
117
 
 
118
 
    assert_match %r{"name":"David"}, json
119
 
    assert_match %r{"posts":\[}, json
120
 
 
121
 
    assert_match %r{"title":"Welcome to the weblog"}, json
122
 
    assert_no_match %r{"body":"Such a lovely day"}, json
123
 
 
124
 
    assert_match %r{"title":"So I was thinking"}, json
125
 
    assert_no_match %r{"body":"Like I hopefully always am"}, json
126
 
  end
127
 
 
128
 
  def test_includes_fetches_second_level_associations
129
 
    json = @david.to_json(:include => { :posts => { :include => { :comments => { :only => :body } } } })
130
 
 
131
 
    assert_match %r{"name":"David"}, json
132
 
    assert_match %r{"posts":\[}, json
133
 
 
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
138
 
  end
139
 
 
140
 
  def test_includes_fetches_nth_level_associations
141
 
    json = @david.to_json(
142
 
      :include => {
143
 
        :posts => {
144
 
          :include => {
145
 
            :taggings => {
146
 
              :include => {
147
 
                :tag => { :only => :name }
148
 
              }
149
 
            }
150
 
          }
151
 
        }
152
 
    })
153
 
 
154
 
    assert_match %r{"name":"David"}, json
155
 
    assert_match %r{"posts":\[}, json
156
 
 
157
 
    assert_match %r{"taggings":\[}, json
158
 
    assert_match %r{"tag":\{"name":"General"\}}, json
159
 
  end
160
 
 
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)
164
 
 
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
168
 
  end
169
 
 
170
 
  def test_should_allow_only_option_for_list_of_authors
171
 
    authors = [@david, @mary]
172
 
 
173
 
    assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
174
 
  end
175
 
 
176
 
  def test_should_allow_except_option_for_list_of_authors
177
 
    authors = [@david, @mary]
178
 
 
179
 
    assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
180
 
  end
181
 
 
182
 
  def test_should_allow_includes_for_list_of_authors
183
 
    authors = [@david, @mary]
184
 
    json = ActiveSupport::JSON.encode(authors,
185
 
      :only => :name,
186
 
      :include => {
187
 
        :posts => { :only => :id }
188
 
      }
189
 
    )
190
 
 
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
194
 
     end
195
 
  end
196
 
 
197
 
  def test_should_allow_options_for_hash_of_authors
198
 
    authors_hash = {
199
 
      1 => @david,
200
 
      2 => @mary
201
 
    }
202
 
 
203
 
    assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
204
 
  end
205
 
end