Skip to content

Commit

Permalink
[ISSUE-66] Improved specs
Browse files Browse the repository at this point in the history
Added coverage report
  • Loading branch information
jlurena committed Jul 9, 2024
1 parent 4f6be17 commit 9693b7a
Show file tree
Hide file tree
Showing 10 changed files with 466 additions and 716 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pkg/*
*DS_Store
.ruby-version
.tool-versions
coverage
10 changes: 7 additions & 3 deletions cached_resource.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ Gem::Specification.new do |s|
s.homepage = "https://github.com/mhgbrown/cached_resource"
s.summary = %q{Caching for ActiveResource}
s.description = %q{Enables request-based caching for ActiveResource}
s.licenses = ['MIT']
s.licenses = ["MIT"]

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.required_ruby_version = '>= 1.9.0'
s.required_ruby_version = ">= 1.9.0"

s.add_runtime_dependency "activeresource", ">= 4.0"
s.add_runtime_dependency "activesupport", ">= 4.0"
s.add_runtime_dependency "nilio", ">= 1.0"

s.add_development_dependency "concurrent-ruby", "~> 1.2", ">= 1.2.3"
s.add_development_dependency "pry-byebug"
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
s.add_development_dependency "concurrent-ruby", '~> 1.2', '>= 1.2.3'
s.add_development_dependency "simplecov", "~> 0.22.0"
s.add_development_dependency "timecop", "~> 0.9.10"

end
2 changes: 1 addition & 1 deletion lib/cached_resource/cached_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setup_cached_resource!(options)
@cached_resource = CachedResource::Configuration.new(options)
if @cached_resource.concurrent_write
begin
send :require, 'concurrent/promise'
require 'concurrent/promise'
rescue LoadError
@cached_resource.logger.error(
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
Expand Down
10 changes: 7 additions & 3 deletions lib/cached_resource/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ def find_with_cache(*arguments)

# Clear the cache.
def clear_cache(options=nil)
cache_clear(options)
if cached_resource.concurrent_write
Concurrent::Promise.execute { cache_clear(options) }
else
cache_clear(options)
end

true
end

private
Expand Down Expand Up @@ -205,8 +211,6 @@ def object_to_json(object, prefix_options, query_options)
:prefix_options => prefix_options,
:original_params => query_options
}.to_json
elsif object.nil?
nil.to_json
else
{
:resource => { :object => object, :persistence => object.persisted? },
Expand Down
3 changes: 3 additions & 0 deletions lib/cached_resource/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def generate_ttl
end

# Enables caching.
# NOTE: Disabled coverage because it is not being picked up by simplecov
# :nocov:
def on!
self.enabled = true
end
Expand All @@ -58,6 +60,7 @@ def on!
def off!
self.enabled = false
end
# :nocov:

private

Expand Down
89 changes: 76 additions & 13 deletions spec/cached_resource/cached_resource_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,92 @@
require 'spec_helper'

RSpec.describe CachedResource do
before do
class BaseThing < ActiveResource::Base
RSpec.describe CachedResource::Model do
let(:logger) { double(:Logger, error: nil)}
let(:dummy_class) do
Class.new(ActiveResource::Base) do
include CachedResource::Model
end
end

describe '.cached_resource' do
context 'when cached resource is not set up' do
it 'initializes and returns a new cached resource configuration' do
options = { logger: logger }
allow(CachedResource::Configuration).to receive(:new).with(options).and_call_original

class FirstChildThing < BaseThing
self.site = 'http://api.first-child-thing.com'
cached_resource
cached_resource = dummy_class.cached_resource(options)

expect(cached_resource).to be_a(CachedResource::Configuration)
expect(dummy_class.instance_variable_get(:@cached_resource)).to eq(cached_resource)
expect(CachedResource::Configuration).to have_received(:new).with(options)
end
end

class SecondChildThing < BaseThing
self.site = 'http://api.second-child-thing.com'
context 'when cached resource is already set up' do
it 'returns the existing cached resource configuration' do
existing_config = CachedResource::Configuration.new
dummy_class.instance_variable_set(:@cached_resource, existing_config)

cached_resource = dummy_class.cached_resource

expect(cached_resource).to eq(existing_config)
end
end
end

after do
[:BaseThing, :FirstChildThing, :SecondChildThing].each do |klass|
Object.send(:remove_const, klass)
describe '.setup_cached_resource!' do
it 'creates a new cached resource configuration' do
options = { logger: logger }
allow(CachedResource::Configuration).to receive(:new).with(options).and_call_original

dummy_class.setup_cached_resource!(options)

expect(dummy_class.instance_variable_get(:@cached_resource)).to be_a(CachedResource::Configuration)
expect(CachedResource::Configuration).to have_received(:new).with(options)
end

context 'when concurrent_write is enabled' do
before do
allow(CachedResource::Configuration).to receive(:new).and_return(
double(concurrent_write: true, logger: logger)
)
end

it 'requires concurrent/promise' do
expect { dummy_class.setup_cached_resource!(concurrent_write: true) }.not_to raise_error(LoadError)
end

context 'When "concurrent/promise" is not installed' do
before do
allow(dummy_class).to receive(:require).with("concurrent/promise").and_raise(LoadError)
end

it 'requires concurrent/promise' do
expect(logger).to receive(:error).with(
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
).once
expect { dummy_class.setup_cached_resource!(concurrent_write: true) }.to raise_error(LoadError)
end
end
end
end

describe '.inherited' do
it 'should include descendants when calling .descendants' do
BaseThing.descendants.sort_by { |klass| klass.name }.should == [FirstChildThing, SecondChildThing]
let(:child_class) { Class.new(dummy_class) }

context 'when cached resource is defined in superclass' do
it 'copies the cached resource configuration to the subclass' do
existing_config = CachedResource::Configuration.new
dummy_class.instance_variable_set(:@cached_resource, existing_config)

expect(child_class.cached_resource).to eq(existing_config)
end
end

context 'when cached resource is not defined in superclass' do
it 'does not set cached resource configuration in the subclass' do
expect(child_class.instance_variable_defined?(:@cached_resource)).to be_falsey
end
end
end
end
Loading

0 comments on commit 9693b7a

Please sign in to comment.