80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package rest
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestQuoteName(t *testing.T) {
|
|
q := &Query{}
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{"Empty string", "", ""},
|
|
{"Control chars", "a\x00b\nc", "`abc`"}, // \x00 and \n should be filtered
|
|
{"Spaces", " test name ", "`testname`"}, // Spaces should be filtered
|
|
{"Properly quoted", "`valid`", "`valid`"}, // Already quoted
|
|
{"Left quote only", "`invalid", "``invalid`"}, // Add missing right quote
|
|
{"Normal unquoted", "normal", "`normal`"}, // Add quotes
|
|
{"All filtered", "\t\r\n", "``"}, // Filter all characters
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := q.quoteName(tt.input)
|
|
if got != tt.expected {
|
|
t.Errorf("quoteName(%q) = %q, want %q", tt.input, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQuery_quoteValue(t *testing.T) {
|
|
type testCase struct {
|
|
name string
|
|
input any
|
|
expected string
|
|
}
|
|
|
|
tests := []testCase{
|
|
// Boolean values
|
|
{"bool true", true, "1"},
|
|
{"bool false", false, "0"},
|
|
{"bool pointer", new(bool), "0"}, // *bool with zero value
|
|
|
|
// Integer family
|
|
{"int", 42, "42"},
|
|
{"int8", int8(127), "127"},
|
|
{"uint", uint(100), "100"},
|
|
{"uint64", uint64(1<<64 - 1), "18446744073709551615"},
|
|
|
|
// Floating points
|
|
{"float64", 3.14, "3.14"},
|
|
{"float64 scientific", 1e10, "10000000000"},
|
|
{"float32", float32(1.5), "1.5"},
|
|
|
|
// Strings
|
|
{"simple string", "hello", `"hello"`},
|
|
{"string with quotes", `"quoted"`, `"\"quoted\""`},
|
|
{"string with newline", "line\nbreak", `"line\nbreak"`},
|
|
|
|
// Default cases
|
|
{"struct", struct{}{}, "{}"},
|
|
{"slice", []int{1, 2}, "[1 2]"},
|
|
{"nil", nil, "IS NULL"},
|
|
}
|
|
|
|
q := &Query{}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := q.quoteValue(tt.input)
|
|
if got != tt.expected {
|
|
t.Errorf("quoteValue(%v) = %v, want %v", tt.input, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|