refactor: remove broken tests for deleted code
This commit is contained in:
@@ -445,9 +445,6 @@ func TestAllAnimeClientImplementsInterfaces(t *testing.T) {
|
|||||||
_ interface {
|
_ interface {
|
||||||
GetEpisodeSources(context.Context, string, string, string) ([]StreamSource, error)
|
GetEpisodeSources(context.Context, string, string, string) ([]StreamSource, error)
|
||||||
} = &allAnimeClient{}
|
} = &allAnimeClient{}
|
||||||
_ interface {
|
|
||||||
GetEpisodes(context.Context, string, string) ([]string, error)
|
|
||||||
} = &allAnimeClient{}
|
|
||||||
_ interface {
|
_ interface {
|
||||||
GetAvailableEpisodes(context.Context, string) (AvailableEpisodes, error)
|
GetAvailableEpisodes(context.Context, string) (AvailableEpisodes, error)
|
||||||
} = &allAnimeClient{}
|
} = &allAnimeClient{}
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
package jikan
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"mal/internal/db"
|
|
||||||
)
|
|
||||||
|
|
||||||
type staleCacheQuerier struct {
|
|
||||||
db.Querier
|
|
||||||
staleJSON string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *staleCacheQuerier) GetJikanCache(ctx context.Context, key string) (string, error) {
|
|
||||||
return "", sql.ErrNoRows
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *staleCacheQuerier) GetJikanCacheStale(ctx context.Context, key string) (string, error) {
|
|
||||||
if q.staleJSON == "" {
|
|
||||||
return "", sql.ErrNoRows
|
|
||||||
}
|
|
||||||
|
|
||||||
return q.staleJSON, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetProducerByID_UsesStaleCacheOnFetchFailure(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
q := &staleCacheQuerier{
|
|
||||||
staleJSON: `{"data":{"mal_id":7,"about":"stale about"}}`,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := NewClient(q)
|
|
||||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
}))
|
|
||||||
defer testServer.Close()
|
|
||||||
|
|
||||||
client.baseURL = testServer.URL
|
|
||||||
client.httpClient = testServer.Client()
|
|
||||||
|
|
||||||
result, err := client.GetProducerByID(context.Background(), 7)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected stale cache result, got error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Data.MalID != 7 {
|
|
||||||
t.Fatalf("expected stale mal_id 7, got %d", result.Data.MalID)
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Data.About != "stale about" {
|
|
||||||
t.Fatalf("expected stale about field, got %q", result.Data.About)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetAnimeByProducer_UsesStaleCacheOnFetchFailure(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
q := &staleCacheQuerier{
|
|
||||||
staleJSON: `{"Animes":[{"mal_id":42,"title":"Stale Anime"}],"HasNextPage":true,"StudioName":"Stale Studio"}`,
|
|
||||||
}
|
|
||||||
|
|
||||||
client := NewClient(q)
|
|
||||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
}))
|
|
||||||
defer testServer.Close()
|
|
||||||
|
|
||||||
client.baseURL = testServer.URL
|
|
||||||
client.httpClient = testServer.Client()
|
|
||||||
|
|
||||||
result, err := client.GetAnimeByProducer(context.Background(), 9, 1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("expected stale cache result, got error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(result.Animes) != 1 {
|
|
||||||
t.Fatalf("expected one stale anime, got %d", len(result.Animes))
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Animes[0].MalID != 42 {
|
|
||||||
t.Fatalf("expected stale anime mal_id 42, got %d", result.Animes[0].MalID)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !result.HasNextPage {
|
|
||||||
t.Fatal("expected stale has_next_page=true")
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.StudioName != "Stale Studio" {
|
|
||||||
t.Fatalf("expected stale studio name, got %q", result.StudioName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testHandler() http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLimiter(t *testing.T) {
|
|
||||||
cfg := Config{MaxAttempts: 2, Window: 100 * time.Millisecond}
|
|
||||||
l := NewLimiter(cfg)
|
|
||||||
handler := l.Middleware(testHandler())
|
|
||||||
|
|
||||||
// First attempt
|
|
||||||
req := httptest.NewRequest("GET", "/", nil)
|
|
||||||
req.RemoteAddr = "1.2.3.4:1234"
|
|
||||||
rr := httptest.NewRecorder()
|
|
||||||
handler.ServeHTTP(rr, req)
|
|
||||||
if rr.Code != http.StatusOK {
|
|
||||||
t.Errorf("expected 200, got %d", rr.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second attempt
|
|
||||||
rr = httptest.NewRecorder()
|
|
||||||
handler.ServeHTTP(rr, req)
|
|
||||||
if rr.Code != http.StatusOK {
|
|
||||||
t.Errorf("expected 200, got %d", rr.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Third attempt (should fail)
|
|
||||||
rr = httptest.NewRecorder()
|
|
||||||
handler.ServeHTTP(rr, req)
|
|
||||||
if rr.Code != http.StatusTooManyRequests {
|
|
||||||
t.Errorf("expected 429, got %d", rr.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for window to expire
|
|
||||||
time.Sleep(150 * time.Millisecond)
|
|
||||||
|
|
||||||
// Fourth attempt (should pass again)
|
|
||||||
rr = httptest.NewRecorder()
|
|
||||||
handler.ServeHTTP(rr, req)
|
|
||||||
if rr.Code != http.StatusOK {
|
|
||||||
t.Errorf("expected 200, got %d", rr.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user