-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from CS3219-AY2425S1/question-tests-create-upd…
…ate-delete Additional Integration tests for question service
- Loading branch information
Showing
7 changed files
with
358 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"question-service/handlers" | ||
"question-service/utils" | ||
"testing" | ||
|
||
"cloud.google.com/go/firestore" | ||
) | ||
|
||
var service *handlers.Service | ||
var ctx = context.Background() | ||
|
||
func TestMain(m *testing.M) { | ||
// Set FIRESTORE_EMULATOR_HOST environment variable. | ||
err := os.Setenv("FIRESTORE_EMULATOR_HOST", "127.0.0.1:8080") | ||
if err != nil { | ||
log.Fatalf("could not set env %v", err) | ||
} | ||
// Create client. | ||
client, err := firestore.NewClient(ctx, "my-project-id") | ||
service = &handlers.Service{Client: client} | ||
|
||
if err != nil { | ||
log.Fatalf("could not create client %v", err) | ||
} | ||
defer client.Close() | ||
|
||
m.Run() | ||
os.Exit(0) | ||
} | ||
|
||
// Sets up the firestore emulator with the sample questions | ||
// This repopulates the db | ||
// Returns the docref of one of the questions if a test need it | ||
func setupDb(t *testing.T) string { | ||
// Repopulate document | ||
utils.Populate(service.Client, false) | ||
|
||
coll := service.Client.Collection("questions") | ||
if coll == nil { | ||
t.Fatalf("Failed to get CollectionRef") | ||
} | ||
docRef, err := coll.DocumentRefs(ctx).Next() | ||
if err != nil { | ||
t.Fatalf("Failed to get DocRef: %v", err) | ||
} | ||
return docRef.ID | ||
} | ||
|
||
func getCount(t *testing.T) int64 { | ||
counterDocRef, err := service.Client.Collection("counters").Doc("questions").Get(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
fields := counterDocRef.Data() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
count := fields["count"].(int64) | ||
return count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"question-service/models" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// tests partially generated using Github Copilot | ||
|
||
func createCreateRequestWithData(_ *testing.T, body []byte) *http.Request { | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost:12345/questions", bytes.NewBuffer(body)) | ||
|
||
return req | ||
} | ||
|
||
func TestCreateQuestion(t *testing.T) { | ||
t.Run("Create new question", func(t *testing.T) { | ||
var err error | ||
|
||
newQuestion := models.Question{ | ||
Title: "New Question", | ||
Description: "New Description", | ||
Complexity: models.Medium, | ||
Categories: []string{"Category1"}, | ||
|
||
DocRefID: "a-doc-ref-id", | ||
} | ||
|
||
setupDb(t) | ||
beforeCount := getCount(t) | ||
|
||
w := httptest.NewRecorder() | ||
data, err := json.Marshal(newQuestion) | ||
assert.NoError(t, err) | ||
req := createCreateRequestWithData(t, data) | ||
service.CreateQuestion(w, req) | ||
afterCount := getCount(t) | ||
// Check response | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
var response models.Question | ||
err = json.NewDecoder(w.Body).Decode(&response) | ||
assert.NoError(t, err) | ||
assert.Equal(t, newQuestion.Title, response.Title) | ||
assert.Equal(t, newQuestion.Description, response.Description) | ||
assert.Equal(t, newQuestion.Complexity, response.Complexity) | ||
assert.Equal(t, newQuestion.Categories, response.Categories) | ||
assert.Equal(t, beforeCount+1, afterCount) | ||
}) | ||
|
||
t.Run("Create question with missing title", func(t *testing.T) { | ||
newQuestion := models.Question{ | ||
Description: "New Description", | ||
Complexity: models.Medium, | ||
Categories: []string{"Category1"}, | ||
} | ||
|
||
setupDb(t) | ||
beforeCount := getCount(t) | ||
|
||
w := httptest.NewRecorder() | ||
data, _ := json.Marshal(newQuestion) | ||
req := createCreateRequestWithData(t, data) | ||
service.CreateQuestion(w, req) | ||
|
||
// Check response | ||
assert.Equal(t, http.StatusBadRequest, w.Code) | ||
assert.Contains(t, w.Body.String(), "Title is required") | ||
assert.Equal(t, beforeCount, getCount(t)) | ||
}) | ||
|
||
t.Run("Create question with duplicate title", func(t *testing.T) { | ||
newQuestion := models.Question{ | ||
Title: "Duplicate Title", | ||
Description: "New Description", | ||
Complexity: models.Medium, | ||
Categories: []string{"Category1"}, | ||
} | ||
|
||
setupDb(t) | ||
|
||
// Create the first question | ||
w := httptest.NewRecorder() | ||
data, _ := json.Marshal(newQuestion) | ||
req := createCreateRequestWithData(t, data) | ||
service.CreateQuestion(w, req) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
|
||
// Try to create the second question with the same title | ||
w = httptest.NewRecorder() | ||
req = createCreateRequestWithData(t, data) | ||
service.CreateQuestion(w, req) | ||
|
||
// Check response | ||
assert.Equal(t, http.StatusBadRequest, w.Code) | ||
assert.Contains(t, w.Body.String(), "Question title already exists") | ||
}) | ||
|
||
t.Run("Create question with empty description", func(t *testing.T) { | ||
newQuestion := models.Question{ | ||
Title: "New Question", | ||
Description: "", | ||
Complexity: models.Medium, | ||
Categories: []string{"Category1"}, | ||
} | ||
|
||
setupDb(t) | ||
|
||
w := httptest.NewRecorder() | ||
data, _ := json.Marshal(newQuestion) | ||
req := createCreateRequestWithData(t, data) | ||
service.CreateQuestion(w, req) | ||
|
||
// Check response | ||
assert.Equal(t, http.StatusBadRequest, w.Code) | ||
assert.Contains(t, w.Body.String(), "Description is required") | ||
}) | ||
|
||
t.Run("Create question with nil title", func(t *testing.T) { | ||
newQuestion := models.Question{ | ||
// Title: "New Question", | ||
Description: "New Description", | ||
Complexity: models.Medium, | ||
Categories: []string{"Category1"}, | ||
} | ||
|
||
setupDb(t) | ||
|
||
w := httptest.NewRecorder() | ||
data, _ := json.Marshal(newQuestion) | ||
req := createCreateRequestWithData(t, data) | ||
service.CreateQuestion(w, req) | ||
|
||
// Check response | ||
assert.Equal(t, http.StatusBadRequest, w.Code) | ||
assert.Contains(t, w.Body.String(), "Title is required") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// tests partially generated using Github Copilot | ||
|
||
func createDeleteRequestWithId(docRefID string) *http.Request { | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add("docRefID", docRefID) | ||
|
||
req := httptest.NewRequest(http.MethodDelete, "/questions/"+docRefID, nil) | ||
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) | ||
return req | ||
} | ||
|
||
func TestDeleteQuestion(t *testing.T) { | ||
|
||
t.Run("Delete existing question", func(t *testing.T) { | ||
docRefID := setupDb(t) | ||
req := createDeleteRequestWithId(docRefID) | ||
res := httptest.NewRecorder() | ||
|
||
service.DeleteQuestion(res, req) | ||
|
||
assert.Equal(t, http.StatusOK, res.Code) | ||
assert.Equal(t, res.Body.String(), "Question with ID "+docRefID+" deleted successfully") | ||
}) | ||
|
||
t.Run("Delete non-existing question", func(t *testing.T) { | ||
nonExistentDocRefID := "non-existent-id" | ||
req := createDeleteRequestWithId(nonExistentDocRefID) | ||
res := httptest.NewRecorder() | ||
|
||
service.DeleteQuestion(res, req) | ||
|
||
assert.Equal(t, http.StatusNotFound, res.Code) | ||
assert.Equal(t, res.Body.String(), "Question not found\n") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.