Files
Gardomatic/internal/storage/species_test.go
T
2026-09-16 20:21:41 +02:00

40 lines
1.4 KiB
Go

package storage
import (
"testing"
"gardomatic.kleiax.de/internal/platform/validate"
)
func TestValidateSpeciesDimensions(t *testing.T) {
tests := []struct {
name string
species Species
wantField string
}{
{name: "valid ranges", species: Species{CommonName: "Rose", HeightCMFrom: intPointer(40), HeightCMTo: intPointer(80), WidthCMFrom: intPointer(30), WidthCMTo: intPointer(60)}},
{name: "height starts above end", species: Species{CommonName: "Rose", HeightCMFrom: intPointer(81), HeightCMTo: intPointer(80)}, wantField: "height_cm_from"},
{name: "width starts above end", species: Species{CommonName: "Rose", WidthCMFrom: intPointer(61), WidthCMTo: intPointer(60)}, wantField: "width_cm_from"},
{name: "non-positive height", species: Species{CommonName: "Rose", HeightCMTo: intPointer(0)}, wantField: "height_cm_to"},
{name: "deprecated height alias", species: Species{CommonName: "Rose", HeightCM: intPointer(80)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := validate.New()
ValidateSpecies(v, tt.species)
_, hasError := v.Errors[tt.wantField]
if tt.wantField == "" && !v.Valid() {
t.Fatalf("unexpected validation errors: %v", v.Errors)
}
if tt.wantField != "" && !hasError {
t.Fatalf("expected validation error for %s, got %v", tt.wantField, v.Errors)
}
})
}
}
func intPointer(value int) *int {
return &value
}