2019-03-27 20:02:25 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Types::BaseField do
|
|
|
|
context 'when considering complexity' do
|
2019-05-06 21:24:19 +00:00
|
|
|
let(:resolver) do
|
|
|
|
Class.new(described_class) do
|
2019-05-31 16:46:16 +00:00
|
|
|
def self.resolver_complexity(args, child_complexity:)
|
2019-05-06 21:24:19 +00:00
|
|
|
2 if args[:foo]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.complexity_multiplier(args)
|
|
|
|
0.01
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 20:02:25 +00:00
|
|
|
it 'defaults to 1' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true)
|
|
|
|
|
|
|
|
expect(field.to_graphql.complexity).to eq 1
|
|
|
|
end
|
|
|
|
|
2019-06-16 10:12:56 +00:00
|
|
|
describe '#base_complexity' do
|
|
|
|
context 'with no gitaly calls' do
|
|
|
|
it 'defaults to 1' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true)
|
|
|
|
|
|
|
|
expect(field.base_complexity).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a gitaly call' do
|
|
|
|
it 'adds 1 to the default value' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, calls_gitaly: true)
|
|
|
|
|
|
|
|
expect(field.base_complexity).to eq 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 20:02:25 +00:00
|
|
|
it 'has specified value' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, complexity: 12)
|
|
|
|
|
|
|
|
expect(field.to_graphql.complexity).to eq 12
|
|
|
|
end
|
2019-05-06 21:24:19 +00:00
|
|
|
|
2019-06-04 22:52:06 +00:00
|
|
|
context 'when field has a resolver proc' do
|
|
|
|
context 'and is a connection' do
|
|
|
|
let(:field) { described_class.new(name: 'test', type: GraphQL::STRING_TYPE.connection_type, resolver_class: resolver, max_page_size: 100, null: true) }
|
2019-05-06 21:24:19 +00:00
|
|
|
|
2019-06-04 22:52:06 +00:00
|
|
|
it 'sets complexity depending on arguments for resolvers' do
|
|
|
|
expect(field.to_graphql.complexity.call({}, {}, 2)).to eq 4
|
|
|
|
expect(field.to_graphql.complexity.call({}, { first: 50 }, 2)).to eq 3
|
|
|
|
end
|
2019-05-06 21:24:19 +00:00
|
|
|
|
2019-06-04 22:52:06 +00:00
|
|
|
it 'sets complexity depending on number load limits for resolvers' do
|
|
|
|
expect(field.to_graphql.complexity.call({}, { first: 1 }, 2)).to eq 2
|
|
|
|
expect(field.to_graphql.complexity.call({}, { first: 1, foo: true }, 2)).to eq 4
|
|
|
|
end
|
|
|
|
end
|
2019-05-06 21:24:19 +00:00
|
|
|
|
2019-06-04 22:52:06 +00:00
|
|
|
context 'and is not a connection' do
|
|
|
|
it 'sets complexity as normal' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, resolver_class: resolver, max_page_size: 100, null: true)
|
|
|
|
|
|
|
|
expect(field.to_graphql.complexity.call({}, {}, 2)).to eq 2
|
|
|
|
expect(field.to_graphql.complexity.call({}, { first: 50 }, 2)).to eq 2
|
|
|
|
end
|
|
|
|
end
|
2019-05-06 21:24:19 +00:00
|
|
|
end
|
2019-06-16 10:12:56 +00:00
|
|
|
|
|
|
|
context 'calls_gitaly' do
|
|
|
|
context 'for fields with a resolver' do
|
|
|
|
it 'adds 1 if true' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, calls_gitaly: true)
|
|
|
|
|
|
|
|
expect(field.to_graphql.complexity).to eq 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for fields without a resolver' do
|
|
|
|
it 'adds 1 if true' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, calls_gitaly: true)
|
|
|
|
|
|
|
|
expect(field.to_graphql.complexity).to eq 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to false' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true)
|
|
|
|
|
|
|
|
expect(field.base_complexity).to eq Types::BaseField::DEFAULT_COMPLEXITY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is overridden by declared complexity value' do
|
|
|
|
field = described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, calls_gitaly: true, complexity: 12)
|
|
|
|
|
|
|
|
expect(field.to_graphql.complexity).to eq 12
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#calls_gitaly_check' do
|
|
|
|
let(:gitaly_field) { described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, calls_gitaly: true) }
|
|
|
|
let(:no_gitaly_field) { described_class.new(name: 'test', type: GraphQL::STRING_TYPE, null: true, calls_gitaly: false) }
|
|
|
|
|
|
|
|
context 'if there are no Gitaly calls' do
|
|
|
|
it 'does not raise an error if calls_gitaly is false' do
|
2019-06-21 14:20:00 +00:00
|
|
|
expect { no_gitaly_field.send(:calls_gitaly_check, 0) }.not_to raise_error
|
2019-06-16 10:12:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if there is at least 1 Gitaly call' do
|
2019-06-21 14:20:00 +00:00
|
|
|
it 'does not raise an error if calls_gitaly is true' do
|
|
|
|
expect { gitaly_field.send(:calls_gitaly_check, 1) }.not_to raise_error
|
2019-06-16 10:12:56 +00:00
|
|
|
end
|
|
|
|
|
2019-06-21 14:20:00 +00:00
|
|
|
it 'raises an error if calls_gitaly: is false or not defined' do
|
|
|
|
expect { no_gitaly_field.send(:calls_gitaly_check, 1) }.to raise_error(/please add `calls_gitaly: true`/)
|
2019-06-16 10:12:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-27 20:02:25 +00:00
|
|
|
end
|
|
|
|
end
|