2008-01-05 08:32:38 -05:00
|
|
|
require 'abstract_unit'
|
2006-09-04 06:04:23 -04:00
|
|
|
require "fixtures/person"
|
|
|
|
require "fixtures/street_address"
|
2009-05-13 04:10:37 -04:00
|
|
|
require 'active_support/core_ext/hash/conversions'
|
2006-09-04 06:04:23 -04:00
|
|
|
|
2007-04-26 15:38:16 -04:00
|
|
|
module Highrise
|
|
|
|
class Note < ActiveResource::Base
|
|
|
|
self.site = "http://37s.sunrise.i:3000"
|
|
|
|
end
|
|
|
|
|
|
|
|
class Comment < ActiveResource::Base
|
|
|
|
self.site = "http://37s.sunrise.i:3000"
|
|
|
|
end
|
2008-01-02 04:14:34 -05:00
|
|
|
|
|
|
|
module Deeply
|
|
|
|
module Nested
|
|
|
|
class Note < ActiveResource::Base
|
|
|
|
self.site = "http://37s.sunrise.i:3000"
|
|
|
|
end
|
|
|
|
|
2009-10-02 11:19:30 -04:00
|
|
|
class Comment < ActiveResource::Base
|
|
|
|
self.site = "http://37s.sunrise.i:3000"
|
|
|
|
end
|
2008-01-02 04:14:34 -05:00
|
|
|
|
2009-10-02 11:19:30 -04:00
|
|
|
module TestDifferentLevels
|
|
|
|
class Note < ActiveResource::Base
|
|
|
|
self.site = "http://37s.sunrise.i:3000"
|
|
|
|
end
|
|
|
|
end
|
2008-01-02 04:14:34 -05:00
|
|
|
end
|
|
|
|
end
|
2007-04-26 15:38:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-09-04 06:04:23 -04:00
|
|
|
class BaseLoadTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@matz = { :id => 1, :name => 'Matz' }
|
2006-09-22 17:29:54 -04:00
|
|
|
|
2011-05-17 19:30:43 -04:00
|
|
|
@first_address = { :address => { :id => 1, :street => '12345 Street' } }
|
|
|
|
@addresses = [@first_address, { :address => { :id => 2, :street => '67890 Street' } }]
|
|
|
|
@addresses_from_json = { :street_addresses => @addresses }
|
|
|
|
@addresses_from_json_single = { :street_addresses => [ @first_address ] }
|
2006-09-22 17:29:54 -04:00
|
|
|
|
2006-09-04 06:04:23 -04:00
|
|
|
@deep = { :id => 1, :street => {
|
|
|
|
:id => 1, :state => { :id => 1, :name => 'Oregon',
|
2006-12-12 10:29:54 -05:00
|
|
|
:notable_rivers => [
|
2006-09-22 17:29:54 -04:00
|
|
|
{ :id => 1, :name => 'Willamette' },
|
2009-08-09 18:24:49 -04:00
|
|
|
{ :id => 2, :name => 'Columbia', :rafted_by => @matz }],
|
2009-08-14 08:00:22 -04:00
|
|
|
:postal_codes => [ 97018, 1234567890 ],
|
2010-07-27 03:55:33 -04:00
|
|
|
:dates => [ Time.now ],
|
|
|
|
:votes => [ true, false, true ],
|
2009-08-14 08:00:22 -04:00
|
|
|
:places => [ "Columbia City", "Unknown" ]}}}
|
2006-09-04 06:04:23 -04:00
|
|
|
|
|
|
|
@person = Person.new
|
|
|
|
end
|
|
|
|
|
2006-09-04 20:02:17 -04:00
|
|
|
def test_load_expects_hash
|
|
|
|
assert_raise(ArgumentError) { @person.load nil }
|
|
|
|
assert_raise(ArgumentError) { @person.load '<person id="1"/>' }
|
2006-09-04 19:36:13 -04:00
|
|
|
end
|
|
|
|
|
2006-09-04 06:04:23 -04:00
|
|
|
def test_load_simple_hash
|
|
|
|
assert_equal Hash.new, @person.attributes
|
|
|
|
assert_equal @matz.stringify_keys, @person.load(@matz).attributes
|
|
|
|
end
|
|
|
|
|
2009-10-02 11:13:40 -04:00
|
|
|
def test_after_load_attributes_are_accessible
|
|
|
|
assert_equal Hash.new, @person.attributes
|
|
|
|
assert_equal @matz.stringify_keys, @person.load(@matz).attributes
|
|
|
|
assert_equal @matz[:name], @person.attributes['name']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_after_load_attributes_are_accessible_via_indifferent_access
|
|
|
|
assert_equal Hash.new, @person.attributes
|
2011-05-17 19:30:43 -04:00
|
|
|
matz_attributes = @matz.values.first
|
2009-10-02 11:13:40 -04:00
|
|
|
assert_equal @matz.stringify_keys, @person.load(@matz).attributes
|
|
|
|
assert_equal @matz[:name], @person.attributes['name']
|
|
|
|
assert_equal @matz[:name], @person.attributes[:name]
|
|
|
|
end
|
|
|
|
|
2006-09-04 06:04:23 -04:00
|
|
|
def test_load_one_with_existing_resource
|
2011-05-17 19:30:43 -04:00
|
|
|
address = @person.load(:street_address => @first_address.values.first).street_address
|
2006-09-04 06:04:23 -04:00
|
|
|
assert_kind_of StreetAddress, address
|
2011-05-17 19:30:43 -04:00
|
|
|
assert_equal @first_address.values.first.stringify_keys, address.attributes
|
2006-09-04 06:04:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_one_with_unknown_resource
|
2011-05-17 19:30:43 -04:00
|
|
|
address = silence_warnings { @person.load(@first_address).address }
|
2006-09-04 06:04:23 -04:00
|
|
|
assert_kind_of Person::Address, address
|
2011-05-17 19:30:43 -04:00
|
|
|
assert_equal @first_address.values.first.stringify_keys, address.attributes
|
2006-09-04 06:04:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_collection_with_existing_resource
|
2011-05-17 19:30:43 -04:00
|
|
|
addresses = @person.load(@addresses_from_json).street_addresses
|
2006-09-22 17:29:54 -04:00
|
|
|
assert_kind_of Array, addresses
|
2006-09-04 06:04:23 -04:00
|
|
|
addresses.each { |address| assert_kind_of StreetAddress, address }
|
2011-05-17 19:30:43 -04:00
|
|
|
assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes)
|
2006-09-04 06:04:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_collection_with_unknown_resource
|
2008-08-31 16:15:26 -04:00
|
|
|
Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address)
|
2006-09-04 06:04:23 -04:00
|
|
|
assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated"
|
2006-09-22 17:29:54 -04:00
|
|
|
addresses = silence_warnings { @person.load(:addresses => @addresses).addresses }
|
2006-09-04 06:04:23 -04:00
|
|
|
assert Person.const_defined?(:Address), "Address should have been autocreated"
|
|
|
|
addresses.each { |address| assert_kind_of Person::Address, address }
|
2011-05-17 19:30:43 -04:00
|
|
|
assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes)
|
2006-09-04 06:04:23 -04:00
|
|
|
end
|
|
|
|
|
2006-12-12 10:29:54 -05:00
|
|
|
def test_load_collection_with_single_existing_resource
|
2011-05-17 19:30:43 -04:00
|
|
|
addresses = @person.load(@addresses_from_json_single).street_addresses
|
2006-12-12 10:29:54 -05:00
|
|
|
assert_kind_of Array, addresses
|
|
|
|
addresses.each { |address| assert_kind_of StreetAddress, address }
|
2011-05-17 19:30:43 -04:00
|
|
|
assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes)
|
2006-12-12 10:29:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_collection_with_single_unknown_resource
|
2008-08-31 16:15:26 -04:00
|
|
|
Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address)
|
2006-12-12 10:29:54 -05:00
|
|
|
assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated"
|
|
|
|
addresses = silence_warnings { @person.load(:addresses => [ @first_address ]).addresses }
|
|
|
|
assert Person.const_defined?(:Address), "Address should have been autocreated"
|
|
|
|
addresses.each { |address| assert_kind_of Person::Address, address }
|
2011-05-17 19:30:43 -04:00
|
|
|
assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes)
|
2006-12-12 10:29:54 -05:00
|
|
|
end
|
|
|
|
|
2006-09-04 06:04:23 -04:00
|
|
|
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
|
2006-09-22 17:29:54 -04:00
|
|
|
assert_kind_of Array, rivers
|
2006-09-04 06:04:23 -04:00
|
|
|
assert_kind_of Person::Street::State::NotableRiver, rivers.first
|
2006-12-12 10:29:54 -05:00
|
|
|
assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id
|
2006-09-04 06:04:23 -04:00
|
|
|
assert_equal @matz[:id], rivers.last.rafted_by.id
|
2009-08-09 18:24:49 -04:00
|
|
|
|
|
|
|
postal_codes = state.postal_codes
|
|
|
|
assert_kind_of Array, postal_codes
|
|
|
|
assert_equal 2, postal_codes.size
|
|
|
|
assert_kind_of Fixnum, postal_codes.first
|
|
|
|
assert_equal @deep[:street][:state][:postal_codes].first, postal_codes.first
|
2009-08-14 08:00:22 -04:00
|
|
|
assert_kind_of Numeric, postal_codes.last
|
2009-08-09 18:24:49 -04:00
|
|
|
assert_equal @deep[:street][:state][:postal_codes].last, postal_codes.last
|
|
|
|
|
|
|
|
places = state.places
|
|
|
|
assert_kind_of Array, places
|
|
|
|
assert_kind_of String, places.first
|
|
|
|
assert_equal @deep[:street][:state][:places].first, places.first
|
2010-07-27 03:55:33 -04:00
|
|
|
|
|
|
|
dates = state.dates
|
|
|
|
assert_kind_of Array, dates
|
|
|
|
assert_kind_of Time, dates.first
|
|
|
|
assert_equal @deep[:street][:state][:dates].first, dates.first
|
|
|
|
|
|
|
|
votes = state.votes
|
|
|
|
assert_kind_of Array, votes
|
|
|
|
assert_kind_of TrueClass, votes.first
|
|
|
|
assert_equal @deep[:street][:state][:votes].first, votes.first
|
2006-09-04 06:04:23 -04:00
|
|
|
end
|
2009-10-02 11:19:30 -04:00
|
|
|
|
2007-04-26 15:38:16 -04:00
|
|
|
def test_nested_collections_within_the_same_namespace
|
2011-05-17 19:30:43 -04:00
|
|
|
n = Highrise::Note.new(:comments => [{ :comment => { :name => "1" } }])
|
2007-04-26 15:38:16 -04:00
|
|
|
assert_kind_of Highrise::Comment, n.comments.first
|
|
|
|
end
|
2008-01-02 04:14:34 -05:00
|
|
|
|
|
|
|
def test_nested_collections_within_deeply_nested_namespace
|
|
|
|
n = Highrise::Deeply::Nested::Note.new(:comments => [{ :name => "1" }])
|
|
|
|
assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_collections_in_different_levels_of_namespaces
|
|
|
|
n = Highrise::Deeply::Nested::TestDifferentLevels::Note.new(:comments => [{ :name => "1" }])
|
|
|
|
assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|