2017-11-14 11:08:04 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 12:49:53 +09:00
|
|
|
RSpec.describe Setting do
|
2017-11-14 11:08:04 +09:00
|
|
|
describe '#to_param' do
|
|
|
|
let(:setting) { Fabricate(:setting, var: var) }
|
|
|
|
let(:var) { 'var' }
|
|
|
|
|
|
|
|
it 'returns setting.var' do
|
|
|
|
expect(setting.to_param).to eq var
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.[]' do
|
2024-01-09 20:50:21 +09:00
|
|
|
let(:key) { 'key' }
|
|
|
|
let(:cache_key) { 'cache-key' }
|
|
|
|
let(:cache_value) { 'cache-value' }
|
|
|
|
|
2017-11-14 11:08:04 +09:00
|
|
|
before do
|
2024-01-09 20:50:21 +09:00
|
|
|
allow(described_class).to receive(:cache_key).with(key).and_return(cache_key)
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
context 'when Rails.cache does not exists' do
|
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:default_settings).and_return(default_settings)
|
2023-11-07 18:46:28 +09:00
|
|
|
|
2024-01-09 23:01:53 +09:00
|
|
|
Fabricate(:setting, var: key, value: 42) if save_setting
|
2017-11-14 11:08:04 +09:00
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
Rails.cache.delete(cache_key)
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
let(:default_value) { 'default_value' }
|
|
|
|
let(:default_settings) { { key => default_value } }
|
2024-01-09 23:01:53 +09:00
|
|
|
let(:save_setting) { true }
|
2017-11-14 11:08:04 +09:00
|
|
|
|
2024-01-09 23:01:53 +09:00
|
|
|
context 'when the setting has been saved to database' do
|
|
|
|
it 'returns the value from database' do
|
|
|
|
callback = double
|
|
|
|
allow(callback).to receive(:call)
|
2023-11-07 18:46:28 +09:00
|
|
|
|
2024-01-09 23:01:53 +09:00
|
|
|
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
|
|
|
|
expect(described_class[key]).to eq 42
|
|
|
|
end
|
2023-11-07 18:46:28 +09:00
|
|
|
|
2024-01-09 23:01:53 +09:00
|
|
|
expect(callback).to have_received(:call)
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-09 23:01:53 +09:00
|
|
|
context 'when the setting has not been saved to database' do
|
|
|
|
let(:save_setting) { false }
|
2018-02-18 06:35:05 +09:00
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
it 'returns default_settings[key]' do
|
|
|
|
expect(described_class[key]).to be default_settings[key]
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
context 'when Rails.cache exists' do
|
|
|
|
before do
|
|
|
|
Rails.cache.write(cache_key, cache_value)
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
it 'does not query the database' do
|
|
|
|
callback = double
|
|
|
|
allow(callback).to receive(:call)
|
|
|
|
ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
|
|
|
|
described_class[key]
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
2024-01-09 20:50:21 +09:00
|
|
|
expect(callback).to_not have_received(:call)
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
|
2024-01-09 20:50:21 +09:00
|
|
|
it 'returns the cached value' do
|
|
|
|
expect(described_class[key]).to eq cache_value
|
2017-11-14 11:08:04 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|