2
2
3
3
require 'set'
4
4
require 'testing_helper'
5
+ require 'redis_client/cluster'
5
6
require 'redis_client/cluster/command'
6
7
7
8
class RedisClient
8
9
class Cluster
9
10
class TestCommand < Minitest ::Test
11
+ include ::RedisClient ::TestingHelper
12
+
10
13
def test_parse_command_details
14
+ keys = %i[ arity flags first last step ] . freeze
11
15
[
12
16
{
13
17
rows : [
@@ -19,24 +23,30 @@ def test_parse_command_details
19
23
'set' => { arity : -3 , flags : Set [ 'write' , 'denyoom' , 'movablekeys' ] , first : 1 , last : 1 , step : 1 }
20
24
}
21
25
} ,
26
+ {
27
+ rows : [
28
+ [ 'GET' , 2 , Set [ 'readonly' , 'fast' ] , 1 , 1 , 1 , Set [ '@read' , '@string' , '@fast' ] , Set [ ] , Set [ ] , Set [ ] ]
29
+ ] ,
30
+ want : {
31
+ 'get' => { arity : 2 , flags : Set [ 'readonly' , 'fast' ] , first : 1 , last : 1 , step : 1 }
32
+ }
33
+ } ,
34
+ { rows : [ [ ] ] , want : { } } ,
22
35
{ rows : [ ] , want : { } } ,
23
36
{ rows : nil , want : { } }
24
- ] . each do |c |
37
+ ] . each_with_index do |c , idx |
38
+ msg = "Case: #{ idx } "
25
39
got = ::RedisClient ::Cluster ::Command . send ( :parse_command_details , c [ :rows ] )
26
- assert_equal ( c [ :want ] . size , got . size )
27
- assert_equal ( c [ :want ] . keys . sort , got . keys . sort )
28
- c [ :want ] . each do |k , v |
29
- a = got [ k ]
30
- assert_equal ( v [ :arity ] , a [ :arity ] )
31
- assert_equal ( v [ :flags ] , a [ :flags ] )
32
- assert_equal ( v [ :first ] , a [ :first ] )
33
- assert_equal ( v [ :last ] , a [ :last ] )
34
- assert_equal ( v [ :step ] , a [ :step ] )
40
+ assert_equal ( c [ :want ] . size , got . size , msg )
41
+ assert_equal ( c [ :want ] . keys . sort , got . keys . sort , msg )
42
+ c [ :want ] . each do |k1 , v |
43
+ keys . each { |k2 | assert_equal ( v [ k2 ] , got [ k1 ] [ k2 ] , "#{ msg } : #{ k2 } " ) }
35
44
end
36
45
end
37
46
end
38
47
39
48
def test_pick_details
49
+ keys = %i[ first_key_position write readonly ] . freeze
40
50
[
41
51
{
42
52
details : {
@@ -50,19 +60,109 @@ def test_pick_details
50
60
} ,
51
61
{ details : { } , want : { } } ,
52
62
{ details : nil , want : { } }
53
- ] . each do |c |
63
+ ] . each_with_index do |c , idx |
64
+ msg = "Case: #{ idx } "
54
65
cmd = ::RedisClient ::Cluster ::Command . new ( c [ :details ] )
55
66
got = cmd . send ( :pick_details , c [ :details ] )
56
- assert_equal ( c [ :want ] . size , got . size )
57
- assert_equal ( c [ :want ] . keys . sort , got . keys . sort )
58
- c [ :want ] . each do |k , v |
59
- a = got [ k ]
60
- assert_equal ( v [ :first_key_position ] , a [ :first_key_position ] )
61
- assert_equal ( v [ :write ] , a [ :write ] )
62
- assert_equal ( v [ :readonly ] , a [ :readonly ] )
67
+ assert_equal ( c [ :want ] . size , got . size , msg )
68
+ assert_equal ( c [ :want ] . keys . sort , got . keys . sort , msg )
69
+ c [ :want ] . each do |k1 , v |
70
+ keys . each { |k2 | assert_equal ( v [ k2 ] , got [ k1 ] [ k2 ] , "#{ msg } : #{ k2 } " ) }
63
71
end
64
72
end
65
73
end
74
+
75
+ def test_dig_details
76
+ cmd = ::RedisClient ::Cluster ::Command . new (
77
+ {
78
+ 'get' => { arity : 2 , flags : Set [ 'readonly' , 'fast' ] , first : 1 , last : 1 , step : 1 } ,
79
+ 'set' => { arity : -3 , flags : Set [ 'write' , 'denyoom' , 'movablekeys' ] , first : 1 , last : 1 , step : 1 }
80
+ }
81
+ )
82
+ [
83
+ { params : { command : %w[ SET foo 1 ] , key : :first_key_position } , want : 1 } ,
84
+ { params : { command : %w[ SET foo 1 ] , key : :write } , want : true } ,
85
+ { params : { command : %w[ set foo 1 ] , key : :write } , want : true } ,
86
+ { params : { command : %w[ SET foo 1 ] , key : :readonly } , want : false } ,
87
+ { params : { command : %w[ GET foo ] , key : :first_key_position } , want : 1 } ,
88
+ { params : { command : %w[ GET foo ] , key : :write } , want : false } ,
89
+ { params : { command : %w[ GET foo ] , key : :readonly } , want : true } ,
90
+ { params : { command : %w[ get foo ] , key : :readonly } , want : true } ,
91
+ { params : { command : %w[ UNKNOWN foo ] , key : :readonly } , want : nil } ,
92
+ { params : { command : [ [ 'SET' ] , 'foo' , 1 ] , key : :write } , want : true } ,
93
+ { params : { command : [ ] , key : :readonly } , want : nil } ,
94
+ { params : { command : nil , key : :readonly } , want : nil }
95
+ ] . each_with_index do |c , idx |
96
+ msg = "Case: #{ idx } "
97
+ got = cmd . send ( :dig_details , c [ :params ] [ :command ] , c [ :params ] [ :key ] )
98
+ c [ :want ] . nil? ? assert_nil ( got , msg ) : assert_equal ( c [ :want ] , got , msg )
99
+ end
100
+ end
101
+
102
+ def test_determine_first_key_position
103
+ cmd = ::RedisClient ::Cluster ::Command . load ( @clients )
104
+ [
105
+ { command : %w[ EVAL "return ARGV[1]" 0 hello ] , want : 3 } ,
106
+ { command : [ [ 'EVAL' ] , '"return ARGV[1]"' , 0 , 'hello' ] , want : 3 } ,
107
+ { command : %w[ EVALSHA sha1 2 foo bar baz zap ] , want : 3 } ,
108
+ { command : %w[ MIGRATE host port key 0 5 COPY ] , want : 3 } ,
109
+ { command : %w[ ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 ] , want : 3 } ,
110
+ { command : %w[ ZUNIONSTORE out 2 zset1 zset2 WEIGHTS 2 3 ] , want : 3 } ,
111
+ { command : %w[ OBJECT HELP ] , want : 2 } ,
112
+ { command : %w[ MEMORY HELP ] , want : 0 } ,
113
+ { command : %w[ MEMORY USAGE key ] , want : 2 } ,
114
+ { command : %w[ XREAD COUNT 2 STREAMS mystream writers 0-0 0-0 ] , want : 4 } ,
115
+ { command : %w[ XREADGROUP GROUP group consumer STREAMS key id ] , want : 5 } ,
116
+ { command : %w[ SET foo 1 ] , want : 1 } ,
117
+ { command : %w[ set foo 1 ] , want : 1 } ,
118
+ { command : [ [ 'SET' ] , 'foo' , 1 ] , want : 1 } ,
119
+ { command : %w[ GET foo ] , want : 1 }
120
+ ] . each_with_index do |c , idx |
121
+ msg = "Case: #{ idx } "
122
+ got = cmd . send ( :determine_first_key_position , c [ :command ] )
123
+ assert_equal ( c [ :want ] , got , msg )
124
+ end
125
+ end
126
+
127
+ def test_determine_optional_key_position
128
+ cmd = ::RedisClient ::Cluster ::Command . load ( @clients )
129
+ [
130
+ { params : { command : %w[ XREAD COUNT 2 STREAMS mystream writers 0-0 0-0 ] , option_name : 'streams' } , want : 4 } ,
131
+ { params : { command : %w[ XREADGROUP GROUP group consumer STREAMS key id ] , option_name : 'streams' } , want : 5 } ,
132
+ { params : { command : %w[ GET foo ] , option_name : 'bar' } , want : 0 } ,
133
+ { params : { command : [ 'FOO' , [ 'BAR' ] , 'BAZ' ] , option_name : 'bar' } , want : 2 } ,
134
+ { params : { command : %w[ FOO BAR BAZ ] , option_name : 'BAR' } , want : 2 } ,
135
+ { params : { command : [ ] , option_name : nil } , want : 0 } ,
136
+ { params : { command : [ ] , option_name : '' } , want : 0 } ,
137
+ { params : { command : nil , option_name : nil } , want : 0 }
138
+ ] . each_with_index do |c , idx |
139
+ msg = "Case: #{ idx } "
140
+ got = cmd . send ( :determine_optional_key_position , c [ :params ] [ :command ] , c [ :params ] [ :option_name ] )
141
+ assert_equal ( c [ :want ] , got , msg )
142
+ end
143
+ end
144
+
145
+ def test_extract_hash_tag
146
+ cmd = ::RedisClient ::Cluster ::Command . load ( @clients )
147
+ [
148
+ { key : 'foo' , want : '' } ,
149
+ { key : 'foo{bar}baz' , want : 'bar' } ,
150
+ { key : 'foo{bar}baz{qux}quuc' , want : 'bar' } ,
151
+ { key : 'foo}bar{baz' , want : '' } ,
152
+ { key : 'foo{bar' , want : '' } ,
153
+ { key : 'foo}bar' , want : '' } ,
154
+ { key : 'foo{}bar' , want : '' } ,
155
+ { key : '{}foo' , want : '' } ,
156
+ { key : 'foo{}' , want : '' } ,
157
+ { key : '{}' , want : '' } ,
158
+ { key : '' , want : '' } ,
159
+ { key : nil , want : '' }
160
+ ] . each_with_index do |c , idx |
161
+ msg = "Case: #{ idx } "
162
+ got = cmd . send ( :extract_hash_tag , c [ :key ] )
163
+ assert_equal ( c [ :want ] , got , msg )
164
+ end
165
+ end
66
166
end
67
167
end
68
168
end
0 commit comments