Lazy attributes and partial updates
[railsex.git] / test / test / unit / person_test.rb
blobd576d836bbaffb36346e9a7248c7e4f2bcbfb220
1 require File.dirname(__FILE__) + '/../test_helper'
3 class PersonTest < ActiveSupport::TestCase
4   def setup
5     super
6     @person = Person.find :first
7     @lazy = ['history', 'bio'].to_set
8     @columns = Person.column_names.to_set - ['id']
9   end
10   
11   def test_lazy_attributes_values
12     assert_equal @lazy, @person.class.lazy_attributes
13   end
14   
15   def test_quoted_attributes_should_include_all_columns
16     assert_equal @columns, @person.send(:attributes_with_quotes).keys.to_set.delete('id')
17   end
18   
19   def test_quoted_attributes_should_exclude_lazy_columns_for_updates
20     assert_equal @columns - @lazy, attr_for_update(@person)
21   end
22   
23   def test_changed_lazy_attributes_behave_well
24     @person.name = "Whatever"
25     assert_equal @columns - @lazy, attr_for_update(@person)
26     
27     @person.bio  = "Hello world"
28     assert_equal @columns - ['history'], attr_for_update(@person)
29     
30     @person.history = "Nothing."
31     assert_equal @columns, attr_for_update(@person)
32     
33     assert @person.save!
34     @person.reload
35     
36     assert_equal "Nothing.", @person.history
37     assert_equal "Whatever", @person.name
38   end
39   
40   def test_save_clears_lazy_data
41     @person.bio  = "DSL"
42     assert_equal @columns - ['history'], attr_for_update(@person)
43     
44     assert @person.save
45     assert_equal @columns - @lazy, attr_for_update(@person)
46     assert_equal "DSL", @person.bio
47   end
48   
49   def test_reload_clears_lazy_data
50     @person.bio  = "Shenanigans"
51     @person.history = "Bloody"
52     assert_equal @columns, attr_for_update(@person)
53     
54     assert @person.reload
55     
56     assert_equal @columns - @lazy, attr_for_update(@person)
57   end
58   
59   private
60   
61   def attr_for_update(person)
62     person.send(:attributes_with_quotes, false, false).keys.to_set
63   end
64 end