Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specs for custom cache clients #8

Merged
merged 1 commit into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop'
require 'rubocop-rspec'
require 'rubocop/rake_task'

RuboCop::RakeTask.new(:rubocop) do |t|
config_path = File.expand_path(File.join('.rubocop.yml'), __dir__)

t.options = ['--config', config_path]
t.requires << 'rubocop-rspec'
end

RSpec::Core::RakeTask.new(:rspec)

Expand Down
1 change: 1 addition & 0 deletions lib/any_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# @since 0.1.0
class AnyCache
require_relative 'any_cache/version'
require_relative 'any_cache/error'
require_relative 'any_cache/drivers'
require_relative 'any_cache/adapters'

Expand Down
4 changes: 2 additions & 2 deletions lib/any_cache/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class << self
# @param driver [Object]
# @return [AnyCache::Adapters::Basic]
#
# @raise [AnyCache::UnsupportedCacheDriverError]
# @raise [AnyCache::UnsupportedDriverError]
#
# @api private
# @since 0.1.0
Expand All @@ -34,7 +34,7 @@ def build(driver)
when ActiveSupportMemCacheStore.supported_driver?(driver) then ActiveSupportMemCacheStore.new(driver)
when Delegator.supported_driver?(driver) then Delegator.new(driver)
else
raise AnyCache::UnsupportedCacheDriverError
raise AnyCache::UnsupportedDriverError
end
end
# rubocop:enable Metrics/LineLength
Expand Down
111 changes: 111 additions & 0 deletions spec/features/custom_cache_clients_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# frozen_string_literal: true

describe 'Custom cache clients' do
context 'when custom cache clinet supports all required methods' do
let(:custom_client) do
Class.new do
# rubocop:disable Layout/EmptyLineBetweenDefs
def read(key, **); end
def write(key, value, **); end
def delete(key, **); end
def increment(key, value, **); end
def decrement(key, value, **); end
def expire(key, **); end
def persist(key, **); end
def clear(key, **); end
def exist?(key, **); end
# rubocop:enable Layout/EmptyLineBetweenDefs
end.new
end

let(:dumb_client) do
# rubocop:disable Layout/EmptyLineBetweenDefs
Class.new do
def read; end
def write; end
def delete; end
def increment; end
def decrement; end
def expire; end
def persist; end
def clear; end
def exist?; end
end.new
# rubocop:enable Layout/EmptyLineBetweenDefs
end

specify 'returns AnyCache instance' do
expect(AnyCache.build(custom_client)).to be_a(AnyCache)
end

specify 'requires only a delegation' do
expect(AnyCache.build(dumb_client)).to be_a(AnyCache)
end

%i[
read
delete
expire
persist
clear
exist?
].each do |operation|
specify "AnyCache instance delegates :#{operation} operation to the custom client" do
cache_store = AnyCache.build(custom_client)

entry_key = SecureRandom.hex
method_options = { SecureRandom.hex.to_sym => SecureRandom.hex }

expect(custom_client).to receive(operation).with(entry_key, method_options)
cache_store.send(operation, entry_key, **method_options)
end
end

%i[
write
increment
decrement
].each do |operation|
specify "AnyCache instance delegates :#{operation} operation to the custom client" do
cache_store = AnyCache.build(custom_client)

entry_key = SecureRandom.hex
method_value = SecureRandom.hex
method_options = { SecureRandom.hex.to_sym => SecureRandom.hex }

expect(custom_client).to receive(operation).with(entry_key, method_value, method_options)
cache_store.send(operation, entry_key, method_value, **method_options)
end
end
end

context 'when custom cache client supports a part of required methods' do
let(:required_methods) do
%i[
read
write
delete
increment
decrement
expire
persist
clear
exist?
]
end

specify 'fails due to instantiation' do
while required_methods.shift
incomplete_cache_client = Class.new.tap do |klass|
required_methods.each do |required_method|
klass.define_method(required_method) {}
end
end.new

expect do
AnyCache.build(incomplete_cache_client)
end.to raise_error(AnyCache::UnsupportedDriverError)
end
end
end
end