diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-03 00:08:11 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-03 00:08:11 +0000 |
commit | bd8fb5668ae739a83d55ec5ca4a04344eef2167e (patch) | |
tree | baf085c6cd58b3b5e5a192d4d3db360d623bb056 /spec/lib/grafana | |
parent | 561e1b470f0a99fe6304c8f197348c47a637d594 (diff) | |
download | gitlab-ce-bd8fb5668ae739a83d55ec5ca4a04344eef2167e.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/grafana')
-rw-r--r-- | spec/lib/grafana/time_window_spec.rb | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/spec/lib/grafana/time_window_spec.rb b/spec/lib/grafana/time_window_spec.rb new file mode 100644 index 00000000000..e70861658ca --- /dev/null +++ b/spec/lib/grafana/time_window_spec.rb @@ -0,0 +1,115 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Grafana::TimeWindow do + let(:from) { '1552799400000' } + let(:to) { '1552828200000' } + + around do |example| + Timecop.freeze(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 + +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| + Timecop.freeze(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 + +describe Grafana::Timestamp do + let(:timestamp) { Time.at(1552799400) } + + around do |example| + Timecop.freeze(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 |