Skip to content

Commit

Permalink
test: update test to remove global variables and add TestGetReceiptPo…
Browse files Browse the repository at this point in the history
…ints
  • Loading branch information
kweeuhree committed Jan 8, 2025
1 parent 8007335 commit ed2542d
Showing 1 changed file with 11 additions and 62 deletions.
73 changes: 11 additions & 62 deletions cmd/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ func setupTestDependencies() *TestDependencies {
}
}

// Writes an OK status to the response
func mockHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusOK)
}

func TestProcessReceipt(t *testing.T) {
deps := setupTestDependencies()
d := setupTestDependencies()
tests := []struct {
name string
input ReceiptInput
Expand Down Expand Up @@ -66,7 +71,7 @@ func TestProcessReceipt(t *testing.T) {
resp := httptest.NewRecorder()

// Test ProcessReceipt function
deps.handlers.ProcessReceipt(resp, req)
d.handlers.ProcessReceipt(resp, req)

// Check the response status
if status := resp.Code; status != entry.expectedStatus {
Expand Down Expand Up @@ -106,6 +111,7 @@ func TestProcessReceipt(t *testing.T) {
}

func TestGetReceiptPoints(t *testing.T) {
d := setupTestDependencies()
router := httprouter.New()
// Register the route
router.GET("/receipts/:id/points", mockHandler)
Expand All @@ -123,64 +129,7 @@ func TestGetReceiptPoints(t *testing.T) {

for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
receiptStore.Insert(*SimpleReceipt)

// Create request and response
req := httptest.NewRequest(http.MethodGet, entry.url, nil)
// Create request context that will enable id extraction
params := httprouter.Params{
httprouter.Param{Key: "id", Value: entry.ID},
}
ctx := context.WithValue(req.Context(), httprouter.ParamsKey, params)
req = req.WithContext(ctx)
// Create response
resp := httptest.NewRecorder()

// Serve the request
router.ServeHTTP(resp, req)

// Test GetReceiptPoints function
handlers.GetReceiptPoints(resp, req)

// Check response status
if resp.Code == http.StatusOK && entry.status == http.StatusOK {
var response PointsResponse
// Decode the request body
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
t.Fatalf("Failed to decode response: %v", err)
}
// Check if the points match
if response.Points != SimpleReceipt.Points {
t.Fatalf("Expected %d points, received %d", SimpleReceipt.Points, response.Points)
}
}

t.Cleanup(func() {
receiptStore = models.NewStore()
})
})
}
}

func TestGetReceiptPoints(t *testing.T) {
router := httprouter.New()
// Register the route
router.GET("/receipts/:id/points", mockHandler)

tests := []struct {
name string
ID string
url string
status int
}{
{"Valid receipt id", SimpleReceipt.ID, "/receipts/123-qwe-456-rty-7890/points", http.StatusOK},
{"Invalid receipt id", "hello-world", "/receipts/hello-world/points", http.StatusNotFound},
{"Invalid request url", SimpleReceipt.ID, "/hello/123-qwe-456-rty-7890/world", http.StatusNotFound},
}

for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
receiptStore.Insert(*SimpleReceipt)
d.receiptStore.Insert(*SimpleReceipt)

// Create request and response
req := httptest.NewRequest(http.MethodGet, entry.url, nil)
Expand All @@ -197,7 +146,7 @@ func TestGetReceiptPoints(t *testing.T) {
router.ServeHTTP(resp, req)

// Test GetReceiptPoints function
handlers.GetReceiptPoints(resp, req)
d.handlers.GetReceiptPoints(resp, req)

// Check response status
if resp.Code == http.StatusOK && entry.status == http.StatusOK {
Expand All @@ -213,7 +162,7 @@ func TestGetReceiptPoints(t *testing.T) {
}

t.Cleanup(func() {
receiptStore = models.NewStore()
d.receiptStore = models.NewStore()
})
})
}
Expand Down

0 comments on commit ed2542d

Please sign in to comment.