cache_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. require 'rails_helper'
  2. RSpec.describe Cache do
  3. describe '.get' do
  4. before { allow(Rails.cache).to receive(:read) }
  5. it 'wraps Rails.cache.read' do
  6. described_class.get('foo')
  7. expect(Rails.cache).to have_received(:read).with('foo')
  8. end
  9. context 'with a non-string argument' do
  10. it 'passes a string' do
  11. described_class.get(:foo)
  12. expect(Rails.cache).to have_received(:read).with('foo')
  13. end
  14. end
  15. end
  16. describe '.write' do
  17. it 'stores string values' do
  18. expect { described_class.write('123', 'some value') }
  19. .to change { described_class.get('123') }.to('some value')
  20. end
  21. it 'stores hash values' do
  22. expect { described_class.write('123', { key: 'some value' }) }
  23. .to change { described_class.get('123') }.to({ key: 'some value' })
  24. end
  25. it 'overwrites previous values' do
  26. described_class.write('123', 'some value')
  27. expect { described_class.write('123', { key: 'some value' }) }
  28. .to change { described_class.get('123') }.to({ key: 'some value' })
  29. end
  30. it 'stores hash values with non-ASCII content' do
  31. expect { described_class.write('123', { key: 'some valueöäüß' }) }
  32. .to change { described_class.get('123') }.to({ key: 'some valueöäüß' })
  33. end
  34. context 'when expiring' do
  35. # we need to travel to a fixed point in time
  36. # to prevent influences of timezone / DST
  37. before do
  38. travel_to '1995-12-21 13:37 +0100'
  39. end
  40. it 'defaults to expires_in: 7.days' do
  41. described_class.write('123', 'some value')
  42. expect { travel 7.days - 1.second }.not_to change { described_class.get('123') }
  43. expect { travel 2.seconds }.to change { described_class.get('123') }.to(nil)
  44. end
  45. it 'accepts a custom :expires_in option' do
  46. described_class.write('123', 'some value', expires_in: 3.seconds)
  47. expect { travel 4.seconds }.to change { described_class.get('123') }.to(nil)
  48. end
  49. end
  50. end
  51. describe '.delete' do
  52. it 'deletes stored values' do
  53. described_class.write('123', 'some value')
  54. expect { described_class.delete('123') }
  55. .to change { described_class.get('123') }.to(nil)
  56. end
  57. it 'is idempotent' do
  58. described_class.write('123', 'some value')
  59. described_class.delete('123')
  60. expect { described_class.delete('123') }.not_to raise_error
  61. end
  62. end
  63. describe '.clear' do
  64. it 'deletes all stored values' do
  65. described_class.write('123', 'some value')
  66. described_class.write('456', 'some value')
  67. expect { described_class.clear }
  68. .to change { described_class.get('123') }.to(nil)
  69. .and change { described_class.get('456') }.to(nil)
  70. end
  71. it 'is idempotent' do
  72. described_class.write('123', 'some value')
  73. described_class.clear
  74. expect { described_class.clear }.not_to raise_error
  75. end
  76. context 'when cache directory is not present on disk' do
  77. before { FileUtils.rm_rf(Rails.cache.cache_path) }
  78. it 'does not raise an error' do
  79. expect { described_class.clear }.not_to raise_error
  80. end
  81. end
  82. end
  83. end