diff options
author | Josh Kalderimis <josh.kalderimis@gmail.com> | 2011-05-17 19:30:43 -0400 |
---|---|---|
committer | Josh Kalderimis <josh.kalderimis@gmail.com> | 2011-05-18 00:34:09 -0400 |
commit | 6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa (patch) | |
tree | 197f51ebb7b9b9c7987bb29aec73b7da9536cff7 /activeresource/test/cases/base | |
parent | 4a9365ee18c055d970a5f8faaac9443a8bfb0d24 (diff) | |
download | rails-6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa.tar.gz rails-6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa.tar.bz2 rails-6b4bbb427455ec1af6bdf0896a62129bd3c8c4aa.zip |
updated all the tests in ARes to work with json
Diffstat (limited to 'activeresource/test/cases/base')
-rw-r--r-- | activeresource/test/cases/base/custom_methods_test.rb | 56 | ||||
-rw-r--r-- | activeresource/test/cases/base/load_test.rb | 31 | ||||
-rw-r--r-- | activeresource/test/cases/base/schema_test.rb | 4 |
3 files changed, 46 insertions, 45 deletions
diff --git a/activeresource/test/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb index 0fbf94bc0e..3eaa9b1c5b 100644 --- a/activeresource/test/cases/base/custom_methods_test.rb +++ b/activeresource/test/cases/base/custom_methods_test.rb @@ -5,32 +5,32 @@ require 'active_support/core_ext/hash/conversions' class CustomMethodsTest < Test::Unit::TestCase def setup - @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') - @matz_deep = { :id => 1, :name => 'Matz', :other => 'other' }.to_xml(:root => 'person') - @matz_array = [{ :id => 1, :name => 'Matz' }].to_xml(:root => 'people') - @ryan = { :name => 'Ryan' }.to_xml(:root => 'person') - @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') - @addy_deep = { :id => 1, :street => '12345 Street', :zip => "27519" }.to_xml(:root => 'address') + @matz = { :person => { :id => 1, :name => 'Matz' } }.to_json + @matz_deep = { :person => { :id => 1, :name => 'Matz', :other => 'other' } }.to_json + @matz_array = { :people => [{ :person => { :id => 1, :name => 'Matz' } }] }.to_json + @ryan = { :person => { :name => 'Ryan' } }.to_json + @addy = { :address => { :id => 1, :street => '12345 Street' } }.to_json + @addy_deep = { :address => { :id => 1, :street => '12345 Street', :zip => "27519" } }.to_json ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.xml", {}, @matz - mock.get "/people/1/shallow.xml", {}, @matz - mock.get "/people/1/deep.xml", {}, @matz_deep - mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array - mock.get "/people/managers.xml", {}, @matz_array - mock.post "/people/hire.xml?name=Matz", {}, nil, 201 - mock.put "/people/1/promote.xml?position=Manager", {}, nil, 204 - mock.put "/people/promote.xml?name=Matz", {}, nil, 204, {} - mock.put "/people/sort.xml?by=name", {}, nil, 204 - mock.delete "/people/deactivate.xml?name=Matz", {}, nil, 200 - mock.delete "/people/1/deactivate.xml", {}, nil, 200 - mock.post "/people/new/register.xml", {}, @ryan, 201, 'Location' => '/people/5.xml' - mock.post "/people/1/register.xml", {}, @matz, 201 - mock.get "/people/1/addresses/1.xml", {}, @addy - mock.get "/people/1/addresses/1/deep.xml", {}, @addy_deep - mock.put "/people/1/addresses/1/normalize_phone.xml?locale=US", {}, nil, 204 - mock.put "/people/1/addresses/sort.xml?by=name", {}, nil, 204 - mock.post "/people/1/addresses/new/link.xml", {}, { :street => '12345 Street' }.to_xml(:root => 'address'), 201, 'Location' => '/people/1/addresses/2.xml' + mock.get "/people/1.json", {}, @matz + mock.get "/people/1/shallow.json", {}, @matz + mock.get "/people/1/deep.json", {}, @matz_deep + mock.get "/people/retrieve.json?name=Matz", {}, @matz_array + mock.get "/people/managers.json", {}, @matz_array + mock.post "/people/hire.json?name=Matz", {}, nil, 201 + mock.put "/people/1/promote.json?position=Manager", {}, nil, 204 + mock.put "/people/promote.json?name=Matz", {}, nil, 204, {} + mock.put "/people/sort.json?by=name", {}, nil, 204 + mock.delete "/people/deactivate.json?name=Matz", {}, nil, 200 + mock.delete "/people/1/deactivate.json", {}, nil, 200 + mock.post "/people/new/register.json", {}, @ryan, 201, 'Location' => '/people/5.json' + mock.post "/people/1/register.json", {}, @matz, 201 + mock.get "/people/1/addresses/1.json", {}, @addy + mock.get "/people/1/addresses/1/deep.json", {}, @addy_deep + mock.put "/people/1/addresses/1/normalize_phone.json?locale=US", {}, nil, 204 + mock.put "/people/1/addresses/sort.json?by=name", {}, nil, 204 + mock.post "/people/1/addresses/new/link.json", {}, { :address => { :street => '12345 Street' } }.to_json, 201, 'Location' => '/people/1/addresses/2.json' end Person.user = nil @@ -81,14 +81,14 @@ class CustomMethodsTest < Test::Unit::TestCase def test_custom_new_element_method # Test POST against a new element URL ryan = Person.new(:name => 'Ryan') - assert_equal ActiveResource::Response.new(@ryan, 201, {'Location' => '/people/5.xml'}), ryan.post(:register) - expected_request = ActiveResource::Request.new(:post, '/people/new/register.xml', @ryan) + assert_equal ActiveResource::Response.new(@ryan, 201, { 'Location' => '/people/5.json' }), ryan.post(:register) + expected_request = ActiveResource::Request.new(:post, '/people/new/register.json', @ryan) assert_equal expected_request.body, ActiveResource::HttpMock.requests.first.body # Test POST against a nested collection URL addy = StreetAddress.new(:street => '123 Test Dr.', :person_id => 1) - assert_equal ActiveResource::Response.new({ :street => '12345 Street' }.to_xml(:root => 'address'), - 201, {'Location' => '/people/1/addresses/2.xml'}), + assert_equal ActiveResource::Response.new({ :address => { :street => '12345 Street' } }.to_json, + 201, { 'Location' => '/people/1/addresses/2.json' }), addy.post(:link) matz = Person.find(1) diff --git a/activeresource/test/cases/base/load_test.rb b/activeresource/test/cases/base/load_test.rb index 228dc36d9b..d6b04cfaa8 100644 --- a/activeresource/test/cases/base/load_test.rb +++ b/activeresource/test/cases/base/load_test.rb @@ -36,10 +36,10 @@ class BaseLoadTest < Test::Unit::TestCase def setup @matz = { :id => 1, :name => 'Matz' } - @first_address = { :id => 1, :street => '12345 Street' } - @addresses = [@first_address, { :id => 2, :street => '67890 Street' }] - @addresses_from_xml = { :street_addresses => @addresses } - @addresses_from_xml_single = { :street_addresses => [ @first_address ] } + @first_address = { :address => { :id => 1, :street => '12345 Street' } } + @addresses = [@first_address, { :address => { :id => 2, :street => '67890 Street' } }] + @addresses_from_json = { :street_addresses => @addresses } + @addresses_from_json_single = { :street_addresses => [ @first_address ] } @deep = { :id => 1, :street => { :id => 1, :state => { :id => 1, :name => 'Oregon', @@ -72,28 +72,29 @@ class BaseLoadTest < Test::Unit::TestCase def test_after_load_attributes_are_accessible_via_indifferent_access assert_equal Hash.new, @person.attributes + matz_attributes = @matz.values.first assert_equal @matz.stringify_keys, @person.load(@matz).attributes assert_equal @matz[:name], @person.attributes['name'] assert_equal @matz[:name], @person.attributes[:name] end def test_load_one_with_existing_resource - address = @person.load(:street_address => @first_address).street_address + address = @person.load(:street_address => @first_address.values.first).street_address assert_kind_of StreetAddress, address - assert_equal @first_address.stringify_keys, address.attributes + assert_equal @first_address.values.first.stringify_keys, address.attributes end def test_load_one_with_unknown_resource - address = silence_warnings { @person.load(:address => @first_address).address } + address = silence_warnings { @person.load(@first_address).address } assert_kind_of Person::Address, address - assert_equal @first_address.stringify_keys, address.attributes + assert_equal @first_address.values.first.stringify_keys, address.attributes end def test_load_collection_with_existing_resource - addresses = @person.load(@addresses_from_xml).street_addresses + addresses = @person.load(@addresses_from_json).street_addresses assert_kind_of Array, addresses addresses.each { |address| assert_kind_of StreetAddress, address } - assert_equal @addresses.map(&:stringify_keys), addresses.map(&:attributes) + assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes) end def test_load_collection_with_unknown_resource @@ -102,14 +103,14 @@ class BaseLoadTest < Test::Unit::TestCase addresses = silence_warnings { @person.load(:addresses => @addresses).addresses } assert Person.const_defined?(:Address), "Address should have been autocreated" addresses.each { |address| assert_kind_of Person::Address, address } - assert_equal @addresses.map(&:stringify_keys), addresses.map(&:attributes) + assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes) end def test_load_collection_with_single_existing_resource - addresses = @person.load(@addresses_from_xml_single).street_addresses + addresses = @person.load(@addresses_from_json_single).street_addresses assert_kind_of Array, addresses addresses.each { |address| assert_kind_of StreetAddress, address } - assert_equal [ @first_address ].map(&:stringify_keys), addresses.map(&:attributes) + assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes) end def test_load_collection_with_single_unknown_resource @@ -118,7 +119,7 @@ class BaseLoadTest < Test::Unit::TestCase addresses = silence_warnings { @person.load(:addresses => [ @first_address ]).addresses } assert Person.const_defined?(:Address), "Address should have been autocreated" addresses.each { |address| assert_kind_of Person::Address, address } - assert_equal [ @first_address ].map(&:stringify_keys), addresses.map(&:attributes) + assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes) end def test_recursively_loaded_collections @@ -164,7 +165,7 @@ class BaseLoadTest < Test::Unit::TestCase end def test_nested_collections_within_the_same_namespace - n = Highrise::Note.new(:comments => [{ :name => "1" }]) + n = Highrise::Note.new(:comments => [{ :comment => { :name => "1" } }]) assert_kind_of Highrise::Comment, n.comments.first end diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index 37f30e4353..48fdeb13df 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -118,7 +118,7 @@ class SchemaTest < ActiveModel::TestCase test "with two instances, default schema should match the attributes of the individual instances - even if they differ" do matz = Person.find(1) - rick = Person.find(5) + rick = Person.find(6) m_attrs = matz.attributes.keys.sort r_attrs = rick.attributes.keys.sort @@ -376,7 +376,7 @@ class SchemaTest < ActiveModel::TestCase test "with two instances, known attributes should match the attributes of the individual instances - even if they differ" do matz = Person.find(1) - rick = Person.find(5) + rick = Person.find(6) m_attrs = matz.attributes.keys.sort r_attrs = rick.attributes.keys.sort |