diff options
author | Adrian Mugnolo <adrian@mugnolo.com> | 2008-08-22 03:03:26 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-08-22 03:06:57 +0100 |
commit | 893fb5bb639b0938f6762ef1165b05abae255986 (patch) | |
tree | 650d95e7a7bd89f4298d316ca5bfeb34c13ec748 /activeresource/test | |
parent | 1129a24caff9f1804c2bff6569c0cbd8598dfa86 (diff) | |
download | rails-893fb5bb639b0938f6762ef1165b05abae255986.tar.gz rails-893fb5bb639b0938f6762ef1165b05abae255986.tar.bz2 rails-893fb5bb639b0938f6762ef1165b05abae255986.zip |
Add ActiveResource::Base.find(:last). [#754 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activeresource/test')
-rw-r--r-- | activeresource/test/base_test.rb | 98 |
1 files changed, 52 insertions, 46 deletions
diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 4addd52636..0d997b2a2f 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -8,7 +8,7 @@ class BaseTest < Test::Unit::TestCase def setup @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') - @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") @@ -50,7 +50,7 @@ class BaseTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/2.xml", {}, @david - mock.get "/people/Greg.xml", {}, @greg + mock.get "/people/Greg.xml", {}, @greg mock.get "/people/4.xml", {'key' => 'value'}, nil, 404 mock.put "/people/1.xml", {}, nil, 204 mock.delete "/people/1.xml", {}, nil, 200 @@ -62,7 +62,7 @@ class BaseTest < Test::Unit::TestCase mock.get "/people/1/addresses/1.xml", {}, @addy mock.get "/people/1/addresses/2.xml", {}, nil, 404 mock.get "/people/2/addresses/1.xml", {}, nil, 404 - mock.get "/people/Greg/addresses/1.xml", {}, @addy + mock.get "/people/Greg/addresses/1.xml", {}, @addy mock.put "/people/1/addresses/1.xml", {}, nil, 204 mock.delete "/people/1/addresses/1.xml", {}, nil, 200 mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5' @@ -101,13 +101,13 @@ class BaseTest < Test::Unit::TestCase assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s end - + def test_site_variable_can_be_reset - actor = Class.new(ActiveResource::Base) + actor = Class.new(ActiveResource::Base) assert_nil actor.site actor.site = 'http://localhost:31337' actor.site = nil - assert_nil actor.site + assert_nil actor.site end def test_should_accept_setting_user @@ -194,18 +194,18 @@ class BaseTest < Test::Unit::TestCase actor.site = 'http://nomad' assert_equal actor.site, jester.site assert jester.site.frozen? - - # Subclasses are always equal to superclass site when not overridden + + # Subclasses are always equal to superclass site when not overridden fruit = Class.new(ActiveResource::Base) apple = Class.new(fruit) - + fruit.site = 'http://market' assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' - + fruit.site = 'http://supermarket' assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' end - + def test_user_reader_uses_superclass_user_until_written # Superclass is Object so returns nil. assert_nil ActiveResource::Base.user @@ -317,14 +317,14 @@ class BaseTest < Test::Unit::TestCase end def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects - # Subclasses are always equal to superclass site when not overridden + # Subclasses are always equal to superclass site when not overridden fruit = Class.new(ActiveResource::Base) apple = Class.new(fruit) - + fruit.site = 'http://market' assert_equal fruit.connection.site, apple.connection.site first_connection = apple.connection.object_id - + fruit.site = 'http://supermarket' assert_equal fruit.connection.site, apple.connection.site second_connection = apple.connection.object_id @@ -393,34 +393,34 @@ class BaseTest < Test::Unit::TestCase assert_equal '/people.xml?gender=', Person.collection_path(:gender => nil) assert_equal '/people.xml?gender=male', Person.collection_path('gender' => 'male') - + # Use includes? because ordering of param hash is not guaranteed assert Person.collection_path(:gender => 'male', :student => true).include?('/people.xml?') assert Person.collection_path(:gender => 'male', :student => true).include?('gender=male') assert Person.collection_path(:gender => 'male', :student => true).include?('student=true') assert_equal '/people.xml?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D=&name%5B%5D=false', Person.collection_path(:name => ['bob', 'your uncle+me', nil, false]) - + assert_equal '/people.xml?struct%5Ba%5D%5B%5D=2&struct%5Ba%5D%5B%5D=1&struct%5Bb%5D=fred', Person.collection_path(:struct => {:a => [2,1], 'b' => 'fred'}) end def test_custom_element_path assert_equal '/people/1/addresses/1.xml', StreetAddress.element_path(1, :person_id => 1) assert_equal '/people/1/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 1) - assert_equal '/people/Greg/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 'Greg') + assert_equal '/people/Greg/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 'Greg') end - + def test_custom_element_path_with_redefined_to_param Person.module_eval do alias_method :original_to_param_element_path, :to_param - def to_param + def to_param name end end # Class method. assert_equal '/people/Greg.xml', Person.element_path('Greg') - + # Protected Instance method. assert_equal '/people/Greg.xml', Person.find('Greg').send(:element_path) @@ -470,21 +470,21 @@ class BaseTest < Test::Unit::TestCase assert_equal "/", Person.prefix assert_equal Set.new, Person.send!(:prefix_parameters) end - + def test_set_prefix SetterTrap.rollback_sets(Person) do |person_class| person_class.prefix = "the_prefix" assert_equal "the_prefix", person_class.prefix end end - + def test_set_prefix_with_inline_keys SetterTrap.rollback_sets(Person) do |person_class| person_class.prefix = "the_prefix:the_param" assert_equal "the_prefixthe_param_value", person_class.prefix(:the_param => "the_param_value") end end - + def test_set_prefix_with_default_value SetterTrap.rollback_sets(Person) do |person_class| person_class.set_prefix @@ -504,7 +504,7 @@ class BaseTest < Test::Unit::TestCase assert_equal "Matz", matz.name assert matz.name? end - + def test_respond_to matz = Person.find(1) assert matz.respond_to?(:name) @@ -533,6 +533,12 @@ class BaseTest < Test::Unit::TestCase assert_equal "Matz", matz.name end + def test_find_last + david = Person.find(:last) + assert_kind_of Person, david + assert_equal 'David', david.name + end + def test_custom_header Person.headers['key'] = 'value' assert_raises(ActiveResource::ResourceNotFound) { Person.find(4) } @@ -547,7 +553,7 @@ class BaseTest < Test::Unit::TestCase def test_find_all_by_from ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david } - + people = Person.find(:all, :from => "/companies/1/people.xml") assert_equal 1, people.size assert_equal "David", people.first.name @@ -555,7 +561,7 @@ class BaseTest < Test::Unit::TestCase def test_find_all_by_from_with_options ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david } - + people = Person.find(:all, :from => "/companies/1/people.xml") assert_equal 1, people.size assert_equal "David", people.first.name @@ -563,7 +569,7 @@ class BaseTest < Test::Unit::TestCase def test_find_all_by_symbol_from ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.xml", {}, @people_david } - + people = Person.find(:all, :from => :managers) assert_equal 1, people.size assert_equal "David", people.first.name @@ -593,7 +599,7 @@ class BaseTest < Test::Unit::TestCase p = Person.new resp = {'Location' => '/foo/bar/1'} assert_equal '1', p.send!(:id_from_response, resp) - + resp['Location'] << '.xml' assert_equal '1', p.send!(:id_from_response, resp) end @@ -610,16 +616,16 @@ class BaseTest < Test::Unit::TestCase ryan = Person.new(:id => 1, :name => 'Ryan', :address => address) assert_equal address.prefix_options, ryan.address.prefix_options end - + def test_reload_works_with_prefix_options address = StreetAddress.find(1, :params => { :person_id => 1 }) assert_equal address, address.reload end - + def test_reload_with_redefined_to_param Person.module_eval do alias_method :original_to_param_reload, :to_param - def to_param + def to_param name end end @@ -634,13 +640,13 @@ class BaseTest < Test::Unit::TestCase alias_method :reload_to_param, :to_param alias_method :to_param, :original_to_param_reload end - end - - def test_reload_works_without_prefix_options + end + + def test_reload_works_without_prefix_options person = Person.find(:first) assert_equal person, person.reload end - + def test_create rick = Person.create(:name => 'Rick') @@ -650,11 +656,11 @@ class BaseTest < Test::Unit::TestCase # test additional attribute returned on create assert_equal 25, rick.age - + # Test that save exceptions get bubbled up too ActiveResource::HttpMock.respond_to do |mock| mock.post "/people.xml", {}, nil, 409 - end + end assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') } end @@ -716,7 +722,7 @@ class BaseTest < Test::Unit::TestCase assert_equal "54321 Lane", addy.street addy.save end - + def test_update_conflict ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/2.xml", {}, @david @@ -748,7 +754,7 @@ class BaseTest < Test::Unit::TestCase end assert_raises(ActiveResource::ResourceNotFound) { Person.find(1) } end - + def test_delete_with_custom_prefix assert StreetAddress.delete(1, :person_id => 1) ActiveResource::HttpMock.respond_to do |mock| @@ -778,23 +784,23 @@ class BaseTest < Test::Unit::TestCase assert !StreetAddress.new({:id => 1, :person_id => 2}).exists? assert !StreetAddress.new({:id => 2, :person_id => 1}).exists? end - + def test_exists_with_redefined_to_param Person.module_eval do alias_method :original_to_param_exists, :to_param - def to_param + def to_param name end end # Class method. - assert Person.exists?('Greg') + assert Person.exists?('Greg') # Instance method. - assert Person.find('Greg').exists? + assert Person.find('Greg').exists? # Nested class method. - assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param }) + assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param }) # Nested instance method. assert StreetAddress.find(1, :params => { :person_id => Person.find('Greg').to_param }).exists? @@ -806,8 +812,8 @@ class BaseTest < Test::Unit::TestCase alias_method :exists_to_param, :to_param alias_method :to_param, :original_to_param_exists end - end - + end + def test_to_xml matz = Person.find(1) xml = matz.to_xml |