Skip to content

Commit

Permalink
Add support for scanning on keys containing hash tags
Browse files Browse the repository at this point in the history
  • Loading branch information
timobleeker committed Aug 8, 2023
1 parent e26d205 commit 8a98543
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mock_redis/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def looks_like_float?(str)
def redis_pattern_to_ruby_regex(pattern)
Regexp.new(
"^#{pattern}$".
gsub(/([+|()])/, '\\\\\1').
gsub(/([+|(){}])/, '\\\\\1').
gsub(/(?<!\\)\?/, '\\1.').
gsub(/([^\\])\*/, '\\1.*')
)
Expand Down
11 changes: 11 additions & 0 deletions spec/commands/scan_each_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
end
end

context 'when giving a custom match filter with a hash tag' do
let(:match) { 'mock:key:{1}:*' }
let(:data) { ['mock:key:{1}:1', 'mock:key:{1}:2', 'mock:key:{2}:1'].to_h { |e| [e, nil] } }
let(:expected) { %w[mock:key:{1}:1 mock:key:{1}:2] }

it 'returns a 0 cursor and the filtered collection' do
expect(subject.scan_each(match: match)).to match_array(expected)
end
end


context 'when giving a custom match and type filter' do
let(:data) do
{ 'mock:stringkey' => 'mockvalue',
Expand Down
10 changes: 10 additions & 0 deletions spec/commands/scan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
end
end

context 'when giving a custom match filter with a hash tag' do
let(:match) { 'mock:key:{1}:*' }
let(:data) { ['mock:key:{1}:1', 'mock:key:{1}:2', 'mock:key:{2}:1'].to_h { |e| [e, nil] } }
let(:expected) { ['0', %w[mock:key:{1}:1 mock:key:{1}:2]] }

it 'returns a 0 cursor and the filtered collection' do
expect(subject.scan(0, count: count, match: match)).to eq(expected)
end
end

context 'when giving a custom match and type filter' do
let(:data) do
{ 'mock:stringkey' => 'mockvalue',
Expand Down

0 comments on commit 8a98543

Please sign in to comment.