| | 1 | require 'abstract_unit' |
|---|
| | 2 | require 'fixtures/post' |
|---|
| | 3 | require 'fixtures/author' |
|---|
| | 4 | require 'fixtures/comment' |
|---|
| | 5 | |
|---|
| | 6 | class Contact < ActiveRecord::Base |
|---|
| | 7 | # mock out self.columns so no pesky db is needed for these tests |
|---|
| | 8 | def self.columns() @columns ||= []; end |
|---|
| | 9 | def self.column(name, sql_type = nil, default = nil, null = true) |
|---|
| | 10 | columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) |
|---|
| | 11 | end |
|---|
| | 12 | |
|---|
| | 13 | column :name, :string |
|---|
| | 14 | column :age, :integer |
|---|
| | 15 | column :avatar, :binary |
|---|
| | 16 | column :created_at, :datetime |
|---|
| | 17 | column :awesome, :boolean |
|---|
| | 18 | column :gender, :string |
|---|
| | 19 | end |
|---|
| | 20 | |
|---|
| | 21 | |
|---|
| | 22 | class JsonEncodingTest < Test::Unit::TestCase |
|---|
| | 23 | |
|---|
| | 24 | def setup |
|---|
| | 25 | @david = Contact.new( |
|---|
| | 26 | :name => 'David', |
|---|
| | 27 | :age => 27, |
|---|
| | 28 | :awesome => true, |
|---|
| | 29 | :gender => 'Male') |
|---|
| | 30 | end |
|---|
| | 31 | |
|---|
| | 32 | def test_should_encode_all_encodable_attributes |
|---|
| | 33 | json = @david.to_json |
|---|
| | 34 | |
|---|
| | 35 | assert_match %r{name: "David"}, json |
|---|
| | 36 | assert_match %r{age: 27}, json |
|---|
| | 37 | assert_match %r{awesome: true}, json |
|---|
| | 38 | assert_match %r{gender: "Male"}, json |
|---|
| | 39 | end |
|---|
| | 40 | |
|---|
| | 41 | def test_binary_attributes_should_be_skipped |
|---|
| | 42 | assert_no_match %r{avatar: }, @david.to_json |
|---|
| | 43 | end |
|---|
| | 44 | |
|---|
| | 45 | def test_should_allow_attribute_filtering_with_only |
|---|
| | 46 | json = @david.to_json(:only => [:age, :name]) |
|---|
| | 47 | |
|---|
| | 48 | assert_match %r{name: "David"}, json |
|---|
| | 49 | assert_match %r{age: 27}, json |
|---|
| | 50 | assert_no_match %r{awesome: true}, json |
|---|
| | 51 | assert_no_match %r{gender: "Male"}, json |
|---|
| | 52 | end |
|---|
| | 53 | |
|---|
| | 54 | def test_should_allow_attribute_filtering_with_except |
|---|
| | 55 | json = @david.to_json(:except => [:age, :gender]) |
|---|
| | 56 | |
|---|
| | 57 | assert_match %r{name: "David"}, json |
|---|
| | 58 | assert_match %r{awesome: true}, json |
|---|
| | 59 | assert_no_match %r{age: 27}, json |
|---|
| | 60 | assert_no_match %r{gender: "Male"}, json |
|---|
| | 61 | end |
|---|
| | 62 | |
|---|
| | 63 | def test_methods_are_called_on_object |
|---|
| | 64 | # Define methods on fixture. |
|---|
| | 65 | def @david.label; "Father of Rails"; end |
|---|
| | 66 | def @david.favorite_quote; "Constraints are liberating"; end |
|---|
| | 67 | |
|---|
| | 68 | # Single method. |
|---|
| | 69 | assert_equal %({name: "David", label: "Father of Rails"}), @david.to_json(:only => :name, :methods => :label) |
|---|
| | 70 | |
|---|
| | 71 | # Both methods. |
|---|
| | 72 | methods_json = @david.to_json(:only => :name, :methods => [:label, :favorite_quote]) |
|---|
| | 73 | assert_match %r{label: "Father of Rails"}, methods_json |
|---|
| | 74 | assert_match %r{favorite_quote: "Constraints are liberating"}, methods_json |
|---|
| | 75 | end |
|---|
| | 76 | end |
|---|
| | 77 | |
|---|
| | 78 | |
|---|
| | 79 | class DatabaseConnectedJsonEncodingTest < Test::Unit::TestCase |
|---|
| | 80 | fixtures :authors, :posts, :comments |
|---|
| | 81 | |
|---|
| | 82 | def setup |
|---|
| | 83 | @david = authors(:david) |
|---|
| | 84 | end |
|---|
| | 85 | |
|---|
| | 86 | def test_includes_uses_association_name |
|---|
| | 87 | json = @david.to_json(:include => :posts) |
|---|
| | 88 | |
|---|
| | 89 | assert_match %r{id: 1}, json |
|---|
| | 90 | assert_match %r{name: "David"}, json |
|---|
| | 91 | assert_match %r{posts: \[}, json |
|---|
| | 92 | |
|---|
| | 93 | assert_match %r{author_id: 1}, json |
|---|
| | 94 | assert_match %r{title: "Welcome to the weblog"}, json |
|---|
| | 95 | assert_match %r{body: "Such a lovely day"}, json |
|---|
| | 96 | |
|---|
| | 97 | assert_match %r{title: "So I was thinking"}, json |
|---|
| | 98 | assert_match %r{body: "Like I hopefully always am"}, json |
|---|
| | 99 | end |
|---|
| | 100 | |
|---|
| | 101 | def test_includes_uses_association_name_and_applies_attribute_filters |
|---|
| | 102 | json = @david.to_json(:include => { :posts => { :only => :title } }) |
|---|
| | 103 | |
|---|
| | 104 | assert_match %r{name: "David"}, json |
|---|
| | 105 | assert_match %r{posts: \[}, json |
|---|
| | 106 | |
|---|
| | 107 | assert_match %r{title: "Welcome to the weblog"}, json |
|---|
| | 108 | assert_no_match %r{body: "Such a lovely day"}, json |
|---|
| | 109 | |
|---|
| | 110 | assert_match %r{title: "So I was thinking"}, json |
|---|
| | 111 | assert_no_match %r{body: "Like I hopefully always am"}, json |
|---|
| | 112 | end |
|---|
| | 113 | |
|---|
| | 114 | def test_includes_fetches_second_level_associations |
|---|
| | 115 | json = @david.to_json(:include => { :posts => { :include => { :comments => { :only => :body } } } }) |
|---|
| | 116 | |
|---|
| | 117 | assert_match %r{name: "David"}, json |
|---|
| | 118 | assert_match %r{posts: \[}, json |
|---|
| | 119 | |
|---|
| | 120 | assert_match %r{comments: \[}, json |
|---|
| | 121 | assert_match %r{\{body: "Thank you again for the welcome"\}}, json |
|---|
| | 122 | assert_match %r{\{body: "Don't think too hard"\}}, json |
|---|
| | 123 | assert_no_match %r{post_id: }, json |
|---|
| | 124 | end |
|---|
| | 125 | |
|---|
| | 126 | def test_should_not_call_methods_on_associations_that_dont_respond |
|---|
| | 127 | def @david.favorite_quote; "Constraints are liberating"; end |
|---|
| | 128 | json = @david.to_json(:include => :posts, :methods => :favorite_quote) |
|---|
| | 129 | |
|---|
| | 130 | assert !@david.posts.first.respond_to?(:favorite_quote) |
|---|
| | 131 | assert_match %r{favorite_quote: "Constraints are liberating"}, json |
|---|
| | 132 | assert_equal %r{favorite_quote: }.match(json).size, 1 |
|---|
| | 133 | end |
|---|
| | 134 | end |