Repository navigation
Expand file tree
/
Copy pathselect.test.ts
More file actions
143 lines (119 loc) · 4.4 KB
/
Copy pathselect.test.ts
File metadata and controls
143 lines (119 loc) · 4.4 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, expect, it } from "vitest"
import { queryBuilder } from "../../../src/query/query-builder.js"
import type { Input, Schema } from "../../../src/query/types.js"
// Test schema
interface Employee extends Input {
id: number
name: string
department_id: number | null
salary: number
}
interface Department extends Input {
id: number
name: string
budget: number
}
// Make sure TestSchema extends Schema
interface TestSchema extends Schema {
employees: Employee
departments: Department
}
describe(`QueryBuilder.select`, () => {
it(`sets the select clause correctly with individual columns`, () => {
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, `@name`)
const builtQuery = query._query
expect(builtQuery.select).toEqual([`@id`, `@name`])
})
it(`handles aliased columns`, () => {
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, { employee_name: `@name` })
const builtQuery = query._query
expect(builtQuery.select).toHaveLength(2)
expect(builtQuery.select![0]).toBe(`@id`)
expect(builtQuery.select![1]).toHaveProperty(`employee_name`, `@name`)
})
it(`handles function calls`, () => {
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, {
upper_name: { UPPER: `@name` },
})
const builtQuery = query._query
expect(builtQuery.select).toHaveLength(2)
expect(builtQuery.select![1]).toHaveProperty(`upper_name`)
})
it(`overrides previous select calls`, () => {
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, `@name`)
.select(`@id`, `@salary`) // This should override the previous select
const builtQuery = query._query
expect(builtQuery.select).toEqual([`@id`, `@salary`])
})
it(`supports qualified table references`, () => {
const query = queryBuilder<TestSchema>()
.from(`employees`, `e`)
.select(`@e.id`, `@e.name`)
const builtQuery = query._query
expect(builtQuery.select).toEqual([`@e.id`, `@e.name`])
})
// Runtime test for the result types
it(`infers correct result types`, () => {
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, `@name`)
// We can't directly assert on types in a test, but we can check
// that the query is constructed correctly, which implies the types work
const builtQuery = query._query
expect(builtQuery.select).toEqual([`@id`, `@name`])
})
it(`supports callback functions`, () => {
const callback = ({ employees }: any) => ({
fullInfo: `${employees.name} (ID: ${employees.id})`,
salaryLevel: employees.salary > 50000 ? `high` : `low`,
})
const query = queryBuilder<TestSchema>().from(`employees`).select(callback)
const builtQuery = query._query
expect(builtQuery.select).toHaveLength(1)
expect(builtQuery.select).toBeDefined()
expect(typeof builtQuery.select![0]).toBe(`function`)
expect(builtQuery.select![0]).toBe(callback)
})
it(`combines callback with traditional selects`, () => {
const callback = ({ employees }: any) => ({
computed: employees.salary * 1.1,
})
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, `@name`, callback, { department_name: `@employees.name` })
const builtQuery = query._query
expect(builtQuery.select).toHaveLength(4)
expect(builtQuery.select).toBeDefined()
expect(builtQuery.select![0]).toBe(`@id`)
expect(builtQuery.select![1]).toBe(`@name`)
expect(typeof builtQuery.select![2]).toBe(`function`)
expect(builtQuery.select![3]).toHaveProperty(`department_name`)
})
it(`supports multiple callback functions`, () => {
const callback1 = ({ employees }: any) => ({
displayName: employees.name.toUpperCase(),
})
const callback2 = ({ employees }: any) => ({
isActive: employees.active,
experience: new Date().getFullYear() - 2020,
})
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(callback1, callback2)
const builtQuery = query._query
expect(builtQuery.select).toHaveLength(2)
expect(builtQuery.select).toBeDefined()
expect(typeof builtQuery.select![0]).toBe(`function`)
expect(typeof builtQuery.select![1]).toBe(`function`)
expect(builtQuery.select![0]).toBe(callback1)
expect(builtQuery.select![1]).toBe(callback2)
})
})