49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/kratisto/tcgdex-golang/models"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetCardByID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
cardID string
|
|
wantErr error
|
|
wantCard *models.Card
|
|
}{
|
|
{"valid card", "xy1-1", nil, &models.Card{ID: "xy1-1"}},
|
|
{"invalid card", "invalid-id", ErrNotFound, nil},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Setup fake server
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/cards/xy1-1" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(tt.wantCard)
|
|
return
|
|
}
|
|
http.Error(w, "", http.StatusNotFound)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClientWithCustomHTTPClient(server.URL, "en", server.Client())
|
|
card, err := client.GetCard(t.Context(), tt.cardID)
|
|
|
|
require.ErrorIs(t, err, tt.wantErr)
|
|
require.Equal(t, tt.wantCard, card, "expected card: %v, got: %v for cardID %v", tt.wantCard, card, tt.cardID)
|
|
})
|
|
}
|
|
}
|