2019-01-14 04:11:50 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Cache do
|
|
|
|
describe '.get' do
|
|
|
|
before { allow(Rails.cache).to receive(:read) }
|
|
|
|
|
|
|
|
it 'wraps Rails.cache.read' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.get('foo')
|
2019-01-14 04:11:50 +00:00
|
|
|
|
|
|
|
expect(Rails.cache).to have_received(:read).with('foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a non-string argument' do
|
|
|
|
it 'passes a string' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.get(:foo)
|
2019-01-14 04:11:50 +00:00
|
|
|
|
|
|
|
expect(Rails.cache).to have_received(:read).with('foo')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.write' do
|
|
|
|
it 'stores string values' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.write('123', 'some value') }
|
|
|
|
.to change { described_class.get('123') }.to('some value')
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores hash values' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.write('123', { key: 'some value' }) }
|
|
|
|
.to change { described_class.get('123') }.to({ key: 'some value' })
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'overwrites previous values' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value')
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.write('123', { key: 'some value' }) }
|
|
|
|
.to change { described_class.get('123') }.to({ key: 'some value' })
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores hash values with non-ASCII content' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.write('123', { key: 'some valueöäüß' }) }
|
|
|
|
.to change { described_class.get('123') }.to({ key: 'some valueöäüß' })
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
|
2019-03-25 17:41:40 +00:00
|
|
|
context 'when expiring' do
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-03-25 17:41:40 +00:00
|
|
|
# we need to travel to a fixed point in time
|
|
|
|
# to prevent influences of timezone / DST
|
|
|
|
before do
|
|
|
|
travel_to '1995-12-21 13:37 +0100'
|
|
|
|
end
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-03-25 17:41:40 +00:00
|
|
|
it 'defaults to expires_in: 7.days' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value')
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { travel 7.days - 1.second }.not_to change { described_class.get('123') }
|
|
|
|
expect { travel 2.seconds }.to change { described_class.get('123') }.to(nil)
|
2019-03-25 17:41:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'accepts a custom :expires_in option' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value', expires_in: 3.seconds)
|
2019-03-25 17:41:40 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { travel 4.seconds }.to change { described_class.get('123') }.to(nil)
|
2019-03-25 17:41:40 +00:00
|
|
|
end
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.delete' do
|
|
|
|
it 'deletes stored values' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value')
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.delete('123') }
|
|
|
|
.to change { described_class.get('123') }.to(nil)
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is idempotent' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value')
|
|
|
|
described_class.delete('123')
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.delete('123') }.not_to raise_error
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.clear' do
|
|
|
|
it 'deletes all stored values' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value')
|
|
|
|
described_class.write('456', 'some value')
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.clear }
|
|
|
|
.to change { described_class.get('123') }.to(nil)
|
|
|
|
.and change { described_class.get('456') }.to(nil)
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is idempotent' do
|
2019-04-15 01:41:17 +00:00
|
|
|
described_class.write('123', 'some value')
|
|
|
|
described_class.clear
|
2019-01-14 04:11:50 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.clear }.not_to raise_error
|
2019-01-15 09:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when cache directory is not present on disk' do
|
|
|
|
before { FileUtils.rm_rf(Rails.cache.cache_path) }
|
|
|
|
|
|
|
|
it 'does not raise an error' do
|
2019-04-15 01:41:17 +00:00
|
|
|
expect { described_class.clear }.not_to raise_error
|
2019-01-15 09:12:44 +00:00
|
|
|
end
|
2019-01-14 04:11:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|