package qso

import (
	"strings"
	"testing"
)

// "equals nothing" and "is empty" are the same question. SQL answers the first
// with nothing at all — a NULL never equals ” — so a filter written in plain
// words returned zero rows and looked broken rather than wrong.
func TestEqualsBlankMeansEmpty(t *testing.T) {
	sql, args, err := conditionSQL(Condition{Field: "freq_hz", Op: "eq", Value: ""})
	if err != nil {
		t.Fatalf("eq blank: %v", err)
	}
	if len(args) != 0 || !strings.Contains(sql, "IS NULL") {
		t.Errorf("sql = %q args = %v — want the empty test", sql, args)
	}
	sql, _, _ = conditionSQL(Condition{Field: "name", Op: "ne", Value: "  "})
	if !strings.Contains(sql, "<> ''") {
		t.Errorf("ne blank on text gave %q — want the not-empty test", sql)
	}
}

// A numeric column is empty when NULL *or* zero, and that must be explicit:
// SQLite compares 0 against ” as false while MySQL calls it true, so one
// expression would answer two different questions depending on the backend.
func TestEmptyOnNumericCoversZeroAndNull(t *testing.T) {
	sql, _, err := conditionSQL(Condition{Field: "freq_hz", Op: "empty"})
	if err != nil {
		t.Fatalf("empty: %v", err)
	}
	if !strings.Contains(sql, "IS NULL") || !strings.Contains(sql, "= 0") {
		t.Errorf("sql = %q — want both NULL and zero", sql)
	}
	// Text keeps the string test: '' is a real value there, 0 is not.
	sql, _, _ = conditionSQL(Condition{Field: "name", Op: "empty"})
	if !strings.Contains(sql, "IFNULL") || strings.Contains(sql, "= 0") {
		t.Errorf("text empty gave %q", sql)
	}
}
