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) } }) } }