mirror of
https://github.com/sstent/go-garminconnect.git
synced 2026-01-25 16:42:32 +00:00
garth more done
This commit is contained in:
@@ -293,4 +293,4 @@ func (c *Client) DownloadActivity(ctx context.Context, activityID int64) ([]byte
|
||||
}
|
||||
|
||||
return resp.Body(), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ func TestActivitiesEndpoints(t *testing.T) {
|
||||
client := NewClientWithBaseURL(mockServer.URL())
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func()
|
||||
testFunc func(t *testing.T)
|
||||
name string
|
||||
setup func()
|
||||
testFunc func(t *testing.T)
|
||||
}{
|
||||
{
|
||||
name: "GetActivitiesSuccess",
|
||||
@@ -28,17 +28,17 @@ func TestActivitiesEndpoints(t *testing.T) {
|
||||
mockServer.SetActivitiesHandler(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Create a properly formatted time string for Garmin format
|
||||
timeStr := time.Now().Add(-24 * time.Hour).Format("2006-01-02T15:04:05")
|
||||
|
||||
|
||||
// Create response with raw JSON to avoid time marshaling issues
|
||||
response := map[string]interface{}{
|
||||
"activities": []map[string]interface{}{
|
||||
{
|
||||
"activityId": 1,
|
||||
"activityName": "Morning Run",
|
||||
"activityType": "RUNNING",
|
||||
"startTimeLocal": timeStr,
|
||||
"duration": 3600.0,
|
||||
"distance": 10.0,
|
||||
"activityId": 1,
|
||||
"activityName": "Morning Run",
|
||||
"activityType": "RUNNING",
|
||||
"startTimeLocal": timeStr,
|
||||
"duration": 3600.0,
|
||||
"distance": 10.0,
|
||||
},
|
||||
},
|
||||
"pagination": map[string]interface{}{
|
||||
@@ -47,7 +47,7 @@ func TestActivitiesEndpoints(t *testing.T) {
|
||||
"totalCount": 1,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
@@ -81,18 +81,18 @@ func TestActivitiesEndpoints(t *testing.T) {
|
||||
|
||||
// Use proper time format for Garmin API
|
||||
timeStr := time.Now().Add(-24 * time.Hour).Format("2006-01-02T15:04:05")
|
||||
|
||||
|
||||
response := map[string]interface{}{
|
||||
"activityId": activityID,
|
||||
"activityName": "Mock Activity",
|
||||
"activityType": "RUNNING",
|
||||
"startTimeLocal": timeStr,
|
||||
"duration": 3600.0,
|
||||
"distance": 10.0,
|
||||
"calories": 500.0,
|
||||
"averageHR": 150,
|
||||
"maxHR": 170,
|
||||
"elevationGain": 100.0,
|
||||
"activityId": activityID,
|
||||
"activityName": "Mock Activity",
|
||||
"activityType": "RUNNING",
|
||||
"startTimeLocal": timeStr,
|
||||
"duration": 3600.0,
|
||||
"distance": 10.0,
|
||||
"calories": 500.0,
|
||||
"averageHR": 150,
|
||||
"maxHR": 170,
|
||||
"elevationGain": 100.0,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -156,7 +156,7 @@ func TestActivitiesEndpoints(t *testing.T) {
|
||||
fitData := make([]byte, 20)
|
||||
fitData[0] = 14 // header size
|
||||
copy(fitData[8:12], []byte(".FIT"))
|
||||
|
||||
|
||||
id, err := client.UploadActivity(context.Background(), fitData)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(12345), id)
|
||||
@@ -187,4 +187,4 @@ func TestActivitiesEndpoints(t *testing.T) {
|
||||
tt.testFunc(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestGetBodyComposition(t *testing.T) {
|
||||
// Check for required parameters without enforcing order
|
||||
startDate := r.URL.Query().Get("startDate")
|
||||
endDate := r.URL.Query().Get("endDate")
|
||||
|
||||
|
||||
assert.Equal(t, "2023-01-01", startDate, "startDate should match")
|
||||
assert.Equal(t, "2023-01-31", endDate, "endDate should match")
|
||||
|
||||
@@ -115,4 +115,4 @@ func TestGetBodyComposition(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestGearService(t *testing.T) {
|
||||
// Create test server
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/gear-service/stats/valid-uuid":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -129,4 +129,4 @@ func TestGearService(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "failed to get gear activities")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@ func (e *APIError) Error() string {
|
||||
|
||||
// Error types for API responses
|
||||
type ErrNotFound struct{}
|
||||
|
||||
func (e ErrNotFound) Error() string { return "resource not found" }
|
||||
|
||||
type ErrBadRequest struct{}
|
||||
|
||||
func (e ErrBadRequest) Error() string { return "bad request" }
|
||||
|
||||
// Time represents a Garmin Connect time value
|
||||
@@ -47,7 +49,7 @@ func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
if err := json.Unmarshal(data, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// Try multiple time formats that Garmin might use
|
||||
formats := []string{
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
@@ -57,14 +59,14 @@ func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
}
|
||||
|
||||
|
||||
for _, format := range formats {
|
||||
if parsedTime, err := time.Parse(format, s); err == nil {
|
||||
*t = Time(parsedTime)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If none of the formats work, try parsing as RFC3339
|
||||
parsedTime, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user