@@ -46,10 +46,10 @@ def test_execute_clear_cache
46
46
mw = middleware { |env |
47
47
Post . first
48
48
query_cache = ActiveRecord ::Base . connection . query_cache
49
- assert_equal 1 , query_cache . length , query_cache . keys
49
+ assert_equal 1 , query_cache . size , query_cache . inspect
50
50
Post . connection . execute ( "SELECT 1" )
51
51
query_cache = ActiveRecord ::Base . connection . query_cache
52
- assert_equal 0 , query_cache . length , query_cache . keys
52
+ assert_equal 0 , query_cache . size , query_cache . inspect
53
53
}
54
54
mw . call ( { } )
55
55
@@ -62,10 +62,10 @@ def test_exec_query_clear_cache
62
62
mw = middleware { |env |
63
63
Post . first
64
64
query_cache = ActiveRecord ::Base . connection . query_cache
65
- assert_equal 1 , query_cache . length , query_cache . keys
65
+ assert_equal 1 , query_cache . size , query_cache . inspect
66
66
Post . connection . exec_query ( "SELECT 1" )
67
67
query_cache = ActiveRecord ::Base . connection . query_cache
68
- assert_equal 0 , query_cache . length , query_cache . keys
68
+ assert_equal 0 , query_cache . size , query_cache . inspect
69
69
}
70
70
mw . call ( { } )
71
71
@@ -78,13 +78,13 @@ def test_writes_should_always_clear_cache
78
78
mw = middleware { |env |
79
79
Post . first
80
80
query_cache = ActiveRecord ::Base . connection . query_cache
81
- assert_equal 1 , query_cache . length , query_cache . keys
81
+ assert_equal 1 , query_cache . size , query_cache . inspect
82
82
Post . connection . uncached do
83
83
# should clear the cache
84
84
Post . create! ( title : "a new post" , body : "and a body" )
85
85
end
86
86
query_cache = ActiveRecord ::Base . connection . query_cache
87
- assert_equal 0 , query_cache . length , query_cache . keys
87
+ assert_equal 0 , query_cache . size , query_cache . inspect
88
88
}
89
89
mw . call ( { } )
90
90
@@ -98,7 +98,7 @@ def test_exceptional_middleware_clears_and_disables_cache_on_error
98
98
Task . find 1
99
99
Task . find 1
100
100
query_cache = ActiveRecord ::Base . connection . query_cache
101
- assert_equal 1 , query_cache . length , query_cache . keys
101
+ assert_equal 1 , query_cache . size , query_cache . inspect
102
102
raise "lol borked"
103
103
}
104
104
assert_raises ( RuntimeError ) { mw . call ( { } ) }
@@ -270,7 +270,7 @@ def test_middleware_caches
270
270
Task . find 1
271
271
Task . find 1
272
272
query_cache = ActiveRecord ::Base . connection . query_cache
273
- assert_equal 1 , query_cache . length , query_cache . keys
273
+ assert_equal 1 , query_cache . size , query_cache . inspect
274
274
[ 200 , { } , nil ]
275
275
}
276
276
mw . call ( { } )
@@ -637,6 +637,8 @@ def test_clear_query_cache_is_called_on_all_connections
637
637
ActiveRecord ::Base . connection_pool . lock_thread = true
638
638
639
639
assert_cache :off
640
+ ActiveRecord ::Base . connection . enable_query_cache!
641
+ assert_cache :clean
640
642
641
643
thread_a = Thread . new do
642
644
middleware { |env |
@@ -668,7 +670,11 @@ def assert_cache(state, connection = ActiveRecord::Base.connection)
668
670
case state
669
671
when :off
670
672
assert_not connection . query_cache_enabled , "cache should be off"
671
- assert_predicate connection . query_cache , :empty? , "cache should be empty"
673
+ if connection . query_cache . nil?
674
+ assert_nil connection . query_cache
675
+ else
676
+ assert_predicate connection . query_cache , :empty? , "cache should be nil or empty"
677
+ end
672
678
when :clean
673
679
assert connection . query_cache_enabled , "cache should be on"
674
680
assert_predicate connection . query_cache , :empty? , "cache should be empty"
@@ -800,7 +806,7 @@ def test_cache_gets_cleared_after_migration
800
806
end
801
807
802
808
def test_find
803
- assert_called ( Task . connection , :clear_query_cache ) do
809
+ assert_called ( Task . connection . query_cache , :clear ) do
804
810
assert_not Task . connection . query_cache_enabled
805
811
Task . cache do
806
812
assert Task . connection . query_cache_enabled
@@ -817,9 +823,19 @@ def test_find
817
823
end
818
824
end
819
825
826
+ def test_enable_disable
827
+ assert_called ( Task . connection . query_cache , :clear , times : 1 ) do
828
+ Task . cache { }
829
+ end
830
+
831
+ assert_called ( Task . connection . query_cache , :clear , times : 1 ) do
832
+ Task . cache { Task . cache { } }
833
+ end
834
+ end
835
+
820
836
def test_update
821
- assert_called ( Task . connection , :clear_query_cache , times : 2 ) do
822
- Task . cache do
837
+ Task . cache do
838
+ assert_called ( Task . connection . query_cache , :clear , times : 1 ) do
823
839
task = Task . find ( 1 )
824
840
task . starting = Time . now . utc
825
841
task . save!
@@ -828,16 +844,16 @@ def test_update
828
844
end
829
845
830
846
def test_destroy
831
- assert_called ( Task . connection , :clear_query_cache , times : 2 ) do
832
- Task . cache do
847
+ Task . cache do
848
+ assert_called ( Task . connection . query_cache , :clear , times : 1 ) do
833
849
Task . find ( 1 ) . destroy
834
850
end
835
851
end
836
852
end
837
853
838
854
def test_insert
839
- assert_called ( ActiveRecord :: Base . connection , :clear_query_cache , times : 2 ) do
840
- Task . cache do
855
+ Task . cache do
856
+ assert_called ( ActiveRecord :: Base . connection . query_cache , :clear , times : 1 ) do
841
857
Task . create!
842
858
end
843
859
end
@@ -846,40 +862,46 @@ def test_insert
846
862
def test_insert_all
847
863
skip unless supports_insert_on_duplicate_skip?
848
864
849
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
850
- Task . cache { Task . insert ( { starting : Time . now } ) }
851
- end
865
+ Task . cache do
866
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
867
+ Task . insert ( { starting : Time . now } )
868
+ end
852
869
853
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
854
- Task . cache { Task . insert_all ( [ { starting : Time . now } ] ) }
870
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
871
+ Task . insert_all ( [ { starting : Time . now } ] )
872
+ end
855
873
end
856
874
end
857
875
858
876
def test_insert_all_bang
859
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
860
- Task . cache { Task . insert! ( { starting : Time . now } ) }
861
- end
877
+ Task . cache do
878
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
879
+ Task . insert! ( { starting : Time . now } )
880
+ end
862
881
863
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
864
- Task . cache { Task . insert_all! ( [ { starting : Time . now } ] ) }
882
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
883
+ Task . insert_all! ( [ { starting : Time . now } ] )
884
+ end
865
885
end
866
886
end
867
887
868
888
def test_upsert_all
869
889
skip unless supports_insert_on_duplicate_update?
870
890
871
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
872
- Task . cache { Task . upsert ( { starting : Time . now } ) }
873
- end
891
+ Task . cache do
892
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
893
+ Task . upsert ( { starting : Time . now } )
894
+ end
874
895
875
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
876
- Task . cache { Task . upsert_all ( [ { starting : Time . now } ] ) }
896
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
897
+ Task . upsert_all ( [ { starting : Time . now } ] )
898
+ end
877
899
end
878
900
end
879
901
880
902
def test_cache_is_expired_by_habtm_update
881
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
882
- ActiveRecord ::Base . cache do
903
+ ActiveRecord ::Base . cache do
904
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
883
905
c = Category . first
884
906
p = Post . first
885
907
p . categories << c
@@ -888,8 +910,8 @@ def test_cache_is_expired_by_habtm_update
888
910
end
889
911
890
912
def test_cache_is_expired_by_habtm_delete
891
- assert_called ( ActiveRecord ::Base . connection , :clear_query_cache , times : 2 ) do
892
- ActiveRecord ::Base . cache do
913
+ ActiveRecord ::Base . cache do
914
+ assert_called ( ActiveRecord ::Base . connection . query_cache , :clear , times : 1 ) do
893
915
p = Post . find ( 1 )
894
916
assert_predicate p . categories , :any?
895
917
p . categories . delete_all
0 commit comments