File tree Expand file tree Collapse file tree 3 files changed +29
-35
lines changed Expand file tree Collapse file tree 3 files changed +29
-35
lines changed Original file line number Diff line number Diff line change 2
2
require "database_cleaner/core"
3
3
require "database_cleaner/redis/truncation"
4
4
5
+ module DatabaseCleaner
6
+ module Redis
7
+ def self . available_strategies
8
+ [ :truncation ]
9
+ end
10
+ end
11
+ end
12
+
5
13
DatabaseCleaner [ :redis ] . strategy = :truncation
Original file line number Diff line number Diff line change 1
- require "database_cleaner/generic/base"
2
- require "database_cleaner/generic/truncation"
1
+ require "database_cleaner/strategy"
3
2
4
3
module DatabaseCleaner
5
4
module Redis
6
- def self . available_strategies
7
- %i[ truncation ]
8
- end
9
-
10
- def self . default_strategy
11
- available_strategies . first
12
- end
13
-
14
- class Truncation
15
- include DatabaseCleaner ::Generic ::Base
16
- include DatabaseCleaner ::Generic ::Truncation
17
-
18
- def db
19
- @db ||= :default
5
+ class Truncation < Strategy
6
+ def initialize only : [ ] , except : [ ]
7
+ @only = only
8
+ @except = except
20
9
end
21
- attr_writer :db
22
-
23
- alias_method :url , :db
24
10
25
11
def clean
26
- if @only
27
- @only . each do |term |
28
- connection . keys ( term ) . each { |k | connection . del k }
29
- end
30
- elsif @tables_to_exclude
31
- keys_except = [ ]
32
- @tables_to_exclude . each { |term | keys_except += connection . keys ( term ) }
33
- connection . keys . each { |k | connection . del ( k ) unless keys_except . include? ( k ) }
34
- else
12
+ only = expand_keys ( @only )
13
+ except = expand_keys ( @except )
14
+
15
+ if only . none? && except . none?
35
16
connection . flushdb
17
+ else
18
+ tables_to_clean ( connection . keys , only : only , except : except ) . each do |key |
19
+ connection . del key
20
+ end
36
21
end
37
- connection . quit unless url == :default
22
+
23
+ connection . quit unless db == :default
38
24
end
39
25
40
26
private
41
27
28
+ def expand_keys keys
29
+ keys . flat_map { |key | connection . keys ( key ) }
30
+ end
31
+
42
32
def connection
43
33
@connection ||= begin
44
- if url == :default
34
+ if db == :default
45
35
::Redis . new
46
36
elsif db . is_a? ( ::Redis ) # pass directly the connection
47
37
db
48
38
else
49
- ::Redis . new ( url : url )
39
+ ::Redis . new ( url : db )
50
40
end
51
41
end
52
42
end
Original file line number Diff line number Diff line change 5
5
module DatabaseCleaner
6
6
RSpec . describe Redis do
7
7
it_behaves_like "a database_cleaner adapter"
8
-
9
- it "has a default_strategy of truncation" do
10
- expect ( described_class . default_strategy ) . to eq ( :truncation )
11
- end
12
8
end
13
9
end
14
10
You can’t perform that action at this time.
0 commit comments