store_test.rb 6.3 KB
Newer Older
1 2 3 4 5
require 'cases/helper'
require 'models/admin'
require 'models/admin/user'

class StoreTest < ActiveRecord::TestCase
6 7
  fixtures :'admin/users'

8
  setup do
9
    @john = Admin::User.create!(:name => 'John Doe', :color => 'black', :remember_login => true, :height => 'tall', :is_a_good_guy => true)
10
  end
11

12 13 14 15
  test "reading store attributes through accessors" do
    assert_equal 'black', @john.color
    assert_nil @john.homepage
  end
16

17 18 19 20 21 22 23
  test "writing store attributes through accessors" do
    @john.color = 'red'
    @john.homepage = '37signals.com'

    assert_equal 'red', @john.color
    assert_equal '37signals.com', @john.homepage
  end
24

25 26 27 28
  test "accessing attributes not exposed by accessors" do
    @john.settings[:icecream] = 'graeters'
    @john.save

J
Jon Leighton 已提交
29
    assert_equal 'graeters', @john.reload.settings[:icecream]
30
  end
J
Jon Leighton 已提交
31

M
Matt Jones 已提交
32 33 34 35 36 37
  test "overriding a read accessor" do
    @john.settings[:phone_number] = '1234567890'

    assert_equal '(123) 456-7890', @john.phone_number
  end

38 39 40 41 42 43
  test "overriding a read accessor using super" do
    @john.settings[:color] = nil

    assert_equal 'red', @john.color
  end

44 45 46 47
  test "updating the store will mark it as changed" do
    @john.color = 'red'
    assert @john.settings_changed?
  end
48

49 50 51 52 53 54
  test "updating the store populates the changed array correctly" do
    @john.color = 'red'
    assert_equal 'black', @john.settings_change[0]['color']
    assert_equal 'red', @john.settings_change[1]['color']
  end

55 56 57 58 59
  test "updating the store won't mark it as changed if an attribute isn't changed" do
    @john.color = @john.color
    assert !@john.settings_changed?
  end

60 61 62 63 64 65 66 67
  test "object initialization with not nullable column" do
    assert_equal true, @john.remember_login
  end

  test "writing with not nullable column" do
    @john.remember_login = false
    assert_equal false, @john.remember_login
  end
68

M
Matt Jones 已提交
69 70 71 72 73 74
  test "overriding a write accessor" do
    @john.phone_number = '(123) 456-7890'

    assert_equal '1234567890', @john.settings[:phone_number]
  end

75 76 77 78 79 80
  test "overriding a write accessor using super" do
    @john.color = 'yellow'

    assert_equal 'blue', @john.color
  end

81
  test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
82
    @john.json_data = ActiveSupport::HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
83
    @john.height = 'low'
84
    assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
85 86 87 88 89 90 91
    assert_equal 'low', @john.json_data[:height]
    assert_equal 'low', @john.json_data['height']
    assert_equal 'heavy', @john.json_data[:weight]
    assert_equal 'heavy', @john.json_data['weight']
  end

  test "convert store attributes from Hash to HashWithIndifferentAccess saving the data and access attributes indifferently" do
92
    user = Admin::User.find_by_name('Jamis')
93 94 95 96 97 98 99 100 101 102 103 104
    assert_equal 'symbol',  user.settings[:symbol]
    assert_equal 'symbol',  user.settings['symbol']
    assert_equal 'string',  user.settings[:string]
    assert_equal 'string',  user.settings['string']
    assert_equal true,      user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess)

    user.height = 'low'
    assert_equal 'symbol',  user.settings[:symbol]
    assert_equal 'symbol',  user.settings['symbol']
    assert_equal 'string',  user.settings[:string]
    assert_equal 'string',  user.settings['string']
    assert_equal true,      user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess)
105 106 107 108 109
  end

  test "convert store attributes from any format other than Hash or HashWithIndifferent access losing the data" do
    @john.json_data = "somedata"
    @john.height = 'low'
110
    assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
111 112 113 114 115
    assert_equal 'low', @john.json_data[:height]
    assert_equal 'low', @john.json_data['height']
    assert_equal false, @john.json_data.delete_if { |k, v| k == 'height' }.any?
  end

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  test "reading store attributes through accessors encoded with JSON" do
    assert_equal 'tall', @john.height
    assert_nil @john.weight
  end

  test "writing store attributes through accessors encoded with JSON" do
    @john.height = 'short'
    @john.weight = 'heavy'

    assert_equal 'short', @john.height
    assert_equal 'heavy', @john.weight
  end

  test "accessing attributes not exposed by accessors encoded with JSON" do
    @john.json_data['somestuff'] = 'somecoolstuff'
    @john.save

    assert_equal 'somecoolstuff', @john.reload.json_data['somestuff']
  end

  test "updating the store will mark it as changed encoded with JSON" do
    @john.height = 'short'
    assert @john.json_data_changed?
  end

  test "object initialization with not nullable column encoded with JSON" do
    assert_equal true, @john.is_a_good_guy
  end

  test "writing with not nullable column encoded with JSON" do
    @john.is_a_good_guy = false
    assert_equal false, @john.is_a_good_guy
  end
149

150
  test "all stored attributes are returned" do
K
kennyj 已提交
151
    assert_equal [:color, :homepage, :favorite_food], Admin::User.stored_attributes[:settings]
152
  end
153 154 155 156 157 158 159 160 161 162 163

  test "stored_attributes are tracked per class" do
    first_model = Class.new(ActiveRecord::Base) do
      store_accessor :data, :color
    end
    second_model = Class.new(ActiveRecord::Base) do
      store_accessor :data, :width, :height
    end

    assert_equal [:color], first_model.stored_attributes[:data]
    assert_equal [:width, :height], second_model.stored_attributes[:data]
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  end

  test "stored_attributes are tracked per subclass" do
    first_model = Class.new(ActiveRecord::Base) do
      store_accessor :data, :color
    end
    second_model = Class.new(first_model) do
      store_accessor :data, :width, :height
    end
    third_model = Class.new(first_model) do
      store_accessor :data, :area, :volume
    end

    assert_equal [:color], first_model.stored_attributes[:data]
    assert_equal [:color, :width, :height], second_model.stored_attributes[:data]
    assert_equal [:color, :area, :volume], third_model.stored_attributes[:data]
180
  end
181 182 183 184

  test "YAML coder initializes the store when a Nil value is given" do
    assert_equal({}, @john.params)
  end
185 186

  test "dump, load and dump again a model" do
R
Rafael Mendonça França 已提交
187 188
    dumped = YAML.dump(@john)
    loaded = YAML.load(dumped)
189 190
    assert_equal @john, loaded

R
Rafael Mendonça França 已提交
191
    second_dump = YAML.dump(loaded)
192
    assert_equal dumped, second_dump
R
Rafael Mendonça França 已提交
193
    assert_equal @john, YAML.load(second_dump)
194
  end
195
end