Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/2-1-caching/activerecord/test/aggregations_test.rb

Revision 8218, 4.2 kB (checked in by bitsweat, 1 year ago)

Foxy fixtures: allow mixed usage to make migration easier and more attractive. Closes #10004.

Line 
1 require 'abstract_unit'
2 require 'fixtures/customer'
3
4 class AggregationsTest < Test::Unit::TestCase
5   fixtures :customers
6
7   def test_find_single_value_object
8     assert_equal 50, customers(:david).balance.amount
9     assert_kind_of Money, customers(:david).balance
10     assert_equal 300, customers(:david).balance.exchange_to("DKK").amount
11   end
12  
13   def test_find_multiple_value_object
14     assert_equal customers(:david).address_street, customers(:david).address.street
15     assert(
16       customers(:david).address.close_to?(Address.new("Different Street", customers(:david).address_city, customers(:david).address_country))
17     )
18   end
19  
20   def test_change_single_value_object
21     customers(:david).balance = Money.new(100)
22     customers(:david).save
23     assert_equal 100, customers(:david).reload.balance.amount
24   end
25  
26   def test_immutable_value_objects
27     customers(:david).balance = Money.new(100)
28     assert_raises(TypeError) {  customers(:david).balance.instance_eval { @amount = 20 } }
29   end 
30  
31   def test_inferred_mapping
32     assert_equal "35.544623640962634", customers(:david).gps_location.latitude
33     assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
34    
35     customers(:david).gps_location = GpsLocation.new("39x-110")
36
37     assert_equal "39", customers(:david).gps_location.latitude
38     assert_equal "-110", customers(:david).gps_location.longitude
39    
40     customers(:david).save
41    
42     customers(:david).reload
43
44     assert_equal "39", customers(:david).gps_location.latitude
45     assert_equal "-110", customers(:david).gps_location.longitude
46   end
47
48   def test_reloaded_instance_refreshes_aggregations
49     assert_equal "35.544623640962634", customers(:david).gps_location.latitude
50     assert_equal "-105.9309951055148", customers(:david).gps_location.longitude
51
52     Customer.update_all("gps_location = '24x113'")
53     customers(:david).reload
54     assert_equal '24x113', customers(:david)['gps_location']
55
56     assert_equal GpsLocation.new('24x113'), customers(:david).gps_location
57   end
58
59   def test_gps_equality
60     assert GpsLocation.new('39x110') == GpsLocation.new('39x110')
61   end
62
63   def test_gps_inequality
64     assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
65   end
66  
67   def test_allow_nil_gps_is_nil
68     assert_equal nil, customers(:zaphod).gps_location
69   end
70  
71   def test_allow_nil_gps_set_to_nil
72     customers(:david).gps_location = nil
73     customers(:david).save
74     customers(:david).reload
75     assert_equal nil, customers(:david).gps_location
76   end
77  
78   def test_allow_nil_set_address_attributes_to_nil
79     customers(:zaphod).address = nil
80     assert_equal nil, customers(:zaphod).attributes[:address_street]
81     assert_equal nil, customers(:zaphod).attributes[:address_city]
82     assert_equal nil, customers(:zaphod).attributes[:address_country]
83   end
84  
85   def test_allow_nil_address_set_to_nil
86     customers(:zaphod).address = nil
87     customers(:zaphod).save
88     customers(:zaphod).reload
89     assert_equal nil, customers(:zaphod).address
90   end
91  
92   def test_nil_raises_error_when_allow_nil_is_false
93     assert_raises(NoMethodError) { customers(:david).balance = nil }
94   end
95
96   def test_allow_nil_address_loaded_when_only_some_attributes_are_nil
97     customers(:zaphod).address_street = nil
98     customers(:zaphod).save
99     customers(:zaphod).reload
100     assert_kind_of Address, customers(:zaphod).address
101     assert customers(:zaphod).address.street.nil?
102   end
103
104   def test_nil_assignment_results_in_nil
105     customers(:david).gps_location = GpsLocation.new('39x111')
106     assert_not_equal nil, customers(:david).gps_location
107     customers(:david).gps_location = nil
108     assert_equal nil, customers(:david).gps_location
109   end
110 end
111
112 class OverridingAggregationsTest < Test::Unit::TestCase
113   class Name; end
114   class DifferentName; end
115
116   class Person   < ActiveRecord::Base
117     composed_of :composed_of, :mapping => %w(person_first_name first_name)
118   end
119
120   class DifferentPerson < Person
121     composed_of :composed_of, :class_name => 'DifferentName', :mapping => %w(different_person_first_name first_name)
122   end
123
124   def test_composed_of_aggregation_redefinition_reflections_should_differ_and_not_inherited
125     assert_not_equal Person.reflect_on_aggregation(:composed_of),
126                      DifferentPerson.reflect_on_aggregation(:composed_of)
127   end
128 end
Note: See TracBrowser for help on using the browser.