aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/aggregations_test.rb29
-rw-r--r--activerecord/test/fixtures/customer.rb4
-rw-r--r--activerecord/test/fixtures/customers.yml9
3 files changed, 40 insertions, 2 deletions
diff --git a/activerecord/test/aggregations_test.rb b/activerecord/test/aggregations_test.rb
index 39adedae53..8cd4bfe481 100644
--- a/activerecord/test/aggregations_test.rb
+++ b/activerecord/test/aggregations_test.rb
@@ -63,4 +63,33 @@ class AggregationsTest < Test::Unit::TestCase
def test_gps_inequality
assert GpsLocation.new('39x110') != GpsLocation.new('39x111')
end
+
+ def test_allow_nil_gps_is_nil
+ assert_equal nil, customers(:zaphod).gps_location
+ end
+
+ def test_allow_nil_gps_set_to_nil
+ customers(:david).gps_location = nil
+ customers(:david).save
+ customers(:david).reload
+ assert_equal nil, customers(:david).gps_location
+ end
+
+ def test_allow_nil_set_address_attributes_to_nil
+ customers(:zaphod).address = nil
+ assert_equal nil, customers(:zaphod).attributes[:address_street]
+ assert_equal nil, customers(:zaphod).attributes[:address_city]
+ assert_equal nil, customers(:zaphod).attributes[:address_country]
+ end
+
+ def test_allow_nil_address_set_to_nil
+ customers(:zaphod).address = nil
+ customers(:zaphod).save
+ customers(:zaphod).reload
+ assert_equal nil, customers(:zaphod).address
+ end
+
+ def test_nil_raises_error_when_allow_nil_is_false
+ assert_raises(NoMethodError) { customers(:david).balance = nil }
+ end
end
diff --git a/activerecord/test/fixtures/customer.rb b/activerecord/test/fixtures/customer.rb
index e23fd03ade..ccbe035931 100644
--- a/activerecord/test/fixtures/customer.rb
+++ b/activerecord/test/fixtures/customer.rb
@@ -1,7 +1,7 @@
class Customer < ActiveRecord::Base
- composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ]
+ composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ], :allow_nil => true
composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
- composed_of :gps_location
+ composed_of :gps_location, :allow_nil => true
end
class Address
diff --git a/activerecord/test/fixtures/customers.yml b/activerecord/test/fixtures/customers.yml
index 9169d7d413..f802aac5ea 100644
--- a/activerecord/test/fixtures/customers.yml
+++ b/activerecord/test/fixtures/customers.yml
@@ -6,3 +6,12 @@ david:
address_city: Scary Town
address_country: Loony Land
gps_location: 35.544623640962634x-105.9309951055148
+
+zaphod:
+ id: 2
+ name: Zaphod
+ balance: 62
+ address_street: Avenue Road
+ address_city: Hamlet Town
+ address_country: Nation Land
+ gps_location: NULL \ No newline at end of file