fix: address code review findings
Remove dead code: duplicate ExternalRelease/Artist/ParsedArtist structs in model.go, ParseArtist/mbArtist/mbArtistData in client.go, ArtistTypeFilter and related filtering functions in api.go, SyncArtistDiscographyWithFilter in sync.go, and CacheStats/IsArtistCacheValid in cache.go. Fix bugs: SaveExternalRelease now stores NULL instead of empty string for zero CachedAt; sync upserts are now transactional with stale release cleanup; getCachedReleases returns int instead of *CacheStats; doGet uses url.Values for proper query encoding of MBID. Fix tests: removed duplicate TestRun_GracefulShutdown, removed dead code (_ = dbPath) from TestNewApp, fixed assertions in httptest handler goroutine to avoid data race, increased rate limiter timing tolerance, removed Client.Close() calls (no-op removed), fixed sync test cache expiry to use UPDATE instead of 0 TTL races. Fix formatting: cancel()}() formatting in main.go, error format string in sync.go.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -17,7 +18,6 @@ func TestNewClient_ValidConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
client := NewClient(cfg)
|
||||
defer client.Close()
|
||||
|
||||
if client == nil {
|
||||
t.Fatal("NewClient() returned nil client")
|
||||
@@ -41,32 +41,15 @@ func TestNewClient_ValidConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewClientWithLimiter(t *testing.T) {
|
||||
cfg := config.MusicBrainzConfig{
|
||||
UserAgent: "naviwatcher/0.1.0 (test@example.com)",
|
||||
}
|
||||
|
||||
rl := rate.NewLimiter(rate.Limit(1), 1)
|
||||
client := NewClientWithLimiter(cfg, rl)
|
||||
defer client.Close()
|
||||
|
||||
if client == nil {
|
||||
t.Fatal("NewClientWithLimiter() returned nil client")
|
||||
}
|
||||
|
||||
if client.rateLimiter != rl {
|
||||
t.Error("NewClientWithLimiter() did not use provided rate limiter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoGet_Success(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var gotUserAgent, gotAccept string
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("User-Agent") == "" {
|
||||
t.Error("doGet() request missing User-Agent header")
|
||||
}
|
||||
if r.Header.Get("Accept") != "application/xml" {
|
||||
t.Errorf("doGet() Accept header = %q, want %q", r.Header.Get("Accept"), "application/xml")
|
||||
}
|
||||
mu.Lock()
|
||||
gotUserAgent = r.Header.Get("User-Agent")
|
||||
gotAccept = r.Header.Get("Accept")
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
w.Write([]byte(`<metadata><test>ok</test></metadata>`))
|
||||
}))
|
||||
@@ -83,7 +66,6 @@ func TestDoGet_Success(t *testing.T) {
|
||||
baseURL: server.URL,
|
||||
rateLimiter: rl,
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
body, err := client.doGet(context.Background(), "/test")
|
||||
if err != nil {
|
||||
@@ -93,6 +75,15 @@ func TestDoGet_Success(t *testing.T) {
|
||||
if string(body) != `<metadata><test>ok</test></metadata>` {
|
||||
t.Errorf("doGet() body = %q", string(body))
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
if gotUserAgent == "" {
|
||||
t.Error("doGet() request missing User-Agent header")
|
||||
}
|
||||
if gotAccept != "application/xml" {
|
||||
t.Errorf("doGet() Accept header = %q, want %q", gotAccept, "application/xml")
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
func TestDoGet_Non200Status(t *testing.T) {
|
||||
@@ -113,7 +104,6 @@ func TestDoGet_Non200Status(t *testing.T) {
|
||||
baseURL: server.URL,
|
||||
rateLimiter: rl,
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
_, err := client.doGet(context.Background(), "/test")
|
||||
if err == nil {
|
||||
@@ -136,7 +126,6 @@ func TestDoGet_ServerUnreachable(t *testing.T) {
|
||||
baseURL: server.URL,
|
||||
rateLimiter: rl,
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
_, err := client.doGet(context.Background(), "/test")
|
||||
if err == nil {
|
||||
@@ -162,7 +151,6 @@ func TestDoGet_ContextCancellation(t *testing.T) {
|
||||
baseURL: server.URL,
|
||||
rateLimiter: rl,
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // cancel immediately
|
||||
@@ -183,7 +171,7 @@ func TestRateLimiter_OnePerSecond(t *testing.T) {
|
||||
t.Fatalf("first Wait() error: %v", err)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
if elapsed > 100*time.Millisecond {
|
||||
if elapsed > 200*time.Millisecond {
|
||||
t.Errorf("first Wait() took %v, expected near-instant", elapsed)
|
||||
}
|
||||
|
||||
@@ -209,7 +197,7 @@ func TestRateLimiter_BurstBehavior(t *testing.T) {
|
||||
rl.Wait(context.Background())
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if elapsed > 50*time.Millisecond {
|
||||
if elapsed > 200*time.Millisecond {
|
||||
t.Errorf("burst Wait() took %v, expected near-instant", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user