MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestSelectStatementSQL

Function TestSelectStatementSQL

pkg/sql/ast/sql_test.go:21–112  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestSelectStatementSQL(t *testing.T) {
22 tests := []struct {
23 name string
24 stmt *SelectStatement
25 want string
26 }{
27 {
28 name: "simple select",
29 stmt: &SelectStatement{
30 Columns: []Expression{&Identifier{Name: "*"}},
31 From: []TableReference{{Name: "users"}},
32 },
33 want: "SELECT * FROM users",
34 },
35 {
36 name: "select with where",
37 stmt: &SelectStatement{
38 Columns: []Expression{&Identifier{Name: "id"}, &Identifier{Name: "name"}},
39 From: []TableReference{{Name: "users"}},
40 Where: &BinaryExpression{
41 Left: &Identifier{Name: "active"}, Operator: "=",
42 Right: &LiteralValue{Value: true, Type: "BOOLEAN"},
43 },
44 },
45 want: "SELECT id, name FROM users WHERE active = TRUE",
46 },
47 {
48 name: "select distinct",
49 stmt: &SelectStatement{
50 Distinct: true,
51 Columns: []Expression{&Identifier{Name: "status"}},
52 From: []TableReference{{Name: "orders"}},
53 },
54 want: "SELECT DISTINCT status FROM orders",
55 },
56 {
57 name: "select with alias",
58 stmt: &SelectStatement{
59 Columns: []Expression{
60 &AliasedExpression{
61 Expr: &FunctionCall{Name: "COUNT", Arguments: []Expression{&Identifier{Name: "*"}}},
62 Alias: "total",
63 },
64 },
65 From: []TableReference{{Name: "users"}},
66 },
67 want: "SELECT COUNT(*) AS total FROM users",
68 },
69 {
70 name: "select with join",
71 stmt: &SelectStatement{
72 Columns: []Expression{&Identifier{Name: "u.name"}, &Identifier{Name: "o.total"}},
73 From: []TableReference{{Name: "users", Alias: "u"}},
74 Joins: []JoinClause{{
75 Type: "LEFT", Right: TableReference{Name: "orders", Alias: "o"},
76 Condition: &BinaryExpression{Left: &Identifier{Name: "u.id"}, Operator: "=", Right: &Identifier{Name: "o.user_id"}},
77 }},
78 },

Callers

nothing calls this directly

Calls 4

RunMethod · 0.80
intPtrFunction · 0.70
ErrorfMethod · 0.65
SQLMethod · 0.45

Tested by

no test coverage detected