Froze rails gems
[depot.git] / vendor / rails / activeresource / test / base_test.rb
1 require 'abstract_unit'
2 require "fixtures/person"
3 require "fixtures/customer"
4 require "fixtures/street_address"
5 require "fixtures/beast"
6
7 class BaseTest < Test::Unit::TestCase
8 def setup
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')
18
19 # - deep nested resource -
20 # - Luis (Customer)
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)
35 #
36 @luis = {:id => 1, :name => 'Luis',
37 :friends => [{:name => 'JK',
38 :brothers => [{:name => 'Mateo',
39 :children => [{:name => 'Edith'},{:name => 'Martha'}]},
40 {:name => 'Felipe',
41 :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]},
42 {:name => 'Eduardo',
43 :brothers => [{:name => 'Sebas',
44 :children => [{:name => 'Andres'},{:name => 'Jorge'}]},
45 {:name => 'Elsa',
46 :children => [{:name => 'Natacha'}]},
47 {:name => 'Milena',
48 :children => []}]}]}.to_xml(:root => 'customer')
49 # - resource with yaml array of strings; for ActiveRecords using serialize :bar, Array
50 @marty = <<-eof
51 <?xml version=\"1.0\" encoding=\"UTF-8\"?>
52 <person>
53 <id type=\"integer\">5</id>
54 <name>Marty</name>
55 <colors type=\"yaml\">---
56 - \"red\"
57 - \"green\"
58 - \"blue\"
59 </colors>
60 </person>
61 eof
62
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
95 # customer
96 mock.get "/customers/1.xml", {}, @luis
97 end
98
99 Person.user = nil
100 Person.password = nil
101 end
102
103
104 def test_site_accessor_accepts_uri_or_string_argument
105 site = URI.parse('http://localhost')
106
107 assert_nothing_raised { Person.site = 'http://localhost' }
108 assert_equal site, Person.site
109
110 assert_nothing_raised { Person.site = site }
111 assert_equal site, Person.site
112 end
113
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
117 end
118
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'
123 actor.site = nil
124 assert_nil actor.site
125 end
126
127 def test_should_accept_setting_user
128 Forum.user = 'david'
129 assert_equal('david', Forum.user)
130 assert_equal('david', Forum.connection.user)
131 end
132
133 def test_should_accept_setting_password
134 Forum.password = 'test123'
135 assert_equal('test123', Forum.password)
136 assert_equal('test123', Forum.connection.password)
137 end
138
139 def test_should_accept_setting_timeout
140 Forum.timeout = 5
141 assert_equal(5, Forum.timeout)
142 assert_equal(5, Forum.connection.timeout)
143 end
144
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'
150 actor.user = nil
151 assert_nil actor.user
152 assert_nil actor.connection.user
153 end
154
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'
160 actor.password = nil
161 assert_nil actor.password
162 assert_nil actor.connection.password
163 end
164
165 def test_timeout_variable_can_be_reset
166 actor = Class.new(ActiveResource::Base)
167 actor.site = 'http://cinema'
168 assert_nil actor.timeout
169 actor.timeout = 5
170 actor.timeout = nil
171 assert_nil actor.timeout
172 assert_nil actor.connection.timeout
173 end
174
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)
180 end
181
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
186
187 # Subclass uses superclass site.
188 actor = Class.new(Person)
189 assert_equal Person.site, actor.site
190
191 # Subclass returns frozen superclass copy.
192 assert !Person.site.frozen?
193 assert actor.site.frozen?
194
195 # Changing subclass site doesn't change superclass site.
196 actor.site = 'http://localhost:31337'
197 assert_not_equal Person.site, actor.site
198
199 # Changed subclass site is not frozen.
200 assert !actor.site.frozen?
201
202 # Changing superclass site doesn't overwrite subclass site.
203 Person.site = 'http://somewhere.else'
204 assert_not_equal Person.site, actor.site
205
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?
211
212 # Subclasses are always equal to superclass site when not overridden
213 fruit = Class.new(ActiveResource::Base)
214 apple = Class.new(fruit)
215
216 fruit.site = 'http://market'
217 assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class'
218
219 fruit.site = 'http://supermarket'
220 assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class'
221 end
222
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'
228
229 # Subclass uses superclass user.
230 actor = Class.new(Person)
231 assert_equal Person.user, actor.user
232
233 # Subclass returns frozen superclass copy.
234 assert !Person.user.frozen?
235 assert actor.user.frozen?
236
237 # Changing subclass user doesn't change superclass user.
238 actor.user = 'david'
239 assert_not_equal Person.user, actor.user
240
241 # Changing superclass user doesn't overwrite subclass user.
242 Person.user = 'john'
243 assert_not_equal Person.user, actor.user
244
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
249
250 # Subclasses are always equal to superclass user when not overridden
251 fruit = Class.new(ActiveResource::Base)
252 apple = Class.new(fruit)
253
254 fruit.user = 'manager'
255 assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class'
256
257 fruit.user = 'client'
258 assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class'
259 end
260
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'
266
267 # Subclass uses superclass password.
268 actor = Class.new(Person)
269 assert_equal Person.password, actor.password
270
271 # Subclass returns frozen superclass copy.
272 assert !Person.password.frozen?
273 assert actor.password.frozen?
274
275 # Changing subclass password doesn't change superclass password.
276 actor.password = 'secret'
277 assert_not_equal Person.password, actor.password
278
279 # Changing superclass password doesn't overwrite subclass password.
280 Person.password = 'super-secret'
281 assert_not_equal Person.password, actor.password
282
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
287
288 # Subclasses are always equal to superclass password when not overridden
289 fruit = Class.new(ActiveResource::Base)
290 apple = Class.new(fruit)
291
292 fruit.password = 'mega-secret'
293 assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class'
294
295 fruit.password = 'ok-password'
296 assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class'
297 end
298
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
303 Person.timeout = 5
304
305 # Subclass uses superclass timeout.
306 actor = Class.new(Person)
307 assert_equal Person.timeout, actor.timeout
308
309 # Changing subclass timeout doesn't change superclass timeout.
310 actor.timeout = 10
311 assert_not_equal Person.timeout, actor.timeout
312
313 # Changing superclass timeout doesn't overwrite subclass timeout.
314 Person.timeout = 15
315 assert_not_equal Person.timeout, actor.timeout
316
317 # Changing superclass timeout after subclassing changes subclass timeout.
318 jester = Class.new(actor)
319 actor.timeout = 20
320 assert_equal actor.timeout, jester.timeout
321
322 # Subclasses are always equal to superclass timeout when not overridden.
323 fruit = Class.new(ActiveResource::Base)
324 apple = Class.new(fruit)
325
326 fruit.timeout = 25
327 assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class'
328
329 fruit.timeout = 30
330 assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class'
331 end
332
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)
337
338 fruit.site = 'http://market'
339 assert_equal fruit.connection.site, apple.connection.site
340 first_connection = apple.connection.object_id
341
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')
346 end
347
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'
353
354 fruit.user = 'david'
355 assert_equal fruit.connection.user, apple.connection.user
356 first_connection = apple.connection.object_id
357
358 fruit.user = 'john'
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')
362 end
363
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'
369
370 fruit.password = 'secret'
371 assert_equal fruit.connection.password, apple.connection.password
372 first_connection = apple.connection.object_id
373
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')
378 end
379
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'
385
386 fruit.timeout = 5
387 assert_equal fruit.connection.timeout, apple.connection.timeout
388 first_connection = apple.connection.object_id
389
390 fruit.timeout = 10
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')
394 end
395
396 def test_collection_name
397 assert_equal "people", Person.collection_name
398 end
399
400 def test_collection_path
401 assert_equal '/people.xml', Person.collection_path
402 end
403
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)
408
409 assert_equal '/people.xml?gender=male', Person.collection_path('gender' => 'male')
410
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')
415
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])
417
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'})
419 end
420
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')
425 end
426
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
430 def to_param
431 name
432 end
433 end
434
435 # Class method.
436 assert_equal '/people/Greg.xml', Person.element_path('Greg')
437
438 # Protected Instance method.
439 assert_equal '/people/Greg.xml', Person.find('Greg').send(:element_path)
440
441 ensure
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
447 end
448 end
449
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'])
455 end
456
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'})
459 end
460
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)
464 end
465
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')
469 end
470
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'})
473 end
474
475 def test_custom_element_name
476 assert_equal 'address', StreetAddress.element_name
477 end
478
479 def test_custom_collection_name
480 assert_equal 'addresses', StreetAddress.collection_name
481 end
482
483 def test_prefix
484 assert_equal "/", Person.prefix
485 assert_equal Set.new, Person.__send__(:prefix_parameters)
486 end
487
488 def test_set_prefix
489 SetterTrap.rollback_sets(Person) do |person_class|
490 person_class.prefix = "the_prefix"
491 assert_equal "the_prefix", person_class.prefix
492 end
493 end
494
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")
499 end
500 end
501
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
508 end
509 end
510
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
515 end
516 end
517
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)
522 end
523
524 def test_find_by_id
525 matz = Person.find(1)
526 assert_kind_of Person, matz
527 assert_equal "Matz", matz.name
528 assert matz.name?
529 end
530
531 def test_respond_to
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)
537 end
538
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
543 end
544
545 def test_find_all
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
551 end
552
553 def test_find_first
554 matz = Person.find(:first)
555 assert_kind_of Person, matz
556 assert_equal "Matz", matz.name
557 end
558
559 def test_find_last
560 david = Person.find(:last)
561 assert_kind_of Person, david
562 assert_equal 'David', david.name
563 end
564
565 def test_custom_header
566 Person.headers['key'] = 'value'
567 assert_raises(ActiveResource::ResourceNotFound) { Person.find(4) }
568 ensure
569 Person.headers.delete('key')
570 end
571
572 def test_find_by_id_not_found
573 assert_raises(ActiveResource::ResourceNotFound) { Person.find(99) }
574 assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
575 end
576
577 def test_find_all_by_from
578 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david }
579
580 people = Person.find(:all, :from => "/companies/1/people.xml")
581 assert_equal 1, people.size
582 assert_equal "David", people.first.name
583 end
584
585 def test_find_all_by_from_with_options
586 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david }
587
588 people = Person.find(:all, :from => "/companies/1/people.xml")
589 assert_equal 1, people.size
590 assert_equal "David", people.first.name
591 end
592
593 def test_find_all_by_symbol_from
594 ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.xml", {}, @people_david }
595
596 people = Person.find(:all, :from => :managers)
597 assert_equal 1, people.size
598 assert_equal "David", people.first.name
599 end
600
601 def test_find_single_by_from
602 ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/manager.xml", {}, @david }
603
604 david = Person.find(:one, :from => "/companies/1/manager.xml")
605 assert_equal "David", david.name
606 end
607
608 def test_find_single_by_symbol_from
609 ActiveResource::HttpMock.respond_to { |m| m.get "/people/leader.xml", {}, @david }
610
611 david = Person.find(:one, :from => :leader)
612 assert_equal "David", david.name
613 end
614
615 def test_save
616 rick = Person.new
617 assert_equal true, rick.save
618 assert_equal '5', rick.id
619 end
620
621 def test_id_from_response
622 p = Person.new
623 resp = {'Location' => '/foo/bar/1'}
624 assert_equal '1', p.__send__(:id_from_response, resp)
625
626 resp['Location'] << '.xml'
627 assert_equal '1', p.__send__(:id_from_response, resp)
628 end
629
630 def test_create_with_custom_prefix
631 matzs_house = StreetAddress.new(:person_id => 1)
632 matzs_house.save
633 assert_equal '5', matzs_house.id
634 end
635
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
641 end
642
643 def test_reload_works_with_prefix_options
644 address = StreetAddress.find(1, :params => { :person_id => 1 })
645 assert_equal address, address.reload
646 end
647
648 def test_reload_with_redefined_to_param
649 Person.module_eval do
650 alias_method :original_to_param_reload, :to_param
651 def to_param
652 name
653 end
654 end
655
656 person = Person.find('Greg')
657 assert_equal person, person.reload
658
659 ensure
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
665 end
666 end
667
668 def test_reload_works_without_prefix_options
669 person = Person.find(:first)
670 assert_equal person, person.reload
671 end
672
673
674 def test_create
675 rick = Person.create(:name => 'Rick')
676 assert rick.valid?
677 assert !rick.new?
678 assert_equal '5', rick.id
679
680 # test additional attribute returned on create
681 assert_equal 25, rick.age
682
683 # Test that save exceptions get bubbled up too
684 ActiveResource::HttpMock.respond_to do |mock|
685 mock.post "/people.xml", {}, nil, 409
686 end
687 assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
688 end
689
690 def test_clone
691 matz = Person.find(1)
692 matz_c = matz.clone
693 assert matz_c.new?
694 matz.attributes.each do |k, v|
695 assert_equal v, matz_c.send(k) if k != Person.primary_key
696 end
697 end
698
699 def test_nested_clone
700 addy = StreetAddress.find(1, :params => {:person_id => 1})
701 addy_c = addy.clone
702 assert addy_c.new?
703 addy.attributes.each do |k, v|
704 assert_equal v, addy_c.send(k) if k != StreetAddress.primary_key
705 end
706 assert_equal addy.prefix_options, addy_c.prefix_options
707 end
708
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"]
714 matz_c = matz.clone
715 assert matz_c.new?
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
719
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
723 end
724
725 def test_update
726 matz = Person.find(:first)
727 matz.name = "David"
728 assert_kind_of Person, matz
729 assert_equal "David", matz.name
730 assert_equal true, matz.save
731 end
732
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
738 addy.save
739 end
740
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
746 addy.save
747 end
748
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
753 end
754 assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save }
755 end
756
757 def test_destroy
758 assert Person.find(1).destroy
759 ActiveResource::HttpMock.respond_to do |mock|
760 mock.get "/people/1.xml", {}, nil, 404
761 end
762 assert_raises(ActiveResource::ResourceNotFound) { Person.find(1).destroy }
763 end
764
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
769 end
770 assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
771 end
772
773 def test_delete
774 assert Person.delete(1)
775 ActiveResource::HttpMock.respond_to do |mock|
776 mock.get "/people/1.xml", {}, nil, 404
777 end
778 assert_raises(ActiveResource::ResourceNotFound) { Person.find(1) }
779 end
780
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
785 end
786 assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
787 end
788
789 def test_exists
790 # Class method.
791 assert !Person.exists?(nil)
792 assert Person.exists?(1)
793 assert !Person.exists?(99)
794
795 # Instance method.
796 assert !Person.new.exists?
797 assert Person.find(1).exists?
798 assert !Person.new(:id => 99).exists?
799
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 })
804
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?
809 end
810
811 def test_exists_with_redefined_to_param
812 Person.module_eval do
813 alias_method :original_to_param_exists, :to_param
814 def to_param
815 name
816 end
817 end
818
819 # Class method.
820 assert Person.exists?('Greg')
821
822 # Instance method.
823 assert Person.find('Greg').exists?
824
825 # Nested class method.
826 assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param })
827
828 # Nested instance method.
829 assert StreetAddress.find(1, :params => { :person_id => Person.find('Greg').to_param }).exists?
830
831 ensure
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
837 end
838 end
839
840 def test_to_xml
841 matz = Person.find(1)
842 xml = matz.encode
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>')
846 end
847
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
853 end
854
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
864 end
865 end
866 end
867 end
868
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
875 end
876 end
877 end
878 end