-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathutils.test.ts
More file actions
147 lines (137 loc) Β· 3.99 KB
/
utils.test.ts
File metadata and controls
147 lines (137 loc) Β· 3.99 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
144
145
146
147
import * as t from '../src';
import { RangeVar, SelectStmt } from '@pgsql/types';
import { deparseSync as deparse } from 'pgsql-deparser';
it('simple SelectStmt', () => {
const stmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
targetList: [
t.nodes.resTarget({
val: t.nodes.columnRef({
fields: [t.nodes.aStar()]
})
})
],
fromClause: [
t.nodes.rangeVar({
relname: 'some_table',
inh: true,
relpersistence: 'p'
})
],
limitOption: 'LIMIT_OPTION_DEFAULT',
op: 'SETOP_NONE'
});
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';
expect(stmt).toMatchSnapshot();
expect(deparse(stmt, { pretty: false })).toMatchSnapshot();
});
it('SelectStmt with WHERE clause', () => {
const selectStmt: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
targetList: [
t.nodes.resTarget({
val: t.nodes.columnRef({
fields: [t.nodes.aStar()]
})
})
],
fromClause: [
t.nodes.rangeVar({
schemaname: 'myschema',
relname: 'mytable',
inh: true,
relpersistence: 'p'
})
],
whereClause: t.nodes.aExpr({
kind: 'AEXPR_OP',
name: [t.nodes.string({ sval: '=' })],
lexpr: t.nodes.columnRef({
fields: [t.nodes.string({ sval: 'a' })]
}),
rexpr: t.nodes.typeCast({
arg: t.nodes.aConst({
sval: t.ast.string({ sval: 't' })
}),
typeName: t.ast.typeName({
names: [
t.nodes.string({ sval: 'pg_catalog' }),
t.nodes.string({ sval: 'bool' })
],
typemod: -1
})
})
}),
limitOption: 'LIMIT_OPTION_DEFAULT',
op: 'SETOP_NONE'
});
expect(deparse(selectStmt, { pretty: false })).toEqual(`SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)`);
});
it('queries', () => {
const query: { SelectStmt: SelectStmt } = t.nodes.selectStmt({
targetList: [
t.nodes.resTarget({
val: t.nodes.columnRef({
fields: [t.nodes.string({ sval: 'name' })]
})
}),
t.nodes.resTarget({
val: t.nodes.columnRef({
fields: [t.nodes.string({ sval: 'email' })]
})
})
],
fromClause: [
t.nodes.rangeVar({
relname: 'users',
inh: true,
relpersistence: 'p'
})
],
whereClause: t.nodes.aExpr({
kind: 'AEXPR_OP',
name: [t.nodes.string({ sval: '>' })],
lexpr: t.nodes.columnRef({
fields: [t.nodes.string({ sval: 'age' })]
}),
rexpr: t.nodes.aConst({
ival: t.ast.integer({ ival: 18 })
})
}),
limitOption: 'LIMIT_OPTION_DEFAULT',
op: 'SETOP_NONE'
});
expect(deparse(query, { pretty: false })).toEqual(`SELECT name, email FROM users WHERE age > 18`);
});
it('dynamic creation of tables', () => {
// Example JSON schema
const schema = {
"tableName": "users",
"columns": [
{ "name": "id", "type": "int", "constraints": ["PRIMARY KEY"] },
{ "name": "username", "type": "text" },
{ "name": "email", "type": "text", "constraints": ["UNIQUE"] },
{ "name": "created_at", "type": "timestamp", "constraints": ["NOT NULL"] }
]
};
// Construct the CREATE TABLE statement
const createStmt = t.nodes.createStmt({
relation: t.ast.rangeVar({
relname: schema.tableName,
inh: true,
relpersistence: 'p'
}),
tableElts: schema.columns.map(column => t.nodes.columnDef({
colname: column.name,
typeName: t.ast.typeName({
names: [t.nodes.string({ sval: column.type })]
}),
constraints: column.constraints?.map(constraint =>
t.nodes.constraint({
contype: constraint === "PRIMARY KEY" ? "CONSTR_PRIMARY" : constraint === "UNIQUE" ? "CONSTR_UNIQUE" : "CONSTR_NOTNULL"
})
)
}))
});
// `deparse` function converts AST to SQL string
const sql = deparse(createStmt, { pretty: false });
expect(sql).toMatchSnapshot();
})