spec/acceptance/rest/stats_spec.rb in ably-0.8.2 vs spec/acceptance/rest/stats_spec.rb in ably-0.8.3
- old
+ new
@@ -5,10 +5,15 @@
include Ably::Modules::Conversions
LAST_YEAR = Time.now.year - 1
LAST_INTERVAL = Time.new(LAST_YEAR, 2, 3, 15, 5, 0) # 3rd Feb 20(x) 16:05:00
+ # Ensure metrics in previous year do not impact on tests for last year
+ PREVIOUS_YEAR = Time.now.year - 2
+ PREVIOUS_INTERVAL = Time.new(PREVIOUS_YEAR, 2, 3, 15, 5, 0)
+ PREVIOUS_YEAR_STATS = 120
+
STATS_FIXTURES = [
{
intervalId: Ably::Models::Stats.to_interval_id(LAST_INTERVAL - 120, :minute),
inbound: { realtime: { messages: { count: 50, data: 5000 } } },
outbound: { realtime: { messages: { count: 20, data: 2000 } } }
@@ -28,23 +33,43 @@
apiRequests: { succeeded: 50, failed: 10 },
tokenRequests: { succeeded: 60, failed: 20 },
}
]
+ PREVIOUS_YEAR_STATS_FIXTURES = PREVIOUS_YEAR_STATS.times.map do |index|
+ {
+ intervalId: Ably::Models::Stats.to_interval_id(PREVIOUS_INTERVAL - (index * 60), :minute),
+ inbound: { realtime: { messages: { count: index } } }
+ }
+ end
+
before(:context) do
reload_test_app # ensure no previous stats interfere
- TestApp.instance.create_test_stats(STATS_FIXTURES)
+ TestApp.instance.create_test_stats(STATS_FIXTURES + PREVIOUS_YEAR_STATS_FIXTURES)
end
vary_by_protocol do
let(:client) { Ably::Rest::Client.new(key: api_key, environment: environment, protocol: protocol) }
describe 'fetching application stats' do
+ it 'returns a PaginatedResult object' do
+ expect(client.stats).to be_kind_of(Ably::Models::PaginatedResult)
+ end
+
context 'by minute' do
let(:first_inbound_realtime_count) { STATS_FIXTURES.first[:inbound][:realtime][:messages][:count] }
let(:last_inbound_realtime_count) { STATS_FIXTURES.last[:inbound][:realtime][:messages][:count] }
+ context 'with no options' do
+ let(:subject) { client.stats(end: LAST_INTERVAL) } # end is needed to ensure no other tests have effected the stats
+ let(:stat) { subject.items.first }
+
+ it 'uses the minute interval by default' do
+ expect(stat.interval_granularity).to eq(:minute)
+ end
+ end
+
context 'with :from set to last interval and :limit set to 1' do
let(:subject) { client.stats(start: as_since_epoch(LAST_INTERVAL), end: LAST_INTERVAL, unit: :minute, limit: 1) }
let(:stat) { subject.items.first }
it 'retrieves only one stat' do
@@ -122,33 +147,56 @@
it 'returns the first interval stats as stats are provided forwards from :start' do
expect(stat.inbound.realtime.all.count).to eql(first_inbound_realtime_count)
end
it 'returns 3 pages of stats' do
- expect(subject).to be_first
expect(subject).to_not be_last
page3 = subject.next.next
expect(page3).to be_last
expect(page3.items.first.inbound.realtime.all.count).to eql(last_inbound_realtime_count)
end
end
context 'with :end set to last interval, :limit set to 1 and direction :backwards' do
- let(:subject) { client.stats(:end => as_since_epoch(LAST_INTERVAL), end: LAST_INTERVAL, unit: :minute, direction: :backwards, limit: 1) }
+ let(:subject) { client.stats(end: LAST_INTERVAL, unit: :minute, direction: :backwards, limit: 1) }
let(:stat) { subject.items.first }
it 'returns the 3rd interval stats first as stats are provided backwards from :end' do
expect(stat.inbound.realtime.all.count).to eql(last_inbound_realtime_count)
end
it 'returns 3 pages of stats' do
- expect(subject).to be_first
expect(subject).to_not be_last
page3 = subject.next.next
expect(page3.items.first.inbound.realtime.all.count).to eql(first_inbound_realtime_count)
end
end
+
+ context 'with :end set to last interval and :limit set to 3 to ensure only last years stats are included' do
+ let(:subject) { client.stats(end: LAST_INTERVAL, unit: :minute, limit: 3) }
+ let(:stats) { subject.items }
+
+ context 'the REST API' do
+ it 'defaults to direction :backwards' do
+ expect(stats.first.inbound.realtime.messages.count).to eql(70) # current minute
+ expect(stats.last.inbound.realtime.messages.count).to eql(50) # 2 minutes back
+ end
+ end
+ end
+
+ context 'with :end set to previous year interval' do
+ let(:subject) { client.stats(end: PREVIOUS_INTERVAL, unit: :minute) }
+ let(:stats) { subject.items }
+
+ context 'the REST API' do
+ it 'defaults to 100 items for pagination' do
+ expect(stats.count).to eql(100)
+ next_page_of_stats = subject.next.items
+ expect(next_page_of_stats.count).to eql(PREVIOUS_YEAR_STATS - 100)
+ end
+ end
+ end
end
[:hour, :day, :month].each do |interval|
context "by #{interval}" do
let(:subject) { client.stats(start: as_since_epoch(LAST_INTERVAL), end: LAST_INTERVAL, unit: interval, direction: 'forwards', limit: 1) }
@@ -168,9 +216,17 @@
expect(subject.items.count).to eql(1)
expect(stat.all.messages.count).to eql(aggregate_messages_count)
expect(stat.all.messages.data).to eql(aggregate_messages_data)
end
+ end
+ end
+
+ context 'when argument start is after end' do
+ let(:subject) { client.stats(start: as_since_epoch(LAST_INTERVAL), end: LAST_INTERVAL - 120, unit: :minute) }
+
+ it 'should raise an exception' do
+ expect { subject.items }.to raise_error ArgumentError
end
end
end
end
end