1 require 'abstract_unit'
2 require 'fixtures/person'
3 require 'fixtures/street_address'
5 class CustomMethodsTest
< Test
::Unit::TestCase
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')
14 ActiveResource
::HttpMock.respond_to
do |mock
|
15 mock
.get
"/people/1.xml", {}, @matz
16 mock
.get
"/people/1/shallow.xml", {}, @matz
17 mock
.get
"/people/1/deep.xml", {}, @matz_deep
18 mock
.get
"/people/retrieve.xml?name=Matz", {}, @matz_array
19 mock
.get
"/people/managers.xml", {}, @matz_array
20 mock
.post
"/people/hire.xml?name=Matz", {}, nil, 201
21 mock
.put
"/people/1/promote.xml?position=Manager", {}, nil, 204
22 mock
.put
"/people/promote.xml?name=Matz", {}, nil, 204, {}
23 mock
.put
"/people/sort.xml?by=name", {}, nil, 204
24 mock
.delete
"/people/deactivate.xml?name=Matz", {}, nil, 200
25 mock
.delete
"/people/1/deactivate.xml", {}, nil, 200
26 mock
.post
"/people/new/register.xml", {}, @ryan, 201, 'Location' => '/people/5.xml'
27 mock
.post
"/people/1/register.xml", {}, @matz, 201
28 mock
.get
"/people/1/addresses/1.xml", {}, @addy
29 mock
.get
"/people/1/addresses/1/deep.xml", {}, @addy_deep
30 mock
.put
"/people/1/addresses/1/normalize_phone.xml?locale=US", {}, nil, 204
31 mock
.put
"/people/1/addresses/sort.xml?by=name", {}, nil, 204
32 mock
.post
"/people/1/addresses/new/link.xml", {}, { :street => '12345 Street' }.to_xml(:root => 'address'), 201, 'Location' => '/people/1/addresses/2.xml'
40 ActiveResource
::HttpMock.reset
!
43 def test_custom_collection_method
45 assert_equal([{ "id" => 1, "name" => 'Matz' }], Person
.get(:retrieve, :name => 'Matz'))
48 assert_equal(ActiveResource
::Response.new("", 201, {}), Person
.post(:hire, :name => 'Matz'))
51 assert_equal ActiveResource
::Response.new("", 204, {}),
52 Person
.put(:promote, {:name => 'Matz'}, 'atestbody')
53 assert_equal ActiveResource
::Response.new("", 204, {}), Person
.put(:sort, :by => 'name')
56 Person
.delete
:deactivate, :name => 'Matz'
59 assert_equal ActiveResource
::Response.new("", 204, {}), StreetAddress
.put(:sort, :person_id => 1, :by => 'name')
62 def test_custom_element_method
63 # Test GET against an element URL
64 assert_equal Person
.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'}
65 assert_equal Person
.find(1).get(:deep), {"id" => 1, "name" => 'Matz', "other" => 'other'}
67 # Test PUT against an element URL
68 assert_equal ActiveResource
::Response.new("", 204, {}), Person
.find(1).put(:promote, {:position => 'Manager'}, 'body')
70 # Test DELETE against an element URL
71 assert_equal ActiveResource
::Response.new("", 200, {}), Person
.find(1).delete(:deactivate)
73 # With nested resources
74 assert_equal StreetAddress
.find(1, :params => { :person_id => 1 }).get(:deep),
75 { "id" => 1, "street" => '12345 Street', "zip" => "27519" }
76 assert_equal ActiveResource
::Response.new("", 204, {}),
77 StreetAddress
.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US')
80 def test_custom_new_element_method
81 # Test POST against a new element URL
82 ryan
= Person
.new(:name => 'Ryan')
83 assert_equal ActiveResource
::Response.new(@ryan, 201, {'Location' => '/people/5.xml'}), ryan
.post(:register)
84 expected_request
= ActiveResource
::Request.new(:post, '/people/new/register.xml', @ryan)
85 assert_equal expected_request
.body
, ActiveResource
::HttpMock.requests
.first
.body
87 # Test POST against a nested collection URL
88 addy
= StreetAddress
.new(:street => '123 Test Dr.', :person_id => 1)
89 assert_equal ActiveResource
::Response.new({ :street => '12345 Street' }.to_xml(:root => 'address'),
90 201, {'Location' => '/people/1/addresses/2.xml'}),
93 matz
= Person
.new(:id => 1, :name => 'Matz')
94 assert_equal ActiveResource
::Response.new(@matz, 201), matz
.post(:register)
97 def test_find_custom_resources
98 assert_equal
'Matz', Person
.find(:all, :from => :managers).first
.name