summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorcharlieablett <cablett@gitlab.com>2019-05-02 19:09:10 +1200
committercharlieablett <cablett@gitlab.com>2019-05-30 18:27:28 +1200
commit2a1006416748950805294793f1bc8d6fa7435eea (patch)
treed54dfd57be5ddd7b9e5fc4e9d1fe271c88c41f3e /spec
parent2c011cb5b452409db7fe1c810f1ad7440a6cedce (diff)
downloadgitlab-ce-2a1006416748950805294793f1bc8d6fa7435eea.tar.gz
Restructure complexity analyzer
Remove instance variables for class re-use, test individual methods, use `monotonic_time`
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb40
-rw-r--r--spec/requests/api/graphql/gitlab_schema_spec.rb2
-rw-r--r--spec/requests/api/graphql_spec.rb26
3 files changed, 54 insertions, 14 deletions
diff --git a/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb b/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb
index 53a1d7f8e42..f85a3a206b1 100644
--- a/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb
+++ b/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb
@@ -7,21 +7,43 @@ describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do
subject { described_class.new }
let(:query_string) { "abc" }
let(:provided_variables) { { a: 1, b: 2, c: 3 } }
+ let!(:now) { Gitlab::Metrics::System.monotonic_time }
let(:complexity) { 4 }
let(:depth) { 2 }
- let(:expected_hash) do
- { query_string: query_string,
+ let(:initial_values) do
+ { time_started: now,
+ query_string: query_string,
variables: provided_variables,
- complexity: complexity,
- depth: depth }
+ complexity: nil,
+ depth: nil,
+ duration: nil }
+ end
+ before do
+ allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(now)
end
- it 'assembles a hash' do
- query = OpenStruct.new(query_string: query_string, provided_variables: provided_variables)
-
- subject.initial_value(query)
+ describe '#initial_value' do
+ it 'assembles a hash with initial values' do
+ query = OpenStruct.new(query_string: query_string, provided_variables: provided_variables)
- expect(subject.instance_variable_get("@info_hash")).to eq expected_hash
+ expect(subject.initial_value(query)).to eq initial_values
+ end
end
+ describe '#call' do
+ before do
+ # some statements to fudge the complexity and depth
+ end
+
+ it 'sets the complexity and depth' do
+ expected_hash = { time_started: now,
+ query_string: query_string,
+ variables: provided_variables,
+ complexity: nil,
+ depth: depth,
+ duration: complexity }
+
+ expect(subject.call(initial_values, nil, nil)).to eq expected_hash
+ end
+ end
end
diff --git a/spec/requests/api/graphql/gitlab_schema_spec.rb b/spec/requests/api/graphql/gitlab_schema_spec.rb
index 0eb026f1ed9..dcce8c1dbad 100644
--- a/spec/requests/api/graphql/gitlab_schema_spec.rb
+++ b/spec/requests/api/graphql/gitlab_schema_spec.rb
@@ -85,7 +85,7 @@ describe 'GitlabSchema configurations' do
context 'logging' do
it 'writes to the GraphQL log' do
- expect(Gitlab::GraphqlLogger).to receive(:info).with(/Query Complexity/)
+ expect(Gitlab::GraphqlLogger).to receive(:info)
query = File.read(Rails.root.join('spec/fixtures/api/graphql/introspection.graphql'))
diff --git a/spec/requests/api/graphql_spec.rb b/spec/requests/api/graphql_spec.rb
index 103b02ba7a7..036dfa41952 100644
--- a/spec/requests/api/graphql_spec.rb
+++ b/spec/requests/api/graphql_spec.rb
@@ -17,14 +17,32 @@ describe 'GraphQL' do
end
context 'logging' do
- it 'logs the query' do
- expected = { query_string: query, variables: {}, duration: anything }
-
+ before do
expect(Gitlab::GraphqlLogger).to receive(:info).with(expected)
+ end
- post_graphql(query)
+ context 'with no variables' do
+ let(:expected) do
+ { query_string: query, variables: {}, duration: anything, depth: 0, complexity: 0 }
+ end
+
+ it 'logs the query' do
+ post_graphql(query)
+ end
end
+ context 'with variables' do
+ let!(:variables) do
+ { foo: "bar" }
+ end
+ let(:expected) do
+ { query_string: query, variables: variables, duration: anything, depth: 0, complexity: 0 }
+ end
+
+ it 'logs the query' do
+ post_graphql(query, variables: variables)
+ end
+ end
end
context 'invalid variables' do