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/company"
|
|
|
|
require "models/developer"
|
|
|
|
require "models/computer"
|
|
|
|
require "models/owner"
|
|
|
|
require "models/pet"
|
2013-05-01 11:10:26 -04:00
|
|
|
|
|
|
|
class IntegrationTest < ActiveRecord::TestCase
|
2014-01-15 18:34:19 -05:00
|
|
|
fixtures :companies, :developers, :owners, :pets
|
2013-05-01 11:10:26 -04:00
|
|
|
|
|
|
|
def test_to_param_should_return_string
|
|
|
|
assert_kind_of String, Client.first.to_param
|
|
|
|
end
|
|
|
|
|
2013-05-02 04:31:23 -04:00
|
|
|
def test_to_param_returns_nil_if_not_persisted
|
|
|
|
client = Client.new
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil client.to_param
|
2013-05-02 04:31:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_param_returns_id_if_not_persisted_but_id_is_set
|
2013-05-01 11:10:26 -04:00
|
|
|
client = Client.new
|
|
|
|
client.id = 1
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "1", client.to_param
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
2013-11-14 13:38:08 -05:00
|
|
|
def test_to_param_class_method
|
|
|
|
firm = Firm.find(4)
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "4-flamboyant-software", firm.to_param
|
2013-11-14 13:38:08 -05:00
|
|
|
end
|
|
|
|
|
2016-02-11 14:22:09 -05:00
|
|
|
def test_to_param_class_method_truncates_words_properly
|
|
|
|
firm = Firm.find(4)
|
2016-08-06 12:26:20 -04:00
|
|
|
firm.name << ", Inc."
|
|
|
|
assert_equal "4-flamboyant-software", firm.to_param
|
2016-02-11 14:22:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_param_class_method_truncates_after_parameterize
|
|
|
|
firm = Firm.find(4)
|
|
|
|
firm.name = "Huey, Dewey, & Louie LLC"
|
|
|
|
# 123456789T123456789v
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "4-huey-dewey-louie-llc", firm.to_param
|
2016-02-11 14:22:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_param_class_method_truncates_after_parameterize_with_hyphens
|
|
|
|
firm = Firm.find(4)
|
|
|
|
firm.name = "Door-to-Door Wash-n-Fold Service"
|
|
|
|
# 123456789T123456789v
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "4-door-to-door-wash-n", firm.to_param
|
2016-02-11 14:22:09 -05:00
|
|
|
end
|
|
|
|
|
2013-11-14 15:02:05 -05:00
|
|
|
def test_to_param_class_method_truncates
|
|
|
|
firm = Firm.find(4)
|
2016-08-06 12:26:20 -04:00
|
|
|
firm.name = "a " * 100
|
|
|
|
assert_equal "4-a-a-a-a-a-a-a-a-a-a", firm.to_param
|
2013-11-14 15:02:05 -05:00
|
|
|
end
|
|
|
|
|
2013-11-15 09:35:17 -05:00
|
|
|
def test_to_param_class_method_truncates_edge_case
|
|
|
|
firm = Firm.find(4)
|
2016-08-06 12:26:20 -04:00
|
|
|
firm.name = "David HeinemeierHansson"
|
|
|
|
assert_equal "4-david", firm.to_param
|
2013-11-15 09:35:17 -05:00
|
|
|
end
|
|
|
|
|
2016-02-11 14:22:09 -05:00
|
|
|
def test_to_param_class_method_truncates_case_shown_in_doc
|
|
|
|
firm = Firm.find(4)
|
2016-08-06 12:26:20 -04:00
|
|
|
firm.name = "David Heinemeier Hansson"
|
|
|
|
assert_equal "4-david-heinemeier", firm.to_param
|
2016-02-11 14:22:09 -05:00
|
|
|
end
|
|
|
|
|
2013-11-14 15:02:05 -05:00
|
|
|
def test_to_param_class_method_squishes
|
|
|
|
firm = Firm.find(4)
|
|
|
|
firm.name = "ab \n" * 100
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "4-ab-ab-ab-ab-ab-ab-ab", firm.to_param
|
2013-11-14 15:02:05 -05:00
|
|
|
end
|
|
|
|
|
2013-12-18 11:35:30 -05:00
|
|
|
def test_to_param_class_method_multibyte_character
|
|
|
|
firm = Firm.find(4)
|
|
|
|
firm.name = "戦場ヶ原 ひたぎ"
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "4", firm.to_param
|
2013-12-18 11:35:30 -05:00
|
|
|
end
|
|
|
|
|
2013-11-14 15:02:05 -05:00
|
|
|
def test_to_param_class_method_uses_default_if_blank
|
2013-11-14 13:38:08 -05:00
|
|
|
firm = Firm.find(4)
|
|
|
|
firm.name = nil
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "4", firm.to_param
|
|
|
|
firm.name = " "
|
|
|
|
assert_equal "4", firm.to_param
|
2013-11-14 13:38:08 -05:00
|
|
|
end
|
|
|
|
|
2013-11-14 15:02:05 -05:00
|
|
|
def test_to_param_class_method_uses_default_if_not_persisted
|
2016-08-06 12:26:20 -04:00
|
|
|
firm = Firm.new(name: "Fancy Shirts")
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil firm.to_param
|
2013-11-14 13:38:08 -05:00
|
|
|
end
|
|
|
|
|
2013-11-14 15:02:05 -05:00
|
|
|
def test_to_param_with_no_arguments
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Firm", Firm.to_param
|
2013-11-14 15:02:05 -05:00
|
|
|
end
|
|
|
|
|
2013-05-01 11:10:26 -04:00
|
|
|
def test_cache_key_for_existing_record_is_not_timezone_dependent
|
|
|
|
utc_key = Developer.first.cache_key
|
|
|
|
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config zone: "EST" do
|
|
|
|
est_key = Developer.first.cache_key
|
|
|
|
assert_equal utc_key, est_key
|
|
|
|
end
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_format_for_existing_record_with_updated_at
|
|
|
|
dev = Developer.first
|
2015-09-29 07:53:39 -04:00
|
|
|
assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:usec)}", dev.cache_key
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_format_for_existing_record_with_updated_at_and_custom_cache_timestamp_format
|
|
|
|
dev = CachedDeveloper.first
|
|
|
|
assert_equal "cached_developers/#{dev.id}-#{dev.updated_at.utc.to_s(:number)}", dev.cache_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_changes_when_child_touched
|
2014-01-15 18:34:19 -05:00
|
|
|
owner = owners(:blackbeard)
|
|
|
|
pet = pets(:parrot)
|
2013-05-01 11:10:26 -04:00
|
|
|
|
2014-01-15 18:34:19 -05:00
|
|
|
owner.update_column :updated_at, Time.current
|
|
|
|
key = owner.cache_key
|
|
|
|
|
2015-10-02 08:14:43 -04:00
|
|
|
travel(1.second) do
|
|
|
|
assert pet.touch
|
|
|
|
end
|
2014-01-15 18:34:19 -05:00
|
|
|
assert_not_equal key, owner.reload.cache_key
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_format_for_existing_record_with_nil_updated_timestamps
|
|
|
|
dev = Developer.first
|
|
|
|
dev.update_columns(updated_at: nil, updated_on: nil)
|
|
|
|
assert_match(/\/#{dev.id}$/, dev.cache_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_for_updated_on
|
|
|
|
dev = Developer.first
|
|
|
|
dev.updated_at = nil
|
2015-09-29 07:53:39 -04:00
|
|
|
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:usec)}", dev.cache_key
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_for_newer_updated_at
|
|
|
|
dev = Developer.first
|
|
|
|
dev.updated_at += 3600
|
2015-09-29 07:53:39 -04:00
|
|
|
assert_equal "developers/#{dev.id}-#{dev.updated_at.utc.to_s(:usec)}", dev.cache_key
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_for_newer_updated_on
|
|
|
|
dev = Developer.first
|
|
|
|
dev.updated_on += 3600
|
2015-09-29 07:53:39 -04:00
|
|
|
assert_equal "developers/#{dev.id}-#{dev.updated_on.utc.to_s(:usec)}", dev.cache_key
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_format_is_precise_enough
|
2015-09-23 11:33:43 -04:00
|
|
|
skip("Subsecond precision is not supported") unless subsecond_precision_supported?
|
2013-05-01 11:10:26 -04:00
|
|
|
dev = Developer.first
|
|
|
|
key = dev.cache_key
|
|
|
|
dev.touch
|
|
|
|
assert_not_equal key, dev.cache_key
|
|
|
|
end
|
2013-11-04 10:06:53 -05:00
|
|
|
|
2015-09-29 07:53:39 -04:00
|
|
|
def test_cache_key_format_is_not_too_precise
|
|
|
|
skip("Subsecond precision is not supported") unless subsecond_precision_supported?
|
|
|
|
dev = Developer.first
|
|
|
|
dev.touch
|
|
|
|
key = dev.cache_key
|
|
|
|
assert_equal key, dev.reload.cache_key
|
|
|
|
end
|
|
|
|
|
2013-11-02 19:05:19 -04:00
|
|
|
def test_named_timestamps_for_cache_key
|
2017-05-19 08:09:09 -04:00
|
|
|
assert_deprecated do
|
|
|
|
owner = owners(:blackbeard)
|
|
|
|
assert_equal "owners/#{owner.id}-#{owner.happy_at.utc.to_s(:usec)}", owner.cache_key(:updated_at, :happy_at)
|
|
|
|
end
|
2013-11-02 19:05:19 -04:00
|
|
|
end
|
2016-09-07 13:31:11 -04:00
|
|
|
|
|
|
|
def test_cache_key_when_named_timestamp_is_nil
|
2017-05-19 08:09:09 -04:00
|
|
|
assert_deprecated do
|
|
|
|
owner = owners(:blackbeard)
|
|
|
|
owner.happy_at = nil
|
|
|
|
assert_equal "owners/#{owner.id}", owner.cache_key(:happy_at)
|
|
|
|
end
|
2016-09-07 13:31:11 -04:00
|
|
|
end
|
2017-05-18 12:12:32 -04:00
|
|
|
|
|
|
|
def test_cache_key_is_stable_with_versioning_on
|
|
|
|
Developer.cache_versioning = true
|
|
|
|
|
|
|
|
developer = Developer.first
|
|
|
|
first_key = developer.cache_key
|
|
|
|
|
|
|
|
developer.touch
|
|
|
|
second_key = developer.cache_key
|
|
|
|
|
|
|
|
assert_equal first_key, second_key
|
|
|
|
ensure
|
|
|
|
Developer.cache_versioning = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_version_changes_with_versioning_on
|
|
|
|
Developer.cache_versioning = true
|
|
|
|
|
|
|
|
developer = Developer.first
|
|
|
|
first_version = developer.cache_version
|
|
|
|
|
|
|
|
travel 10.seconds do
|
|
|
|
developer.touch
|
|
|
|
end
|
|
|
|
|
|
|
|
second_version = developer.cache_version
|
|
|
|
|
|
|
|
assert_not_equal first_version, second_version
|
|
|
|
ensure
|
|
|
|
Developer.cache_versioning = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_key_retains_version_when_custom_timestamp_is_used
|
|
|
|
Developer.cache_versioning = true
|
|
|
|
|
|
|
|
developer = Developer.first
|
2017-05-19 08:09:09 -04:00
|
|
|
first_key = developer.cache_key_with_version
|
2017-05-18 12:12:32 -04:00
|
|
|
|
|
|
|
travel 10.seconds do
|
|
|
|
developer.touch
|
|
|
|
end
|
|
|
|
|
2017-05-19 08:09:09 -04:00
|
|
|
second_key = developer.cache_key_with_version
|
2017-05-18 12:12:32 -04:00
|
|
|
|
|
|
|
assert_not_equal first_key, second_key
|
|
|
|
ensure
|
|
|
|
Developer.cache_versioning = false
|
|
|
|
end
|
2013-05-01 11:10:26 -04:00
|
|
|
end
|