Skip to content

Commit 63bd440

Browse files
updating tests
1 parent 85b5d9c commit 63bd440

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

card_data/pipelines/defs/extract/testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import polars as pl
33
import responses
4-
from extract_data import extract_series_data
4+
from .extract_data import extract_series_data
55

66
@pytest.fixture
77
def mock_api_response():

cmd/card/cardlist.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ type cardData struct {
101101
// CardsList creates and returns a new CardsModel with cards from a specific set
102102
func CardsList(setID string) (CardsModel, error) {
103103
url := fmt.Sprintf("https://uoddayfnfkebrijlpfbh.supabase.co/rest/v1/card_pricing_view?set_id=eq.%s&select=number_plus_name,market_price,image_url,illustrator&order=localId", setID)
104-
body, err := CallCardData(url)
104+
body, err := getCardData(url)
105105
if err != nil {
106106
return CardsModel{}, fmt.Errorf("failed to fetch card data: %w", err)
107107
}
@@ -159,6 +159,9 @@ func CardsList(setID string) (CardsModel, error) {
159159
}, nil
160160
}
161161

162+
// creating a function variable to swap the implementation in tests
163+
var getCardData = CallCardData
164+
162165
func CallCardData(url string) ([]byte, error) {
163166
req, err := http.NewRequest("GET", url, nil)
164167
if err != nil {

cmd/card/cardlist_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package card
22

33
import (
4+
"errors"
45
"strings"
56
"testing"
67

@@ -254,3 +255,113 @@ func TestCardsModel_View_MissingPrice(t *testing.T) {
254255
t.Error("View() should display 'Price: Not available' for cards without pricing")
255256
}
256257
}
258+
259+
func TestCardsList_SuccessAndFallbacks(t *testing.T) {
260+
// Save and restore getCardData stub
261+
original := getCardData
262+
defer func() { getCardData = original }()
263+
264+
var capturedURL string
265+
getCardData = func(url string) ([]byte, error) {
266+
capturedURL = url
267+
// Return two cards: one with price + illustrator, one with fallbacks
268+
json := `[
269+
{"number_plus_name":"001/198 - Bulbasaur","market_price":1.5,"image_url":"https://example.com/bulba.png","illustrator":"Ken Sugimori"},
270+
{"number_plus_name":"002/198 - Ivysaur","market_price":0,"image_url":"https://example.com/ivy.png","illustrator":""}
271+
]`
272+
return []byte(json), nil
273+
}
274+
275+
model, err := CardsList("set123")
276+
if err != nil {
277+
t.Fatalf("CardsList returned error: %v", err)
278+
}
279+
280+
// URL should target the correct set id and select fields
281+
if !strings.Contains(capturedURL, "set_id=eq.set123") {
282+
t.Errorf("expected URL to contain set_id filter, got %s", capturedURL)
283+
}
284+
if !strings.Contains(capturedURL, "select=number_plus_name,market_price,image_url,illustrator") {
285+
t.Errorf("expected URL to contain select fields, got %s", capturedURL)
286+
}
287+
288+
// PriceMap expectations
289+
if got := model.PriceMap["001/198 - Bulbasaur"]; got != "Price: $1.50" {
290+
t.Errorf("unexpected price for Bulbasaur: %s", got)
291+
}
292+
if got := model.PriceMap["002/198 - Ivysaur"]; got != "Pricing not available" {
293+
t.Errorf("unexpected price for Ivysaur: %s", got)
294+
}
295+
296+
// IllustratorMap expectations
297+
if got := model.IllustratorMap["001/198 - Bulbasaur"]; got != "Illustrator: Ken Sugimori" {
298+
t.Errorf("unexpected illustrator for Bulbasaur: %s", got)
299+
}
300+
if got := model.IllustratorMap["002/198 - Ivysaur"]; got != "Illustrator not available" {
301+
t.Errorf("unexpected illustrator for Ivysaur: %s", got)
302+
}
303+
304+
// Image map
305+
if model.ImageMap["001/198 - Bulbasaur"] != "https://example.com/bulba.png" {
306+
t.Errorf("unexpected image url for Bulbasaur: %s", model.ImageMap["001/198 - Bulbasaur"])
307+
}
308+
if model.ImageMap["002/198 - Ivysaur"] != "https://example.com/ivy.png" {
309+
t.Errorf("unexpected image url for Ivysaur: %s", model.ImageMap["002/198 - Ivysaur"])
310+
}
311+
312+
if row := model.Table.SelectedRow(); len(row) == 0 {
313+
if model.View() == "" {
314+
t.Error("model view should render even if no row is selected")
315+
}
316+
}
317+
}
318+
319+
func TestCardsList_FetchError(t *testing.T) {
320+
original := getCardData
321+
defer func() { getCardData = original }()
322+
323+
getCardData = func(url string) ([]byte, error) {
324+
return nil, errors.New("network error")
325+
}
326+
327+
_, err := CardsList("set123")
328+
if err == nil {
329+
t.Fatal("expected error when fetch fails")
330+
}
331+
}
332+
333+
func TestCardsList_BadJSON(t *testing.T) {
334+
original := getCardData
335+
defer func() { getCardData = original }()
336+
337+
getCardData = func(url string) ([]byte, error) {
338+
return []byte("not-json"), nil
339+
}
340+
341+
_, err := CardsList("set123")
342+
if err == nil {
343+
t.Fatal("expected error for bad JSON payload")
344+
}
345+
}
346+
347+
func TestCardsList_EmptyResult(t *testing.T) {
348+
original := getCardData
349+
defer func() { getCardData = original }()
350+
351+
getCardData = func(url string) ([]byte, error) {
352+
return []byte("[]"), nil
353+
}
354+
355+
model, err := CardsList("set123")
356+
if err != nil {
357+
t.Fatalf("unexpected error: %v", err)
358+
}
359+
360+
if len(model.PriceMap) != 0 || len(model.IllustratorMap) != 0 || len(model.ImageMap) != 0 {
361+
t.Errorf("expected empty maps, got price:%d illus:%d image:%d", len(model.PriceMap), len(model.IllustratorMap), len(model.ImageMap))
362+
}
363+
364+
if model.View() == "" {
365+
t.Error("expected view to render with empty data")
366+
}
367+
}

cmd/card/serieslist_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,26 @@ func TestSeriesModelView(t *testing.T) {
142142
t.Errorf("Expected non-empty view for choice, got empty string")
143143
}
144144
}
145+
146+
func TestSeriesList(t *testing.T) {
147+
model := SeriesList()
148+
items := model.List.Items()
149+
150+
// Check that list has 3 items
151+
if items == nil {
152+
t.Error("SeriesList() should create a list with items")
153+
}
154+
155+
if len(items) != 3 {
156+
t.Errorf("Expected 3 items, got %d", len(items))
157+
}
158+
159+
// Verify all three series are present
160+
expectedSeries := []string{"Mega Evolution", "Scarlet & Violet", "Sword & Shield"}
161+
for i, expected := range expectedSeries {
162+
itemStr := string(items[i].(item))
163+
if itemStr != expected {
164+
t.Errorf("Expected item %d to be '%s', got '%s'", i, expected, itemStr)
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)