Repository navigation
Expand file tree
/
Copy pathstatement.test.ts
More file actions
93 lines (77 loc) · 3.31 KB
/
Copy pathstatement.test.ts
File metadata and controls
93 lines (77 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Schema } from "effect"
import { describe, expect, it } from "vitest"
import {
ClickHouseStatement,
ClickHouseStatementFromString,
parseStatement,
renderStatement,
withFormat,
withSettings,
} from "./statement"
describe("parseStatement / renderStatement", () => {
it("round-trips a statement with both clauses", () => {
const sql = "SELECT 1\nSETTINGS max_threads=2\nFORMAT JSON"
expect(renderStatement(parseStatement(sql))).toBe(sql)
})
it("normalizes the legacy clause order on the way back out", () => {
expect(renderStatement(parseStatement("SELECT 1 FORMAT JSON SETTINGS max_threads=2"))).toBe(
"SELECT 1\nSETTINGS max_threads=2\nFORMAT JSON",
)
})
it("exposes the rendered text on the instance", () => {
expect(parseStatement("SELECT 1 FORMAT JSON").text).toBe("SELECT 1\nFORMAT JSON")
})
it("keeps the body free of terminal clauses so it is safe to nest", () => {
expect(parseStatement("SELECT 1 SETTINGS max_threads=2 FORMAT JSON").body).toBe("SELECT 1")
})
it("renders a bare body unchanged", () => {
expect(renderStatement(parseStatement("SELECT 1"))).toBe("SELECT 1")
})
})
describe("withSettings / withFormat", () => {
const base = parseStatement("SELECT 1")
it("adds clauses in grammar order", () => {
const statement = withFormat(withSettings(base, "SETTINGS max_threads=2"), "FORMAT JSON")
expect(statement.text).toBe("SELECT 1\nSETTINGS max_threads=2\nFORMAT JSON")
})
it("drops a clause with undefined", () => {
const statement = parseStatement("SELECT 1 SETTINGS max_threads=2 FORMAT JSON")
expect(withFormat(statement, undefined).text).toBe("SELECT 1\nSETTINGS max_threads=2")
expect(withSettings(statement, undefined).text).toBe("SELECT 1\nFORMAT JSON")
})
it("replaces rather than appends", () => {
const statement = parseStatement("SELECT 1 SETTINGS max_threads=2")
expect(withSettings(statement, "SETTINGS max_threads=8").text).toBe(
"SELECT 1\nSETTINGS max_threads=8",
)
})
it("cannot swallow a clause behind a trailing comment", () => {
const statement = withSettings(parseStatement("SELECT 1 -- note"), "SETTINGS max_threads=2")
expect(statement.text).toBe("SELECT 1 -- note\nSETTINGS max_threads=2")
})
})
describe("ClickHouseStatementFromString", () => {
const decode = Schema.decodeUnknownSync(ClickHouseStatementFromString)
const encode = Schema.encodeSync(ClickHouseStatementFromString)
it("decodes text into the parsed shape", () => {
const statement = decode("SELECT 1 SETTINGS max_threads=2 FORMAT JSON")
expect(statement).toBeInstanceOf(ClickHouseStatement)
expect(statement.body).toBe("SELECT 1")
expect(statement.settings).toBe("SETTINGS max_threads=2")
expect(statement.format).toBe("FORMAT JSON")
})
it("encodes back to SQL text", () => {
expect(encode(decode("SELECT 1\nFORMAT JSON"))).toBe("SELECT 1\nFORMAT JSON")
})
it("decodes a statement with no terminal clause", () => {
const statement = decode("SELECT format FROM t")
expect(statement.body).toBe("SELECT format FROM t")
expect(statement.settings).toBeUndefined()
expect(statement.format).toBeUndefined()
})
})
it("replaces a format before a semicolon and trailing comment", () => {
const statement = parseStatement("SELECT 1 FORMAT JSONEachRow; -- tail")
expect(statement.body).toBe("SELECT 1")
expect(withFormat(statement, "FORMAT CSV").text).toBe("SELECT 1\nFORMAT CSV")
})