mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-10-13 08:46:28 +00:00
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).
42 lines
1.4 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|