Files
ResolveSpec/pkg/resolvespec/null_as_zero_test.go
T
Hein d3252f3d02 feat(restheadspec): match NULL when x-fieldfilter compares a numeric column to 0
Adds a SetNullAsZero handler option: on by default in restheadspec (for
x-fieldfilter only), off by default in resolvespec. When enabled, an eq
filter of 0 on a numeric column renders (col = 0 OR col IS NULL).
2026-10-12 15:49:57 +02:00

42 lines
1.4 KiB
Go

package resolvespec
import (
"testing"
"github.com/bitechdev/ResolveSpec/pkg/common"
)
type nullAsZeroModel struct {
Inactive int `json:"inactive" bun:"inactive"`
Name string `json:"name" bun:"name"`
}
func TestNullAsZero_ResolveSpec(t *testing.T) {
model := nullAsZeroModel{}
tests := []struct {
name string
enabled bool
filter common.FilterOption
want string
}{
{"default off, zero", false, common.FilterOption{Column: "inactive", Operator: "eq", Value: float64(0)}, `"t"."inactive" = ?`},
{"on, zero", true, common.FilterOption{Column: "inactive", Operator: "eq", Value: float64(0)}, `("t"."inactive" = ? OR "t"."inactive" IS NULL)`},
{"on, int zero", true, common.FilterOption{Column: "inactive", Operator: "eq", Value: 0}, `("t"."inactive" = ? OR "t"."inactive" IS NULL)`},
{"on, positive", true, common.FilterOption{Column: "inactive", Operator: "eq", Value: float64(5)}, `"t"."inactive" = ?`},
{"on, string column", true, common.FilterOption{Column: "name", Operator: "eq", Value: float64(0)}, `"t"."name" = ?`},
{"on, neq untouched", true, common.FilterOption{Column: "inactive", Operator: "neq", Value: float64(0)}, `"t"."inactive" != ?`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &Handler{}
if tt.enabled {
h.SetNullAsZero(true)
}
got, _ := h.buildFilterConditionAlias(tt.filter, model, "t")
if got != tt.want {
t.Fatalf("got %q, want %q", got, tt.want)
}
})
}
}