diff --git a/backend/graph/schema.resolvers_test.go b/backend/graph/schema.resolvers_test.go index c4d471b..1ad0c33 100644 --- a/backend/graph/schema.resolvers_test.go +++ b/backend/graph/schema.resolvers_test.go @@ -280,6 +280,9 @@ func TestMutationResolver(t *testing.T) { func TestQueryResolver(t *testing.T) { t.Parallel() t.Run("Cards", func(t *testing.T) { + // Clean up existing cards + db.Where("1 = 1").Delete(&repository.Card{}) + db.Where("1 = 1").Delete(&repository.Cardgroup{}) // Step 1: Create a Cardgroup now := time.Now() @@ -305,32 +308,46 @@ func TestQueryResolver(t *testing.T) { // Prepare GraphQL query jsonInput, _ := json.Marshal(map[string]interface{}{ "query": `{ - cards { - id - front - back - review_date - interval_days - created - updated - } - }`, - }) - expected := fmt.Sprintf(`{ - "data": { - "cards": [{ - "id": %d, - "front": "Cards Front", - "back": "Cards Back", - "interval_days": 1 - }] + cards { + id + front + back + review_date + interval_days + created + updated } - }`, card.ID) + }`, + }) + + // Execute GraphQL query + req := httptest.NewRequest(http.MethodPost, "/query", bytes.NewBuffer(jsonInput)) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() - testGraphQLQuery(t, e, jsonInput, expected, "data.cards.created", "data.cards.updated", "data.cards.review_date") + e.ServeHTTP(rec, req) + + // Check HTTP status code + assert.Equal(t, http.StatusOK, rec.Code) + + // Parse response body + var response map[string]interface{} + if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil { + t.Fatalf("Failed to unmarshal response: %v", err) + } + + // Check number of cards in the response + cards, ok := response["data"].(map[string]interface{})["cards"].([]interface{}) + if !ok { + t.Fatalf("Failed to parse cards from response") + } + assert.Len(t, cards, 1, "Expected number of cards to be 1") }) t.Run("Card", func(t *testing.T) { + // Clean up existing cards + db.Where("1 = 1").Delete(&repository.Card{}) + db.Where("1 = 1").Delete(&repository.Cardgroup{}) // Step 1: Create a Cardgroup now := time.Now()