MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestClickHouseMacros

Function TestClickHouseMacros

server/chsql_test.go:11–68  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9)
10
11func TestClickHouseMacros(t *testing.T) {
12 db, err := sql.Open("duckdb", ":memory:")
13 if err != nil {
14 t.Fatalf("Failed to open database: %v", err)
15 }
16 defer func() { _ = db.Close() }()
17
18 chsql.InitMacros(db)
19
20 tests := []struct {
21 name string
22 query string
23 want string
24 }{
25 {"toString", "SELECT toString(42)", "42"},
26 {"toInt32", "SELECT toInt32('123')", "123"},
27 {"toInt64", "SELECT toInt64('9999999999')", "9999999999"},
28 {"toFloat", "SELECT toFloat('3.14')", "3.14"},
29 {"toInt32OrNull", "SELECT toInt32OrNull('abc')", ""},
30 {"toInt32OrZero", "SELECT toInt32OrZero('abc')", "0"},
31 {"intDiv", "SELECT intDiv(10, 3)", "3"},
32 {"modulo", "SELECT modulo(10, 3)", "1"},
33 {"empty_true", "SELECT empty('')", "true"},
34 {"empty_false", "SELECT empty('hi')", "false"},
35 {"notEmpty", "SELECT notEmpty('hi')", "true"},
36 {"lengthUTF8", "SELECT lengthUTF8('hello')", "5"},
37 {"toYear", "SELECT toYear(DATE '2024-06-15')", "2024"},
38 {"toMonth", "SELECT toMonth(DATE '2024-06-15')", "6"},
39 {"toDayOfMonth", "SELECT toDayOfMonth(DATE '2024-06-15')", "15"},
40 {"toYYYYMMDD", "SELECT toYYYYMMDD(DATE '2024-06-15')", "20240615"},
41 {"toYYYYMM", "SELECT toYYYYMM(DATE '2024-06-15')", "202406"},
42 {"protocol", "SELECT protocol('https://duckdb.org/docs')", "https"},
43 {"protocol_empty", "SELECT protocol('no-scheme')", ""},
44 {"domain", "SELECT domain('https://duckdb.org/docs')", "duckdb.org"},
45 {"topLevelDomain", "SELECT topLevelDomain('https://duckdb.org/docs')", "org"},
46 {"IPv4NumToString", "SELECT IPv4NumToString(3232235777)", "192.168.1.1"},
47 {"JSONExtractString", `SELECT JSONExtractString('{"a":"hello"}', '$.a')`, `"hello"`},
48 {"JSONHas_true", `SELECT JSONHas('{"a":1}', '$.a')`, "true"},
49 {"JSONHas_false", `SELECT JSONHas('{"a":1}', '$.b')`, "false"},
50 {"ifNull", "SELECT ifNull(NULL, 42)", "42"},
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 var got sql.NullString
56 if err := db.QueryRow(tt.query).Scan(&got); err != nil {
57 t.Fatalf("query %q failed: %v", tt.query, err)
58 }
59 actual := ""
60 if got.Valid {
61 actual = got.String
62 }
63 if actual != tt.want {
64 t.Errorf("got %q, want %q", actual, tt.want)
65 }
66 })
67 }
68}

Callers

nothing calls this directly

Calls 5

InitMacrosFunction · 0.92
CloseMethod · 0.65
RunMethod · 0.65
ScanMethod · 0.65
OpenMethod · 0.45

Tested by

no test coverage detected