Repository navigation
Expand file tree
/
Copy pathsql-query.test.ts
More file actions
87 lines (75 loc) · 2.05 KB
/
Copy pathsql-query.test.ts
File metadata and controls
87 lines (75 loc) · 2.05 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
import { describe, expect, it } from "vitest"
import { raw, int, ident, as_, when } from "./sql-fragment"
import { compileQuery } from "./sql-query"
describe("compileQuery", () => {
it("compiles a basic SELECT query", () => {
const sql = compileQuery({
select: [as_(raw("count()"), "count")],
from: ident("traces"),
where: [raw("OrgId = 'test'")],
groupBy: [],
orderBy: [],
})
expect(sql).toBe(
`SELECT\n count() AS count\n FROM traces\n WHERE OrgId = 'test'`,
)
})
it("compiles with GROUP BY and ORDER BY", () => {
const sql = compileQuery({
select: [ident("bucket"), as_(raw("count()"), "count")],
from: ident("traces"),
where: [raw("OrgId = 'test'")],
groupBy: [ident("bucket")],
orderBy: [raw("bucket ASC")],
})
expect(sql).toContain("GROUP BY bucket")
expect(sql).toContain("ORDER BY bucket ASC")
})
it("includes LIMIT when provided", () => {
const sql = compileQuery({
select: [ident("name")],
from: ident("traces"),
where: [],
groupBy: [],
orderBy: [],
limit: int(10),
})
expect(sql).toContain("LIMIT 10")
})
it("includes FORMAT when provided", () => {
const sql = compileQuery({
select: [ident("name")],
from: ident("traces"),
where: [],
groupBy: [],
orderBy: [],
format: "JSON",
})
expect(sql).toContain("FORMAT JSON")
})
it("filters out When(false) from WHERE clauses", () => {
const sql = compileQuery({
select: [as_(raw("count()"), "count")],
from: ident("traces"),
where: [
raw("OrgId = 'test'"),
when(false, raw("ServiceName = 'api'")),
raw("Timestamp >= '2024-01-01'"),
],
groupBy: [],
orderBy: [],
})
expect(sql).not.toContain("ServiceName")
expect(sql).toContain("OrgId = 'test'\n AND Timestamp >= '2024-01-01'")
})
it("omits WHERE when all clauses are When(false)", () => {
const sql = compileQuery({
select: [as_(raw("count()"), "count")],
from: ident("traces"),
where: [when(false, raw("x = 1"))],
groupBy: [],
orderBy: [],
})
expect(sql).not.toContain("WHERE")
})
})