This commit is contained in:
+27
-1
@@ -25,6 +25,14 @@ type speciesInput struct {
|
||||
WinterProtection *string `json:"winter_protection"`
|
||||
SpacingCM *int `json:"spacing_cm"`
|
||||
HeightCM *int `json:"height_cm"`
|
||||
HeightCMFrom *int `json:"height_cm_from"`
|
||||
HeightCMTo *int `json:"height_cm_to"`
|
||||
WidthCMFrom *int `json:"width_cm_from"`
|
||||
WidthCMTo *int `json:"width_cm_to"`
|
||||
ClearHeightCMFrom bool `json:"clear_height_cm_from"`
|
||||
ClearHeightCMTo bool `json:"clear_height_cm_to"`
|
||||
ClearWidthCMFrom bool `json:"clear_width_cm_from"`
|
||||
ClearWidthCMTo bool `json:"clear_width_cm_to"`
|
||||
SowMonthFrom *int `json:"sow_month_from"`
|
||||
SowDayFrom *int `json:"sow_day_from"`
|
||||
ClearSowDayFrom bool `json:"clear_sow_day_from"`
|
||||
@@ -70,7 +78,19 @@ func (input speciesInput) apply(species *storage.Species) {
|
||||
assignStringPointer(input.SoilReaction, &species.SoilReaction)
|
||||
assignStringPointer(input.WinterProtection, &species.WinterProtection)
|
||||
assignIntPointer(input.SpacingCM, &species.SpacingCM)
|
||||
assignIntPointer(input.HeightCM, &species.HeightCM)
|
||||
assignIntPointer(input.HeightCMFrom, &species.HeightCMFrom)
|
||||
if input.HeightCMTo != nil {
|
||||
assignIntPointer(input.HeightCMTo, &species.HeightCMTo)
|
||||
} else {
|
||||
assignIntPointer(input.HeightCM, &species.HeightCMTo)
|
||||
}
|
||||
assignIntPointer(input.WidthCMFrom, &species.WidthCMFrom)
|
||||
assignIntPointer(input.WidthCMTo, &species.WidthCMTo)
|
||||
clearIntPointer(input.ClearHeightCMFrom, &species.HeightCMFrom)
|
||||
clearIntPointer(input.ClearHeightCMTo, &species.HeightCMTo)
|
||||
clearIntPointer(input.ClearWidthCMFrom, &species.WidthCMFrom)
|
||||
clearIntPointer(input.ClearWidthCMTo, &species.WidthCMTo)
|
||||
species.HeightCM = species.HeightCMTo
|
||||
assignIntPointer(input.SowMonthFrom, &species.SowMonthFrom)
|
||||
assignIntPointer(input.SowDayFrom, &species.SowDayFrom)
|
||||
assignIntPointer(input.SowMonthTo, &species.SowMonthTo)
|
||||
@@ -138,6 +158,12 @@ func assignIntPointer(input *int, destination **int) {
|
||||
}
|
||||
}
|
||||
|
||||
func clearIntPointer(clear bool, destination **int) {
|
||||
if clear {
|
||||
*destination = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) createSpeciesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
gardenID, _ := app.readGardenIDParam(r)
|
||||
var input speciesInput
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gardomatic.kleiax.de/internal/storage"
|
||||
)
|
||||
|
||||
func TestSpeciesInputAppliesAndClearsDimensions(t *testing.T) {
|
||||
heightFrom, heightTo, widthFrom, widthTo := 20, 80, 30, 60
|
||||
species := storage.Species{
|
||||
HeightCMFrom: &heightFrom,
|
||||
HeightCMTo: &heightTo,
|
||||
WidthCMFrom: &widthFrom,
|
||||
WidthCMTo: &widthTo,
|
||||
}
|
||||
newHeightFrom, newWidthTo := 40, 70
|
||||
|
||||
speciesInput{
|
||||
HeightCMFrom: &newHeightFrom,
|
||||
ClearHeightCMTo: true,
|
||||
ClearWidthCMFrom: true,
|
||||
WidthCMTo: &newWidthTo,
|
||||
}.apply(&species)
|
||||
|
||||
if species.HeightCMFrom == nil || *species.HeightCMFrom != 40 {
|
||||
t.Fatalf("height from = %v, want 40", species.HeightCMFrom)
|
||||
}
|
||||
if species.HeightCMTo != nil {
|
||||
t.Fatalf("height to = %v, want nil", species.HeightCMTo)
|
||||
}
|
||||
if species.WidthCMFrom != nil {
|
||||
t.Fatalf("width from = %v, want nil", species.WidthCMFrom)
|
||||
}
|
||||
if species.WidthCMTo == nil || *species.WidthCMTo != 70 {
|
||||
t.Fatalf("width to = %v, want 70", species.WidthCMTo)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpeciesInputAcceptsDeprecatedHeight(t *testing.T) {
|
||||
height := 90
|
||||
var species storage.Species
|
||||
|
||||
speciesInput{HeightCM: &height}.apply(&species)
|
||||
|
||||
if species.HeightCMTo == nil || *species.HeightCMTo != height {
|
||||
t.Fatalf("height to = %v, want %d", species.HeightCMTo, height)
|
||||
}
|
||||
if species.HeightCM == nil || *species.HeightCM != height {
|
||||
t.Fatalf("deprecated height alias = %v, want %d", species.HeightCM, height)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user