diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-04 10:04:23 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-09-04 10:04:23 +0000 |
commit | c918fbf14b96319412cb6195e7a8a3229613340c (patch) | |
tree | aa3f86f93ce445cb5dd75d312bf5ae75457062e5 /activeresource/test | |
parent | c003a4acea24aad279b8cd6eec49e20bc45859f3 (diff) | |
download | rails-c918fbf14b96319412cb6195e7a8a3229613340c.tar.gz rails-c918fbf14b96319412cb6195e7a8a3229613340c.tar.bz2 rails-c918fbf14b96319412cb6195e7a8a3229613340c.zip |
Deep hashes are converted into collections of resources. Class attribute writer methods.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4985 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/test')
-rw-r--r-- | activeresource/test/base/load_test.rb | 65 | ||||
-rw-r--r-- | activeresource/test/fixtures/street_address.rb | 4 | ||||
-rw-r--r-- | activeresource/test/http_mock.rb | 8 |
3 files changed, 72 insertions, 5 deletions
diff --git a/activeresource/test/base/load_test.rb b/activeresource/test/base/load_test.rb new file mode 100644 index 0000000000..674f8456f0 --- /dev/null +++ b/activeresource/test/base/load_test.rb @@ -0,0 +1,65 @@ +require "#{File.dirname(__FILE__)}/../abstract_unit" +require "fixtures/person" +require "fixtures/street_address" + +class BaseLoadTest < Test::Unit::TestCase + def setup + @matz = { :id => 1, :name => 'Matz' } + @addys = [{ :id => 1, :street => '12345 Street' }, { :id => 2, :street => '67890 Street' }] + @deep = { :id => 1, :street => { + :id => 1, :state => { :id => 1, :name => 'Oregon', + :notable_rivers => [{ :id => 1, :name => 'Willamette' }, + { :id => 2, :name => 'Columbia', :rafted_by => @matz }] }}} + + @person = Person.new + end + + def test_load_simple_hash + assert_equal Hash.new, @person.attributes + assert_equal @matz.stringify_keys, @person.load(@matz).attributes + end + + def test_load_one_with_existing_resource + address = @person.load(:street_address => @addys.first).street_address + assert_kind_of StreetAddress, address + assert_equal @addys.first.stringify_keys, address.attributes + end + + def test_load_one_with_unknown_resource + address = silence_warnings { @person.load(:address => @addys.first).address } + assert_kind_of Person::Address, address + assert_equal @addys.first.stringify_keys, address.attributes + end + + def test_load_collection_with_existing_resource + addresses = @person.load(:street_addresses => @addys).street_addresses + addresses.each { |address| assert_kind_of StreetAddress, address } + assert_equal @addys.map(&:stringify_keys), addresses.map(&:attributes) + end + + def test_load_collection_with_unknown_resource + assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" + addresses = silence_warnings { @person.load(:addresses => @addys).addresses } + assert Person.const_defined?(:Address), "Address should have been autocreated" + addresses.each { |address| assert_kind_of Person::Address, address } + assert_equal @addys.map(&:stringify_keys), addresses.map(&:attributes) + end + + def test_recursively_loaded_collections + person = @person.load(@deep) + assert_equal @deep[:id], person.id + + street = person.street + assert_kind_of Person::Street, street + assert_equal @deep[:street][:id], street.id + + state = street.state + assert_kind_of Person::Street::State, state + assert_equal @deep[:street][:state][:id], state.id + + rivers = state.notable_rivers + assert_kind_of Person::Street::State::NotableRiver, rivers.first + assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id + assert_equal @matz[:id], rivers.last.rafted_by.id + end +end diff --git a/activeresource/test/fixtures/street_address.rb b/activeresource/test/fixtures/street_address.rb index 84f20bbed6..94a86702b0 100644 --- a/activeresource/test/fixtures/street_address.rb +++ b/activeresource/test/fixtures/street_address.rb @@ -1,4 +1,4 @@ class StreetAddress < ActiveResource::Base self.site = "http://37s.sunrise.i:3000/people/:person_id/" - set_element_name 'address' -end
\ No newline at end of file + self.element_name = 'address' +end diff --git a/activeresource/test/http_mock.rb b/activeresource/test/http_mock.rb index 75a54e71fe..40921f6b6b 100644 --- a/activeresource/test/http_mock.rb +++ b/activeresource/test/http_mock.rb @@ -101,8 +101,10 @@ module ActiveResource class Connection private - def http - @http ||= HttpMock.new(@site) + silence_warnings do + def http + @http ||= HttpMock.new(@site) + end end end -end
\ No newline at end of file +end |