summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authormhasbini <mohammad.hasbini@gmail.com>2017-04-04 13:54:58 +0300
committermhasbini <mohammad.hasbini@gmail.com>2017-04-04 13:54:58 +0300
commite52b1df1d5391f0fa0b9ec7eb2d3492b05f64ba4 (patch)
treec9fbd2c57dae0a5872653d3df0b9a681a5441404 /spec
parent9fc17f6f4abdb04f3cf1b60b87bd67b894a19c39 (diff)
downloadgitlab-ce-e52b1df1d5391f0fa0b9ec7eb2d3492b05f64ba4.tar.gz
Remove useless queries with false conditions (e.g 1=0)
Diffstat (limited to 'spec')
-rw-r--r--spec/support/issuables_list_metadata_shared_examples.rb15
-rw-r--r--spec/support/matchers/query_matcher.rb33
-rw-r--r--spec/workers/process_commit_worker_spec.rb7
3 files changed, 55 insertions, 0 deletions
diff --git a/spec/support/issuables_list_metadata_shared_examples.rb b/spec/support/issuables_list_metadata_shared_examples.rb
index 4c0f556e736..e1ac8fd60cd 100644
--- a/spec/support/issuables_list_metadata_shared_examples.rb
+++ b/spec/support/issuables_list_metadata_shared_examples.rb
@@ -33,4 +33,19 @@ shared_examples 'issuables list meta-data' do |issuable_type, action = nil|
expect(meta_data[id].upvotes).to eq(id + 2)
end
end
+
+ describe "when given empty collection" do
+ let(:project2) { create(:empty_project, :public) }
+
+ it "doesn't execute any queries with false conditions" do
+ get_action =
+ if action
+ proc { get action }
+ else
+ proc { get :index, namespace_id: project2.namespace, project_id: project2 }
+ end
+
+ expect(&get_action).not_to make_queries_matching(/WHERE (?:1=0|0=1)/)
+ end
+ end
end
diff --git a/spec/support/matchers/query_matcher.rb b/spec/support/matchers/query_matcher.rb
new file mode 100644
index 00000000000..ac8c4ab91d9
--- /dev/null
+++ b/spec/support/matchers/query_matcher.rb
@@ -0,0 +1,33 @@
+RSpec::Matchers.define :make_queries_matching do |matcher, expected_count = nil|
+ supports_block_expectations
+
+ match do |block|
+ @counter = query_count(matcher, &block)
+ if expected_count
+ @counter.count == expected_count
+ else
+ @counter.count > 0
+ end
+ end
+
+ failure_message_when_negated do |_|
+ if expected_count
+ "expected #{matcher} not to match #{expected_count} queries, got #{@counter.count} matches:\n\n#{@counter.inspect}"
+ else
+ "expected #{matcher} not to match any query, got #{@counter.count} matches:\n\n#{@counter.inspect}"
+ end
+ end
+
+ failure_message do |_|
+ if expected_count
+ "expected #{matcher} to match #{expected_count} queries, got #{@counter.count} matches:\n\n#{@counter.inspect}"
+ else
+ "expected #{matcher} to match at least one query, got #{@counter.count} matches:\n\n#{@counter.inspect}"
+ end
+ end
+
+ def query_count(regex, &block)
+ @recorder = ActiveRecord::QueryRecorder.new(&block).log
+ @recorder.select{ |q| q.match(regex) }
+ end
+end
diff --git a/spec/workers/process_commit_worker_spec.rb b/spec/workers/process_commit_worker_spec.rb
index 1c383d0514d..9afe2e610b9 100644
--- a/spec/workers/process_commit_worker_spec.rb
+++ b/spec/workers/process_commit_worker_spec.rb
@@ -99,6 +99,13 @@ describe ProcessCommitWorker do
expect(metric.first_mentioned_in_commit_at).to eq(commit.committed_date)
end
+
+ it "doesn't execute any queries with false conditions" do
+ allow(commit).to receive(:safe_message).
+ and_return("Lorem Ipsum")
+
+ expect { worker.update_issue_metrics(commit, user) }.not_to make_queries_matching(/WHERE (?:1=0|0=1)/)
+ end
end
describe '#build_commit' do