Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 14, 2024
1 parent 4157046 commit 6da1254
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions backend/graph/schema.resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 6da1254

Please sign in to comment.