diff --git a/spec/lib/cache_spec.rb b/spec/lib/cache_spec.rb new file mode 100644 index 000000000..e54208ca2 --- /dev/null +++ b/spec/lib/cache_spec.rb @@ -0,0 +1,94 @@ +require 'rails_helper' + +RSpec.describe Cache do + describe '.get' do + before { allow(Rails.cache).to receive(:read) } + + it 'wraps Rails.cache.read' do + Cache.get('foo') + + expect(Rails.cache).to have_received(:read).with('foo') + end + + context 'with a non-string argument' do + it 'passes a string' do + Cache.get(:foo) + + expect(Rails.cache).to have_received(:read).with('foo') + end + end + end + + describe '.write' do + it 'stores string values' do + expect { Cache.write('123', 'some value') } + .to change { Cache.get('123') }.to('some value') + end + + it 'stores hash values' do + expect { Cache.write('123', { key: 'some value' }) } + .to change { Cache.get('123') }.to({ key: 'some value' }) + end + + it 'overwrites previous values' do + Cache.write('123', 'some value') + + expect { Cache.write('123', { key: 'some value' }) } + .to change { Cache.get('123') }.to({ key: 'some value' }) + end + + it 'stores hash values with non-ASCII content' do + expect { Cache.write('123', { key: 'some valueöäüß' }) } + .to change { Cache.get('123') }.to({ key: 'some valueöäüß' }) + end + + it 'defaults to expires_in: 7.days' do + Cache.write('123', 'some value') + + expect { travel 7.days }.not_to change { Cache.get('123') } + expect { travel 1.second }.to change { Cache.get('123') }.to(nil) + end + + it 'accepts a custom :expires_in option' do + Cache.write('123', 'some value', expires_in: 3.seconds) + + expect { travel 4.seconds }.to change { Cache.get('123') }.to(nil) + end + end + + describe '.delete' do + it 'deletes stored values' do + Cache.write('123', 'some value') + + expect { Cache.delete('123') } + .to change { Cache.get('123') }.to(nil) + end + + it 'is idempotent' do + Cache.write('123', 'some value') + Cache.delete('123') + + expect { Cache.delete('123') }.not_to raise_error + end + end + + describe '.clear' do + it 'deletes all stored values' do + Cache.write('123', 'some value') + Cache.write('456', 'some value') + + # rubocop:disable Layout/MultilineMethodCallIndentation + expect { Cache.clear } + .to change { Cache.get('123') }.to(nil) + .and change { Cache.get('456') }.to(nil) + # rubocop:enable Layout/MultilineMethodCallIndentation + end + + it 'is idempotent' do + Cache.write('123', 'some value') + Cache.clear + + expect { Cache.delete('123') }.not_to raise_error + end + end +end diff --git a/test/unit/cache_test.rb b/test/unit/cache_test.rb deleted file mode 100644 index d1a0facda..000000000 --- a/test/unit/cache_test.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'test_helper' - -class CacheTest < ActiveSupport::TestCase - test 'cache' do - - # test 1 - Cache.write('123', 'some value') - cache = Cache.get('123') - assert_equal(cache, 'some value') - - Cache.write('123', { key: 'some value' }) - cache = Cache.get('123') - assert_equal(cache, { key: 'some value' }) - - # test 2 - Cache.write('123', { key: 'some valueöäüß' }) - cache = Cache.get('123') - assert_equal(cache, { key: 'some valueöäüß' }) - - # test 3 - Cache.delete('123') - cache = Cache.get('123') - assert_nil(cache) - - # test 4 - Cache.write('123', { key: 'some valueöäüß2' }) - cache = Cache.get('123') - assert_equal(cache, { key: 'some valueöäüß2' }) - - Cache.delete('123') - cache = Cache.get('123') - assert_nil(cache) - - # test 5 - Cache.clear - cache = Cache.get('123') - assert_nil(cache) - - Cache.delete('123') - cache = Cache.get('123') - assert_nil(cache) - - # test 6 - Cache.write('123', { key: 'some valueöäüß2' }, expires_in: 3.seconds) - travel 5.seconds - cache = Cache.get('123') - assert_nil(cache) - end - - # verify if second cache write overwrite first one - test 'cache reset' do - Cache.write('some_reset_key', 123) - Cache.write('some_reset_key', 12_356) - cache = Cache.get('some_reset_key') - assert_equal(cache, 12_356, 'verify') - end -end