- F396: Track listening heatmap (segment-level aggregated data) - F397: Period comparison (week/month/quarter with % changes) - F398: Marketplace analytics (product views, conversion rates, revenue) - F399: Metric alerts (opt-in thresholds, preferences, CRUD) - Unit tests for service (percent change calculations) and handler (auth, validation) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package services
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestCalcPercentChange(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
previous int64
|
|
current int64
|
|
expected float64
|
|
}{
|
|
{"zero to positive", 0, 100, 100.0},
|
|
{"zero to zero", 0, 0, 0.0},
|
|
{"positive to zero", 100, 0, -100.0},
|
|
{"same value", 100, 100, 0.0},
|
|
{"50% increase", 100, 150, 50.0},
|
|
{"50% decrease", 200, 100, -50.0},
|
|
{"double", 50, 100, 100.0},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := calcPercentChange(tt.previous, tt.current)
|
|
if result != tt.expected {
|
|
t.Errorf("calcPercentChange(%d, %d) = %f, want %f", tt.previous, tt.current, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCalcFloatPercentChange(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
previous float64
|
|
current float64
|
|
expected float64
|
|
}{
|
|
{"zero to positive", 0.0, 50.0, 100.0},
|
|
{"zero to zero", 0.0, 0.0, 0.0},
|
|
{"50% increase", 80.0, 120.0, 50.0},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := calcFloatPercentChange(tt.previous, tt.current)
|
|
if result != tt.expected {
|
|
t.Errorf("calcFloatPercentChange(%f, %f) = %f, want %f", tt.previous, tt.current, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewAdvancedAnalyticsService(t *testing.T) {
|
|
// Test nil logger fallback
|
|
svc := NewAdvancedAnalyticsService(nil, nil)
|
|
if svc == nil {
|
|
t.Fatal("expected non-nil service")
|
|
}
|
|
if svc.logger == nil {
|
|
t.Fatal("expected non-nil logger (nop logger)")
|
|
}
|
|
}
|