Skip to content

Commit 66579a1

Browse files
rnubelbyroot
authored andcommitted
Adds test case for query cache false positive bug
1 parent 25c3d42 commit 66579a1

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

activerecord/test/cases/query_cache_test.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,51 @@ def assert_cache(state, connection = ActiveRecord::Base.connection)
646646
end
647647
end
648648

649+
class QueryCacheMutableParamTest < ActiveRecord::TestCase
650+
self.use_transactional_tests = false
651+
652+
class JsonObj < ActiveRecord::Base
653+
self.table_name = "json_objs"
654+
655+
attribute :payload, :json
656+
end
657+
658+
class HashWithFixedHash < Hash
659+
# this isn't very realistic, but it is the worst case and therefore a good
660+
# case to test
661+
def hash
662+
1
663+
end
664+
end
665+
666+
def setup
667+
ActiveRecord::Base.connection.create_table("json_objs", force: true) do |t|
668+
if current_adapter?(:PostgreSQLAdapter)
669+
t.jsonb "payload"
670+
else
671+
t.json "payload"
672+
end
673+
end
674+
675+
ActiveRecord::Base.connection.enable_query_cache!
676+
end
677+
678+
def test_query_cache_handles_mutated_binds
679+
record = JsonObj.create(payload: { a: 1 })
680+
681+
search = HashWithFixedHash[a: 1]
682+
the_record = JsonObj.where(payload: search).first # populate the cache
683+
684+
search.merge!(b: 2)
685+
assert_nil JsonObj.where(payload: search).first, "cache returned a false positive"
686+
end
687+
688+
def teardown
689+
ActiveRecord::Base.connection.disable_query_cache!
690+
ActiveRecord::Base.connection.drop_table("json_objs", if_exists: true)
691+
end
692+
end
693+
649694
class QueryCacheExpiryTest < ActiveRecord::TestCase
650695
fixtures :tasks, :posts, :categories, :categories_posts
651696

0 commit comments

Comments
 (0)