From dbf24b2f6c77eb071b8efdb57bc4552e6be01508 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Mon, 14 Jan 2019 12:11:50 +0800 Subject: [PATCH] Refactoring: Migrate cache_test.rb to RSpec --- spec/lib/cache_spec.rb | 94 +++++++++++++++++++++++++++++++++++++++++ test/unit/cache_test.rb | 57 ------------------------- 2 files changed, 94 insertions(+), 57 deletions(-) create mode 100644 spec/lib/cache_spec.rb delete mode 100644 test/unit/cache_test.rb 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