76 lines
3.6 KiB
Go
76 lines
3.6 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestLocationAndAssignmentClientPaths(t *testing.T) {
|
|
t.Parallel()
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
switch {
|
|
case r.Method == http.MethodPost && r.URL.Path == "/v1/gardens/4/locations":
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"location":{"id":8,"garden_id":4,"name":"Beet","version":1}}`))
|
|
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/locations":
|
|
_, _ = w.Write([]byte(`{"locations":[{"id":8,"garden_id":4,"name":"Beet"}]}`))
|
|
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/locations/8":
|
|
_, _ = w.Write([]byte(`{"location":{"id":8,"garden_id":4,"name":"Beet"}}`))
|
|
case r.Method == http.MethodPatch && r.URL.Path == "/v1/gardens/4/locations/8":
|
|
_, _ = w.Write([]byte(`{"location":{"id":8,"garden_id":4,"name":"Beet","version":2}}`))
|
|
case r.Method == http.MethodDelete && r.URL.Path == "/v1/gardens/4/locations/8":
|
|
w.WriteHeader(http.StatusNoContent)
|
|
case r.Method == http.MethodPost && r.URL.Path == "/v1/gardens/4/plants/9/locations":
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"plant_location":{"id":10,"plant_id":9,"location_id":8,"quantity":2,"version":1}}`))
|
|
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/plants/9/locations":
|
|
_, _ = w.Write([]byte(`{"plant_locations":[{"id":10,"plant_id":9,"location_id":8,"quantity":2}]}`))
|
|
case r.Method == http.MethodPatch && r.URL.Path == "/v1/gardens/4/plants/9/locations/10":
|
|
_, _ = w.Write([]byte(`{"plant_location":{"id":10,"plant_id":9,"location_id":8,"quantity":3,"version":2}}`))
|
|
case r.Method == http.MethodDelete && r.URL.Path == "/v1/gardens/4/plants/9/locations/10":
|
|
w.WriteHeader(http.StatusNoContent)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
apiClient := newTestClient(t, handler)
|
|
name := "Beet"
|
|
location, _, err := apiClient.CreateLocation(context.Background(), 4, LocationInput{Name: &name})
|
|
if err != nil || location.ID != 8 {
|
|
t.Fatalf("CreateLocation(): location=%+v err=%v", location, err)
|
|
}
|
|
locations, _, err := apiClient.Locations(context.Background(), 4)
|
|
if err != nil || len(locations) != 1 {
|
|
t.Fatalf("Locations(): locations=%+v err=%v", locations, err)
|
|
}
|
|
if _, _, err = apiClient.Location(context.Background(), 4, 8); err != nil {
|
|
t.Fatalf("Location(): %v", err)
|
|
}
|
|
updated, _, err := apiClient.UpdateLocation(context.Background(), 4, 8, LocationInput{Name: &name})
|
|
if err != nil || updated.Version != 2 {
|
|
t.Fatalf("UpdateLocation(): location=%+v err=%v", updated, err)
|
|
}
|
|
if _, err = apiClient.DeleteLocation(context.Background(), 4, 8); err != nil {
|
|
t.Fatalf("DeleteLocation(): %v", err)
|
|
}
|
|
locationID, quantity := 8, 2
|
|
assignment, _, err := apiClient.CreatePlantLocation(context.Background(), 4, 9, PlantLocationInput{LocationID: &locationID, Quantity: &quantity})
|
|
if err != nil || assignment.ID != 10 {
|
|
t.Fatalf("CreatePlantLocation(): assignment=%+v err=%v", assignment, err)
|
|
}
|
|
assignments, _, err := apiClient.PlantLocations(context.Background(), 4, 9)
|
|
if err != nil || len(assignments) != 1 {
|
|
t.Fatalf("PlantLocations(): assignments=%+v err=%v", assignments, err)
|
|
}
|
|
quantity = 3
|
|
assignment, _, err = apiClient.UpdatePlantLocation(context.Background(), 4, 9, 10, PlantLocationInput{Quantity: &quantity})
|
|
if err != nil || assignment.Version != 2 {
|
|
t.Fatalf("UpdatePlantLocation(): assignment=%+v err=%v", assignment, err)
|
|
}
|
|
if _, err = apiClient.DeletePlantLocation(context.Background(), 4, 9, 10); err != nil {
|
|
t.Fatalf("DeletePlantLocation(): %v", err)
|
|
}
|
|
}
|