Skip to content

Commit

Permalink
test: test for calculate_averages_and_totals
Browse files Browse the repository at this point in the history
  • Loading branch information
HlisTilen committed Aug 9, 2024
1 parent 4d9f1d2 commit 329676a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ TCXReader.jl is a Julia package designed to simplify the process of reading and
## Detailed insights ✨
🚀
- **TCXReader** is a Julia package that provides a simple interface for reading and processing .tcx files, commonly used by Garmin devices and other GPS-enabled fitness devices to store workout data.
- **TCXActivity**: Access metadata about the workout, including the activity type, start time, and total distance.
- **TCXActivity**: Access metadata about the workout, including the activity type, start time, total distance, max speed, average/max heart rate, average/max cadence, average/max watts, total ascent, total descent, and max altitude.
- **TCXAuthor**: Retrieve information about the author of the workout, including name, build version, language ID, and part number.
- **TCXLap**: Retrieve information about laps within a workout, including start and end times, total distance, and total time.
- **TCXTrackPoint**: Access detailed information about each trackpoint in a workout, including time, latitude, longitude, altitude, and heart rate.
- **TCXLap**: Retrieve information about laps within a workout, including start and end times, total distance, total time, and maximum speed.
- **TCXTrackPoint**: Access detailed information about each trackpoint in a workout, including time, latitude, longitude, altitude, heart rate, cadence, speed, and watts.

## Installation 📦

Expand Down Expand Up @@ -59,6 +59,24 @@ function main()
println("Device Name: ", activity.device.name)
println("Device Version: ", activity.device.version)

# Display overall metrics for the activity
println("Total Time (seconds): ", activity.total_time)
println("Total Distance (meters): ", activity.total_distance)
println("Maximum Speed: ", activity.max_speed)
println("Total Calories: ", activity.total_calories)
println("Average Heart Rate (BPM): ", activity.avg_hr)
println("Maximum Heart Rate (BPM): ", activity.max_hr)
println("Average Cadence (Zero Averaging ON): ", activity.avg_cadence_zero_avg_on)
println("Average Cadence (Zero Averaging OFF): ", activity.avg_cadence_zero_avg_off)
println("Max Cadence: ", activity.max_cadence)
println("Average Speed: ", activity.avg_speed)
println("Total Ascent (meters): ", activity.total_ascent)
println("Total Descent (meters): ", activity.total_descent)
println("Max Altitude (meters): ", activity.max_altitude)
println("Average Watts (Zero Averaging ON): ", activity.avg_watts_zero_avg_on)
println("Average Watts (Zero Averaging OFF): ", activity.avg_watts_zero_avg_off)
println("Max Watts: ", activity.max_watts)

# Display information about each lap within the activity
println("\nLaps and Track Points:")
for (i, lap) in enumerate(activity.laps)
Expand All @@ -75,6 +93,7 @@ function main()
end

main()

```

## Datasets
Expand Down
2 changes: 1 addition & 1 deletion examples/reader_demo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function main()
end

# Load a TCX file and export the data to CSV
# author, activities = loadTCXFile("../example_data/15.tcx", "tcx_data_export.csv")
author, activities = loadTCXFile("../example_data/23.tcx", "tcx_data_export.csv")
end

main()
42 changes: 21 additions & 21 deletions test/test_TCXActivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ using Dates

@testset "TCXActivity Tests" begin
activity = TCXActivity(
"Biking",
DateTime(2021, 1, 1, 12),
Vector{TCXLap}(),
DeviceInfo("Garmin", "123", 456, "1.0"),
0.0, # total_time
0.0, # total_distance
0.0, # max_speed
0.0, # total_calories
0.0, # avg_hr
0.0, # max_hr
0.0, # avg_cadence_zero_avg_on
0.0, # avg_cadence_zero_avg_off
0.0, # avg_speed
0.0, # total_ascent
0.0, # total_descent
0.0, # max_altitude
0.0, # avg_watts_zero_avg_on
0.0, # avg_watts_zero_avg_off
0.0, # max_watts
0 # max_cadence
"Biking", # sport
DateTime(2021, 1, 1, 12), # id
Vector{TCXLap}(), # laps
DeviceInfo("Garmin", "123", 456, "1.0"), # device
0.0, # total_time
0.0, # total_distance
0.0, # max_speed
0.0, # total_calories
0.0, # avg_hr
0.0, # max_hr
0.0, # avg_cadence_zero_avg_on
0.0, # avg_cadence_zero_avg_off
0, # max_cadence (make sure this is an Int)
0.0, # avg_speed
0.0, # total_ascent
0.0, # total_descent
0.0, # max_altitude
0.0, # avg_watts_zero_avg_on
0.0, # avg_watts_zero_avg_off
0.0 # max_watts
)

@test activity.sport == "Biking"
Expand All @@ -41,12 +41,12 @@ using Dates
@test activity.max_hr == 0.0
@test activity.avg_cadence_zero_avg_on == 0.0
@test activity.avg_cadence_zero_avg_off == 0.0
@test activity.max_cadence == 0
@test activity.avg_speed == 0.0
@test activity.total_ascent == 0.0
@test activity.total_descent == 0.0
@test activity.max_altitude == 0.0
@test activity.avg_watts_zero_avg_on == 0.0
@test activity.avg_watts_zero_avg_off == 0.0
@test activity.max_watts == 0.0
@test activity.max_cadence == 0
end
46 changes: 33 additions & 13 deletions test/test_TCXReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ end
TCXTrackPoint(DateTime(2021, 1, 1, 12, 10), 45.1, 13.1, 105.0, 1500.0, 155, 90, 2.6, 210),
TCXTrackPoint(DateTime(2021, 1, 1, 12, 20), 45.2, 13.2, 110.0, 2000.0, 160, 100, 2.7, 220),
TCXTrackPoint(DateTime(2021, 1, 1, 12, 30), 45.3, 13.3, 115.0, 2500.0, 165, 110, 2.8, 230),
TCXTrackPoint(DateTime(2021, 1, 1, 12, 40), 45.4, 13.4, 120.0, 3000.0, 170, 0, 2.9, 240) # Cadence 0 for testing zero averaging
TCXTrackPoint(DateTime(2021, 1, 1, 12, 40), 45.4, 13.4, 120.0, 3000.0, 170, 0, 2.9, 0) # Cadence 0 for testing zero averaging
]

# Create a lap
# Create a lap with consistent data
lap = TCXLap(
DateTime(2021, 1, 1, 12),
totalTimeSeconds = 3600.0,
distanceMeters = 3000.0,
maximumSpeed = 3.0,
maximumSpeed = 2.9, # Ensure this matches the test expectation
calories = 500,
averageHeartRateBpm = 160,
maximumHeartRateBpm = 170,
Expand All @@ -135,32 +135,34 @@ end
)

# Calculate averages and totals
total_time, total_distance, max_speed, total_calories, avg_hr, max_hr, avg_cadence_zero_avg_on, avg_cadence_zero_avg_off, avg_speed, total_ascent, total_descent, max_altitude, avg_watts_zero_avg_on, avg_watts_zero_avg_off, max_watts, max_cadence = TCXReader.calculate_averages_and_totals([lap])
total_time, total_distance, max_speed, total_calories, avg_hr, max_hr, avg_cadence_zero_avg_on, avg_cadence_zero_avg_off, max_cadence, avg_speed, ascent, descent, max_altitude, avg_watts_zero_avg_on, avg_watts_zero_avg_off, max_watts = TCXReader.calculate_averages_and_totals([lap])

@test total_time == 3600.0
@test total_distance == 3000.0
@test max_speed == 3.0
@test max_speed == 2.9
@test total_calories == 500.0
@test avg_hr == 160.0
@test max_hr == 170.0
@test avg_cadence_zero_avg_on 95.0 # Average without considering cadence 0
@test avg_cadence_zero_avg_off 76.0 # Average considering cadence 0
@test avg_speed 2.7 # Average speed
@test total_ascent == 20.0 # Ascent from 100m to 120m
@test total_descent == 0.0 # No descent in this data
@test ascent == 20.0 # Ascent from 100m to 120m
@test descent == 0.0 # No descent in this data
@test max_altitude == 120.0 # Highest altitude
@test avg_watts_zero_avg_on 220.0 # Average watts without considering zeros
@test avg_watts_zero_avg_off 176.0 # Average watts considering zeros
@test max_watts == 240.0 # Maximum watts
@test avg_watts_zero_avg_on 215.0 # Average watts without considering zeros
@test avg_watts_zero_avg_off 172.0 # Average watts considering zeros
@test max_watts == 230.0 # Maximum watts
@test max_cadence == 110 # Maximum cadence
end

@testset "TCXReader Tests -> exportCSV" begin
author = TCXAuthor("Test Author", BuildVersion(1, 0, 0, 0), "en-US", "000-00000-00")

trackPoints = [
TCXTrackPoint(DateTime(2021, 1, 1, 12), 45.0, 13.0, 100.0, 1000.0, 150, 100, 2.5, 200),
TCXTrackPoint(DateTime(2021, 1, 1, 12, 30), 45.1, 13.1, 105.0, 1500.0, 155, 101, 2.6, 201)
]

lap = TCXLap(
DateTime(2021, 1, 1, 12),
totalTimeSeconds = 1800.0,
Expand All @@ -175,12 +177,29 @@ end
triggerMethod = "Manual",
avgSpeed = 2.75
)

activities = [
TCXActivity(
"Biking", DateTime(2021, 1, 1, 12), [lap],
"Biking",
DateTime(2021, 1, 1, 12),
[lap],
DeviceInfo("Garmin Edge 530", "123456789", 1, "1.0"),
1800.0, 5000.0, 3.0, 250.0, 150.0, 160.0, 85.0,
2.75, 0.0, 0.0, 105.0, 150.0, 200.0, 101.0
1800.0, # total_time
5000.0, # total_distance
3.0, # max_speed
250.0, # total_calories
150.0, # avg_hr
160.0, # max_hr
85.0, # avg_cadence_zero_avg_on
75.0, # avg_cadence_zero_avg_off
100, # max_cadence
2.75, # avg_speed
100.0, # total_ascent
50.0, # total_descent
105.0, # max_altitude
150.0, # avg_watts_zero_avg_on
100.0, # avg_watts_zero_avg_off
200.0 # max_watts
)
]

Expand All @@ -192,4 +211,5 @@ end

isfile(csv_filepath) && rm(csv_filepath)
end

end

0 comments on commit 329676a

Please sign in to comment.