Commit 1e9e0c96f0367ad181287d38e52532347b2ec357
1 parent
4dff9514
Exists in
master
and in
1 other branch
Improved test performance
Showing
1 changed file
with
17 additions
and
5 deletions
Show diff stats
test/support/helpers.rb
... | ... | @@ -26,19 +26,23 @@ class Minitest::Test |
26 | 26 | ($setup_model ||= {})[model] ||= (model.reindex || true) |
27 | 27 | |
28 | 28 | # clear every time |
29 | - model.destroy_all | |
29 | + Searchkick.callbacks(:bulk) do | |
30 | + model.destroy_all | |
31 | + end | |
30 | 32 | end |
31 | 33 | |
32 | 34 | def store(documents, model = default_model, reindex: true) |
33 | 35 | if reindex |
34 | - documents.shuffle.each do |document| | |
35 | - model.create!(document) | |
36 | + Searchkick.callbacks(:bulk) do | |
37 | + with_transaction(model) do | |
38 | + model.create!(documents.shuffle) | |
39 | + end | |
36 | 40 | end |
37 | 41 | model.searchkick_index.refresh |
38 | 42 | else |
39 | 43 | Searchkick.callbacks(false) do |
40 | - documents.shuffle.each do |document| | |
41 | - model.create!(document) | |
44 | + with_transaction(model) do | |
45 | + model.create!(documents.shuffle) | |
42 | 46 | end |
43 | 47 | end |
44 | 48 | end |
... | ... | @@ -102,6 +106,14 @@ class Minitest::Test |
102 | 106 | end |
103 | 107 | end |
104 | 108 | |
109 | + def with_transaction(model, &block) | |
110 | + if model.respond_to?(:transaction) | |
111 | + model.transaction(&block) | |
112 | + else | |
113 | + yield | |
114 | + end | |
115 | + end | |
116 | + | |
105 | 117 | def activerecord? |
106 | 118 | defined?(ActiveRecord) |
107 | 119 | end | ... | ... |