diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-04-19 21:02:07 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-19 21:02:07 -0400 |
commit | d87d0e34dae7511b234058d374507721843dc8ee (patch) | |
tree | a1e211359a20284969a1e53131906ed9bd86bb53 /activerecord/test | |
parent | e8b4a927340183acba7fd0a2c3864afc2388a9aa (diff) | |
parent | c2dbdd814097031d437aca06f46032c8111d4880 (diff) | |
download | rails-d87d0e34dae7511b234058d374507721843dc8ee.tar.gz rails-d87d0e34dae7511b234058d374507721843dc8ee.tar.bz2 rails-d87d0e34dae7511b234058d374507721843dc8ee.zip |
Merge pull request #28776 from kamipo/respect_limit_for_cache_key
`cache_key` respects the limit in a relation even if a relation is not loaded
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) |