better api paths
This commit is contained in:
@@ -25,9 +25,9 @@ func newBudgetServer(t *testing.T) *httptest.Server {
|
||||
h := handler.NewBudgetHandler(service.NewBudgetService(repo))
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("POST /api/v1/budgets", h.Create)
|
||||
mux.HandleFunc("PUT /api/v1/budgets/{id}", h.Update)
|
||||
mux.HandleFunc("DELETE /api/v1/budgets/{id}", h.Delete)
|
||||
mux.HandleFunc("POST /api/v1/budget/create", h.Create)
|
||||
mux.HandleFunc("PUT /api/v1/budgets/update", h.Update)
|
||||
mux.HandleFunc("DELETE /api/v1/budgets/delete", h.Delete)
|
||||
|
||||
srv := httptest.NewServer(mux)
|
||||
t.Cleanup(srv.Close)
|
||||
@@ -108,40 +108,76 @@ func TestUpdateBudget_OK(t *testing.T) {
|
||||
srv := newBudgetServer(t)
|
||||
client := srv.Client()
|
||||
|
||||
// Create first
|
||||
resp, err := client.Post(srv.URL+"/api/v1/budgets", "application/json",
|
||||
mustJSON(t, validBudget()))
|
||||
// Create a budget to update
|
||||
resp, err := client.Post(
|
||||
srv.URL+"/api/v1/budget/create",
|
||||
"application/json",
|
||||
mustJSON(t, validBudget()),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var created model.Budget
|
||||
json.NewDecoder(resp.Body).Decode(&created)
|
||||
resp.Body.Close()
|
||||
if err := json.NewDecoder(resp.Body).Decode(&created); err != nil {
|
||||
t.Fatalf("decode created budget: %v", err)
|
||||
}
|
||||
if created.ID == 0 {
|
||||
t.Fatal("expected a non-zero ID from create")
|
||||
}
|
||||
|
||||
// Update amount
|
||||
updated := validBudget()
|
||||
updated["amount"] = 9999.99
|
||||
// Update only the fields the endpoint is meant to change
|
||||
wantAmount := 9999.99
|
||||
wantNotes := "updated in test"
|
||||
updateReq := model.UpdateBudgetRequest{
|
||||
ID: created.ID,
|
||||
Amount: &wantAmount,
|
||||
Notes: &wantNotes,
|
||||
ChangedBy: created.CreatedBy,
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPut,
|
||||
fmt.Sprintf("%s/api/v1/budgets/%d", srv.URL, created.ID),
|
||||
mustJSON(t, updated))
|
||||
req, err := http.NewRequest(
|
||||
http.MethodPut,
|
||||
srv.URL+"/api/v1/budget/update",
|
||||
mustJSON(t, updateReq),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp2, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
|
||||
if resp2.StatusCode != http.StatusOK && resp2.StatusCode != http.StatusNoContent {
|
||||
t.Errorf("update: got %d, want 200 or 204", resp2.StatusCode)
|
||||
if resp2.StatusCode != http.StatusOK {
|
||||
t.Fatalf("update: got %d, want 200", resp2.StatusCode)
|
||||
}
|
||||
|
||||
if resp2.StatusCode == http.StatusOK {
|
||||
var got model.Budget
|
||||
json.NewDecoder(resp2.Body).Decode(&got)
|
||||
if got.Amount != 9999.99 {
|
||||
t.Errorf("updated amount: got %v, want 9999.99", got.Amount)
|
||||
}
|
||||
var got model.Budget
|
||||
if err := json.NewDecoder(resp2.Body).Decode(&got); err != nil {
|
||||
t.Fatalf("decode updated budget: %v", err)
|
||||
}
|
||||
|
||||
if got.Amount != wantAmount {
|
||||
t.Errorf("amount: got %v, want %v", got.Amount, wantAmount)
|
||||
}
|
||||
if got.Notes != wantNotes {
|
||||
t.Errorf("notes: got %q, want %q", got.Notes, wantNotes)
|
||||
}
|
||||
if got.ID != created.ID {
|
||||
t.Errorf("ID changed after update: got %v, want %v", got.ID, created.ID)
|
||||
}
|
||||
|
||||
// Fields not included in the update should be unchanged
|
||||
if got.FiscalYear != created.FiscalYear {
|
||||
t.Errorf("fiscal_year changed unexpectedly: got %v, want %v", got.FiscalYear, created.FiscalYear)
|
||||
}
|
||||
if got.DepartmentID != created.DepartmentID {
|
||||
t.Errorf("department_id changed unexpectedly: got %v, want %v", got.DepartmentID, created.DepartmentID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user