PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/IronLanguages/main
Ruby | 1116 lines | 822 code | 191 blank | 103 comment | 4 complexity | 8ff368297abcfc6a3a3b52d494cccb72 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/customer"
  4. require "fixtures/street_address"
  5. require "fixtures/sound"
  6. require "fixtures/beast"
  7. require "fixtures/proxy"
  8. require 'active_support/json'
  9. require 'active_support/ordered_hash'
  10. require 'active_support/core_ext/hash/conversions'
  11. require 'mocha'
  12. class BaseTest < Test::Unit::TestCase
  13. def setup
  14. @default_request_headers = { 'Content-Type' => 'application/xml' }
  15. @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
  16. @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
  17. @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person')
  18. @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address')
  19. @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
  20. @joe = { 'person' => { :id => 6, :name => 'Joe' }}.to_json rescue 0 # TODO(IronRuby): this throws
  21. @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people')
  22. @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
  23. @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses')
  24. # - deep nested resource -
  25. # - Luis (Customer)
  26. # - JK (Customer::Friend)
  27. # - Mateo (Customer::Friend::Brother)
  28. # - Edith (Customer::Friend::Brother::Child)
  29. # - Martha (Customer::Friend::Brother::Child)
  30. # - Felipe (Customer::Friend::Brother)
  31. # - Bryan (Customer::Friend::Brother::Child)
  32. # - Luke (Customer::Friend::Brother::Child)
  33. # - Eduardo (Customer::Friend)
  34. # - Sebas (Customer::Friend::Brother)
  35. # - Andres (Customer::Friend::Brother::Child)
  36. # - Jorge (Customer::Friend::Brother::Child)
  37. # - Elsa (Customer::Friend::Brother)
  38. # - Natacha (Customer::Friend::Brother::Child)
  39. # - Milena (Customer::Friend::Brother)
  40. #
  41. @luis = {:id => 1, :name => 'Luis',
  42. :friends => [{:name => 'JK',
  43. :brothers => [{:name => 'Mateo',
  44. :children => [{:name => 'Edith'},{:name => 'Martha'}]},
  45. {:name => 'Felipe',
  46. :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]},
  47. {:name => 'Eduardo',
  48. :brothers => [{:name => 'Sebas',
  49. :children => [{:name => 'Andres'},{:name => 'Jorge'}]},
  50. {:name => 'Elsa',
  51. :children => [{:name => 'Natacha'}]},
  52. {:name => 'Milena',
  53. :children => []}]}]}.to_xml(:root => 'customer')
  54. # - resource with yaml array of strings; for ARs using serialize :bar, Array
  55. @marty = <<-eof.strip
  56. <?xml version=\"1.0\" encoding=\"UTF-8\"?>
  57. <person>
  58. <id type=\"integer\">5</id>
  59. <name>Marty</name>
  60. <colors type=\"yaml\">---
  61. - \"red\"
  62. - \"green\"
  63. - \"blue\"
  64. </colors>
  65. </person>
  66. eof
  67. ActiveResource::HttpMock.respond_to do |mock|
  68. mock.get "/people/1.xml", {}, @matz
  69. mock.get "/people/2.xml", {}, @david
  70. mock.get "/people/6.json", {}, @joe
  71. mock.get "/people/5.xml", {}, @marty
  72. mock.get "/people/Greg.xml", {}, @greg
  73. mock.get "/people/4.xml", {'key' => 'value'}, nil, 404
  74. mock.put "/people/1.xml", {}, nil, 204
  75. mock.delete "/people/1.xml", {}, nil, 200
  76. mock.delete "/people/2.xml", {}, nil, 400
  77. mock.get "/people/99.xml", {}, nil, 404
  78. mock.post "/people.xml", {}, @rick, 201, 'Location' => '/people/5.xml'
  79. mock.get "/people.xml", {}, @people
  80. mock.get "/people/1/addresses.xml", {}, @addresses
  81. mock.get "/people/1/addresses/1.xml", {}, @addy
  82. mock.get "/people/1/addresses/2.xml", {}, nil, 404
  83. mock.get "/people/2/addresses/1.xml", {}, nil, 404
  84. mock.get "/people/Greg/addresses/1.xml", {}, @addy
  85. mock.put "/people/1/addresses/1.xml", {}, nil, 204
  86. mock.delete "/people/1/addresses/1.xml", {}, nil, 200
  87. mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5'
  88. mock.get "/people//addresses.xml", {}, nil, 404
  89. mock.get "/people//addresses/1.xml", {}, nil, 404
  90. mock.put "/people//addresses/1.xml", {}, nil, 404
  91. mock.delete "/people//addresses/1.xml", {}, nil, 404
  92. mock.post "/people//addresses.xml", {}, nil, 404
  93. mock.head "/people/1.xml", {}, nil, 200
  94. mock.head "/people/Greg.xml", {}, nil, 200
  95. mock.head "/people/99.xml", {}, nil, 404
  96. mock.head "/people/1/addresses/1.xml", {}, nil, 200
  97. mock.head "/people/1/addresses/2.xml", {}, nil, 404
  98. mock.head "/people/2/addresses/1.xml", {}, nil, 404
  99. mock.head "/people/Greg/addresses/1.xml", {}, nil, 200
  100. # customer
  101. mock.get "/customers/1.xml", {}, @luis
  102. end
  103. @original_person_site = Person.site
  104. Person.user = nil
  105. Person.password = nil
  106. end
  107. def teardown
  108. Person.site = @original_person_site
  109. end
  110. ########################################################################
  111. # Tests relating to setting up the API-connection configuration
  112. ########################################################################
  113. def test_site_accessor_accepts_uri_or_string_argument
  114. site = URI.parse('http://localhost')
  115. assert_nothing_raised { Person.site = 'http://localhost' }
  116. assert_equal site, Person.site
  117. assert_nothing_raised { Person.site = site }
  118. assert_equal site, Person.site
  119. end
  120. def test_should_use_site_prefix_and_credentials
  121. assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s
  122. assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s
  123. end
  124. def test_site_variable_can_be_reset
  125. actor = Class.new(ActiveResource::Base)
  126. assert_nil actor.site
  127. actor.site = 'http://localhost:31337'
  128. actor.site = nil
  129. assert_nil actor.site
  130. end
  131. def test_proxy_accessor_accepts_uri_or_string_argument
  132. proxy = URI.parse('http://localhost')
  133. assert_nothing_raised { Person.proxy = 'http://localhost' }
  134. assert_equal proxy, Person.proxy
  135. assert_nothing_raised { Person.proxy = proxy }
  136. assert_equal proxy, Person.proxy
  137. end
  138. def test_should_use_proxy_prefix_and_credentials
  139. assert_equal 'http://user:password@proxy.local:3000', ProxyResource.proxy.to_s
  140. end
  141. def test_proxy_variable_can_be_reset
  142. actor = Class.new(ActiveResource::Base)
  143. assert_nil actor.site
  144. actor.proxy = 'http://localhost:31337'
  145. actor.proxy = nil
  146. assert_nil actor.site
  147. end
  148. def test_should_accept_setting_user
  149. Forum.user = 'david'
  150. assert_equal('david', Forum.user)
  151. assert_equal('david', Forum.connection.user)
  152. end
  153. def test_should_accept_setting_password
  154. Forum.password = 'test123'
  155. assert_equal('test123', Forum.password)
  156. assert_equal('test123', Forum.connection.password)
  157. end
  158. def test_should_accept_setting_auth_type
  159. Forum.auth_type = :digest
  160. assert_equal(:digest, Forum.auth_type)
  161. assert_equal(:digest, Forum.connection.auth_type)
  162. end
  163. def test_should_accept_setting_timeout
  164. Forum.timeout = 5
  165. assert_equal(5, Forum.timeout)
  166. assert_equal(5, Forum.connection.timeout)
  167. end
  168. def test_should_accept_setting_ssl_options
  169. expected = {:verify => 1}
  170. Forum.ssl_options= expected
  171. assert_equal(expected, Forum.ssl_options)
  172. assert_equal(expected, Forum.connection.ssl_options)
  173. end
  174. def test_user_variable_can_be_reset
  175. actor = Class.new(ActiveResource::Base)
  176. actor.site = 'http://cinema'
  177. assert_nil actor.user
  178. actor.user = 'username'
  179. actor.user = nil
  180. assert_nil actor.user
  181. assert_nil actor.connection.user
  182. end
  183. def test_password_variable_can_be_reset
  184. actor = Class.new(ActiveResource::Base)
  185. actor.site = 'http://cinema'
  186. assert_nil actor.password
  187. actor.password = 'username'
  188. actor.password = nil
  189. assert_nil actor.password
  190. assert_nil actor.connection.password
  191. end
  192. def test_timeout_variable_can_be_reset
  193. actor = Class.new(ActiveResource::Base)
  194. actor.site = 'http://cinema'
  195. assert_nil actor.timeout
  196. actor.timeout = 5
  197. actor.timeout = nil
  198. assert_nil actor.timeout
  199. assert_nil actor.connection.timeout
  200. end
  201. def test_ssl_options_hash_can_be_reset
  202. actor = Class.new(ActiveResource::Base)
  203. actor.site = 'https://cinema'
  204. assert_nil actor.ssl_options
  205. actor.ssl_options = {:foo => 5}
  206. actor.ssl_options = nil
  207. assert_nil actor.ssl_options
  208. assert_nil actor.connection.ssl_options
  209. end
  210. def test_credentials_from_site_are_decoded
  211. actor = Class.new(ActiveResource::Base)
  212. actor.site = 'http://my%40email.com:%31%32%33@cinema'
  213. assert_equal("my@email.com", actor.user)
  214. assert_equal("123", actor.password)
  215. end
  216. def test_site_reader_uses_superclass_site_until_written
  217. # Superclass is Object so returns nil.
  218. assert_nil ActiveResource::Base.site
  219. assert_nil Class.new(ActiveResource::Base).site
  220. # Subclass uses superclass site.
  221. actor = Class.new(Person)
  222. assert_equal Person.site, actor.site
  223. # Subclass returns frozen superclass copy.
  224. assert !Person.site.frozen?
  225. assert actor.site.frozen?
  226. # Changing subclass site doesn't change superclass site.
  227. actor.site = 'http://localhost:31337'
  228. assert_not_equal Person.site, actor.site
  229. # Changed subclass site is not frozen.
  230. assert !actor.site.frozen?
  231. # Changing superclass site doesn't overwrite subclass site.
  232. Person.site = 'http://somewhere.else'
  233. assert_not_equal Person.site, actor.site
  234. # Changing superclass site after subclassing changes subclass site.
  235. jester = Class.new(actor)
  236. actor.site = 'http://nomad'
  237. assert_equal actor.site, jester.site
  238. assert jester.site.frozen?
  239. # Subclasses are always equal to superclass site when not overridden
  240. fruit = Class.new(ActiveResource::Base)
  241. apple = Class.new(fruit)
  242. fruit.site = 'http://market'
  243. assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class'
  244. fruit.site = 'http://supermarket'
  245. assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class'
  246. end
  247. def test_proxy_reader_uses_superclass_site_until_written
  248. # Superclass is Object so returns nil.
  249. assert_nil ActiveResource::Base.proxy
  250. assert_nil Class.new(ActiveResource::Base).proxy
  251. # Subclass uses superclass proxy.
  252. actor = Class.new(Person)
  253. assert_equal Person.proxy, actor.proxy
  254. # Subclass returns frozen superclass copy.
  255. assert !Person.proxy.frozen?
  256. assert actor.proxy.frozen?
  257. # Changing subclass proxy doesn't change superclass site.
  258. actor.proxy = 'http://localhost:31337'
  259. assert_not_equal Person.proxy, actor.proxy
  260. # Changed subclass proxy is not frozen.
  261. assert !actor.proxy.frozen?
  262. # Changing superclass proxy doesn't overwrite subclass site.
  263. Person.proxy = 'http://somewhere.else'
  264. assert_not_equal Person.proxy, actor.proxy
  265. # Changing superclass proxy after subclassing changes subclass site.
  266. jester = Class.new(actor)
  267. actor.proxy = 'http://nomad'
  268. assert_equal actor.proxy, jester.proxy
  269. assert jester.proxy.frozen?
  270. # Subclasses are always equal to superclass proxy when not overridden
  271. fruit = Class.new(ActiveResource::Base)
  272. apple = Class.new(fruit)
  273. fruit.proxy = 'http://market'
  274. assert_equal fruit.proxy, apple.proxy, 'subclass did not adopt changes from parent class'
  275. fruit.proxy = 'http://supermarket'
  276. assert_equal fruit.proxy, apple.proxy, 'subclass did not adopt changes from parent class'
  277. end
  278. def test_user_reader_uses_superclass_user_until_written
  279. # Superclass is Object so returns nil.
  280. assert_nil ActiveResource::Base.user
  281. assert_nil Class.new(ActiveResource::Base).user
  282. Person.user = 'anonymous'
  283. # Subclass uses superclass user.
  284. actor = Class.new(Person)
  285. assert_equal Person.user, actor.user
  286. # Subclass returns frozen superclass copy.
  287. assert !Person.user.frozen?
  288. assert actor.user.frozen?
  289. # Changing subclass user doesn't change superclass user.
  290. actor.user = 'david'
  291. assert_not_equal Person.user, actor.user
  292. # Changing superclass user doesn't overwrite subclass user.
  293. Person.user = 'john'
  294. assert_not_equal Person.user, actor.user
  295. # Changing superclass user after subclassing changes subclass user.
  296. jester = Class.new(actor)
  297. actor.user = 'john.doe'
  298. assert_equal actor.user, jester.user
  299. # Subclasses are always equal to superclass user when not overridden
  300. fruit = Class.new(ActiveResource::Base)
  301. apple = Class.new(fruit)
  302. fruit.user = 'manager'
  303. assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class'
  304. fruit.user = 'client'
  305. assert_equal fruit.user, apple.user, 'subclass did not adopt changes from parent class'
  306. end
  307. def test_password_reader_uses_superclass_password_until_written
  308. # Superclass is Object so returns nil.
  309. assert_nil ActiveResource::Base.password
  310. assert_nil Class.new(ActiveResource::Base).password
  311. Person.password = 'my-password'
  312. # Subclass uses superclass password.
  313. actor = Class.new(Person)
  314. assert_equal Person.password, actor.password
  315. # Subclass returns frozen superclass copy.
  316. assert !Person.password.frozen?
  317. assert actor.password.frozen?
  318. # Changing subclass password doesn't change superclass password.
  319. actor.password = 'secret'
  320. assert_not_equal Person.password, actor.password
  321. # Changing superclass password doesn't overwrite subclass password.
  322. Person.password = 'super-secret'
  323. assert_not_equal Person.password, actor.password
  324. # Changing superclass password after subclassing changes subclass password.
  325. jester = Class.new(actor)
  326. actor.password = 'even-more-secret'
  327. assert_equal actor.password, jester.password
  328. # Subclasses are always equal to superclass password when not overridden
  329. fruit = Class.new(ActiveResource::Base)
  330. apple = Class.new(fruit)
  331. fruit.password = 'mega-secret'
  332. assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class'
  333. fruit.password = 'ok-password'
  334. assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class'
  335. end
  336. def test_timeout_reader_uses_superclass_timeout_until_written
  337. # Superclass is Object so returns nil.
  338. assert_nil ActiveResource::Base.timeout
  339. assert_nil Class.new(ActiveResource::Base).timeout
  340. Person.timeout = 5
  341. # Subclass uses superclass timeout.
  342. actor = Class.new(Person)
  343. assert_equal Person.timeout, actor.timeout
  344. # Changing subclass timeout doesn't change superclass timeout.
  345. actor.timeout = 10
  346. assert_not_equal Person.timeout, actor.timeout
  347. # Changing superclass timeout doesn't overwrite subclass timeout.
  348. Person.timeout = 15
  349. assert_not_equal Person.timeout, actor.timeout
  350. # Changing superclass timeout after subclassing changes subclass timeout.
  351. jester = Class.new(actor)
  352. actor.timeout = 20
  353. assert_equal actor.timeout, jester.timeout
  354. # Subclasses are always equal to superclass timeout when not overridden.
  355. fruit = Class.new(ActiveResource::Base)
  356. apple = Class.new(fruit)
  357. fruit.timeout = 25
  358. assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class'
  359. fruit.timeout = 30
  360. assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class'
  361. end
  362. def test_ssl_options_reader_uses_superclass_ssl_options_until_written
  363. # Superclass is Object so returns nil.
  364. assert_nil ActiveResource::Base.ssl_options
  365. assert_nil Class.new(ActiveResource::Base).ssl_options
  366. Person.ssl_options = {:foo => 'bar'}
  367. # Subclass uses superclass ssl_options.
  368. actor = Class.new(Person)
  369. assert_equal Person.ssl_options, actor.ssl_options
  370. # Changing subclass ssl_options doesn't change superclass ssl_options.
  371. actor.ssl_options = {:baz => ''}
  372. assert_not_equal Person.ssl_options, actor.ssl_options
  373. # Changing superclass ssl_options doesn't overwrite subclass ssl_options.
  374. Person.ssl_options = {:color => 'blue'}
  375. assert_not_equal Person.ssl_options, actor.ssl_options
  376. # Changing superclass ssl_options after subclassing changes subclass ssl_options.
  377. jester = Class.new(actor)
  378. actor.ssl_options = {:color => 'red'}
  379. assert_equal actor.ssl_options, jester.ssl_options
  380. # Subclasses are always equal to superclass ssl_options when not overridden.
  381. fruit = Class.new(ActiveResource::Base)
  382. apple = Class.new(fruit)
  383. fruit.ssl_options = {:alpha => 'betas'}
  384. assert_equal fruit.ssl_options, apple.ssl_options, 'subclass did not adopt changes from parent class'
  385. fruit.ssl_options = {:omega => 'moos'}
  386. assert_equal fruit.ssl_options, apple.ssl_options, 'subclass did not adopt changes from parent class'
  387. end
  388. def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects
  389. # Subclasses are always equal to superclass site when not overridden
  390. fruit = Class.new(ActiveResource::Base)
  391. apple = Class.new(fruit)
  392. fruit.site = 'http://market'
  393. assert_equal fruit.connection.site, apple.connection.site
  394. first_connection = apple.connection.object_id
  395. fruit.site = 'http://supermarket'
  396. assert_equal fruit.connection.site, apple.connection.site
  397. second_connection = apple.connection.object_id
  398. assert_not_equal(first_connection, second_connection, 'Connection should be re-created')
  399. end
  400. def test_updating_baseclass_user_wipes_descendent_cached_connection_objects
  401. # Subclasses are always equal to superclass user when not overridden
  402. fruit = Class.new(ActiveResource::Base)
  403. apple = Class.new(fruit)
  404. fruit.site = 'http://market'
  405. fruit.user = 'david'
  406. assert_equal fruit.connection.user, apple.connection.user
  407. first_connection = apple.connection.object_id
  408. fruit.user = 'john'
  409. assert_equal fruit.connection.user, apple.connection.user
  410. second_connection = apple.connection.object_id
  411. assert_not_equal(first_connection, second_connection, 'Connection should be re-created')
  412. end
  413. def test_updating_baseclass_password_wipes_descendent_cached_connection_objects
  414. # Subclasses are always equal to superclass password when not overridden
  415. fruit = Class.new(ActiveResource::Base)
  416. apple = Class.new(fruit)
  417. fruit.site = 'http://market'
  418. fruit.password = 'secret'
  419. assert_equal fruit.connection.password, apple.connection.password
  420. first_connection = apple.connection.object_id
  421. fruit.password = 'supersecret'
  422. assert_equal fruit.connection.password, apple.connection.password
  423. second_connection = apple.connection.object_id
  424. assert_not_equal(first_connection, second_connection, 'Connection should be re-created')
  425. end
  426. def test_updating_baseclass_timeout_wipes_descendent_cached_connection_objects
  427. # Subclasses are always equal to superclass timeout when not overridden
  428. fruit = Class.new(ActiveResource::Base)
  429. apple = Class.new(fruit)
  430. fruit.site = 'http://market'
  431. fruit.timeout = 5
  432. assert_equal fruit.connection.timeout, apple.connection.timeout
  433. first_connection = apple.connection.object_id
  434. fruit.timeout = 10
  435. assert_equal fruit.connection.timeout, apple.connection.timeout
  436. second_connection = apple.connection.object_id
  437. assert_not_equal(first_connection, second_connection, 'Connection should be re-created')
  438. end
  439. ########################################################################
  440. # Tests for setting up remote URLs for a given model (including adding
  441. # parameters appropriately)
  442. ########################################################################
  443. def test_collection_name
  444. assert_equal "people", Person.collection_name
  445. end
  446. def test_collection_path
  447. assert_equal '/people.xml', Person.collection_path
  448. end
  449. def test_collection_path_with_parameters
  450. assert_equal '/people.xml?gender=male', Person.collection_path(:gender => 'male')
  451. assert_equal '/people.xml?gender=false', Person.collection_path(:gender => false)
  452. assert_equal '/people.xml?gender=', Person.collection_path(:gender => nil)
  453. assert_equal '/people.xml?gender=male', Person.collection_path('gender' => 'male')
  454. # Use includes? because ordering of param hash is not guaranteed
  455. assert Person.collection_path(:gender => 'male', :student => true).include?('/people.xml?')
  456. assert Person.collection_path(:gender => 'male', :student => true).include?('gender=male')
  457. assert Person.collection_path(:gender => 'male', :student => true).include?('student=true')
  458. assert_equal '/people.xml?name[]=bob&name[]=your+uncle%2Bme&name[]=&name[]=false', Person.collection_path(:name => ['bob', 'your uncle+me', nil, false])
  459. assert_equal '/people.xml?struct[a][]=2&struct[a][]=1&struct[b]=fred', Person.collection_path(:struct => ActiveSupport::OrderedHash[:a, [2,1], 'b', 'fred'])
  460. end
  461. def test_custom_element_path
  462. assert_equal '/people/1/addresses/1.xml', StreetAddress.element_path(1, :person_id => 1)
  463. assert_equal '/people/1/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 1)
  464. assert_equal '/people/Greg/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 'Greg')
  465. assert_equal '/people/ann%20mary/addresses/ann%20mary.xml', StreetAddress.element_path(:'ann mary', 'person_id' => 'ann mary')
  466. end
  467. def test_module_element_path
  468. assert_equal '/sounds/1.xml', Asset::Sound.element_path(1)
  469. end
  470. def test_custom_element_path_with_redefined_to_param
  471. Person.module_eval do
  472. alias_method :original_to_param_element_path, :to_param
  473. def to_param
  474. name
  475. end
  476. end
  477. # Class method.
  478. assert_equal '/people/Greg.xml', Person.element_path('Greg')
  479. # Protected Instance method.
  480. assert_equal '/people/Greg.xml', Person.find('Greg').send(:element_path)
  481. ensure
  482. # revert back to original
  483. Person.module_eval do
  484. # save the 'new' to_param so we don't get a warning about discarding the method
  485. alias_method :element_path_to_param, :to_param
  486. alias_method :to_param, :original_to_param_element_path
  487. end
  488. end
  489. def test_custom_element_path_with_parameters
  490. assert_equal '/people/1/addresses/1.xml?type=work', StreetAddress.element_path(1, :person_id => 1, :type => 'work')
  491. assert_equal '/people/1/addresses/1.xml?type=work', StreetAddress.element_path(1, 'person_id' => 1, :type => 'work')
  492. assert_equal '/people/1/addresses/1.xml?type=work', StreetAddress.element_path(1, :type => 'work', :person_id => 1)
  493. assert_equal '/people/1/addresses/1.xml?type[]=work&type[]=play+time', StreetAddress.element_path(1, :person_id => 1, :type => ['work', 'play time'])
  494. end
  495. def test_custom_element_path_with_prefix_and_parameters
  496. assert_equal '/people/1/addresses/1.xml?type=work', StreetAddress.element_path(1, {:person_id => 1}, {:type => 'work'})
  497. end
  498. def test_custom_collection_path
  499. assert_equal '/people/1/addresses.xml', StreetAddress.collection_path(:person_id => 1)
  500. assert_equal '/people/1/addresses.xml', StreetAddress.collection_path('person_id' => 1)
  501. end
  502. def test_custom_collection_path_with_parameters
  503. assert_equal '/people/1/addresses.xml?type=work', StreetAddress.collection_path(:person_id => 1, :type => 'work')
  504. assert_equal '/people/1/addresses.xml?type=work', StreetAddress.collection_path('person_id' => 1, :type => 'work')
  505. end
  506. def test_custom_collection_path_with_prefix_and_parameters
  507. assert_equal '/people/1/addresses.xml?type=work', StreetAddress.collection_path({:person_id => 1}, {:type => 'work'})
  508. end
  509. def test_custom_element_name
  510. assert_equal 'address', StreetAddress.element_name
  511. end
  512. def test_custom_collection_name
  513. assert_equal 'addresses', StreetAddress.collection_name
  514. end
  515. def test_prefix
  516. assert_equal "/", Person.prefix
  517. assert_equal Set.new, Person.__send__(:prefix_parameters)
  518. end
  519. def test_set_prefix
  520. SetterTrap.rollback_sets(Person) do |person_class|
  521. person_class.prefix = "the_prefix"
  522. assert_equal "the_prefix", person_class.prefix
  523. end
  524. end
  525. def test_set_prefix_with_inline_keys
  526. SetterTrap.rollback_sets(Person) do |person_class|
  527. person_class.prefix = "the_prefix:the_param"
  528. assert_equal "the_prefixthe_param_value", person_class.prefix(:the_param => "the_param_value")
  529. end
  530. end
  531. def test_set_prefix_twice_should_clear_params
  532. SetterTrap.rollback_sets(Person) do |person_class|
  533. person_class.prefix = "the_prefix/:the_param1"
  534. assert_equal Set.new([:the_param1]), person_class.prefix_parameters
  535. person_class.prefix = "the_prefix/:the_param2"
  536. assert_equal Set.new([:the_param2]), person_class.prefix_parameters
  537. end
  538. end
  539. def test_set_prefix_with_default_value
  540. SetterTrap.rollback_sets(Person) do |person_class|
  541. person_class.set_prefix
  542. assert_equal "/", person_class.prefix
  543. end
  544. end
  545. def test_custom_prefix
  546. assert_equal '/people//', StreetAddress.prefix
  547. assert_equal '/people/1/', StreetAddress.prefix(:person_id => 1)
  548. assert_equal [:person_id].to_set, StreetAddress.__send__(:prefix_parameters)
  549. end
  550. ########################################################################
  551. # Tests basic CRUD functions (find/save/create etc)
  552. ########################################################################
  553. def test_respond_to
  554. matz = Person.find(1)
  555. assert_respond_to matz, :name
  556. assert_respond_to matz, :name=
  557. assert_respond_to matz, :name?
  558. assert !matz.respond_to?(:super_scalable_stuff)
  559. end
  560. def test_custom_header
  561. Person.headers['key'] = 'value'
  562. assert_raise(ActiveResource::ResourceNotFound) { Person.find(4) }
  563. ensure
  564. Person.headers.delete('key')
  565. end
  566. def test_save
  567. rick = Person.new
  568. assert rick.save
  569. assert_equal '5', rick.id
  570. end
  571. def test_save!
  572. rick = Person.new
  573. assert rick.save!
  574. assert_equal '5', rick.id
  575. end
  576. def test_id_from_response
  577. p = Person.new
  578. resp = {'Location' => '/foo/bar/1'}
  579. assert_equal '1', p.__send__(:id_from_response, resp)
  580. resp['Location'] << '.xml'
  581. assert_equal '1', p.__send__(:id_from_response, resp)
  582. end
  583. def test_id_from_response_without_location
  584. p = Person.new
  585. resp = {}
  586. assert_nil p.__send__(:id_from_response, resp)
  587. end
  588. def test_create_with_custom_prefix
  589. matzs_house = StreetAddress.new(:person_id => 1)
  590. matzs_house.save
  591. assert_equal '5', matzs_house.id
  592. end
  593. # Test that loading a resource preserves its prefix_options.
  594. def test_load_preserves_prefix_options
  595. address = StreetAddress.find(1, :params => { :person_id => 1 })
  596. ryan = Person.new(:id => 1, :name => 'Ryan', :address => address)
  597. assert_equal address.prefix_options, ryan.address.prefix_options
  598. end
  599. def test_reload_works_with_prefix_options
  600. address = StreetAddress.find(1, :params => { :person_id => 1 })
  601. assert_equal address, address.reload
  602. end
  603. def test_reload_with_redefined_to_param
  604. Person.module_eval do
  605. alias_method :original_to_param_reload, :to_param
  606. def to_param
  607. name
  608. end
  609. end
  610. person = Person.find('Greg')
  611. assert_equal person, person.reload
  612. ensure
  613. # revert back to original
  614. Person.module_eval do
  615. # save the 'new' to_param so we don't get a warning about discarding the method
  616. alias_method :reload_to_param, :to_param
  617. alias_method :to_param, :original_to_param_reload
  618. end
  619. end
  620. def test_reload_works_without_prefix_options
  621. person = Person.find(:first)
  622. assert_equal person, person.reload
  623. end
  624. def test_create
  625. rick = Person.create(:name => 'Rick')
  626. assert rick.valid?
  627. assert !rick.new?
  628. assert_equal '5', rick.id
  629. # test additional attribute returned on create
  630. assert_equal 25, rick.age
  631. # Test that save exceptions get bubbled up too
  632. ActiveResource::HttpMock.respond_to do |mock|
  633. mock.post "/people.xml", {}, nil, 409
  634. end
  635. assert_raise(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') }
  636. end
  637. def test_create_without_location
  638. ActiveResource::HttpMock.respond_to do |mock|
  639. mock.post "/people.xml", {}, nil, 201
  640. end
  641. person = Person.create(:name => 'Rick')
  642. assert_nil person.id
  643. end
  644. def test_clone
  645. matz = Person.find(1)
  646. matz_c = matz.clone
  647. assert matz_c.new?
  648. matz.attributes.each do |k, v|
  649. assert_equal v, matz_c.send(k) if k != Person.primary_key
  650. end
  651. end
  652. def test_nested_clone
  653. addy = StreetAddress.find(1, :params => {:person_id => 1})
  654. addy_c = addy.clone
  655. assert addy_c.new?
  656. addy.attributes.each do |k, v|
  657. assert_equal v, addy_c.send(k) if k != StreetAddress.primary_key
  658. end
  659. assert_equal addy.prefix_options, addy_c.prefix_options
  660. end
  661. def test_complex_clone
  662. matz = Person.find(1)
  663. matz.address = StreetAddress.find(1, :params => {:person_id => matz.id})
  664. matz.non_ar_hash = {:not => "an ARes instance"}
  665. matz.non_ar_arr = ["not", "ARes"]
  666. matz_c = matz.clone
  667. assert matz_c.new?
  668. assert_raise(NoMethodError) {matz_c.address}
  669. assert_equal matz.non_ar_hash, matz_c.non_ar_hash
  670. assert_equal matz.non_ar_arr, matz_c.non_ar_arr
  671. # Test that actual copy, not just reference copy
  672. matz.non_ar_hash[:not] = "changed"
  673. assert_not_equal matz.non_ar_hash, matz_c.non_ar_hash
  674. end
  675. def test_update
  676. matz = Person.find(:first)
  677. matz.name = "David"
  678. assert_kind_of Person, matz
  679. assert_equal "David", matz.name
  680. assert_equal true, matz.save
  681. end
  682. def test_update_with_custom_prefix_with_specific_id
  683. addy = StreetAddress.find(1, :params => { :person_id => 1 })
  684. addy.street = "54321 Street"
  685. assert_kind_of StreetAddress, addy
  686. assert_equal "54321 Street", addy.street
  687. addy.save
  688. end
  689. def test_update_with_custom_prefix_without_specific_id
  690. addy = StreetAddress.find(:first, :params => { :person_id => 1 })
  691. addy.street = "54321 Lane"
  692. assert_kind_of StreetAddress, addy
  693. assert_equal "54321 Lane", addy.street
  694. addy.save
  695. end
  696. def test_update_conflict
  697. ActiveResource::HttpMock.respond_to do |mock|
  698. mock.get "/people/2.xml", {}, @david
  699. mock.put "/people/2.xml", @default_request_headers, nil, 409
  700. end
  701. assert_raise(ActiveResource::ResourceConflict) { Person.find(2).save }
  702. end
  703. ######
  704. # update_attribute(s)(!)
  705. def test_update_attribute_as_symbol
  706. matz = Person.first
  707. matz.expects(:save).returns(true)
  708. assert_equal "Matz", matz.name
  709. assert matz.update_attribute(:name, "David")
  710. assert_equal "David", matz.name
  711. end
  712. def test_update_attribute_as_string
  713. matz = Person.first
  714. matz.expects(:save).returns(true)
  715. assert_equal "Matz", matz.name
  716. assert matz.update_attribute('name', "David")
  717. assert_equal "David", matz.name
  718. end
  719. def test_update_attributes_as_symbols
  720. addy = StreetAddress.first(:params => {:person_id => 1})
  721. addy.expects(:save).returns(true)
  722. assert_equal "12345 Street", addy.street
  723. assert_equal "Australia", addy.country
  724. assert addy.update_attributes(:street => '54321 Street', :country => 'USA')
  725. assert_equal "54321 Street", addy.street
  726. assert_equal "USA", addy.country
  727. end
  728. def test_update_attributes_as_strings
  729. addy = StreetAddress.first(:params => {:person_id => 1})
  730. addy.expects(:save).returns(true)
  731. assert_equal "12345 Street", addy.street
  732. assert_equal "Australia", addy.country
  733. assert addy.update_attributes('street' => '54321 Street', 'country' => 'USA')
  734. assert_equal "54321 Street", addy.street
  735. assert_equal "USA", addy.country
  736. end
  737. #####
  738. # Mayhem and destruction
  739. def test_destroy
  740. assert Person.find(1).destroy
  741. ActiveResource::HttpMock.respond_to do |mock|
  742. mock.get "/people/1.xml", {}, nil, 404
  743. end
  744. assert_raise(ActiveResource::ResourceNotFound) { Person.find(1).destroy }
  745. end
  746. def test_destroy_with_custom_prefix
  747. assert StreetAddress.find(1, :params => { :person_id => 1 }).destroy
  748. ActiveResource::HttpMock.respond_to do |mock|
  749. mock.get "/people/1/addresses/1.xml", {}, nil, 404
  750. end
  751. assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
  752. end
  753. def test_destroy_with_410_gone
  754. assert Person.find(1).destroy
  755. ActiveResource::HttpMock.respond_to do |mock|
  756. mock.get "/people/1.xml", {}, nil, 410
  757. end
  758. assert_raise(ActiveResource::ResourceGone) { Person.find(1).destroy }
  759. end
  760. def test_delete
  761. assert Person.delete(1)
  762. ActiveResource::HttpMock.respond_to do |mock|
  763. mock.get "/people/1.xml", {}, nil, 404
  764. end
  765. assert_raise(ActiveResource::ResourceNotFound) { Person.find(1) }
  766. end
  767. def test_delete_with_custom_prefix
  768. assert StreetAddress.delete(1, :person_id => 1)
  769. ActiveResource::HttpMock.respond_to do |mock|
  770. mock.get "/people/1/addresses/1.xml", {}, nil, 404
  771. end
  772. assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
  773. end
  774. def test_delete_with_410_gone
  775. assert Person.delete(1)
  776. ActiveResource::HttpMock.respond_to do |mock|
  777. mock.get "/people/1.xml", {}, nil, 410
  778. end
  779. assert_raise(ActiveResource::ResourceGone) { Person.find(1) }
  780. end
  781. ########################################################################
  782. # Tests the more miscelaneous helper methods
  783. ########################################################################
  784. def test_exists
  785. # Class method.
  786. assert !Person.exists?(nil)
  787. assert Person.exists?(1)
  788. assert !Person.exists?(99)
  789. # Instance method.
  790. assert !Person.new.exists?
  791. assert Person.find(1).exists?
  792. assert !Person.new(:id => 99).exists?
  793. # Nested class method.
  794. assert StreetAddress.exists?(1, :params => { :person_id => 1 })
  795. assert !StreetAddress.exists?(1, :params => { :person_id => 2 })
  796. assert !StreetAddress.exists?(2, :params => { :person_id => 1 })
  797. # Nested instance method.
  798. assert StreetAddress.find(1, :params => { :person_id => 1 }).exists?
  799. assert !StreetAddress.new({:id => 1, :person_id => 2}).exists?
  800. assert !StreetAddress.new({:id => 2, :person_id => 1}).exists?
  801. end
  802. def test_exists_with_redefined_to_param
  803. Person.module_eval do
  804. alias_method :original_to_param_exists, :to_param
  805. def to_param
  806. name
  807. end
  808. end
  809. # Class method.
  810. assert Person.exists?('Greg')
  811. # Instance method.
  812. assert Person.find('Greg').exists?
  813. # Nested class method.
  814. assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param })
  815. # Nested instance method.
  816. assert StreetAddress.find(1, :params => { :person_id => Person.find('Greg').to_param }).exists?
  817. ensure
  818. # revert back to original
  819. Person.module_eval do
  820. # save the 'new' to_param so we don't get a warning about discarding the method
  821. alias_method :exists_to_param, :to_param
  822. alias_method :to_param, :original_to_param_exists
  823. end
  824. end
  825. def test_exists_without_http_mock
  826. http = Net::HTTP.new(Person.site.host, Person.site.port)
  827. ActiveResource::Connection.any_instance.expects(:http).returns(http)
  828. http.expects(:request).returns(ActiveResource::Response.new(""))
  829. assert Person.exists?('not-mocked')
  830. end
  831. def test_exists_with_410_gone
  832. ActiveResource::HttpMock.respond_to do |mock|
  833. mock.head "/people/1.xml", {}, nil, 410
  834. end
  835. assert !Person.exists?(1)
  836. end
  837. def test_to_xml
  838. matz = Person.find(1)
  839. encode = matz.encode
  840. xml = matz.to_xml
  841. assert_equal encode, xml
  842. assert xml.include?('<?xml version="1.0" encoding="UTF-8"?>')
  843. assert xml.include?('<name>Matz</name>')
  844. assert xml.include?('<id type="integer">1</id>')
  845. end
  846. def test_to_xml_with_element_name
  847. old_elem_name = Person.element_name
  848. matz = Person.find(1)
  849. Person.element_name = 'ruby_creator'
  850. encode = matz.encode
  851. xml = matz.to_xml
  852. assert_equal encode, xml
  853. assert xml.include?('<?xml version="1.0" encoding="UTF-8"?>')
  854. assert xml.include?('<ruby-creator>')
  855. assert xml.include?('<name>Matz</name>')
  856. assert xml.include?('<id type="integer">1</id>')
  857. assert xml.include?('</ruby-creator>')
  858. ensure
  859. Person.element_name = old_elem_name
  860. end
  861. def test_to_json
  862. Person.include_root_in_json = true
  863. Person.format = :json
  864. joe = Person.find(6)
  865. encode = joe.encode
  866. json = joe.to_json
  867. Person.format = :xml
  868. assert_equal encode, json
  869. assert_match %r{^\{"person":\{"person":\{}, json
  870. assert_match %r{"id":6}, json
  871. assert_match %r{"name":"Joe"}, json
  872. assert_match %r{\}\}\}$}, json
  873. end
  874. def test_to_json_with_element_name
  875. old_elem_name = Person.element_name
  876. Person.include_root_in_json = true
  877. Person.format = :json
  878. joe = Person.find(6)
  879. Person.element_name = 'ruby_creator'
  880. encode = joe.encode
  881. json = joe.to_json
  882. Person.format = :xml
  883. assert_equal encode, json
  884. assert_match %r{^\{"ruby_creator":\{"person":\{}, json
  885. assert_match %r{"id":6}, json
  886. assert_match %r{"name":"Joe"}, json
  887. assert_match %r{\}\}\}$}, json
  888. ensure
  889. Person.element_name = old_elem_name
  890. end
  891. def test_to_param_quacks_like_active_record
  892. new_person = Person.new
  893. assert_nil new_person.to_param
  894. matz = Person.find(1)
  895. assert_equal '1', matz.to_param
  896. end
  897. def test_to_key_quacks_like_active_record
  898. new_person = Person.new
  899. assert_nil new_person.to_key
  900. matz = Person.find(1)
  901. assert_equal [1], matz.to_key
  902. end
  903. def test_parse_deep_nested_resources
  904. luis = Customer.find(1)
  905. assert_kind_of Customer, luis
  906. luis.friends.each do |friend|
  907. assert_kind_of Customer::Friend, friend
  908. friend.brothers.each do |brother|
  909. assert_kind_of Customer::Friend::Brother, brother
  910. brother.children.each do |child|
  911. assert_kind_of Customer::Friend::Brother::Child, child
  912. end
  913. end
  914. end
  915. end
  916. def test_load_yaml_array
  917. assert_nothing_raised do
  918. marty = Person.find(5)
  919. assert_equal 3, marty.colors.size
  920. marty.colors.each do |color|
  921. assert_kind_of String, color
  922. end
  923. end
  924. end
  925. end