1 require 'abstract_unit'
2 require "fixtures/person"
3 require "fixtures/customer"
4 require "fixtures/street_address"
5 require "fixtures/beast"
7 class BaseTest
< Test
::Unit::TestCase
9 @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
10 @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
11 @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person')
12 @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address')
13 @default_request_headers = { 'Content-Type' => 'application/xml' }
14 @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
15 @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people')
16 @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
17 @addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses')
19 # - deep nested resource -
21 # - JK (Customer::Friend)
22 # - Mateo (Customer::Friend::Brother)
23 # - Edith (Customer::Friend::Brother::Child)
24 # - Martha (Customer::Friend::Brother::Child)
25 # - Felipe (Customer::Friend::Brother)
26 # - Bryan (Customer::Friend::Brother::Child)
27 # - Luke (Customer::Friend::Brother::Child)
28 # - Eduardo (Customer::Friend)
29 # - Sebas (Customer::Friend::Brother)
30 # - Andres (Customer::Friend::Brother::Child)
31 # - Jorge (Customer::Friend::Brother::Child)
32 # - Elsa (Customer::Friend::Brother)
33 # - Natacha (Customer::Friend::Brother::Child)
34 # - Milena (Customer::Friend::Brother)
36 @luis = {:id => 1, :name => 'Luis',
37 :friends => [{:name => 'JK',
38 :brothers => [{:name => 'Mateo',
39 :children => [{:name => 'Edith'},{:name => 'Martha'}]},
41 :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]},
43 :brothers => [{:name => 'Sebas',
44 :children => [{:name => 'Andres'},{:name => 'Jorge'}]},
46 :children => [{:name => 'Natacha'}]},
48 :children => []}]}]}.to_xml(:root => 'customer')
49 # - resource with yaml array of strings; for ActiveRecords using serialize :bar, Array
51 <?xml version=\"1.0\" encoding=\"UTF-8\"?>
53 <id type=\"integer\">5</id>
55 <colors type=\"yaml\">---
63 ActiveResource
::HttpMock.respond_to
do |mock
|
64 mock
.get
"/people/1.xml", {}, @matz
65 mock
.get
"/people/2.xml", {}, @david
66 mock
.get
"/people/5.xml", {}, @marty
67 mock
.get
"/people/Greg.xml", {}, @greg
68 mock
.get
"/people/4.xml", {'key' => 'value'}, nil, 404
69 mock
.put
"/people/1.xml", {}, nil, 204
70 mock
.delete
"/people/1.xml", {}, nil, 200
71 mock
.delete
"/people/2.xml", {}, nil, 400
72 mock
.get
"/people/99.xml", {}, nil, 404
73 mock
.post
"/people.xml", {}, @rick, 201, 'Location' => '/people/5.xml'
74 mock
.get
"/people.xml", {}, @people
75 mock
.get
"/people/1/addresses.xml", {}, @addresses
76 mock
.get
"/people/1/addresses/1.xml", {}, @addy
77 mock
.get
"/people/1/addresses/2.xml", {}, nil, 404
78 mock
.get
"/people/2/addresses/1.xml", {}, nil, 404
79 mock
.get
"/people/Greg/addresses/1.xml", {}, @addy
80 mock
.put
"/people/1/addresses/1.xml", {}, nil, 204
81 mock
.delete
"/people/1/addresses/1.xml", {}, nil, 200
82 mock
.post
"/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5'
83 mock
.get
"/people//addresses.xml", {}, nil, 404
84 mock
.get
"/people//addresses/1.xml", {}, nil, 404
85 mock
.put
"/people//addresses/1.xml", {}, nil, 404
86 mock
.delete
"/people//addresses/1.xml", {}, nil, 404
87 mock
.post
"/people//addresses.xml", {}, nil, 404
88 mock
.head
"/people/1.xml", {}, nil, 200
89 mock
.head
"/people/Greg.xml", {}, nil, 200
90 mock
.head
"/people/99.xml", {}, nil, 404
91 mock
.head
"/people/1/addresses/1.xml", {}, nil, 200
92 mock
.head
"/people/1/addresses/2.xml", {}, nil, 404
93 mock
.head
"/people/2/addresses/1.xml", {}, nil, 404
94 mock
.head
"/people/Greg/addresses/1.xml", {}, nil, 200
96 mock
.get
"/customers/1.xml", {}, @luis
100 Person
.password
= nil
104 def test_site_accessor_accepts_uri_or_string_argument
105 site
= URI
.parse('http://localhost')
107 assert_nothing_raised
{ Person
.site
= 'http://localhost' }
108 assert_equal site
, Person
.site
110 assert_nothing_raised
{ Person
.site
= site
}
111 assert_equal site
, Person
.site
114 def test_should_use_site_prefix_and_credentials
115 assert_equal
'http://foo:bar@beast.caboo.se', Forum
.site
.to_s
116 assert_equal
'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic
.site
.to_s
119 def test_site_variable_can_be_reset
120 actor
= Class
.new(ActiveResource
::Base)
121 assert_nil actor
.site
122 actor
.site
= 'http://localhost:31337'
124 assert_nil actor
.site
127 def test_should_accept_setting_user
129 assert_equal('david', Forum
.user
)
130 assert_equal('david', Forum
.connection
.user
)
133 def test_should_accept_setting_password
134 Forum
.password
= 'test123'
135 assert_equal('test123', Forum
.password
)
136 assert_equal('test123', Forum
.connection
.password
)
139 def test_should_accept_setting_timeout
141 assert_equal(5, Forum
.timeout
)
142 assert_equal(5, Forum
.connection
.timeout
)
145 def test_user_variable_can_be_reset
146 actor
= Class
.new(ActiveResource
::Base)
147 actor
.site
= 'http://cinema'
148 assert_nil actor
.user
149 actor
.user
= 'username'
151 assert_nil actor
.user
152 assert_nil actor
.connection
.user
155 def test_password_variable_can_be_reset
156 actor
= Class
.new(ActiveResource
::Base)
157 actor
.site
= 'http://cinema'
158 assert_nil actor
.password
159 actor
.password
= 'username'
161 assert_nil actor
.password
162 assert_nil actor
.connection
.password
165 def test_timeout_variable_can_be_reset
166 actor
= Class
.new(ActiveResource
::Base)
167 actor
.site
= 'http://cinema'
168 assert_nil actor
.timeout
171 assert_nil actor
.timeout
172 assert_nil actor
.connection
.timeout
175 def test_credentials_from_site_are_decoded
176 actor
= Class
.new(ActiveResource
::Base)
177 actor
.site
= 'http://my%40email.com:%31%32%33@cinema'
178 assert_equal("my@email.com", actor
.user
)
179 assert_equal("123", actor
.password
)
182 def test_site_reader_uses_superclass_site_until_written
183 # Superclass is Object so returns nil.
184 assert_nil ActiveResource
::Base.site
185 assert_nil Class
.new(ActiveResource
::Base).site
187 # Subclass uses superclass site.
188 actor
= Class
.new(Person
)
189 assert_equal Person
.site
, actor
.site
191 # Subclass returns frozen superclass copy.
192 assert
!Person
.site
.frozen
?
193 assert actor
.site
.frozen
?
195 # Changing subclass site doesn't change superclass site.
196 actor
.site
= 'http://localhost:31337'
197 assert_not_equal Person
.site
, actor
.site
199 # Changed subclass site is not frozen.
200 assert
!actor
.site
.frozen
?
202 # Changing superclass site doesn't overwrite subclass site.
203 Person
.site
= 'http://somewhere.else'
204 assert_not_equal Person
.site
, actor
.site
206 # Changing superclass site after subclassing changes subclass site.
207 jester
= Class
.new(actor
)
208 actor
.site
= 'http://nomad'
209 assert_equal actor
.site
, jester
.site
210 assert jester
.site
.frozen
?
212 # Subclasses are always equal to superclass site when not overridden
213 fruit
= Class
.new(ActiveResource
::Base)
214 apple
= Class
.new(fruit
)
216 fruit
.site
= 'http://market'
217 assert_equal fruit
.site
, apple
.site
, 'subclass did not adopt changes from parent class'
219 fruit
.site
= 'http://supermarket'
220 assert_equal fruit
.site
, apple
.site
, 'subclass did not adopt changes from parent class'
223 def test_user_reader_uses_superclass_user_until_written
224 # Superclass is Object so returns nil.
225 assert_nil ActiveResource
::Base.user
226 assert_nil Class
.new(ActiveResource
::Base).user
227 Person
.user
= 'anonymous'
229 # Subclass uses superclass user.
230 actor
= Class
.new(Person
)
231 assert_equal Person
.user
, actor
.user
233 # Subclass returns frozen superclass copy.
234 assert
!Person
.user
.frozen
?
235 assert actor
.user
.frozen
?
237 # Changing subclass user doesn't change superclass user.
239 assert_not_equal Person
.user
, actor
.user
241 # Changing superclass user doesn't overwrite subclass user.
243 assert_not_equal Person
.user
, actor
.user
245 # Changing superclass user after subclassing changes subclass user.
246 jester
= Class
.new(actor
)
247 actor
.user
= 'john.doe'
248 assert_equal actor
.user
, jester
.user
250 # Subclasses are always equal to superclass user when not overridden
251 fruit
= Class
.new(ActiveResource
::Base)
252 apple
= Class
.new(fruit
)
254 fruit
.user
= 'manager'
255 assert_equal fruit
.user
, apple
.user
, 'subclass did not adopt changes from parent class'
257 fruit
.user
= 'client'
258 assert_equal fruit
.user
, apple
.user
, 'subclass did not adopt changes from parent class'
261 def test_password_reader_uses_superclass_password_until_written
262 # Superclass is Object so returns nil.
263 assert_nil ActiveResource
::Base.password
264 assert_nil Class
.new(ActiveResource
::Base).password
265 Person
.password
= 'my-password'
267 # Subclass uses superclass password.
268 actor
= Class
.new(Person
)
269 assert_equal Person
.password
, actor
.password
271 # Subclass returns frozen superclass copy.
272 assert
!Person
.password
.frozen
?
273 assert actor
.password
.frozen
?
275 # Changing subclass password doesn't change superclass password.
276 actor
.password
= 'secret'
277 assert_not_equal Person
.password
, actor
.password
279 # Changing superclass password doesn't overwrite subclass password.
280 Person
.password
= 'super-secret'
281 assert_not_equal Person
.password
, actor
.password
283 # Changing superclass password after subclassing changes subclass password.
284 jester
= Class
.new(actor
)
285 actor
.password
= 'even-more-secret'
286 assert_equal actor
.password
, jester
.password
288 # Subclasses are always equal to superclass password when not overridden
289 fruit
= Class
.new(ActiveResource
::Base)
290 apple
= Class
.new(fruit
)
292 fruit
.password
= 'mega-secret'
293 assert_equal fruit
.password
, apple
.password
, 'subclass did not adopt changes from parent class'
295 fruit
.password
= 'ok-password'
296 assert_equal fruit
.password
, apple
.password
, 'subclass did not adopt changes from parent class'
299 def test_timeout_reader_uses_superclass_timeout_until_written
300 # Superclass is Object so returns nil.
301 assert_nil ActiveResource
::Base.timeout
302 assert_nil Class
.new(ActiveResource
::Base).timeout
305 # Subclass uses superclass timeout.
306 actor
= Class
.new(Person
)
307 assert_equal Person
.timeout
, actor
.timeout
309 # Changing subclass timeout doesn't change superclass timeout.
311 assert_not_equal Person
.timeout
, actor
.timeout
313 # Changing superclass timeout doesn't overwrite subclass timeout.
315 assert_not_equal Person
.timeout
, actor
.timeout
317 # Changing superclass timeout after subclassing changes subclass timeout.
318 jester
= Class
.new(actor
)
320 assert_equal actor
.timeout
, jester
.timeout
322 # Subclasses are always equal to superclass timeout when not overridden.
323 fruit
= Class
.new(ActiveResource
::Base)
324 apple
= Class
.new(fruit
)
327 assert_equal fruit
.timeout
, apple
.timeout
, 'subclass did not adopt changes from parent class'
330 assert_equal fruit
.timeout
, apple
.timeout
, 'subclass did not adopt changes from parent class'
333 def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects
334 # Subclasses are always equal to superclass site when not overridden
335 fruit
= Class
.new(ActiveResource
::Base)
336 apple
= Class
.new(fruit
)
338 fruit
.site
= 'http://market'
339 assert_equal fruit
.connection
.site
, apple
.connection
.site
340 first_connection
= apple
.connection
.object_id
342 fruit
.site
= 'http://supermarket'
343 assert_equal fruit
.connection
.site
, apple
.connection
.site
344 second_connection
= apple
.connection
.object_id
345 assert_not_equal(first_connection
, second_connection
, 'Connection should be re-created')
348 def test_updating_baseclass_user_wipes_descendent_cached_connection_objects
349 # Subclasses are always equal to superclass user when not overridden
350 fruit
= Class
.new(ActiveResource
::Base)
351 apple
= Class
.new(fruit
)
352 fruit
.site
= 'http://market'
355 assert_equal fruit
.connection
.user
, apple
.connection
.user
356 first_connection
= apple
.connection
.object_id
359 assert_equal fruit
.connection
.user
, apple
.connection
.user
360 second_connection
= apple
.connection
.object_id
361 assert_not_equal(first_connection
, second_connection
, 'Connection should be re-created')
364 def test_updating_baseclass_password_wipes_descendent_cached_connection_objects
365 # Subclasses are always equal to superclass password when not overridden
366 fruit
= Class
.new(ActiveResource
::Base)
367 apple
= Class
.new(fruit
)
368 fruit
.site
= 'http://market'
370 fruit
.password
= 'secret'
371 assert_equal fruit
.connection
.password
, apple
.connection
.password
372 first_connection
= apple
.connection
.object_id
374 fruit
.password
= 'supersecret'
375 assert_equal fruit
.connection
.password
, apple
.connection
.password
376 second_connection
= apple
.connection
.object_id
377 assert_not_equal(first_connection
, second_connection
, 'Connection should be re-created')
380 def test_updating_baseclass_timeout_wipes_descendent_cached_connection_objects
381 # Subclasses are always equal to superclass timeout when not overridden
382 fruit
= Class
.new(ActiveResource
::Base)
383 apple
= Class
.new(fruit
)
384 fruit
.site
= 'http://market'
387 assert_equal fruit
.connection
.timeout
, apple
.connection
.timeout
388 first_connection
= apple
.connection
.object_id
391 assert_equal fruit
.connection
.timeout
, apple
.connection
.timeout
392 second_connection
= apple
.connection
.object_id
393 assert_not_equal(first_connection
, second_connection
, 'Connection should be re-created')
396 def test_collection_name
397 assert_equal
"people", Person
.collection_name
400 def test_collection_path
401 assert_equal
'/people.xml', Person
.collection_path
404 def test_collection_path_with_parameters
405 assert_equal
'/people.xml?gender=male', Person
.collection_path(:gender => 'male')
406 assert_equal
'/people.xml?gender=false', Person
.collection_path(:gender => false)
407 assert_equal
'/people.xml?gender=', Person
.collection_path(:gender => nil)
409 assert_equal
'/people.xml?gender=male', Person
.collection_path('gender' => 'male')
411 # Use includes? because ordering of param hash is not guaranteed
412 assert Person
.collection_path(:gender => 'male', :student => true).include?('/people.xml?')
413 assert Person
.collection_path(:gender => 'male', :student => true).include?('gender=male')
414 assert Person
.collection_path(:gender => 'male', :student => true).include?('student=true')
416 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])
418 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'})
421 def test_custom_element_path
422 assert_equal
'/people/1/addresses/1.xml', StreetAddress
.element_path(1, :person_id => 1)
423 assert_equal
'/people/1/addresses/1.xml', StreetAddress
.element_path(1, 'person_id' => 1)
424 assert_equal
'/people/Greg/addresses/1.xml', StreetAddress
.element_path(1, 'person_id' => 'Greg')
427 def test_custom_element_path_with_redefined_to_param
428 Person
.module_eval
do
429 alias_method
:original_to_param_element_path, :to_param
436 assert_equal
'/people/Greg.xml', Person
.element_path('Greg')
438 # Protected Instance method.
439 assert_equal
'/people/Greg.xml', Person
.find('Greg').send(:element_path)
442 # revert back to original
443 Person
.module_eval
do
444 # save the 'new' to_param so we don't get a warning about discarding the method
445 alias_method
:element_path_to_param, :to_param
446 alias_method
:to_param, :original_to_param_element_path
450 def test_custom_element_path_with_parameters
451 assert_equal
'/people/1/addresses/1.xml?type=work', StreetAddress
.element_path(1, :person_id => 1, :type => 'work')
452 assert_equal
'/people/1/addresses/1.xml?type=work', StreetAddress
.element_path(1, 'person_id' => 1, :type => 'work')
453 assert_equal
'/people/1/addresses/1.xml?type=work', StreetAddress
.element_path(1, :type => 'work', :person_id => 1)
454 assert_equal
'/people/1/addresses/1.xml?type%5B%5D=work&type%5B%5D=play+time', StreetAddress
.element_path(1, :person_id => 1, :type => ['work', 'play time'])
457 def test_custom_element_path_with_prefix_and_parameters
458 assert_equal
'/people/1/addresses/1.xml?type=work', StreetAddress
.element_path(1, {:person_id => 1}, {:type => 'work'})
461 def test_custom_collection_path
462 assert_equal
'/people/1/addresses.xml', StreetAddress
.collection_path(:person_id => 1)
463 assert_equal
'/people/1/addresses.xml', StreetAddress
.collection_path('person_id' => 1)
466 def test_custom_collection_path_with_parameters
467 assert_equal
'/people/1/addresses.xml?type=work', StreetAddress
.collection_path(:person_id => 1, :type => 'work')
468 assert_equal
'/people/1/addresses.xml?type=work', StreetAddress
.collection_path('person_id' => 1, :type => 'work')
471 def test_custom_collection_path_with_prefix_and_parameters
472 assert_equal
'/people/1/addresses.xml?type=work', StreetAddress
.collection_path({:person_id => 1}, {:type => 'work'})
475 def test_custom_element_name
476 assert_equal
'address', StreetAddress
.element_name
479 def test_custom_collection_name
480 assert_equal
'addresses', StreetAddress
.collection_name
484 assert_equal
"/", Person
.prefix
485 assert_equal Set
.new
, Person
.__send__(:prefix_parameters)
489 SetterTrap
.rollback_sets(Person
) do |person_class
|
490 person_class
.prefix
= "the_prefix"
491 assert_equal
"the_prefix", person_class
.prefix
495 def test_set_prefix_with_inline_keys
496 SetterTrap
.rollback_sets(Person
) do |person_class
|
497 person_class
.prefix
= "the_prefix:the_param"
498 assert_equal
"the_prefixthe_param_value", person_class
.prefix(:the_param => "the_param_value")
502 def test_set_prefix_twice_should_clear_params
503 SetterTrap
.rollback_sets(Person
) do |person_class
|
504 person_class
.prefix
= "the_prefix/:the_param1"
505 assert_equal Set
.new([:the_param1]), person_class
.prefix_parameters
506 person_class
.prefix
= "the_prefix/:the_param2"
507 assert_equal Set
.new([:the_param2]), person_class
.prefix_parameters
511 def test_set_prefix_with_default_value
512 SetterTrap
.rollback_sets(Person
) do |person_class
|
513 person_class
.set_prefix
514 assert_equal
"/", person_class
.prefix
518 def test_custom_prefix
519 assert_equal
'/people//', StreetAddress
.prefix
520 assert_equal
'/people/1/', StreetAddress
.prefix(:person_id => 1)
521 assert_equal
[:person_id].to_set
, StreetAddress
.__send__(:prefix_parameters)
525 matz
= Person
.find(1)
526 assert_kind_of Person
, matz
527 assert_equal
"Matz", matz
.name
532 matz
= Person
.find(1)
533 assert matz
.respond_to
?(:name)
534 assert matz
.respond_to
?(:name=)
535 assert matz
.respond_to
?(:name?)
536 assert
!matz
.respond_to
?(:super_scalable_stuff)
539 def test_find_by_id_with_custom_prefix
540 addy
= StreetAddress
.find(1, :params => { :person_id => 1 })
541 assert_kind_of StreetAddress
, addy
542 assert_equal
'12345 Street', addy
.street
546 all
= Person
.find(:all)
547 assert_equal
2, all
.size
548 assert_kind_of Person
, all
.first
549 assert_equal
"Matz", all
.first
.name
550 assert_equal
"David", all
.last
.name
554 matz
= Person
.find(:first)
555 assert_kind_of Person
, matz
556 assert_equal
"Matz", matz
.name
560 david
= Person
.find(:last)
561 assert_kind_of Person
, david
562 assert_equal
'David', david
.name
565 def test_custom_header
566 Person
.headers
['key'] = 'value'
567 assert_raises(ActiveResource
::ResourceNotFound) { Person
.find(4) }
569 Person
.headers
.delete('key')
572 def test_find_by_id_not_found
573 assert_raises(ActiveResource
::ResourceNotFound) { Person
.find(99) }
574 assert_raises(ActiveResource
::ResourceNotFound) { StreetAddress
.find(1) }
577 def test_find_all_by_from
578 ActiveResource
::HttpMock.respond_to
{ |m
| m
.get
"/companies/1/people.xml", {}, @people_david }
580 people
= Person
.find(:all, :from => "/companies/1/people.xml")
581 assert_equal
1, people
.size
582 assert_equal
"David", people
.first
.name
585 def test_find_all_by_from_with_options
586 ActiveResource
::HttpMock.respond_to
{ |m
| m
.get
"/companies/1/people.xml", {}, @people_david }
588 people
= Person
.find(:all, :from => "/companies/1/people.xml")
589 assert_equal
1, people
.size
590 assert_equal
"David", people
.first
.name
593 def test_find_all_by_symbol_from
594 ActiveResource
::HttpMock.respond_to
{ |m
| m
.get
"/people/managers.xml", {}, @people_david }
596 people
= Person
.find(:all, :from => :managers)
597 assert_equal
1, people
.size
598 assert_equal
"David", people
.first
.name
601 def test_find_single_by_from
602 ActiveResource
::HttpMock.respond_to
{ |m
| m
.get
"/companies/1/manager.xml", {}, @david }
604 david
= Person
.find(:one, :from => "/companies/1/manager.xml")
605 assert_equal
"David", david
.name
608 def test_find_single_by_symbol_from
609 ActiveResource
::HttpMock.respond_to
{ |m
| m
.get
"/people/leader.xml", {}, @david }
611 david
= Person
.find(:one, :from => :leader)
612 assert_equal
"David", david
.name
617 assert_equal
true, rick
.save
618 assert_equal
'5', rick
.id
621 def test_id_from_response
623 resp
= {'Location' => '/foo/bar/1'}
624 assert_equal
'1', p
.__send__(:id_from_response, resp
)
626 resp
['Location'] << '.xml'
627 assert_equal
'1', p
.__send__(:id_from_response, resp
)
630 def test_create_with_custom_prefix
631 matzs_house
= StreetAddress
.new(:person_id => 1)
633 assert_equal
'5', matzs_house
.id
636 # Test that loading a resource preserves its prefix_options.
637 def test_load_preserves_prefix_options
638 address
= StreetAddress
.find(1, :params => { :person_id => 1 })
639 ryan
= Person
.new(:id => 1, :name => 'Ryan', :address => address
)
640 assert_equal address
.prefix_options
, ryan
.address
.prefix_options
643 def test_reload_works_with_prefix_options
644 address
= StreetAddress
.find(1, :params => { :person_id => 1 })
645 assert_equal address
, address
.reload
648 def test_reload_with_redefined_to_param
649 Person
.module_eval
do
650 alias_method
:original_to_param_reload, :to_param
656 person
= Person
.find('Greg')
657 assert_equal person
, person
.reload
660 # revert back to original
661 Person
.module_eval
do
662 # save the 'new' to_param so we don't get a warning about discarding the method
663 alias_method
:reload_to_param, :to_param
664 alias_method
:to_param, :original_to_param_reload
668 def test_reload_works_without_prefix_options
669 person
= Person
.find(:first)
670 assert_equal person
, person
.reload
675 rick
= Person
.create(:name => 'Rick')
678 assert_equal
'5', rick
.id
680 # test additional attribute returned on create
681 assert_equal
25, rick
.age
683 # Test that save exceptions get bubbled up too
684 ActiveResource
::HttpMock.respond_to
do |mock
|
685 mock
.post
"/people.xml", {}, nil, 409
687 assert_raises(ActiveResource
::ResourceConflict) { Person
.create(:name => 'Rick') }
691 matz
= Person
.find(1)
694 matz
.attributes
.each
do |k
, v
|
695 assert_equal v
, matz_c
.send(k
) if k
!= Person
.primary_key
699 def test_nested_clone
700 addy
= StreetAddress
.find(1, :params => {:person_id => 1})
703 addy
.attributes
.each
do |k
, v
|
704 assert_equal v
, addy_c
.send(k
) if k
!= StreetAddress
.primary_key
706 assert_equal addy
.prefix_options
, addy_c
.prefix_options
709 def test_complex_clone
710 matz
= Person
.find(1)
711 matz
.address
= StreetAddress
.find(1, :params => {:person_id => matz
.id
})
712 matz
.non_ar_hash
= {:not => "an ARes instance"}
713 matz
.non_ar_arr
= ["not", "ARes"]
716 assert_raises(NoMethodError
) {matz_c
.address
}
717 assert_equal matz
.non_ar_hash
, matz_c
.non_ar_hash
718 assert_equal matz
.non_ar_arr
, matz_c
.non_ar_arr
720 # Test that actual copy, not just reference copy
721 matz
.non_ar_hash
[:not] = "changed"
722 assert_not_equal matz
.non_ar_hash
, matz_c
.non_ar_hash
726 matz
= Person
.find(:first)
728 assert_kind_of Person
, matz
729 assert_equal
"David", matz
.name
730 assert_equal
true, matz
.save
733 def test_update_with_custom_prefix_with_specific_id
734 addy
= StreetAddress
.find(1, :params => { :person_id => 1 })
735 addy
.street
= "54321 Street"
736 assert_kind_of StreetAddress
, addy
737 assert_equal
"54321 Street", addy
.street
741 def test_update_with_custom_prefix_without_specific_id
742 addy
= StreetAddress
.find(:first, :params => { :person_id => 1 })
743 addy
.street
= "54321 Lane"
744 assert_kind_of StreetAddress
, addy
745 assert_equal
"54321 Lane", addy
.street
749 def test_update_conflict
750 ActiveResource
::HttpMock.respond_to
do |mock
|
751 mock
.get
"/people/2.xml", {}, @david
752 mock
.put
"/people/2.xml", @default_request_headers, nil, 409
754 assert_raises(ActiveResource
::ResourceConflict) { Person
.find(2).save
}
758 assert Person
.find(1).destroy
759 ActiveResource
::HttpMock.respond_to
do |mock
|
760 mock
.get
"/people/1.xml", {}, nil, 404
762 assert_raises(ActiveResource
::ResourceNotFound) { Person
.find(1).destroy
}
765 def test_destroy_with_custom_prefix
766 assert StreetAddress
.find(1, :params => { :person_id => 1 }).destroy
767 ActiveResource
::HttpMock.respond_to
do |mock
|
768 mock
.get
"/people/1/addresses/1.xml", {}, nil, 404
770 assert_raises(ActiveResource
::ResourceNotFound) { StreetAddress
.find(1, :params => { :person_id => 1 }) }
774 assert Person
.delete(1)
775 ActiveResource
::HttpMock.respond_to
do |mock
|
776 mock
.get
"/people/1.xml", {}, nil, 404
778 assert_raises(ActiveResource
::ResourceNotFound) { Person
.find(1) }
781 def test_delete_with_custom_prefix
782 assert StreetAddress
.delete(1, :person_id => 1)
783 ActiveResource
::HttpMock.respond_to
do |mock
|
784 mock
.get
"/people/1/addresses/1.xml", {}, nil, 404
786 assert_raises(ActiveResource
::ResourceNotFound) { StreetAddress
.find(1, :params => { :person_id => 1 }) }
791 assert
!Person
.exists
?(nil)
792 assert Person
.exists
?(1)
793 assert
!Person
.exists
?(99)
796 assert
!Person
.new
.exists
?
797 assert Person
.find(1).exists
?
798 assert
!Person
.new(:id => 99).exists
?
800 # Nested class method.
801 assert StreetAddress
.exists
?(1, :params => { :person_id => 1 })
802 assert
!StreetAddress
.exists
?(1, :params => { :person_id => 2 })
803 assert
!StreetAddress
.exists
?(2, :params => { :person_id => 1 })
805 # Nested instance method.
806 assert StreetAddress
.find(1, :params => { :person_id => 1 }).exists
?
807 assert
!StreetAddress
.new({:id => 1, :person_id => 2}).exists
?
808 assert
!StreetAddress
.new({:id => 2, :person_id => 1}).exists
?
811 def test_exists_with_redefined_to_param
812 Person
.module_eval
do
813 alias_method
:original_to_param_exists, :to_param
820 assert Person
.exists
?('Greg')
823 assert Person
.find('Greg').exists
?
825 # Nested class method.
826 assert StreetAddress
.exists
?(1, :params => { :person_id => Person
.find('Greg').to_param
})
828 # Nested instance method.
829 assert StreetAddress
.find(1, :params => { :person_id => Person
.find('Greg').to_param
}).exists
?
832 # revert back to original
833 Person
.module_eval
do
834 # save the 'new' to_param so we don't get a warning about discarding the method
835 alias_method
:exists_to_param, :to_param
836 alias_method
:to_param, :original_to_param_exists
841 matz
= Person
.find(1)
843 assert xml
.starts_with
?('<?xml version="1.0" encoding="UTF-8"?>')
844 assert xml
.include?('<name>Matz</name>')
845 assert xml
.include?('<id type="integer">1</id>')
848 def test_to_param_quacks_like_active_record
849 new_person
= Person
.new
850 assert_nil new_person
.to_param
851 matz
= Person
.find(1)
852 assert_equal
'1', matz
.to_param
855 def test_parse_deep_nested_resources
856 luis
= Customer
.find(1)
857 assert_kind_of Customer
, luis
858 luis
.friends
.each
do |friend
|
859 assert_kind_of Customer
::Friend, friend
860 friend
.brothers
.each
do |brother
|
861 assert_kind_of Customer
::Friend::Brother, brother
862 brother
.children
.each
do |child
|
863 assert_kind_of Customer
::Friend::Brother::Child, child
869 def test_load_yaml_array
870 assert_nothing_raised
do
871 marty
= Person
.find(5)
872 assert_equal
3, marty
.colors
.size
873 marty
.colors
.each
do |color
|
874 assert_kind_of String
, color