2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/admin"
|
|
|
|
require "models/admin/user"
|
2011-10-13 17:23:48 -04:00
|
|
|
|
|
|
|
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
|
2018-03-20 02:02:23 -04:00
|
|
|
@john = Admin::User.create!(
|
|
|
|
name: "John Doe", color: "black", remember_login: true,
|
|
|
|
height: "tall", is_a_good_guy: true,
|
|
|
|
parent_name: "Quinn", partner_name: "Dallas",
|
|
|
|
partner_birthday: "1997-11-1"
|
|
|
|
)
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "black", @john.color
|
2011-10-13 17:23:48 -04:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.color = "red"
|
|
|
|
@john.homepage = "37signals.com"
|
2011-10-13 17:23:48 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "red", @john.color
|
|
|
|
assert_equal "37signals.com", @john.homepage
|
2011-10-13 17:23:48 -04:00
|
|
|
end
|
2011-10-13 19:11:00 -04:00
|
|
|
|
2018-03-20 02:02:23 -04:00
|
|
|
test "reading store attributes through accessors with prefix" do
|
|
|
|
assert_equal "Quinn", @john.parent_name
|
|
|
|
assert_nil @john.parent_birthday
|
|
|
|
assert_equal "Dallas", @john.partner_name
|
|
|
|
assert_equal "1997-11-1", @john.partner_birthday
|
|
|
|
end
|
|
|
|
|
|
|
|
test "writing store attributes through accessors with prefix" do
|
|
|
|
@john.partner_name = "River"
|
|
|
|
@john.partner_birthday = "1999-2-11"
|
|
|
|
|
|
|
|
assert_equal "River", @john.partner_name
|
|
|
|
assert_equal "1999-2-11", @john.partner_birthday
|
|
|
|
end
|
|
|
|
|
2011-10-13 17:23:48 -04:00
|
|
|
test "accessing attributes not exposed by accessors" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.settings[:icecream] = "graeters"
|
2011-10-13 17:23:48 -04:00
|
|
|
@john.save
|
|
|
|
|
2016-08-06 12:26:20 -04: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
|
|
|
|
2012-09-13 10:12:11 -04:00
|
|
|
test "overriding a read accessor" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.settings[:phone_number] = "1234567890"
|
2012-09-13 10:12:11 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "(123) 456-7890", @john.phone_number
|
2012-09-13 10:12:11 -04:00
|
|
|
end
|
|
|
|
|
2013-02-11 09:00:55 -05:00
|
|
|
test "overriding a read accessor using super" do
|
|
|
|
@john.settings[:color] = nil
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "red", @john.color
|
2013-02-11 09:00:55 -05:00
|
|
|
end
|
|
|
|
|
2011-10-25 18:22:52 -04:00
|
|
|
test "updating the store will mark it as changed" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.color = "red"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @john, :settings_changed?
|
2011-10-25 18:22:52 -04:00
|
|
|
end
|
2012-02-02 13:57:15 -05:00
|
|
|
|
2012-09-05 11:01:26 -04:00
|
|
|
test "updating the store populates the changed array correctly" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.color = "red"
|
|
|
|
assert_equal "black", @john.settings_change[0]["color"]
|
|
|
|
assert_equal "red", @john.settings_change[1]["color"]
|
2012-09-05 11:01:26 -04:00
|
|
|
end
|
|
|
|
|
2012-07-02 12:29:37 -04:00
|
|
|
test "updating the store won't mark it as changed if an attribute isn't changed" do
|
|
|
|
@john.color = @john.color
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @john, :settings_changed?
|
2012-07-02 12:29:37 -04:00
|
|
|
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-09-13 10:12:11 -04:00
|
|
|
test "overriding a write accessor" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.phone_number = "(123) 456-7890"
|
2012-09-13 10:12:11 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "1234567890", @john.settings[:phone_number]
|
2012-09-13 10:12:11 -04:00
|
|
|
end
|
|
|
|
|
2013-02-11 09:00:55 -05:00
|
|
|
test "overriding a write accessor using super" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.color = "yellow"
|
2013-02-11 09:00:55 -05:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "blue", @john.color
|
2013-02-11 09:00:55 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 10:08:36 -04:00
|
|
|
test "preserve store attributes data in HashWithIndifferentAccess format without any conversion" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.json_data = ActiveSupport::HashWithIndifferentAccess.new(:height => "tall", "weight" => "heavy")
|
|
|
|
@john.height = "low"
|
2013-01-06 16:00:10 -05:00
|
|
|
assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
2016-08-06 12:26:20 -04:00
|
|
|
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"]
|
2012-05-22 10:08:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "convert store attributes from Hash to HashWithIndifferentAccess saving the data and access attributes indifferently" do
|
2016-08-06 12:26:20 -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"]
|
2012-05-31 02:18:08 -04:00
|
|
|
assert_equal true, user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
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"]
|
2012-05-31 02:18:08 -04:00
|
|
|
assert_equal true, user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
2012-05-22 10:08:36 -04:00
|
|
|
end
|
|
|
|
|
2016-02-04 03:24:00 -05:00
|
|
|
test "convert store attributes from any format other than Hash or HashWithIndifferentAccess losing the data" do
|
2012-05-22 10:08:36 -04:00
|
|
|
@john.json_data = "somedata"
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.height = "low"
|
2013-01-06 16:00:10 -05:00
|
|
|
assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
2016-08-06 12:26:20 -04:00
|
|
|
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?
|
2012-05-22 10:08:36 -04:00
|
|
|
end
|
|
|
|
|
2012-05-09 11:20:14 -04:00
|
|
|
test "reading store attributes through accessors encoded with JSON" do
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "tall", @john.height
|
2012-05-09 11:20:14 -04:00
|
|
|
assert_nil @john.weight
|
|
|
|
end
|
|
|
|
|
|
|
|
test "writing store attributes through accessors encoded with JSON" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.height = "short"
|
|
|
|
@john.weight = "heavy"
|
2012-05-09 11:20:14 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "short", @john.height
|
|
|
|
assert_equal "heavy", @john.weight
|
2012-05-09 11:20:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "accessing attributes not exposed by accessors encoded with JSON" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.json_data["somestuff"] = "somecoolstuff"
|
2012-05-09 11:20:14 -04:00
|
|
|
@john.save
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "somecoolstuff", @john.reload.json_data["somestuff"]
|
2012-05-09 11:20:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "updating the store will mark it as changed encoded with JSON" do
|
2016-08-06 12:26:20 -04:00
|
|
|
@john.height = "short"
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @john, :json_data_changed?
|
2012-05-09 11:20:14 -04:00
|
|
|
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
|
|
|
|
2012-09-05 11:01:26 -04:00
|
|
|
test "all stored attributes are returned" do
|
2012-09-15 02:29:22 -04:00
|
|
|
assert_equal [:color, :homepage, :favorite_food], Admin::User.stored_attributes[:settings]
|
2012-03-13 11:14:20 -04:00
|
|
|
end
|
2013-10-10 04:32:52 -04:00
|
|
|
|
|
|
|
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]
|
2014-04-21 18:08:54 -04:00
|
|
|
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]
|
2016-02-16 11:40:26 -05:00
|
|
|
assert_equal [:color], first_model.stored_attributes[:data]
|
2013-10-10 04:32:52 -04:00
|
|
|
end
|
2014-01-05 17:55:02 -05:00
|
|
|
|
|
|
|
test "YAML coder initializes the store when a Nil value is given" do
|
|
|
|
assert_equal({}, @john.params)
|
|
|
|
end
|
2014-01-28 22:54:13 -05:00
|
|
|
|
|
|
|
test "dump, load and dump again a model" do
|
2014-01-29 17:48:43 -05:00
|
|
|
dumped = YAML.dump(@john)
|
|
|
|
loaded = YAML.load(dumped)
|
2014-01-28 22:54:13 -05:00
|
|
|
assert_equal @john, loaded
|
|
|
|
|
2014-01-29 17:48:43 -05:00
|
|
|
second_dump = YAML.dump(loaded)
|
|
|
|
assert_equal @john, YAML.load(second_dump)
|
2014-01-28 22:54:13 -05:00
|
|
|
end
|
2018-05-25 20:43:28 -04:00
|
|
|
|
|
|
|
test "read store attributes through accessors with default suffix" do
|
|
|
|
@john.configs[:two_factor_auth] = true
|
|
|
|
assert_equal true, @john.two_factor_auth_configs
|
|
|
|
end
|
|
|
|
|
|
|
|
test "write store attributes through accessors with default suffix" do
|
|
|
|
@john.two_factor_auth_configs = false
|
|
|
|
assert_equal false, @john.configs[:two_factor_auth]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "read store attributes through accessors with custom suffix" do
|
|
|
|
@john.configs[:login_retry] = 3
|
|
|
|
assert_equal 3, @john.login_retry_config
|
|
|
|
end
|
|
|
|
|
|
|
|
test "write store attributes through accessors with custom suffix" do
|
|
|
|
@john.login_retry_config = 5
|
|
|
|
assert_equal 5, @john.configs[:login_retry]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "read accessor without pre/suffix in the same store as other pre/suffixed accessors still works" do
|
|
|
|
@john.configs[:secret_question] = "What is your high school?"
|
|
|
|
assert_equal "What is your high school?", @john.secret_question
|
|
|
|
end
|
|
|
|
|
|
|
|
test "write accessor without pre/suffix in the same store as other pre/suffixed accessors still works" do
|
|
|
|
@john.secret_question = "What was the Rails version when you first worked on it?"
|
|
|
|
assert_equal "What was the Rails version when you first worked on it?", @john.configs[:secret_question]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "prefix/suffix do not affect stored attributes" do
|
|
|
|
assert_equal [:secret_question, :two_factor_auth, :login_retry], Admin::User.stored_attributes[:configs]
|
|
|
|
end
|
2011-10-13 17:23:48 -04:00
|
|
|
end
|