Files
Gardomatic/lib/client/resources_test.go
T
kleiax 904d14b64c
CI / test (push) Canceled after 0s
Initial commit
2026-09-12 22:22:17 +02:00

84 lines
3.8 KiB
Go

package client
import (
"context"
"net/http"
"testing"
)
func TestSpeciesAndPlantClientPaths(t *testing.T) {
t.Parallel()
commonName := "Tomate"
plantName := "Tomate am Zaun"
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/species":
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"species":{"id":8,"garden_id":4,"common_name":"Tomate","version":1}}`))
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/species":
_, _ = w.Write([]byte(`{"species":[{"id":8,"garden_id":4,"common_name":"Tomate"}]}`))
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/species/8":
_, _ = w.Write([]byte(`{"species":{"id":8,"garden_id":4,"common_name":"Tomate"}}`))
case r.Method == http.MethodPatch && r.URL.Path == "/v1/gardens/4/species/8":
_, _ = w.Write([]byte(`{"species":{"id":8,"garden_id":4,"common_name":"Tomate","version":2}}`))
case r.Method == http.MethodDelete && r.URL.Path == "/v1/gardens/4/species/8":
w.WriteHeader(http.StatusNoContent)
case r.Method == http.MethodPost && r.URL.Path == "/v1/gardens/4/plants":
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"plant":{"id":9,"garden_id":4,"species_id":8,"name":"Tomate am Zaun","status":"active","version":1}}`))
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/plants":
_, _ = w.Write([]byte(`{"plants":[{"id":9,"garden_id":4,"name":"Tomate am Zaun","status":"active"}]}`))
case r.Method == http.MethodGet && r.URL.Path == "/v1/gardens/4/plants/9":
_, _ = w.Write([]byte(`{"plant":{"id":9,"garden_id":4,"name":"Tomate am Zaun","status":"active"}}`))
case r.Method == http.MethodPatch && r.URL.Path == "/v1/gardens/4/plants/9":
_, _ = w.Write([]byte(`{"plant":{"id":9,"garden_id":4,"name":"Tomate am Zaun","status":"active","version":2}}`))
case r.Method == http.MethodDelete && r.URL.Path == "/v1/gardens/4/plants/9":
w.WriteHeader(http.StatusNoContent)
default:
http.NotFound(w, r)
}
})
apiClient := newTestClient(t, handler)
species, _, err := apiClient.CreateSpecies(context.Background(), 4, SpeciesInput{CommonName: &commonName})
if err != nil || species.ID != 8 {
t.Fatalf("CreateSpecies(): species=%+v err=%v", species, err)
}
allSpecies, _, err := apiClient.SpeciesForGarden(context.Background(), 4)
if err != nil || len(allSpecies) != 1 {
t.Fatalf("SpeciesForGarden(): species=%+v err=%v", allSpecies, err)
}
if _, _, err := apiClient.Species(context.Background(), 4, 8); err != nil {
t.Fatalf("Species(): %v", err)
}
updatedSpecies, _, err := apiClient.UpdateSpecies(context.Background(), 4, 8, SpeciesInput{CommonName: &commonName})
if err != nil || updatedSpecies.Version != 2 {
t.Fatalf("UpdateSpecies(): species=%+v err=%v", updatedSpecies, err)
}
if _, err := apiClient.DeleteSpecies(context.Background(), 4, 8); err != nil {
t.Fatalf("DeleteSpecies(): %v", err)
}
speciesID := 8
plant, _, err := apiClient.CreatePlant(context.Background(), 4, PlantInput{Name: &plantName, SpeciesID: &speciesID})
if err != nil || plant.ID != 9 {
t.Fatalf("CreatePlant(): plant=%+v err=%v", plant, err)
}
plants, _, err := apiClient.Plants(context.Background(), 4)
if err != nil || len(plants) != 1 {
t.Fatalf("Plants(): plants=%+v err=%v", plants, err)
}
if _, _, err := apiClient.Plant(context.Background(), 4, 9); err != nil {
t.Fatalf("Plant(): %v", err)
}
updatedPlant, _, err := apiClient.UpdatePlant(context.Background(), 4, 9, PlantInput{Name: &plantName})
if err != nil || updatedPlant.Version != 2 {
t.Fatalf("UpdatePlant(): plant=%+v err=%v", updatedPlant, err)
}
if _, err := apiClient.DeletePlant(context.Background(), 4, 9); err != nil {
t.Fatalf("DeletePlant(): %v", err)
}
}