Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 14, 2024
1 parent 9845132 commit 911f13c
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 69 deletions.
20 changes: 12 additions & 8 deletions backend/graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 128 additions & 34 deletions backend/graph/schema.resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -73,23 +74,69 @@ func setupEchoServer(db *gorm.DB) *echo.Echo {
return e
}

func testGraphQLQuery(t *testing.T, e *echo.Echo, jsonInput []byte, expected string) {
func testGraphQLQuery(t *testing.T, e *echo.Echo, jsonInput []byte, expected string, ignoreFields ...string) {
req := httptest.NewRequest(http.MethodPost, "/query", bytes.NewBuffer(jsonInput))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()

e.ServeHTTP(rec, req)

assert.JSONEq(t, expected, rec.Body.String())
var actualResponse map[string]interface{}
var expectedResponse map[string]interface{}

// Parse the actual response
if err := json.Unmarshal(rec.Body.Bytes(), &actualResponse); err != nil {
t.Fatalf("Failed to unmarshal actual response: %v", err)
}

// Parse the expected response
if err := json.Unmarshal([]byte(expected), &expectedResponse); err != nil {
t.Fatalf("Failed to unmarshal expected response: %v", err)
}

// Remove the fields to ignore from both responses
for _, field := range ignoreFields {
removeField(actualResponse, field)
removeField(expectedResponse, field)
}

// Compare the modified responses
assert.Equal(t, expectedResponse, actualResponse)
}

func removeField(data map[string]interface{}, field string) {
// Split the field into parts if it's nested (e.g., data.createCard.created)
parts := strings.Split(field, ".")
current := data

for i, part := range parts {
if i == len(parts)-1 {
delete(current, part)
} else if next, ok := current[part].(map[string]interface{}); ok {
current = next
} else {
break
}
}
}

func TestMutationResolver_CreateCard(t *testing.T) {
// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a new card with the CardgroupID
input := model.NewCard{
Front: "Front of card",
Back: "Back of card",
ReviewDate: time.Now(),
ReviewDate: now,
IntervalDays: new(int),
CardgroupID: 1,
CardgroupID: cardgroup.ID,
}
jsonInput, _ := json.Marshal(map[string]interface{}{
"query": `mutation ($input: NewCard!) {
Expand All @@ -107,25 +154,40 @@ func TestMutationResolver_CreateCard(t *testing.T) {
"input": input,
},
})
expected := `{
expected := fmt.Sprintf(`{
"data": {
"createCard": {
"front": "Front of card",
"back": "Back of card"
"back": "Back of card",
"review_date": "%s",
"interval_days": 1,
"created": "%s",
"updated": "%s",
"id": 1
}
}
}`
testGraphQLQuery(t, e, jsonInput, expected)
}`, cardgroup.Created.Format(time.RFC3339Nano), cardgroup.Created.Format(time.RFC3339Nano), cardgroup.Updated.Format(time.RFC3339Nano))

testGraphQLQuery(t, e, jsonInput, expected, "data.createCard.created", "data.createCard.updated")
}

func TestMutationResolver_UpdateCard(t *testing.T) {
// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Create a card to update
card := repository.Card{
Front: "Old Front",
Back: "Old Back",
ReviewDate: time.Now(),
IntervalDays: 1,
CardGroupID: 1,
CardGroupID: cardgroup.ID,
Created: time.Now(),
Updated: time.Now(),
}
Expand All @@ -137,7 +199,7 @@ func TestMutationResolver_UpdateCard(t *testing.T) {
ReviewDate: time.Now(),
}
jsonInput, _ := json.Marshal(map[string]interface{}{
"query": `mutation ($id: Int!, $input: NewCard!) {
"query": `mutation ($id: ID!, $input: NewCard!) {
updateCard(id: $id, input: $input) {
id
front
Expand All @@ -162,24 +224,33 @@ func TestMutationResolver_UpdateCard(t *testing.T) {
}
}
}`
testGraphQLQuery(t, e, jsonInput, expected)
testGraphQLQuery(t, e, jsonInput, expected, "data.updateCard.created", "data.updateCard.updated", "data.updateCard.review_date", "data.updateCard.interval_days")
}

func TestMutationResolver_DeleteCard(t *testing.T) {
// Create a card to delete
// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a card to delete
card := repository.Card{
Front: "Front to delete",
Back: "Back to delete",
ReviewDate: time.Now(),
IntervalDays: 1,
CardGroupID: 1,
CardGroupID: cardgroup.ID, // Ensure this matches the created cardgroup
Created: time.Now(),
Updated: time.Now(),
}
db.Create(&card)

jsonInput, _ := json.Marshal(map[string]interface{}{
"query": `mutation ($id: Int!) {
"query": `mutation ($id: ID!) {
deleteCard(id: $id)
}`,
"variables": map[string]interface{}{
Expand All @@ -195,17 +266,28 @@ func TestMutationResolver_DeleteCard(t *testing.T) {
}

func TestQueryResolver_Cards(t *testing.T) {
// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a Card associated with the created Cardgroup
card := repository.Card{
Front: "Front",
Back: "Back",
ReviewDate: time.Now(),
ReviewDate: now,
IntervalDays: 1,
CardGroupID: 1,
Created: time.Now(),
Updated: time.Now(),
CardGroupID: cardgroup.ID, // Ensure this matches the created Cardgroup ID
Created: now,
Updated: now,
}
db.Create(&card)

// Prepare GraphQL query
jsonInput, _ := json.Marshal(map[string]interface{}{
"query": `{
cards {
Expand All @@ -219,32 +301,45 @@ func TestQueryResolver_Cards(t *testing.T) {
}
}`,
})
expected := `{
expected := fmt.Sprintf(`{
"data": {
"cards": [{
"id": ` + fmt.Sprintf("%d", card.ID) + `,
"id": %d,
"front": "Front",
"back": "Back"
"back": "Back",
"interval_days": 1,
}]
}
}`
testGraphQLQuery(t, e, jsonInput, expected)
}`, card.ID)

testGraphQLQuery(t, e, jsonInput, expected, "data.cards.created", "data.cards.updated", "data.cards.review_date")
}

func TestQueryResolver_Card(t *testing.T) {
// Step 1: Create a Cardgroup
now := time.Now()
cardgroup := repository.Cardgroup{
Name: "Test Cardgroup",
Created: now,
Updated: now,
}
db.Create(&cardgroup)

// Step 2: Create a Card associated with the created Cardgroup
card := repository.Card{
Front: "Front",
Back: "Back",
ReviewDate: time.Now(),
ReviewDate: now,
IntervalDays: 1,
CardGroupID: 1,
Created: time.Now(),
Updated: time.Now(),
CardGroupID: cardgroup.ID, // Ensure this matches the created Cardgroup ID
Created: now,
Updated: now,
}
db.Create(&card)

// Prepare GraphQL query
jsonInput, _ := json.Marshal(map[string]interface{}{
"query": `query ($id: Int!) {
"query": `query ($id: ID!) {
card(id: $id) {
id
front
Expand All @@ -259,19 +354,18 @@ func TestQueryResolver_Card(t *testing.T) {
"id": card.ID,
},
})
expected := `{
expected := fmt.Sprintf(`{
"data": {
"card": {
"id": ` + fmt.Sprintf("%d", card.ID) + `,
"id": %d,
"front": "Front",
"back": "Back"
}
}
}`
testGraphQLQuery(t, e, jsonInput, expected)
}
}`, card.ID)

// Add similar tests for the remaining resolvers
testGraphQLQuery(t, e, jsonInput, expected, "data.card.created", "data.card.updated", "data.card.review_date", "data.card.interval_days")
}

func getDB() *gorm.DB {
return db
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/flashcard/flashcard_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func CreateCardReview(card *model.Card, db *gorm.DB) error {
// MigrateDB performs the database migration for the Card struct
func MigrateDB(db *gorm.DB) error {
return db.AutoMigrate(
&model.CardGroup{},
&model.Cardgroup{},
&model.Card{},
&model.User{},
&model.Role{},
Expand Down
Loading

0 comments on commit 911f13c

Please sign in to comment.