feat: add missing endpoitn implementation
This commit is contained in:
@@ -1,27 +1,47 @@
|
||||
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 bool
|
||||
wantCard *Card
|
||||
wantErr error
|
||||
wantCard *models.Card
|
||||
}{
|
||||
{"valid card", "xy1-1", false, &Card{ID: "xy1-1"}},
|
||||
{"invalid card", "invalid-id", true, nil},
|
||||
{"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) {
|
||||
card, err := GetCardByID(tt.cardID)
|
||||
t.Parallel()
|
||||
|
||||
require.ErrorIs(t, err, ErrNotFound)
|
||||
// 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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user