diff options
Diffstat (limited to 'activeresource/test/cases/base')
-rw-r--r-- | activeresource/test/cases/base/custom_methods_test.rb | 101 | ||||
-rw-r--r-- | activeresource/test/cases/base/equality_test.rb | 52 | ||||
-rw-r--r-- | activeresource/test/cases/base/load_test.rb | 163 |
3 files changed, 316 insertions, 0 deletions
diff --git a/activeresource/test/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb new file mode 100644 index 0000000000..2d81549a65 --- /dev/null +++ b/activeresource/test/cases/base/custom_methods_test.rb @@ -0,0 +1,101 @@ +require 'abstract_unit' +require 'fixtures/person' +require 'fixtures/street_address' +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') + + 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' + end + + Person.user = nil + Person.password = nil + end + + def teardown + ActiveResource::HttpMock.reset! + end + + def test_custom_collection_method + # GET + assert_equal([{ "id" => 1, "name" => 'Matz' }], Person.get(:retrieve, :name => 'Matz')) + + # POST + assert_equal(ActiveResource::Response.new("", 201, {}), Person.post(:hire, :name => 'Matz')) + + # PUT + assert_equal ActiveResource::Response.new("", 204, {}), + Person.put(:promote, {:name => 'Matz'}, 'atestbody') + assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:sort, :by => 'name') + + # DELETE + Person.delete :deactivate, :name => 'Matz' + + # Nested resource + assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name') + end + + def test_custom_element_method + # Test GET against an element URL + assert_equal Person.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'} + assert_equal Person.find(1).get(:deep), {"id" => 1, "name" => 'Matz', "other" => 'other'} + + # Test PUT against an element URL + assert_equal ActiveResource::Response.new("", 204, {}), Person.find(1).put(:promote, {:position => 'Manager'}, 'body') + + # Test DELETE against an element URL + assert_equal ActiveResource::Response.new("", 200, {}), Person.find(1).delete(:deactivate) + + # With nested resources + assert_equal StreetAddress.find(1, :params => { :person_id => 1 }).get(:deep), + { "id" => 1, "street" => '12345 Street', "zip" => "27519" } + assert_equal ActiveResource::Response.new("", 204, {}), + StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US') + end + + 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 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'}), + addy.post(:link) + + matz = Person.new(:id => 1, :name => 'Matz') + assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register) + end + + def test_find_custom_resources + assert_equal 'Matz', Person.find(:all, :from => :managers).first.name + end +end diff --git a/activeresource/test/cases/base/equality_test.rb b/activeresource/test/cases/base/equality_test.rb new file mode 100644 index 0000000000..84f1a7b998 --- /dev/null +++ b/activeresource/test/cases/base/equality_test.rb @@ -0,0 +1,52 @@ +require 'abstract_unit' +require "fixtures/person" +require "fixtures/street_address" + +class BaseEqualityTest < Test::Unit::TestCase + def setup + @new = Person.new + @one = Person.new(:id => 1) + @two = Person.new(:id => 2) + @street = StreetAddress.new(:id => 2) + end + + def test_should_equal_self + assert @new == @new, '@new == @new' + assert @one == @one, '@one == @one' + end + + def test_shouldnt_equal_new_resource + assert @new != @one, '@new != @one' + assert @one != @new, '@one != @new' + end + + def test_shouldnt_equal_different_class + assert @two != @street, 'person != street_address with same id' + assert @street != @two, 'street_address != person with same id' + end + + def test_eql_should_alias_equals_operator + assert_equal @new == @new, @new.eql?(@new) + assert_equal @new == @one, @new.eql?(@one) + + assert_equal @one == @one, @one.eql?(@one) + assert_equal @one == @new, @one.eql?(@new) + + assert_equal @one == @street, @one.eql?(@street) + end + + def test_hash_should_be_id_hash + [@new, @one, @two, @street].each do |resource| + assert_equal resource.id.hash, resource.hash + end + end + + def test_with_prefix_options + assert_equal @one == @one, @one.eql?(@one) + assert_equal @one == @one.dup, @one.eql?(@one.dup) + new_one = @one.dup + new_one.prefix_options = {:foo => 'bar'} + assert_not_equal @one, new_one + end + +end diff --git a/activeresource/test/cases/base/load_test.rb b/activeresource/test/cases/base/load_test.rb new file mode 100644 index 0000000000..1952f5b5f0 --- /dev/null +++ b/activeresource/test/cases/base/load_test.rb @@ -0,0 +1,163 @@ +require 'abstract_unit' +require "fixtures/person" +require "fixtures/street_address" +require 'active_support/core_ext/symbol' +require 'active_support/core_ext/hash/conversions' + +module Highrise + class Note < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + class Comment < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + module Deeply + module Nested + + class Note < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + class Comment < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + module TestDifferentLevels + + class Note < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + end + + end + end + +end + + +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 ] } + + @deep = { :id => 1, :street => { + :id => 1, :state => { :id => 1, :name => 'Oregon', + :notable_rivers => [ + { :id => 1, :name => 'Willamette' }, + { :id => 2, :name => 'Columbia', :rafted_by => @matz }], + :postal_codes => [ 97018, 1234567890 ], + :places => [ "Columbia City", "Unknown" ]}}} + + @person = Person.new + end + + def test_load_expects_hash + assert_raise(ArgumentError) { @person.load nil } + assert_raise(ArgumentError) { @person.load '<person id="1"/>' } + end + + def test_load_simple_hash + assert_equal Hash.new, @person.attributes + assert_equal @matz.stringify_keys, @person.load(@matz).attributes + end + + def test_load_one_with_existing_resource + address = @person.load(:street_address => @first_address).street_address + assert_kind_of StreetAddress, address + assert_equal @first_address.stringify_keys, address.attributes + end + + def test_load_one_with_unknown_resource + address = silence_warnings { @person.load(:address => @first_address).address } + assert_kind_of Person::Address, address + assert_equal @first_address.stringify_keys, address.attributes + end + + def test_load_collection_with_existing_resource + addresses = @person.load(@addresses_from_xml).street_addresses + assert_kind_of Array, addresses + addresses.each { |address| assert_kind_of StreetAddress, address } + assert_equal @addresses.map(&:stringify_keys), addresses.map(&:attributes) + end + + def test_load_collection_with_unknown_resource + Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) + assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" + 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) + end + + def test_load_collection_with_single_existing_resource + addresses = @person.load(@addresses_from_xml_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) + end + + def test_load_collection_with_single_unknown_resource + Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) + assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" + 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) + end + + def test_recursively_loaded_collections + person = @person.load(@deep) + assert_equal @deep[:id], person.id + + street = person.street + assert_kind_of Person::Street, street + assert_equal @deep[:street][:id], street.id + + state = street.state + assert_kind_of Person::Street::State, state + assert_equal @deep[:street][:state][:id], state.id + + rivers = state.notable_rivers + assert_kind_of Array, rivers + assert_kind_of Person::Street::State::NotableRiver, rivers.first + assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id + assert_equal @matz[:id], rivers.last.rafted_by.id + + postal_codes = state.postal_codes + assert_kind_of Array, postal_codes + assert_equal 2, postal_codes.size + assert_kind_of Fixnum, postal_codes.first + assert_equal @deep[:street][:state][:postal_codes].first, postal_codes.first + assert_kind_of Numeric, postal_codes.last + assert_equal @deep[:street][:state][:postal_codes].last, postal_codes.last + + places = state.places + assert_kind_of Array, places + assert_kind_of String, places.first + assert_equal @deep[:street][:state][:places].first, places.first + end + + def test_nested_collections_within_the_same_namespace + n = Highrise::Note.new(:comments => [{ :name => "1" }]) + assert_kind_of Highrise::Comment, n.comments.first + end + + def test_nested_collections_within_deeply_nested_namespace + n = Highrise::Deeply::Nested::Note.new(:comments => [{ :name => "1" }]) + assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first + end + + def test_nested_collections_in_different_levels_of_namespaces + n = Highrise::Deeply::Nested::TestDifferentLevels::Note.new(:comments => [{ :name => "1" }]) + assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first + end + + +end |