Files
2026-12-01 21:25:01 +01:00

93 lines
4.2 KiB
Go

package dbcopy
import (
"testing"
"time"
"github.com/phishingclub/phishingclub/database/dbengine"
)
// TestConvert checks each conversion rule, on both engines (pure code)
func TestConvert(t *testing.T) {
notNull := func(k kind) *column { return &column{name: "c", kind: k} }
nullable := func(k kind) *column { return &column{name: "c", kind: k, nullable: true} }
utc := func(s string) time.Time {
v, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
t.Fatal(err)
}
return v.UTC()
}
cases := []struct {
name string
col *column
in any
want any
invalid invalidKind
cleaned bool
}{
{"null nullable", nullable(kindText), nil, nil, "", false},
{"null not null", notNull(kindText), nil, nil, invalidNull, false},
{"uuid", notNull(kindUUID), "0b1e6f43-5a8e-4f4c-9f3e-2a3b4c5d6e7f", "0b1e6f43-5a8e-4f4c-9f3e-2a3b4c5d6e7f", "", false},
{"uuid upper", notNull(kindUUID), "0B1E6F43-5A8E-4F4C-9F3E-2A3B4C5D6E7F", nil, invalidUUID, false},
{"uuid braces", notNull(kindUUID), "{0b1e6f43-5a8e-4f4c-9f3e-2a3b4c5d6e7f}", nil, invalidUUID, false},
{"uuid empty", notNull(kindUUID), "", nil, invalidUUID, false},
{"uuid blob", notNull(kindUUID), []byte("x"), nil, invalidUUID, false},
{"time Z", notNull(kindTime), "2025-01-02T03:04:05.123456789Z", utc("2025-01-02T03:04:05.123456789Z"), "", false},
{"time offset", notNull(kindTime), "2025-01-02 03:04:05.5+02:00", utc("2025-01-02T01:04:05.5Z"), "", false},
{"time no offset", notNull(kindTime), "2025-01-02 03:04:05", utc("2025-01-02T03:04:05Z"), "", false},
{"time date", notNull(kindTime), "2025-01-02", utc("2025-01-02T00:00:00Z"), "", false},
{"time seconds", notNull(kindTime), int64(1735787045), utc("2025-01-02T03:04:05Z"), "", false},
{"time millis", notNull(kindTime), int64(1735787045123), utc("2025-01-02T03:04:05.123Z"), "", false},
{"time text bad", notNull(kindTime), "yesterday", nil, invalidTime, false},
{"time real", notNull(kindTime), 1.5, nil, invalidTime, false},
{"bool 1", notNull(kindBool), int64(1), true, "", false},
{"bool 0", notNull(kindBool), int64(0), false, "", false},
{"bool 2", notNull(kindBool), int64(2), nil, invalidBool, false},
{"bool 2 boolean column", &column{kind: kindBool, srcType: "boolean"}, int64(2), true, "", false},
{"bool text", notNull(kindBool), "true", true, "", false},
{"int", &column{kind: kindInt, intBits: 32}, int64(7), int64(7), "", false},
{"int text", &column{kind: kindInt, intBits: 64}, "42", int64(42), "", false},
{"int real", &column{kind: kindInt, intBits: 64}, 3.0, int64(3), "", false},
{"int fraction", &column{kind: kindInt, intBits: 64}, 3.5, nil, invalidInt, false},
{"int4 range", &column{kind: kindInt, intBits: 32}, int64(1) << 31, nil, invalidRange, false},
{"int2 range", &column{kind: kindInt, intBits: 16}, int64(-32769), nil, invalidRange, false},
{"text", notNull(kindText), "plain", "plain", "", false},
{"text blob", notNull(kindText), []byte("blob"), "blob", "", false},
{"text int", notNull(kindText), int64(12), "12", "", false},
{"text real", notNull(kindText), 1.25, "1.25", "", false},
{"text NUL", notNull(kindText), "a\x00b", dbengine.CleanText("a\x00b"), "", true},
{"text latin1", notNull(kindText), "caf\xe9", dbengine.CleanText("caf\xe9"), "", true},
{"varchar at limit", &column{kind: kindText, maxLen: 3}, "ééé", "ééé", "", false},
{"varchar over", &column{kind: kindText, maxLen: 3}, "éééé", nil, invalidTooLong, false},
{"float", notNull(kindFloat), int64(2), 2.0, "", false},
{"float text", notNull(kindFloat), "x", nil, invalidFloat, false},
}
for _, tc := range cases {
got := convert(tc.col, tc.in)
if got.invalid != tc.invalid {
t.Errorf("%s: invalid %q, want %q", tc.name, got.invalid, tc.invalid)
continue
}
if tc.invalid != "" {
continue
}
if got.cleaned != tc.cleaned {
t.Errorf("%s: cleaned %v", tc.name, got.cleaned)
}
if w, ok := tc.want.(time.Time); ok {
g, ok := got.value.(time.Time)
if !ok || !g.Equal(w) || g.Location() != time.UTC {
t.Errorf("%s: %v, want %v", tc.name, got.value, w)
}
continue
}
if got.value != tc.want {
t.Errorf("%s: %#v, want %#v", tc.name, got.value, tc.want)
}
if tc.cleaned && got.raw != tc.in {
t.Errorf("%s: raw %q", tc.name, got.raw)
}
}
}