|
4 | 4 | require 'testing_helper'
|
5 | 5 | require 'redis_client/cluster'
|
6 | 6 | require 'redis_client/cluster/command'
|
| 7 | +require 'redis_client/cluster/errors' |
7 | 8 |
|
8 | 9 | class RedisClient
|
9 | 10 | class Cluster
|
10 | 11 | class TestCommand < Minitest::Test
|
11 | 12 | include ::RedisClient::TestingHelper
|
12 | 13 |
|
| 14 | + def test_load |
| 15 | + [ |
| 16 | + { nodes: @clients, error: nil }, |
| 17 | + { nodes: [], error: ::RedisClient::Cluster::InitialSetupError }, |
| 18 | + { nodes: [''], error: NoMethodError }, |
| 19 | + { nodes: nil, error: ::RedisClient::Cluster::InitialSetupError } |
| 20 | + ].each_with_index do |c, idx| |
| 21 | + msg = "Case: #{idx}" |
| 22 | + got = -> { ::RedisClient::Cluster::Command.load(c[:nodes]) } |
| 23 | + if c[:error].nil? |
| 24 | + assert_instance_of(::RedisClient::Cluster::Command, got.call, msg) |
| 25 | + else |
| 26 | + assert_raises(c[:error], msg, &got) |
| 27 | + end |
| 28 | + end |
| 29 | + end |
| 30 | + |
13 | 31 | def test_parse_command_details
|
14 | 32 | keys = %i[arity flags first last step].freeze
|
15 | 33 | [
|
@@ -45,6 +63,55 @@ def test_parse_command_details
|
45 | 63 | end
|
46 | 64 | end
|
47 | 65 |
|
| 66 | + def test_extract_first_key |
| 67 | + cmd = ::RedisClient::Cluster::Command.load(@clients) |
| 68 | + [ |
| 69 | + { command: %w[SET foo 1], want: 'foo' }, |
| 70 | + { command: %w[GET foo], want: 'foo' }, |
| 71 | + { command: %w[GET foo{bar}baz], want: 'bar' }, |
| 72 | + { command: %w[MGET foo bar baz], want: 'foo' }, |
| 73 | + { command: %w[UNKNOWN foo bar], want: '' }, |
| 74 | + { command: [['GET'], 'foo'], want: 'foo' }, |
| 75 | + { command: ['GET', ['foo']], want: 'foo' }, |
| 76 | + { command: [], want: '' }, |
| 77 | + { command: nil, want: '' } |
| 78 | + ].each_with_index do |c, idx| |
| 79 | + msg = "Case: #{idx}" |
| 80 | + got = cmd.extract_first_key(c[:command]) |
| 81 | + assert_equal(c[:want], got, msg) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + def test_should_send_to_primary? |
| 86 | + cmd = ::RedisClient::Cluster::Command.load(@clients) |
| 87 | + [ |
| 88 | + { command: %w[SET foo 1], want: true }, |
| 89 | + { command: %w[GET foo], want: false }, |
| 90 | + { command: %w[UNKNOWN foo bar], want: nil }, |
| 91 | + { command: [], want: nil }, |
| 92 | + { command: nil, want: nil } |
| 93 | + ].each_with_index do |c, idx| |
| 94 | + msg = "Case: #{idx}" |
| 95 | + got = cmd.should_send_to_primary?(c[:command]) |
| 96 | + c[:want].nil? ? assert_nil(got, msg) : assert_equal(c[:want], got, msg) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def test_should_send_to_replica? |
| 101 | + cmd = ::RedisClient::Cluster::Command.load(@clients) |
| 102 | + [ |
| 103 | + { command: %w[SET foo 1], want: false }, |
| 104 | + { command: %w[GET foo], want: true }, |
| 105 | + { command: %w[UNKNOWN foo bar], want: nil }, |
| 106 | + { command: [], want: nil }, |
| 107 | + { command: nil, want: nil } |
| 108 | + ].each_with_index do |c, idx| |
| 109 | + msg = "Case: #{idx}" |
| 110 | + got = cmd.should_send_to_replica?(c[:command]) |
| 111 | + c[:want].nil? ? assert_nil(got, msg) : assert_equal(c[:want], got, msg) |
| 112 | + end |
| 113 | + end |
| 114 | + |
48 | 115 | def test_pick_details
|
49 | 116 | keys = %i[first_key_position write readonly].freeze
|
50 | 117 | [
|
|
0 commit comments