Skip to content

Commit

Permalink
Fixed broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfarrelldev committed Nov 22, 2024
1 parent ec3d57a commit c853a8e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
19 changes: 7 additions & 12 deletions internal/account/account_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package account

import (
"bytes"
"database/sql"
"encoding/json"
"net/http"
Expand Down Expand Up @@ -34,9 +33,7 @@ func TestGetAccount_Success(t *testing.T) {
WithArgs(accountID).
WillReturnRows(rows)

args := GetAccountArgs{AccountId: accountID}
body, _ := json.Marshal(args)
req, err := http.NewRequest("GET", "/account/get_account", bytes.NewBuffer(body))
req, err := http.NewRequest("GET", "/account/get_account?account_id=1", nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -79,9 +76,7 @@ func TestGetAccount_NotFound(t *testing.T) {
WithArgs(accountID).
WillReturnError(sql.ErrNoRows)

args := GetAccountArgs{AccountId: accountID}
body, _ := json.Marshal(args)
req, err := http.NewRequest("GET", "/account/get_account", bytes.NewBuffer(body))
req, err := http.NewRequest("GET", "/account/get_account?account_id=1", nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -113,7 +108,7 @@ func TestGetAccount_InvalidMethod(t *testing.T) {
}
defer db.Close()

req, err := http.NewRequest("POST", "/account/get_account", nil)
req, err := http.NewRequest("POST", "/account/get_account?account_id=1", nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -145,7 +140,7 @@ func TestGetAccount_DecodeError(t *testing.T) {
}
defer db.Close()

req, err := http.NewRequest("GET", "/account/get_account", bytes.NewBuffer([]byte("invalid json")))
req, err := http.NewRequest("GET", "/account/get_account?account_id=invalid", nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -164,8 +159,8 @@ func TestGetAccount_DecodeError(t *testing.T) {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError)
}

expectedError := "an error occurred while decoding the request body:invalid character 'i' looking for beginning of value"
if strings.TrimSpace(rr.Body.String()) != expectedError {
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError)
expectedError := "invalid account_id"
if strings.ReplaceAll(strings.TrimSpace(rr.Body.String()), "\n", "") != expectedError {
t.Errorf("handler returned unexpected body: got %v want %v", strings.ReplaceAll(strings.TrimSpace(rr.Body.String()), "\n", ""), expectedError)
}
}
2 changes: 0 additions & 2 deletions internal/account/get_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ func GetAccount(w http.ResponseWriter, r *http.Request, db *sql.DB) error {
queryParams := r.URL.Query()
accountIdStr := queryParams.Get("account_id")
if accountIdStr == "" {
http.Error(w, "account_id is required", http.StatusBadRequest)
return errors.New("account_id is required")
}

accountId, err := strconv.ParseInt(accountIdStr, 10, 64)
if err != nil {
http.Error(w, "invalid account_id", http.StatusBadRequest)
return errors.New("invalid account_id")
}

Expand Down

0 comments on commit c853a8e

Please sign in to comment.