Skip to content

Commit

Permalink
Add integration test for querying with result cache
Browse files Browse the repository at this point in the history
  • Loading branch information
saharaying committed Nov 17, 2021
1 parent bd5b781 commit e26bfe7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/pkg/
/spec/reports/
/tmp/
/tags

/.idea/
/graphql-result_cache-*.gem
2 changes: 2 additions & 0 deletions lib/graphql/result_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ def self.use(schema_def, options: {})
end
end

GraphQL::Schema::Field.accepts_definition(:result_cache)

require 'graphql/result_cache/rails' if defined?(::Rails::Engine)
9 changes: 9 additions & 0 deletions spec/fixtures/query_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class QueryType < GraphQL::Schema::Object
field_class GraphQL::ResultCache::Field

field :colors, [String], null: false, result_cache: true

def colors
%w[red yellow blue]
end
end
4 changes: 4 additions & 0 deletions spec/fixtures/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Schema < GraphQL::Schema
query QueryType
use GraphQL::ResultCache
end
4 changes: 2 additions & 2 deletions spec/graphql/result_cache/condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

context 'with global except' do
context 'as proc' do
before(:all) do
::GraphQL::ResultCache.except = ->(ctx) { !ctx[:result_cacheable] }
before do
allow(::GraphQL::ResultCache).to receive(:except).and_return ->(ctx) { !ctx[:result_cacheable] }
end

context 'evaluated as true' do
Expand Down
16 changes: 8 additions & 8 deletions spec/graphql/result_cache/context_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
let(:context) { instance_double('GraphQL::Context', query: query, path: path) }
let(:result) { instance_double('GraphQL::Result', query: query) }
let(:cache_key) { 'cache_key' }
let(:cache) { double('cache') }
let(:cache_store) { double('cache') }
let(:callback) { instance_double('GraphQL::ResultCache::Callback') }

before do
::GraphQL::ResultCache.cache = cache
allow(::GraphQL::ResultCache).to receive(:cache).and_return cache_store
end

describe '#add' do
context 'when not cached' do
before do
expect(cache).to receive(:exist?).with(cache_key).and_return false
expect(cache_store).to receive(:exist?).with(cache_key).and_return false
end

after do
Expand All @@ -34,8 +34,8 @@

context 'when cached' do
before do
expect(cache).to receive(:exist?).with(cache_key).and_return true
allow(cache).to receive(:read).with(cache_key).and_return 'cached_result'
expect(cache_store).to receive(:exist?).with(cache_key).and_return true
allow(cache_store).to receive(:read).with(cache_key).and_return 'cached_result'
end

it 'should add with cached result' do
Expand All @@ -55,7 +55,7 @@
context 'without cached result' do
after do
expect(subject).to receive(:dig).with(result, 'data', *path).and_return 'result_on_path'
expect(cache).to receive(:write).with(cache_key, 'result_on_path', expires_in: 3600)
expect(cache_store).to receive(:write).with(cache_key, 'result_on_path', expires_in: 3600)
expect(subject.process(result)).to eq result
end

Expand All @@ -75,7 +75,7 @@

after do
allow(result).to receive(:to_h).and_return result_value
expect(cache).to receive(:write).never
expect(cache_store).to receive(:write).never
expect(subject.process(result)).to eq result
expect(result_value).to eq expected_result
end
Expand All @@ -98,7 +98,7 @@
it 'should return result without process' do
subject.value[other_query] = [{path: path, key: cache_key, result: nil}]
expect(subject).to receive(:dig).never
expect(cache).to receive(:write).never
expect(cache_store).to receive(:write).never
expect(subject.process(result)).to eq result
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/graphql/result_cache/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@
let(:client_hash_value) { Time.now.to_i }

it 'should call the proc' do
::GraphQL::ResultCache.client_hash = -> { client_hash_value }
allow(::GraphQL::ResultCache).to receive(:client_hash).and_return -> { client_hash_value }
expect(clause).to eq(client_hash_value)
end
end

context 'when client hash is a scala value' do
it 'should be nil when client hash not set' do
::GraphQL::ResultCache.client_hash = nil
allow(::GraphQL::ResultCache).to receive(:client_hash).and_return nil
expect(clause).to be_nil
end

it 'should be the value when client hash is a string' do
::GraphQL::ResultCache.client_hash = 'abcdef'
allow(::GraphQL::ResultCache).to receive(:client_hash).and_return 'abcdef'
expect(clause).to eq('abcdef')
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'
require 'fixtures/query_type'
require 'fixtures/schema'

RSpec.describe 'query with result cache' do
let(:cache_store) { double('cache_store') }
let(:query_string) { 'query { colors }' }
let(:result) { GraphQL::ResultCache::Result.new(Schema.execute(query_string)).value }

before do
allow(GraphQL::ResultCache).to receive(:cache).and_return cache_store
end

it 'query with cache missed' do
cache_key = 'GraphQL:Result:colors'
expect(cache_store).to receive(:exist?).with(cache_key).and_return false
expect(cache_store).not_to receive(:read)
expect(cache_store).to receive(:write).with(cache_key, %w[red yellow blue], expires_in: 3600)
expect(result).to eq 'data' => { 'colors' => %w[red yellow blue] }
end

it 'queries with cache hits' do
cache_key = 'GraphQL:Result:colors'
expect(cache_store).to receive(:exist?).with(cache_key).and_return true
expect(cache_store).to receive(:read).with(cache_key).and_return %w[red yellow]
expect(cache_store).not_to receive(:write)
expect(result).to eq 'data' => { 'colors' => %w[red yellow] }
end
end

0 comments on commit e26bfe7

Please sign in to comment.