forked from supabase-community/postgres-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.rs
192 lines (168 loc) · 6.14 KB
/
codegen.rs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use codegen::parser_codegen;
parser_codegen!();
#[cfg(test)]
mod tests {
use log::debug;
use proptest::prelude::*;
use crate::codegen::{get_nodes, SyntaxKind, TokenProperty};
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
#[test]
fn test_get_nodes() {
init();
let input = "with c as (insert into contact (id) values ('id')) select * from c;";
let pg_query_root = match pg_query::parse(input) {
Ok(parsed) => Some(
parsed
.protobuf
.nodes()
.iter()
.find(|n| n.1 == 1)
.unwrap()
.0
.to_enum(),
),
Err(_) => None,
};
let node_graph = get_nodes(&pg_query_root.unwrap(), 0);
assert_eq!(node_graph.node_count(), 13);
}
fn test_get_node_properties(input: &str, kind: SyntaxKind, expected: Vec<TokenProperty>) {
init();
let pg_query_root = match pg_query::parse(input) {
Ok(parsed) => Some(
parsed
.protobuf
.nodes()
.iter()
.find(|n| n.1 == 1)
.unwrap()
.0
.to_enum(),
),
Err(_) => None,
};
debug!("pg_query_root: {:#?}", pg_query_root);
let node_graph = get_nodes(&pg_query_root.unwrap(), 0);
debug!("node graph: {:#?}", node_graph);
let node_index = node_graph
.node_indices()
.find(|n| node_graph[*n].kind == kind)
.unwrap();
debug!("selected node: {:#?}", node_graph[node_index]);
// note: even though we test for strict equality of the two vectors the order
// of the properties does not have to match the order of the tokens in the string
assert_eq!(node_graph[node_index].properties, expected);
assert_eq!(node_graph[node_index].properties.len(), expected.len());
}
proptest! {
#[test]
fn test_simple_select(n in 0..100i32) {
test_get_node_properties(&format!("select {};", n), SyntaxKind::SelectStmt, vec![TokenProperty::from(SyntaxKind::Select)])
}
}
proptest! {
#[test]
fn test_select_with_from(n in 0..100i32, table_name in "ab?c?d?") {
test_get_node_properties(
&format!("select {} from {};", n, table_name),
SyntaxKind::SelectStmt,
vec![
TokenProperty::from(SyntaxKind::Select),
TokenProperty::from(SyntaxKind::From),
],
)
}
}
proptest! {
#[test]
fn test_select_with_where(table_name in "ab?c?d?", condition in "<|>|=|<>|!=", n in 0..100i32) {
test_get_node_properties(
&format!("select * from {} where id {} {};", table_name, condition, n),
SyntaxKind::SelectStmt,
vec![
TokenProperty::from(SyntaxKind::Select),
TokenProperty::from(SyntaxKind::From),
TokenProperty::from(SyntaxKind::Where),
],
)
}
}
#[test]
fn test_create_domain() {
test_get_node_properties(
"create domain us_postal_code as text check (value is not null);",
SyntaxKind::CreateDomainStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::DomainP),
TokenProperty::from(SyntaxKind::As),
TokenProperty::from("us_postal_code".to_string()),
],
)
}
#[test]
fn test_create_schema() {
test_get_node_properties(
"create schema if not exists test authorization joe;",
SyntaxKind::CreateSchemaStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::Schema),
TokenProperty::from(SyntaxKind::IfP),
TokenProperty::from(SyntaxKind::Not),
TokenProperty::from(SyntaxKind::Exists),
TokenProperty::from(SyntaxKind::Authorization),
TokenProperty::from("test".to_string()),
],
)
}
#[test]
fn test_create_view() {
test_get_node_properties(
"create or replace temporary view comedies as select * from films;",
SyntaxKind::ViewStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::View),
TokenProperty::from(SyntaxKind::As),
TokenProperty::from(SyntaxKind::Or),
TokenProperty::from(SyntaxKind::Replace),
TokenProperty::from(SyntaxKind::Temporary),
],
)
}
#[test]
fn test_create_enum() {
test_get_node_properties(
"create type status as enum ('open', 'closed');",
SyntaxKind::CreateEnumStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::TypeP),
TokenProperty::from(SyntaxKind::As),
TokenProperty::from(SyntaxKind::EnumP),
TokenProperty::from("status".to_string()),
TokenProperty::from("open".to_string()),
TokenProperty::from("closed".to_string()),
],
)
}
#[test]
fn test_create_cast() {
test_get_node_properties(
"create cast (bigint as int4) with inout as assignment;",
SyntaxKind::CreateCastStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::Cast),
TokenProperty::from(SyntaxKind::As),
TokenProperty::from(SyntaxKind::With),
TokenProperty::from(SyntaxKind::Inout),
TokenProperty::from(SyntaxKind::As),
TokenProperty::from(SyntaxKind::Assignment),
],
)
}
}