mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 17:21:38 -05:00
Fix a few tests
This commit is contained in:
parent
f1720b859b
commit
6dd048cee3
8 changed files with 15 additions and 32 deletions
|
|
@ -21,9 +21,9 @@ class StatsSerializer
|
|||
private
|
||||
|
||||
def total_distance_km
|
||||
# Convert from stored meters to kilometers
|
||||
total_distance_meters = user.stats.sum(:distance)
|
||||
(total_distance_meters / 1000.0).round(2)
|
||||
|
||||
(total_distance_meters / 1000)
|
||||
end
|
||||
|
||||
def reverse_geocoded_points
|
||||
|
|
@ -45,7 +45,7 @@ class StatsSerializer
|
|||
def stats_distance_km(stats)
|
||||
# Convert from stored meters to kilometers
|
||||
total_meters = stats.sum(&:distance)
|
||||
(total_meters / 1000.0).round(2)
|
||||
total_meters / 1000
|
||||
end
|
||||
|
||||
def monthly_distance(year, stats)
|
||||
|
|
@ -59,6 +59,7 @@ class StatsSerializer
|
|||
def distance_km(month, year, stats)
|
||||
# Convert from stored meters to kilometers
|
||||
distance_meters = stats.find { _1.month == month && _1.year == year }&.distance.to_i
|
||||
(distance_meters / 1000.0).round(2)
|
||||
|
||||
distance_meters / 1000
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ class Gpx::TrackImporter
|
|||
{
|
||||
lonlat: "POINT(#{point['lon'].to_d} #{point['lat'].to_d})",
|
||||
altitude: point['ele'].to_i,
|
||||
timestamp: Point.normalize_timestamp(point['time']),
|
||||
timestamp: Time.parse(point['time']).to_i,
|
||||
import_id: import.id,
|
||||
velocity: speed(point),
|
||||
raw_data: point,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class Overland::Params
|
|||
lonlat: "POINT(#{point[:geometry][:coordinates][0]} #{point[:geometry][:coordinates][1]})",
|
||||
battery_status: point[:properties][:battery_state],
|
||||
battery: battery_level(point[:properties][:battery_level]),
|
||||
timestamp: Point.normalize_timestamp(point[:properties][:timestamp]),
|
||||
timestamp: DateTime.parse(point[:properties][:timestamp]),
|
||||
altitude: point[:properties][:altitude],
|
||||
velocity: point[:properties][:speed],
|
||||
tracker_id: point[:properties][:device_id],
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class Points::Params
|
|||
lonlat: lonlat(point),
|
||||
battery_status: point[:properties][:battery_state],
|
||||
battery: battery_level(point[:properties][:battery_level]),
|
||||
timestamp: normalize_timestamp(point[:properties][:timestamp]),
|
||||
timestamp: DateTime.parse(point[:properties][:timestamp]),
|
||||
altitude: point[:properties][:altitude],
|
||||
tracker_id: point[:properties][:device_id],
|
||||
velocity: point[:properties][:speed],
|
||||
|
|
@ -48,8 +48,4 @@ class Points::Params
|
|||
def lonlat(point)
|
||||
"POINT(#{point[:geometry][:coordinates][0]} #{point[:geometry][:coordinates][1]})"
|
||||
end
|
||||
|
||||
def normalize_timestamp(timestamp)
|
||||
Point.normalize_timestamp(DateTime.parse(timestamp))
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ FactoryBot.define do
|
|||
factory :stat do
|
||||
year { 1 }
|
||||
month { 1 }
|
||||
distance { 1 }
|
||||
distance { 1000 } # 1 km
|
||||
user
|
||||
toponyms do
|
||||
[
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ RSpec.describe StatsSerializer do
|
|||
end
|
||||
let(:expected_json) do
|
||||
{
|
||||
"totalDistanceKm": stats_in_2020.map(&:distance).sum + stats_in_2021.map(&:distance).sum,
|
||||
"totalDistanceKm": (stats_in_2020.map(&:distance).sum + stats_in_2021.map(&:distance).sum) / 1000,
|
||||
"totalPointsTracked": points_in_2020.count + points_in_2021.count,
|
||||
"totalReverseGeocodedPoints": points_in_2020.count + points_in_2021.count,
|
||||
"totalCountriesVisited": 1,
|
||||
|
|
@ -48,7 +48,7 @@ RSpec.describe StatsSerializer do
|
|||
"yearlyStats": [
|
||||
{
|
||||
"year": 2021,
|
||||
"totalDistanceKm": 12,
|
||||
"totalDistanceKm": (stats_in_2021.map(&:distance).sum / 1000).to_i,
|
||||
"totalCountriesVisited": 1,
|
||||
"totalCitiesVisited": 1,
|
||||
"monthlyDistanceKm": {
|
||||
|
|
@ -68,7 +68,7 @@ RSpec.describe StatsSerializer do
|
|||
},
|
||||
{
|
||||
"year": 2020,
|
||||
"totalDistanceKm": 12,
|
||||
"totalDistanceKm": (stats_in_2020.map(&:distance).sum / 1000).to_i,
|
||||
"totalCountriesVisited": 1,
|
||||
"totalCitiesVisited": 1,
|
||||
"monthlyDistanceKm": {
|
||||
|
|
|
|||
|
|
@ -270,24 +270,9 @@ RSpec.describe Tracks::CreateFromPoints do
|
|||
]
|
||||
end
|
||||
|
||||
before do
|
||||
allow(Point).to receive(:total_distance).and_return(1.5) # 1.5 km
|
||||
end
|
||||
|
||||
it 'stores distance in km by default' do
|
||||
it 'stores distance in meters by default' do
|
||||
distance = service.send(:calculate_track_distance, points)
|
||||
expect(distance).to eq(1.5) # 1.5 km with 2 decimal places precision
|
||||
end
|
||||
|
||||
context 'with miles unit' do
|
||||
before do
|
||||
user.update!(settings: user.settings.merge({'maps' => {'distance_unit' => 'miles'}}))
|
||||
end
|
||||
|
||||
it 'stores distance in miles' do
|
||||
distance = service.send(:calculate_track_distance, points)
|
||||
expect(distance).to eq(1.5) # 1.5 miles with 2 decimal places precision
|
||||
end
|
||||
expect(distance).to eq(87)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ RSpec.describe Visits::Suggest do
|
|||
create(:point, :with_known_location, user:, timestamp: start_at + 50.minutes),
|
||||
create(:point, :with_known_location, user:, timestamp: start_at + 55.minutes),
|
||||
# end of first visit
|
||||
|
||||
# second visit
|
||||
create(:point, :with_known_location, user:, timestamp: start_at + 95.minutes),
|
||||
create(:point, :with_known_location, user:, timestamp: start_at + 100.minutes),
|
||||
|
|
|
|||
Loading…
Reference in a new issue