summaryrefslogtreecommitdiff
path: root/spec/lib/grafana/time_window_spec.rb
blob: 0657bed7b28b69cf986b6288952694e47ac17655 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Grafana::TimeWindow do
  let(:from) { '1552799400000' }
  let(:to) { '1552828200000' }

  around do |example|
    travel_to(Time.utc(2019, 3, 17, 13, 10)) { example.run }
  end

  describe '#formatted' do
    subject { described_class.new(from, to).formatted }

    it { is_expected.to eq(start: "2019-03-17T05:10:00Z", end: "2019-03-17T13:10:00Z") }
  end

  describe '#in_milliseconds' do
    subject { described_class.new(from, to).in_milliseconds }

    it { is_expected.to eq(from: 1552799400000, to: 1552828200000) }

    context 'when non-unix parameters are provided' do
      let(:to) { Time.now.to_s }

      let(:default_from) { 8.hours.ago.to_i * 1000 }
      let(:default_to) { Time.now.to_i * 1000 }

      it { is_expected.to eq(from: default_from, to: default_to) }
    end
  end
end

RSpec.describe Grafana::RangeWithDefaults do
  let(:from) { Grafana::Timestamp.from_ms_since_epoch('1552799400000') }
  let(:to) { Grafana::Timestamp.from_ms_since_epoch('1552828200000') }

  around do |example|
    travel_to(Time.utc(2019, 3, 17, 13, 10)) { example.run }
  end

  describe '#to_hash' do
    subject { described_class.new(from: from, to: to).to_hash }

    it { is_expected.to eq(from: from, to: to) }

    context 'when only "to" is provided' do
      let(:from) { nil }

      it 'has the expected properties' do
        expect(subject[:to]).to eq(to)
        expect(subject[:from].time).to eq(to.time - 8.hours)
      end
    end

    context 'when only "from" is provided' do
      let(:to) { nil }

      it 'has the expected properties' do
        expect(subject[:to].time).to eq(from.time + 8.hours)
        expect(subject[:from]).to eq(from)
      end
    end

    context 'when no parameters are provided' do
      let(:to) { nil }
      let(:from) { nil }

      let(:default_from) { 8.hours.ago }
      let(:default_to) { Time.now }

      it 'has the expected properties' do
        expect(subject[:to].time).to eq(default_to)
        expect(subject[:from].time).to eq(default_from)
      end
    end
  end
end

RSpec.describe Grafana::Timestamp do
  let(:timestamp) { Time.at(1552799400) }

  around do |example|
    travel_to(Time.utc(2019, 3, 17, 13, 10)) { example.run }
  end

  describe '#formatted' do
    subject { described_class.new(timestamp).formatted }

    it { is_expected.to eq "2019-03-17T05:10:00Z" }
  end

  describe '#to_ms' do
    subject { described_class.new(timestamp).to_ms }

    it { is_expected.to eq 1552799400000 }
  end

  describe '.from_ms_since_epoch' do
    let(:timestamp) { '1552799400000' }

    subject { described_class.from_ms_since_epoch(timestamp) }

    it { is_expected.to be_a described_class }

    context 'when the input is not a unix-ish timestamp' do
      let(:timestamp) { Time.now.to_s }

      it 'raises an error' do
        expect { subject }.to raise_error(Grafana::Timestamp::Error)
      end
    end
  end
end