Skip to content

Commit

Permalink
Add stats API endpoint spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Freika committed Aug 20, 2024
1 parent 7ed7f97 commit d5ba51f
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions spec/requests/api/v1/stats_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe "Api::V1::Stats", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
RSpec.describe 'Api::V1::Stats', type: :request do
let(:user) { create(:user) }

describe 'GET /index' do
let!(:user) { create(:user) }
let!(:stats_in_2020) { create_list(:stat, 12, year: 2020, user:) }
let!(:stats_in_2021) { create_list(:stat, 12, year: 2021, user:) }
let!(:points_in_2020) { create_list(:point, 85, :with_geodata, timestamp: Time.zone.local(2020), user:) }
let!(:points_in_2021) { create_list(:point, 95, timestamp: Time.zone.local(2021), user:) }
let(:expected_json) do
{
totalDistanceKm: stats_in_2020.map(&:distance).sum + stats_in_2021.map(&:distance).sum,
totalPointsTracked: points_in_2020.count + points_in_2021.count,
totalReverseGeocodedPoints: points_in_2020.count,
totalCountriesVisited: 1,
totalCitiesVisited: 1,
yearlyStats: [
{
year: 2021,
totalDistanceKm: 12,
totalCountriesVisited: 1,
totalCitiesVisited: 1,
monthlyDistanceKm: {
january: 1,
february: 0,
march: 0,
april: 0,
may: 0,
june: 0,
july: 0,
august: 0,
september: 0,
october: 0,
november: 0,
december: 0
}
},
{
year: 2020,
totalDistanceKm: 12,
totalCountriesVisited: 1,
totalCitiesVisited: 1,
monthlyDistanceKm: {
january: 1,
february: 0,
march: 0,
april: 0,
may: 0,
june: 0,
july: 0,
august: 0,
september: 0,
october: 0,
november: 0,
december: 0
}
}
]
}.to_json
end

it 'renders a successful response' do
get api_v1_areas_url(api_key: user.api_key)
expect(response).to be_successful
end

it 'returns the stats' do
get api_v1_stats_url(api_key: user.api_key)

expect(response).to be_successful
expect(response.body).to eq(expected_json)
end
end
end

0 comments on commit d5ba51f

Please sign in to comment.