1package norm
2
3import (
4 "testing"
5)
6
7func TestIdentExpr(t *testing.T) {
8 ident := IdentExpr("username")
9
10 if ident.String() != "username" {
11 t.Errorf("Expected 'username', got '%s'", ident.String())
12 }
13
14 if ident.Binds() != nil {
15 t.Errorf("Expected nil binds, got %v", ident.Binds())
16 }
17
18 expr := ident.AsExpr()
19 if expr.kind != exprKindIdent {
20 t.Errorf("Expected exprKindIdent, got %d", expr.kind)
21 }
22
23 if expr.String() != "username" {
24 t.Errorf("Expected 'username', got '%s'", expr.String())
25 }
26}
27
28func TestValueExpr(t *testing.T) {
29 value := ValueExpr{inner: "test"}
30
31 if value.String() != "?" {
32 t.Errorf("Expected '?', got '%s'", value.String())
33 }
34
35 if value.Binds()[0] != "test" {
36 t.Errorf("Expected %q, got %v", []any{"test"}, value.Binds())
37 }
38
39 expr := value.AsExpr()
40 if expr.kind != exprKindValue {
41 t.Errorf("Expected exprKindValue, got %d", expr.kind)
42 }
43}
44
45func TestBinaryExpressions(t *testing.T) {
46 tests := []struct {
47 name string
48 expr Expr
49 expected string
50 }{
51 {"Eq", Eq("age", 25), "(age) = (?)"},
52 {"Neq", Neq("status", "active"), "(status) <> (?)"},
53 {"Gt", Gt("score", 100), "(score) > (?)"},
54 {"Gte", Gte("rating", 4.5), "(rating) >= (?)"},
55 {"Lt", Lt("count", 10), "(count) < (?)"},
56 {"Lte", Lte("price", 99.99), "(price) <= (?)"},
57 }
58
59 for _, test := range tests {
60 t.Run(test.name, func(t *testing.T) {
61 if test.expr.String() != test.expected {
62 t.Errorf("Expected '%s', got '%s'", test.expected, test.expr.String())
63 }
64
65 if test.expr.kind != exprKindBinary {
66 t.Errorf("Expected exprKindBinary, got %d", test.expr.kind)
67 }
68 })
69 }
70}
71
72func TestLogicalOperators(t *testing.T) {
73 left := Eq("age", 25)
74 right := Eq("status", "active")
75
76 andExpr := left.And(right)
77 expectedAnd := "((age) = (?)) AND ((status) = (?))"
78 if andExpr.String() != expectedAnd {
79 t.Errorf("Expected '%s', got '%s'", expectedAnd, andExpr.String())
80 }
81
82 orExpr := left.Or(right)
83 expectedOr := "((age) = (?)) OR ((status) = (?))"
84 if orExpr.String() != expectedOr {
85 t.Errorf("Expected '%s', got '%s'", expectedOr, orExpr.String())
86 }
87}
88
89func TestComplexExpressions(t *testing.T) {
90 age := Eq("age", 25)
91 status := Eq("status", "active")
92 score := Gt("score", 100)
93
94 complex := age.
95 And(status).
96 Or(score)
97 expected := "(((age) = (?)) AND ((status) = (?))) OR ((score) > (?))"
98
99 if complex.String() != expected {
100 t.Errorf("Expected '%s', got '%s'", expected, complex.String())
101 }
102}
103
104// this just needs to compile
105func TestImplsInterfaces(t *testing.T) {
106 sel := Select()
107 _ = isCompiler(sel) & isBuilder(sel) & isExecer(sel) & isQuerier(sel)
108
109 del := Delete()
110 _ = isCompiler(del) & isBuilder(del) & isExecer(del)
111
112 ins := Insert()
113 _ = isCompiler(ins) & isBuilder(ins) & isExecer(ins)
114}
115
116func isCompiler[S Compiler](S) int { return 0 }
117func isBuilder[S Builder](S) int { return 0 }
118func isExecer[S Execer](S) int { return 0 }
119func isQuerier[S Querier](S) int { return 0 }