diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-12 21:09:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-12 21:09:21 +0300 |
commit | 43e3dc2f95a25c600e08f65d4f1c406a1a63ed3d (patch) | |
tree | db5c72020c7c8916020c8aff7c1b7128028d650b /spec/support_specs | |
parent | 2c89e169769ead722394a79ed67fcd08e96863dd (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support_specs')
-rw-r--r-- | spec/support_specs/helpers/active_record/query_recorder_spec.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/spec/support_specs/helpers/active_record/query_recorder_spec.rb b/spec/support_specs/helpers/active_record/query_recorder_spec.rb new file mode 100644 index 00000000000..48069c6a766 --- /dev/null +++ b/spec/support_specs/helpers/active_record/query_recorder_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ActiveRecord::QueryRecorder do + class TestQueries < ActiveRecord::Base + self.table_name = 'schema_migrations' + end + + describe 'detecting the right number of calls and their origin' do + it 'detects two separate queries' do + control = ActiveRecord::QueryRecorder.new query_recorder_debug: true do + 2.times { TestQueries.count } + TestQueries.first + end + + # Test first_only flag works as expected + expect(control.find_query(/.*query_recorder_spec.rb.*/, 0, first_only: true)) + .to eq(control.find_query(/.*query_recorder_spec.rb.*/, 0).first) + # Check #find_query + expect(control.find_query(/.*/, 0).size) + .to eq(control.data.keys.size) + # Ensure exactly 2 COUNT queries were detected + expect(control.occurrences_by_line_method.last[1][:occurrences] + .find_all {|i| i.match(/SELECT COUNT/) }.count).to eq(2) + # Ensure exactly 1 LIMIT 1 (#first) + expect(control.occurrences_by_line_method.first[1][:occurrences] + .find_all { |i| i.match(/ORDER BY.*#{TestQueries.table_name}.*LIMIT 1/) }.count).to eq(1) + + # Ensure 3 DB calls overall were executed + expect(control.log.size).to eq(3) + # Ensure memoization value match the raw value above + expect(control.count).to eq(control.log.size) + # Ensure we have only two sources of queries + expect(control.data.keys.size).to eq(2) + # Ensure we detect only queries from this file + expect(control.data.keys.find_all { |i| i.match(/query_recorder_spec.rb/) }.count).to eq(2) + end + end +end |