mirror of
https://github.com/sstent/go-garth.git
synced 2026-01-26 17:11:42 +00:00
feat(refactor): Implement 1A.1 Package Structure Refactoring
This commit implements the package structure refactoring as outlined in phase1.md (Task 1A.1). Key changes include: - Reorganized packages into `pkg/garmin` for public API and `internal/` for internal implementations. - Updated all import paths to reflect the new structure. - Consolidated types and client logic into their respective new packages. - Updated `cmd/garth/main.go` to use the new public API. - Fixed various compilation and test issues encountered during the refactoring process. - Converted `internal/api/client/auth_test.go` to a functional test. This establishes a solid foundation for future enhancements and improves maintainability.
This commit is contained in:
74
internal/data/base_test.go
Normal file
74
internal/data/base_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"garmin-connect/internal/api/client"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// MockData implements Data interface for testing
|
||||
type MockData struct {
|
||||
BaseData
|
||||
}
|
||||
|
||||
// MockClient simulates API client for tests
|
||||
type MockClient struct{}
|
||||
|
||||
func (mc *MockClient) Get(endpoint string) (interface{}, error) {
|
||||
if endpoint == "error" {
|
||||
return nil, errors.New("mock API error")
|
||||
}
|
||||
return "data for " + endpoint, nil
|
||||
}
|
||||
|
||||
func TestBaseData_List(t *testing.T) {
|
||||
// Setup mock data type
|
||||
mockData := &MockData{}
|
||||
mockData.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
|
||||
return "data for " + day.Format("2006-01-02"), nil
|
||||
}
|
||||
|
||||
// Test parameters
|
||||
end := time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)
|
||||
days := 5
|
||||
c := &client.Client{}
|
||||
maxWorkers := 3
|
||||
|
||||
// Execute
|
||||
results, errs := mockData.List(end, days, c, maxWorkers)
|
||||
|
||||
// Verify
|
||||
assert.Empty(t, errs)
|
||||
assert.Len(t, results, days)
|
||||
assert.Contains(t, results, "data for 2023-06-15")
|
||||
assert.Contains(t, results, "data for 2023-06-11")
|
||||
}
|
||||
|
||||
func TestBaseData_List_ErrorHandling(t *testing.T) {
|
||||
// Setup mock data type that returns error on specific date
|
||||
mockData := &MockData{}
|
||||
mockData.GetFunc = func(day time.Time, c *client.Client) (interface{}, error) {
|
||||
if day.Day() == 13 {
|
||||
return nil, errors.New("bad luck day")
|
||||
}
|
||||
return "data for " + day.Format("2006-01-02"), nil
|
||||
}
|
||||
|
||||
// Test parameters
|
||||
end := time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC)
|
||||
days := 5
|
||||
c := &client.Client{}
|
||||
maxWorkers := 2
|
||||
|
||||
// Execute
|
||||
results, errs := mockData.List(end, days, c, maxWorkers)
|
||||
|
||||
// Verify
|
||||
assert.Len(t, errs, 1)
|
||||
assert.Equal(t, "bad luck day", errs[0].Error())
|
||||
assert.Len(t, results, 4) // Should have results for non-error days
|
||||
}
|
||||
Reference in New Issue
Block a user