File: test_ttl_sanitizer.rb

package info (click to toggle)
ruby-dalli 5.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 992 kB
  • sloc: ruby: 9,447; sh: 19; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,503 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# frozen_string_literal: true

require_relative '../helper'

describe Dalli::Protocol::TtlSanitizer do
  describe 'sanitize' do
    subject { Dalli::Protocol::TtlSanitizer.sanitize(arg_value) }

    describe 'when the argument is an integer' do
      let(:arg_value) { arg_value_as_i }

      describe 'when the value is less than 30 days in seconds' do
        let(:arg_value_as_i) { rand((30 * 24 * 60 * 60) + 1) }

        it 'just returns the value' do
          assert_equal subject, arg_value_as_i
        end
      end

      describe 'when the value is more than 30 days in seconds, but less than the current timestamp' do
        let(:arg_value_as_i) { (30 * 24 * 60 * 60) + 1 + rand(100 * 24 * 60 * 60) }
        let(:now) { 1_634_706_177 }

        it 'converts to a future timestamp' do
          Dalli::Protocol::TtlSanitizer.stub :current_timestamp, now do
            assert_equal subject, arg_value_as_i + now
          end
        end
      end

      describe 'when the value is more than the current timestamp' do
        let(:now) { 1_634_706_177 }
        let(:arg_value_as_i) { now + 1 + rand(100 * 24 * 60 * 60) }

        it 'just returns the value' do
          Dalli::Protocol::TtlSanitizer.stub :current_timestamp, now do
            assert_equal subject, arg_value_as_i
          end
        end
      end
    end

    describe 'when the argument is a string' do
      let(:arg_value) { arg_value_as_i.to_s }

      describe 'when the value is less than 30 days in seconds' do
        let(:arg_value_as_i) { rand((30 * 24 * 60 * 60) + 1) }

        it 'just returns the value' do
          assert_equal subject, arg_value_as_i
        end
      end

      describe 'when the value is more than 30 days in seconds, but less than the current timestamp' do
        let(:arg_value_as_i) { (30 * 24 * 60 * 60) + 1 + rand(100 * 24 * 60 * 60) }
        let(:now) { 1_634_706_177 }

        it 'converts to a future timestamp' do
          Dalli::Protocol::TtlSanitizer.stub :current_timestamp, now do
            assert_equal subject, arg_value_as_i + now
          end
        end
      end

      describe 'when the value is more than the current timestamp' do
        let(:now) { 1_634_706_177 }
        let(:arg_value_as_i) { now + 1 + rand(100 * 24 * 60 * 60) }

        it 'just returns the value' do
          Dalli::Protocol::TtlSanitizer.stub :current_timestamp, now do
            assert_equal subject, arg_value_as_i
          end
        end
      end
    end
  end
end