2004-11-23 20:04:44 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/customer'
|
|
|
|
|
|
|
|
class AggregationsTest < Test::Unit::TestCase
|
2005-03-01 18:52:36 -05:00
|
|
|
fixtures :customers
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def test_find_single_value_object
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal 50, customers(:david).balance.amount
|
|
|
|
assert_kind_of Money, customers(:david).balance
|
|
|
|
assert_equal 300, customers(:david).balance.exchange_to("DKK").amount
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_multiple_value_object
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal customers(:david).address_street, customers(:david).address.street
|
2004-11-23 20:04:44 -05:00
|
|
|
assert(
|
2005-06-10 10:58:02 -04:00
|
|
|
customers(:david).address.close_to?(Address.new("Different Street", customers(:david).address_city, customers(:david).address_country))
|
2004-11-23 20:04:44 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_change_single_value_object
|
2005-06-10 10:58:02 -04:00
|
|
|
customers(:david).balance = Money.new(100)
|
|
|
|
customers(:david).save
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal 100, Customer.find(1).balance.amount
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_immutable_value_objects
|
2005-06-10 10:58:02 -04:00
|
|
|
customers(:david).balance = Money.new(100)
|
|
|
|
assert_raises(TypeError) { customers(:david).balance.instance_eval { @amount = 20 } }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-03-01 18:52:36 -05:00
|
|
|
|
|
|
|
def test_inferred_mapping
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal "35.544623640962634", customers(:david).gps_location.latitude
|
|
|
|
assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
|
2005-03-01 18:52:36 -05:00
|
|
|
|
2005-06-10 10:58:02 -04:00
|
|
|
customers(:david).gps_location = GpsLocation.new("39x-110")
|
2005-03-01 18:52:36 -05:00
|
|
|
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal "39", customers(:david).gps_location.latitude
|
|
|
|
assert_equal "-110", customers(:david).gps_location.longitude
|
2005-03-01 18:52:36 -05:00
|
|
|
|
2005-06-10 10:58:02 -04:00
|
|
|
customers(:david).save
|
2005-03-01 18:52:36 -05:00
|
|
|
|
2005-06-10 10:58:02 -04:00
|
|
|
customers(:david).reload
|
2005-03-01 18:52:36 -05:00
|
|
|
|
2005-06-10 10:58:02 -04:00
|
|
|
assert_equal "39", customers(:david).gps_location.latitude
|
|
|
|
assert_equal "-110", customers(:david).gps_location.longitude
|
2005-03-01 18:52:36 -05:00
|
|
|
end
|
2005-12-07 23:46:57 -05:00
|
|
|
|
|
|
|
def test_reloaded_instance_refreshes_aggregations
|
|
|
|
assert_equal "35.544623640962634", customers(:david).gps_location.latitude
|
|
|
|
assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
|
|
|
|
|
|
|
|
Customer.update_all("gps_location = '24x113'")
|
|
|
|
customers(:david).reload
|
|
|
|
assert_equal '24x113', customers(:david)['gps_location']
|
|
|
|
|
|
|
|
assert_equal GpsLocation.new('24x113'), customers(:david).gps_location
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gps_equality
|
|
|
|
assert GpsLocation.new('39x110') == GpsLocation.new('39x110')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gps_inequality
|
|
|
|
assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
|
|
|
|
end
|
2005-06-10 10:58:02 -04:00
|
|
|
end
|