PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/activeresource/test/cases/base/custom_methods_test.rb

http://github.com/IronLanguages/main
Ruby | 101 lines | 73 code | 17 blank | 11 comment | 0 complexity | f0a2fd53aa48ce7ca62664c79f0f3f3a MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require 'fixtures/person'
  3. require 'fixtures/street_address'
  4. require 'active_support/core_ext/hash/conversions'
  5. class CustomMethodsTest < Test::Unit::TestCase
  6. def setup
  7. @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
  8. @matz_deep = { :id => 1, :name => 'Matz', :other => 'other' }.to_xml(:root => 'person')
  9. @matz_array = [{ :id => 1, :name => 'Matz' }].to_xml(:root => 'people')
  10. @ryan = { :name => 'Ryan' }.to_xml(:root => 'person')
  11. @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address')
  12. @addy_deep = { :id => 1, :street => '12345 Street', :zip => "27519" }.to_xml(:root => 'address')
  13. ActiveResource::HttpMock.respond_to do |mock|
  14. mock.get "/people/1.xml", {}, @matz
  15. mock.get "/people/1/shallow.xml", {}, @matz
  16. mock.get "/people/1/deep.xml", {}, @matz_deep
  17. mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array
  18. mock.get "/people/managers.xml", {}, @matz_array
  19. mock.post "/people/hire.xml?name=Matz", {}, nil, 201
  20. mock.put "/people/1/promote.xml?position=Manager", {}, nil, 204
  21. mock.put "/people/promote.xml?name=Matz", {}, nil, 204, {}
  22. mock.put "/people/sort.xml?by=name", {}, nil, 204
  23. mock.delete "/people/deactivate.xml?name=Matz", {}, nil, 200
  24. mock.delete "/people/1/deactivate.xml", {}, nil, 200
  25. mock.post "/people/new/register.xml", {}, @ryan, 201, 'Location' => '/people/5.xml'
  26. mock.post "/people/1/register.xml", {}, @matz, 201
  27. mock.get "/people/1/addresses/1.xml", {}, @addy
  28. mock.get "/people/1/addresses/1/deep.xml", {}, @addy_deep
  29. mock.put "/people/1/addresses/1/normalize_phone.xml?locale=US", {}, nil, 204
  30. mock.put "/people/1/addresses/sort.xml?by=name", {}, nil, 204
  31. mock.post "/people/1/addresses/new/link.xml", {}, { :street => '12345 Street' }.to_xml(:root => 'address'), 201, 'Location' => '/people/1/addresses/2.xml'
  32. end
  33. Person.user = nil
  34. Person.password = nil
  35. end
  36. def teardown
  37. ActiveResource::HttpMock.reset!
  38. end
  39. def test_custom_collection_method
  40. # GET
  41. assert_equal([{ "id" => 1, "name" => 'Matz' }], Person.get(:retrieve, :name => 'Matz'))
  42. # POST
  43. assert_equal(ActiveResource::Response.new("", 201, {}), Person.post(:hire, :name => 'Matz'))
  44. # PUT
  45. assert_equal ActiveResource::Response.new("", 204, {}),
  46. Person.put(:promote, {:name => 'Matz'}, 'atestbody')
  47. assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:sort, :by => 'name')
  48. # DELETE
  49. Person.delete :deactivate, :name => 'Matz'
  50. # Nested resource
  51. assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name')
  52. end
  53. def test_custom_element_method
  54. # Test GET against an element URL
  55. assert_equal Person.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'}
  56. assert_equal Person.find(1).get(:deep), {"id" => 1, "name" => 'Matz', "other" => 'other'}
  57. # Test PUT against an element URL
  58. assert_equal ActiveResource::Response.new("", 204, {}), Person.find(1).put(:promote, {:position => 'Manager'}, 'body')
  59. # Test DELETE against an element URL
  60. assert_equal ActiveResource::Response.new("", 200, {}), Person.find(1).delete(:deactivate)
  61. # With nested resources
  62. assert_equal StreetAddress.find(1, :params => { :person_id => 1 }).get(:deep),
  63. { "id" => 1, "street" => '12345 Street', "zip" => "27519" }
  64. assert_equal ActiveResource::Response.new("", 204, {}),
  65. StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US')
  66. end
  67. def test_custom_new_element_method
  68. # Test POST against a new element URL
  69. ryan = Person.new(:name => 'Ryan')
  70. assert_equal ActiveResource::Response.new(@ryan, 201, {'Location' => '/people/5.xml'}), ryan.post(:register)
  71. expected_request = ActiveResource::Request.new(:post, '/people/new/register.xml', @ryan)
  72. assert_equal expected_request.body, ActiveResource::HttpMock.requests.first.body
  73. # Test POST against a nested collection URL
  74. addy = StreetAddress.new(:street => '123 Test Dr.', :person_id => 1)
  75. assert_equal ActiveResource::Response.new({ :street => '12345 Street' }.to_xml(:root => 'address'),
  76. 201, {'Location' => '/people/1/addresses/2.xml'}),
  77. addy.post(:link)
  78. matz = Person.new(:id => 1, :name => 'Matz')
  79. assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register)
  80. end
  81. def test_find_custom_resources
  82. assert_equal 'Matz', Person.find(:all, :from => :managers).first.name
  83. end
  84. end