diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-04-17 08:56:10 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-04-17 09:05:14 +0900 |
commit | c2dbdd814097031d437aca06f46032c8111d4880 (patch) | |
tree | 865fab7abc4c5ab1b695e104fc7fc0166d9d7e74 /activerecord/test | |
parent | e1e3be7c02acb0facbf81a97bbfe6d1a6e9ca598 (diff) | |
download | rails-c2dbdd814097031d437aca06f46032c8111d4880.tar.gz rails-c2dbdd814097031d437aca06f46032c8111d4880.tar.bz2 rails-c2dbdd814097031d437aca06f46032c8111d4880.zip |
`cache_key` respects the limit in a relation even if a relation is not loaded
`cache_key` includes the size of a relation. But if a relation is not
loadded, the size is not respected even if a relation has a limit. It
should be respected for consistency.
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/collection_cache_key_test.rb | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/activerecord/test/cases/collection_cache_key_test.rb b/activerecord/test/cases/collection_cache_key_test.rb index 381a78a8e2..92620761e4 100644 --- a/activerecord/test/cases/collection_cache_key_test.rb +++ b/activerecord/test/cases/collection_cache_key_test.rb @@ -15,8 +15,34 @@ module ActiveRecord end test "cache_key for relation" do - developers = Developer.where(name: "David") - last_developer_timestamp = developers.order(updated_at: :desc).first.updated_at + developers = Developer.where(salary: 100000).order(updated_at: :desc) + last_developer_timestamp = developers.first.updated_at + + assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/, developers.cache_key) + + /\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/ =~ developers.cache_key + + assert_equal Digest::MD5.hexdigest(developers.to_sql), $1 + assert_equal developers.count.to_s, $2 + assert_equal last_developer_timestamp.to_s(ActiveRecord::Base.cache_timestamp_format), $3 + end + + test "cache_key for relation with limit" do + developers = Developer.where(salary: 100000).order(updated_at: :desc).limit(5) + last_developer_timestamp = developers.first.updated_at + + assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/, developers.cache_key) + + /\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/ =~ developers.cache_key + + assert_equal Digest::MD5.hexdigest(developers.to_sql), $1 + assert_equal developers.count.to_s, $2 + assert_equal last_developer_timestamp.to_s(ActiveRecord::Base.cache_timestamp_format), $3 + end + + test "cache_key for loaded relation" do + developers = Developer.where(salary: 100000).order(updated_at: :desc).limit(5).load + last_developer_timestamp = developers.first.updated_at assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/, developers.cache_key) |