@@ -4,85 +4,122 @@ module CacheStoreVersionBehavior
4
4
ModelWithKeyAndVersion = Struct . new ( :cache_key , :cache_version )
5
5
6
6
def test_fetch_with_right_version_should_hit
7
- @cache . fetch ( "foo" , version : 1 ) { "bar" }
8
- assert_equal "bar" , @cache . read ( "foo" , version : 1 )
7
+ key = SecureRandom . uuid
8
+ value = SecureRandom . alphanumeric
9
+
10
+ @cache . fetch ( key , version : 1 ) { value }
11
+ assert_equal value , @cache . read ( key , version : 1 )
9
12
end
10
13
11
14
def test_fetch_with_wrong_version_should_miss
12
- @cache . fetch ( "foo" , version : 1 ) { "bar" }
13
- assert_nil @cache . read ( "foo" , version : 2 )
15
+ key = SecureRandom . uuid
16
+
17
+ @cache . fetch ( key , version : 1 ) { SecureRandom . alphanumeric }
18
+ assert_nil @cache . read ( key , version : 2 )
14
19
end
15
20
16
21
def test_read_with_right_version_should_hit
17
- @cache . write ( "foo" , "bar" , version : 1 )
18
- assert_equal "bar" , @cache . read ( "foo" , version : 1 )
22
+ key = SecureRandom . uuid
23
+ value = SecureRandom . alphanumeric
24
+
25
+ @cache . write ( key , value , version : 1 )
26
+ assert_equal value , @cache . read ( key , version : 1 )
19
27
end
20
28
21
29
def test_read_with_wrong_version_should_miss
22
- @cache . write ( "foo" , "bar" , version : 1 )
23
- assert_nil @cache . read ( "foo" , version : 2 )
30
+ key = SecureRandom . uuid
31
+ value = SecureRandom . alphanumeric
32
+
33
+ @cache . write ( key , value , version : 1 )
34
+ assert_nil @cache . read ( key , version : 2 )
24
35
end
25
36
26
37
def test_exist_with_right_version_should_be_true
27
- @cache . write ( "foo" , "bar" , version : 1 )
28
- assert @cache . exist? ( "foo" , version : 1 )
38
+ key = SecureRandom . uuid
39
+
40
+ @cache . write ( key , SecureRandom . alphanumeric , version : 1 )
41
+ assert @cache . exist? ( key , version : 1 )
29
42
end
30
43
31
44
def test_exist_with_wrong_version_should_be_false
32
- @cache . write ( "foo" , "bar" , version : 1 )
33
- assert_not @cache . exist? ( "foo" , version : 2 )
45
+ key = SecureRandom . uuid
46
+
47
+ @cache . write ( key , SecureRandom . alphanumeric , version : 1 )
48
+ assert_not @cache . exist? ( key , version : 2 )
34
49
end
35
50
36
51
def test_reading_and_writing_with_model_supporting_cache_version
37
- m1v1 = ModelWithKeyAndVersion . new ( "model/1" , 1 )
38
- m1v2 = ModelWithKeyAndVersion . new ( "model/1" , 2 )
52
+ model_name = SecureRandom . alphanumeric
53
+
54
+ m1v1 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 1 )
55
+ m1v2 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 2 )
56
+
57
+ value = SecureRandom . alphanumeric
39
58
40
- @cache . write ( m1v1 , "bar" )
41
- assert_equal "bar" , @cache . read ( m1v1 )
59
+ @cache . write ( m1v1 , value )
60
+ assert_equal value , @cache . read ( m1v1 )
42
61
assert_nil @cache . read ( m1v2 )
43
62
end
44
63
45
64
def test_reading_and_writing_with_model_supporting_cache_version_using_nested_key
46
- m1v1 = ModelWithKeyAndVersion . new ( "model/1" , 1 )
47
- m1v2 = ModelWithKeyAndVersion . new ( "model/1" , 2 )
65
+ model_name = SecureRandom . alphanumeric
48
66
49
- @cache . write ( [ "something" , m1v1 ] , "bar" )
50
- assert_equal "bar" , @cache . read ( [ "something" , m1v1 ] )
67
+ m1v1 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 1 )
68
+ m1v2 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 2 )
69
+
70
+ value = SecureRandom . alphanumeric
71
+
72
+ @cache . write ( [ "something" , m1v1 ] , value )
73
+ assert_equal value , @cache . read ( [ "something" , m1v1 ] )
51
74
assert_nil @cache . read ( [ "something" , m1v2 ] )
52
75
end
53
76
54
77
def test_fetching_with_model_supporting_cache_version
55
- m1v1 = ModelWithKeyAndVersion . new ( "model/1" , 1 )
56
- m1v2 = ModelWithKeyAndVersion . new ( "model/1" , 2 )
78
+ model_name = SecureRandom . alphanumeric
79
+
80
+ m1v1 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 1 )
81
+ m1v2 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 2 )
82
+
83
+ value = SecureRandom . alphanumeric
84
+ other_value = SecureRandom . alphanumeric
57
85
58
- @cache . fetch ( m1v1 ) { "bar" }
59
- assert_equal "bar" , @cache . fetch ( m1v1 ) { "bu" }
60
- assert_equal "bu" , @cache . fetch ( m1v2 ) { "bu" }
86
+ @cache . fetch ( m1v1 ) { value }
87
+ assert_equal value , @cache . fetch ( m1v1 ) { other_value }
88
+ assert_equal other_value , @cache . fetch ( m1v2 ) { other_value }
61
89
end
62
90
63
91
def test_exist_with_model_supporting_cache_version
64
- m1v1 = ModelWithKeyAndVersion . new ( "model/1" , 1 )
65
- m1v2 = ModelWithKeyAndVersion . new ( "model/1" , 2 )
92
+ model_name = SecureRandom . alphanumeric
66
93
67
- @cache . write ( m1v1 , "bar" )
94
+ m1v1 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 1 )
95
+ m1v2 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 2 )
96
+
97
+ value = SecureRandom . alphanumeric
98
+
99
+ @cache . write ( m1v1 , value )
68
100
assert @cache . exist? ( m1v1 )
69
101
assert_not @cache . fetch ( m1v2 )
70
102
end
71
103
72
104
def test_fetch_multi_with_model_supporting_cache_version
73
- m1v1 = ModelWithKeyAndVersion . new ( "model/1" , 1 )
74
- m2v1 = ModelWithKeyAndVersion . new ( "model/2" , 1 )
75
- m2v2 = ModelWithKeyAndVersion . new ( "model/2" , 2 )
105
+ model_name = SecureRandom . alphanumeric
106
+
107
+ m1v1 = ModelWithKeyAndVersion . new ( "#{ model_name } /1" , 1 )
108
+ m2v1 = ModelWithKeyAndVersion . new ( "#{ model_name } /2" , 1 )
109
+ m2v2 = ModelWithKeyAndVersion . new ( "#{ model_name } /2" , 2 )
76
110
77
111
first_fetch_values = @cache . fetch_multi ( m1v1 , m2v1 ) { |m | m . cache_key }
78
112
second_fetch_values = @cache . fetch_multi ( m1v1 , m2v2 ) { |m | m . cache_key + " 2nd" }
79
113
80
- assert_equal ( { m1v1 => "model /1" , m2v1 => "model /2" } , first_fetch_values )
81
- assert_equal ( { m1v1 => "model /1" , m2v2 => "model /2 2nd" } , second_fetch_values )
114
+ assert_equal ( { m1v1 => "#{ model_name } /1" , m2v1 => "#{ model_name } /2" } , first_fetch_values )
115
+ assert_equal ( { m1v1 => "#{ model_name } /1" , m2v2 => "#{ model_name } /2 2nd" } , second_fetch_values )
82
116
end
83
117
84
118
def test_version_is_normalized
85
- @cache . write ( "foo" , "bar" , version : 1 )
86
- assert_equal "bar" , @cache . read ( "foo" , version : "1" )
119
+ key = SecureRandom . uuid
120
+ value = SecureRandom . alphanumeric
121
+
122
+ @cache . write ( key , value , version : 1 )
123
+ assert_equal value , @cache . read ( key , version : "1" )
87
124
end
88
125
end
0 commit comments