diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-10 00:10:04 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-10 00:10:04 +0000 |
commit | 1bf4fece121298260663c6ca73d39716d3548a03 (patch) | |
tree | 5c82dd2fa63552ecb67fcb034740ec7e8fdba25a /spec/support | |
parent | 254ec28f5448f6f353cd98f637985de3d1405854 (diff) | |
download | gitlab-ce-1bf4fece121298260663c6ca73d39716d3548a03.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/matchers/exceed_query_limit.rb | 36 | ||||
-rw-r--r-- | spec/support/shared_examples/models/concerns/composite_id_shared_examples.rb | 47 |
2 files changed, 83 insertions, 0 deletions
diff --git a/spec/support/matchers/exceed_query_limit.rb b/spec/support/matchers/exceed_query_limit.rb index f38ae44a577..b9630b00038 100644 --- a/spec/support/matchers/exceed_query_limit.rb +++ b/spec/support/matchers/exceed_query_limit.rb @@ -73,6 +73,42 @@ module ExceedQueryLimitHelpers end end +RSpec::Matchers.define :issue_same_number_of_queries_as do + supports_block_expectations + + include ExceedQueryLimitHelpers + + def control + block_arg + end + + def control_recorder + @control_recorder ||= ActiveRecord::QueryRecorder.new(&control) + end + + def expected_count + @expected_count ||= control_recorder.count + end + + def verify_count(&block) + @subject_block = block + + (expected_count - actual_count).abs <= threshold + end + + match do |block| + verify_count(&block) + end + + failure_message_when_negated do |actual| + failure_message + end + + def skip_cached + false + end +end + RSpec::Matchers.define :exceed_all_query_limit do |expected| supports_block_expectations diff --git a/spec/support/shared_examples/models/concerns/composite_id_shared_examples.rb b/spec/support/shared_examples/models/concerns/composite_id_shared_examples.rb new file mode 100644 index 00000000000..cd925659311 --- /dev/null +++ b/spec/support/shared_examples/models/concerns/composite_id_shared_examples.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'a where_composite scope' do |scope_name| + let(:result) { described_class.public_send(scope_name, ids) } + + context 'we pass an empty array' do + let(:ids) { [] } + + it 'returns a null relation' do + expect(result).to be_empty + end + end + + context 'we pass nil' do + let(:ids) { nil } + + it 'returns a null relation' do + expect(result).to be_empty + end + end + + context 'we pass a singleton composite id' do + let(:ids) { composite_ids.first } + + it 'finds the first result' do + expect(result).to contain_exactly(first_result) + end + end + + context 'we pass group of ids' do + let(:ids) { composite_ids } + + it 'finds all the results' do + expect(result).to contain_exactly(*all_results) + end + end + + describe 'performance' do + it 'is not O(N)' do + all_ids = composite_ids + one_id = composite_ids.first + + expect { described_class.public_send(scope_name, all_ids) } + .to issue_same_number_of_queries_as { described_class.public_send(scope_name, one_id) } + end + end +end |