Fix tests failing due to new index on stats table

This commit is contained in:
Eugene Burmakin 2025-12-08 23:06:44 +01:00
parent 31edce0276
commit a56889ef8d
7 changed files with 69 additions and 62 deletions

View file

@ -1 +0,0 @@
ed88027f79a12643f6491f78ce705b17a2b00948174575c1b18f64692660e7cd

View file

@ -6,12 +6,19 @@ module SafeTimestampParser
private
def safe_timestamp(date_string)
return Time.zone.now.to_i if date_string.blank?
parsed_time = Time.zone.parse(date_string)
# Time.zone.parse returns epoch time (2000-01-01) for unparseable strings
# Check if it's a valid parse by seeing if year is suspiciously at epoch
return Time.zone.now.to_i if parsed_time.nil? || (parsed_time.year == 2000 && !date_string.include?('2000'))
min_timestamp = Time.zone.parse('1970-01-01').to_i
max_timestamp = Time.zone.parse('2100-01-01').to_i
parsed_time.to_i.clamp(min_timestamp, max_timestamp)
rescue ArgumentError
rescue ArgumentError, TypeError
Time.zone.now.to_i
end
end

View file

@ -3,7 +3,9 @@
require 'rails_helper'
RSpec.describe SafeTimestampParser, type: :controller do
controller(ApplicationController) do
include ActiveSupport::Testing::TimeHelpers
controller(ActionController::Base) do
include SafeTimestampParser
def index
@ -66,7 +68,7 @@ RSpec.describe SafeTimestampParser, type: :controller do
context 'with invalid date strings' do
it 'returns current time for unparseable date' do
freeze_time do
travel_to Time.zone.parse('2023-06-15 12:00:00') do
get :index, params: { date: 'not-a-date' }
expected = Time.zone.now.to_i
expect(response.body).to eq(expected.to_s)
@ -74,7 +76,7 @@ RSpec.describe SafeTimestampParser, type: :controller do
end
it 'returns current time for empty string' do
freeze_time do
travel_to Time.zone.parse('2023-06-15 12:00:00') do
get :index, params: { date: '' }
expected = Time.zone.now.to_i
expect(response.body).to eq(expected.to_s)

View file

@ -199,8 +199,8 @@ RSpec.describe User, type: :model do
describe '#total_distance' do
subject { user.total_distance }
let!(:stat1) { create(:stat, user:, distance: 10_000) }
let!(:stat2) { create(:stat, user:, distance: 20_000) }
let!(:stat1) { create(:stat, user:, year: 2020, month: 10, distance: 10_000) }
let!(:stat2) { create(:stat, user:, year: 2020, month: 11, distance: 20_000) }
it 'returns sum of distances' do
expect(subject).to eq(30) # 30 km
@ -342,13 +342,13 @@ RSpec.describe User, type: :model do
describe '.from_omniauth' do
let(:auth_hash) do
OmniAuth::AuthHash.new({
provider: 'github',
provider: 'github',
uid: '123545',
info: {
email: email,
name: 'Test User'
}
})
})
end
context 'when user exists with the same email' do
@ -395,13 +395,13 @@ RSpec.describe User, type: :model do
let(:email) { 'google@example.com' }
let(:auth_hash) do
OmniAuth::AuthHash.new({
provider: 'google_oauth2',
provider: 'google_oauth2',
uid: '123545',
info: {
email: email,
name: 'Google User'
}
})
})
end
it 'creates a user from Google OAuth data' do

View file

@ -5,8 +5,8 @@ require 'rails_helper'
RSpec.describe 'Api::V1::Stats', type: :request do
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(:stats_in_2020) { (1..12).map { |month| create(:stat, year: 2020, month:, user:) } }
let(:stats_in_2021) { (1..12).map { |month| create(:stat, year: 2021, month:, user:) } }
let(:points_in_2020) do
(1..85).map do |i|
create(:point, :with_geodata,
@ -50,17 +50,17 @@ RSpec.describe 'Api::V1::Stats', type: :request do
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
february: 1,
march: 1,
april: 1,
may: 1,
june: 1,
july: 1,
august: 1,
september: 1,
october: 1,
november: 1,
december: 1
}
},
{
@ -70,17 +70,17 @@ RSpec.describe 'Api::V1::Stats', type: :request do
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
february: 1,
march: 1,
april: 1,
may: 1,
june: 1,
july: 1,
august: 1,
september: 1,
october: 1,
november: 1,
december: 1
}
}
]
@ -100,4 +100,3 @@ RSpec.describe 'Api::V1::Stats', type: :request do
end
end
end

View file

@ -26,8 +26,8 @@ RSpec.describe StatsSerializer do
end
context 'when the user has stats' do
let!(:stats_in_2020) { create_list(:stat, 12, year: 2020, user:) }
let!(:stats_in_2021) { create_list(:stat, 12, year: 2021, user:) }
let!(:stats_in_2020) { (1..12).map { |month| create(:stat, year: 2020, month:, user:) } }
let!(:stats_in_2021) { (1..12).map { |month| create(:stat, year: 2021, month:, user:) } }
let!(:points_in_2020) do
(1..85).map do |i|
create(:point, :with_geodata,
@ -63,17 +63,17 @@ RSpec.describe StatsSerializer do
"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
"february": 1,
"march": 1,
"april": 1,
"may": 1,
"june": 1,
"july": 1,
"august": 1,
"september": 1,
"october": 1,
"november": 1,
"december": 1
}
},
{
@ -83,17 +83,17 @@ RSpec.describe StatsSerializer do
"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
"february": 1,
"march": 1,
"april": 1,
"may": 1,
"june": 1,
"july": 1,
"august": 1,
"september": 1,
"october": 1,
"november": 1,
"december": 1
}
}
]

View file

@ -55,8 +55,8 @@ describe 'Stats API', type: :request 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!(:stats_in_2020) { (1..12).map { |month| create(:stat, year: 2020, month:, user:) } }
let!(:stats_in_2021) { (1..12).map { |month| create(:stat, year: 2021, month:, user:) } }
let!(:points_in_2020) do
(1..85).map do |i|
create(:point, :with_geodata, :reverse_geocoded, timestamp: Time.zone.local(2020, 1, 1).to_i + i.hours,