2011-10-13 17:23:48 -04:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/admin'
|
|
|
|
require 'models/admin/user'
|
|
|
|
|
|
|
|
class StoreTest < ActiveRecord::TestCase
|
2012-05-31 02:18:08 -04:00
|
|
|
fixtures :'admin/users'
|
|
|
|
|
2011-10-13 17:23:48 -04:00
|
|
|
setup do
|
2012-05-31 02:18:08 -04:00
|
|
|
@john = Admin::User.create!(:name => 'John Doe', :color => 'black', :remember_login => true, :height => 'tall', :is_a_good_guy => true)
|
2011-10-13 17:23:48 -04:00
|
|
|
end
|
2011-10-13 19:11:00 -04:00
|
|
|
|
2011-10-13 17:23:48 -04:00
|
|
|
test "reading store attributes through accessors" do
|
|
|
|
assert_equal 'black', @john.color
|
|
|
|
assert_nil @john.homepage
|
|
|
|
end
|
2012-03-13 11:14:20 -04:00
|
|
|
|
2011-10-13 17:23:48 -04:00
|
|
|
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
|
2011-10-13 19:11:00 -04:00
|
|
|
|
2011-10-13 17:23:48 -04:00
|
|
|
test "accessing attributes not exposed by accessors" do
|
|
|
|
@john.settings[:icecream] = 'graeters'
|
|
|
|
@john.save
|
|
|
|
|
2011-11-30 16:49:24 -05:00
|
|
|
assert_equal 'graeters', @john.reload.settings[:icecream]
|
2011-10-13 17:23:48 -04:00
|
|
|
end
|
2011-11-30 16:49:24 -05:00
|
|
|
|
2011-10-25 18:22:52 -04:00
|
|
|
test "updating the store will mark it as changed" do
|
|
|
|
@john.color = 'red'
|
|
|
|
assert @john.settings_changed?
|
|
|
|
end
|
2012-02-02 13:57:15 -05:00
|
|
|
|
|
|
|
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
|
2012-05-09 11:20:14 -04:00
|
|
|
|
2012-05-22 10:08:36 -04:00
|
|
|
test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
|
|
|
|
@john.json_data = HashWithIndifferentAccess.new(:height => 'tall', 'weight' => 'heavy')
|
|
|
|
@john.height = 'low'
|
|
|
|
assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
|
|
|
|
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
|
2012-05-31 02:18:08 -04:00
|
|
|
user = Admin::User.find_by_name('Jamis')
|
|
|
|
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)
|
2012-05-22 10:08:36 -04:00
|
|
|
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'
|
|
|
|
assert_equal true, @john.json_data.instance_of?(HashWithIndifferentAccess)
|
|
|
|
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
|
|
|
|
|
2012-05-09 11:20:14 -04:00
|
|
|
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
|
2012-03-13 11:14:20 -04:00
|
|
|
|
|
|
|
test "stored attributes are returned" do
|
|
|
|
assert_equal [:color, :homepage], Admin::User.stored_attributes[:settings]
|
|
|
|
end
|
2011-10-13 17:23:48 -04:00
|
|
|
end
|