comparison .cms/lib/codemirror/test/sql-hint-test.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE
3
4 (function() {
5 var Pos = CodeMirror.Pos;
6
7 var simpleTables = {
8 "users": ["name", "score", "birthDate"],
9 "xcountries": ["name", "population", "size"]
10 };
11
12 var schemaTables = {
13 "schema.users": ["name", "score", "birthDate"],
14 "schema.countries": ["name", "population", "size"]
15 };
16
17 var displayTextTables = [{
18 text: "mytable",
19 displayText: "mytable | The main table",
20 columns: [{text: "id", displayText: "id | Unique ID"},
21 {text: "name", displayText: "name | The name"}]
22 }];
23
24 var displayTextTablesWithDefault = [
25 {
26 text: "Api__TokenAliases",
27 columns: [
28 {
29 text: "token",
30 displayText: "token | varchar(255) | Primary",
31 columnName: "token",
32 columnHint: "varchar(255) | Primary"
33 },
34 {
35 text: "alias",
36 displayText: "alias | varchar(255) | Primary",
37 columnName: "alias",
38 columnHint: "varchar(255) | Primary"
39 }
40 ]
41 },
42 {
43 text: "mytable",
44 columns: [
45 { text: "id", displayText: "id | Unique ID" },
46 { text: "name", displayText: "name | The name" }
47 ]
48 }
49 ];
50
51 namespace = "sql-hint_";
52
53 function test(name, spec) {
54 testCM(name, function(cm) {
55 cm.setValue(spec.value);
56 cm.setCursor(spec.cursor);
57 var completion = CodeMirror.hint.sql(cm, {
58 tables: spec.tables,
59 defaultTable: spec.defaultTable,
60 disableKeywords: spec.disableKeywords
61 });
62 if (!deepCompare(completion.list, spec.list))
63 throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list));
64 eqCharPos(completion.from, spec.from);
65 eqCharPos(completion.to, spec.to);
66 }, {
67 value: spec.value,
68 mode: spec.mode || "text/x-mysql"
69 });
70 }
71
72 test("keywords", {
73 value: "SEL",
74 cursor: Pos(0, 3),
75 list: [{"text":"SELECT","className":"CodeMirror-hint-keyword"}],
76 from: Pos(0, 0),
77 to: Pos(0, 3)
78 });
79
80 test("keywords_disabled", {
81 value: "SEL",
82 cursor: Pos(0, 3),
83 disableKeywords: true,
84 list: [],
85 from: Pos(0, 0),
86 to: Pos(0, 3)
87 });
88
89 test("from", {
90 value: "SELECT * fr",
91 cursor: Pos(0, 11),
92 list: [{"text":"FROM","className":"CodeMirror-hint-keyword"}],
93 from: Pos(0, 9),
94 to: Pos(0, 11)
95 });
96
97 test("table", {
98 value: "SELECT xc",
99 cursor: Pos(0, 9),
100 tables: simpleTables,
101 list: [{"text":"xcountries","className":"CodeMirror-hint-table"}],
102 from: Pos(0, 7),
103 to: Pos(0, 9)
104 });
105
106 test("columns", {
107 value: "SELECT users.",
108 cursor: Pos(0, 13),
109 tables: simpleTables,
110 list: ["users.name", "users.score", "users.birthDate"],
111 from: Pos(0, 7),
112 to: Pos(0, 13)
113 });
114
115 test("singlecolumn", {
116 value: "SELECT users.na",
117 cursor: Pos(0, 15),
118 tables: simpleTables,
119 list: ["users.name"],
120 from: Pos(0, 7),
121 to: Pos(0, 15)
122 });
123
124 test("quoted", {
125 value: "SELECT `users`.`na",
126 cursor: Pos(0, 18),
127 tables: simpleTables,
128 list: ["`users`.`name`"],
129 from: Pos(0, 7),
130 to: Pos(0, 18)
131 });
132
133 test("doublequoted", {
134 value: "SELECT \"users\".\"na",
135 cursor: Pos(0, 18),
136 tables: simpleTables,
137 list: ["\"users\".\"name\""],
138 from: Pos(0, 7),
139 to: Pos(0, 18),
140 mode: "text/x-sqlite"
141 });
142
143 test("quotedcolumn", {
144 value: "SELECT users.`na",
145 cursor: Pos(0, 16),
146 tables: simpleTables,
147 list: ["`users`.`name`"],
148 from: Pos(0, 7),
149 to: Pos(0, 16)
150 });
151
152 test("doublequotedcolumn", {
153 value: "SELECT users.\"na",
154 cursor: Pos(0, 16),
155 tables: simpleTables,
156 list: ["\"users\".\"name\""],
157 from: Pos(0, 7),
158 to: Pos(0, 16),
159 mode: "text/x-sqlite"
160 });
161
162 test("schema", {
163 value: "SELECT schem",
164 cursor: Pos(0, 12),
165 tables: schemaTables,
166 list: [{"text":"schema.users","className":"CodeMirror-hint-table"},
167 {"text":"schema.countries","className":"CodeMirror-hint-table"},
168 {"text":"SCHEMA","className":"CodeMirror-hint-keyword"},
169 {"text":"SCHEMA_NAME","className":"CodeMirror-hint-keyword"},
170 {"text":"SCHEMAS","className":"CodeMirror-hint-keyword"}],
171 from: Pos(0, 7),
172 to: Pos(0, 12)
173 });
174
175 test("schemaquoted", {
176 value: "SELECT `sch",
177 cursor: Pos(0, 11),
178 tables: schemaTables,
179 list: ["`schema`.`users`", "`schema`.`countries`"],
180 from: Pos(0, 7),
181 to: Pos(0, 11)
182 });
183
184 test("schemadoublequoted", {
185 value: "SELECT \"sch",
186 cursor: Pos(0, 11),
187 tables: schemaTables,
188 list: ["\"schema\".\"users\"", "\"schema\".\"countries\""],
189 from: Pos(0, 7),
190 to: Pos(0, 11),
191 mode: "text/x-sqlite"
192 });
193
194 test("schemacolumn", {
195 value: "SELECT schema.users.",
196 cursor: Pos(0, 20),
197 tables: schemaTables,
198 list: ["schema.users.name",
199 "schema.users.score",
200 "schema.users.birthDate"],
201 from: Pos(0, 7),
202 to: Pos(0, 20)
203 });
204
205 test("schemacolumnquoted", {
206 value: "SELECT `schema`.`users`.",
207 cursor: Pos(0, 24),
208 tables: schemaTables,
209 list: ["`schema`.`users`.`name`",
210 "`schema`.`users`.`score`",
211 "`schema`.`users`.`birthDate`"],
212 from: Pos(0, 7),
213 to: Pos(0, 24)
214 });
215
216 test("schemacolumndoublequoted", {
217 value: "SELECT \"schema\".\"users\".",
218 cursor: Pos(0, 24),
219 tables: schemaTables,
220 list: ["\"schema\".\"users\".\"name\"",
221 "\"schema\".\"users\".\"score\"",
222 "\"schema\".\"users\".\"birthDate\""],
223 from: Pos(0, 7),
224 to: Pos(0, 24),
225 mode: "text/x-sqlite"
226 });
227
228 test("displayText_default_table", {
229 value: "SELECT a",
230 cursor: Pos(0, 8),
231 disableKeywords: true,
232 defaultTable: "Api__TokenAliases",
233 tables: displayTextTablesWithDefault,
234 list: [
235 {
236 text: "alias",
237 displayText: "alias | varchar(255) | Primary",
238 columnName: "alias",
239 columnHint: "varchar(255) | Primary",
240 className: "CodeMirror-hint-table CodeMirror-hint-default-table"
241 },
242 { text: "Api__TokenAliases", className: "CodeMirror-hint-table" }
243 ],
244 from: Pos(0, 7),
245 to: Pos(0, 8)
246 });
247
248 test("displayText_table", {
249 value: "SELECT myt",
250 cursor: Pos(0, 10),
251 tables: displayTextTables,
252 list: [{text: "mytable", displayText: "mytable | The main table", "className":"CodeMirror-hint-table"}],
253 from: Pos(0, 7),
254 to: Pos(0, 10)
255 });
256
257 test("displayText_column", {
258 value: "SELECT mytable.",
259 cursor: Pos(0, 15),
260 tables: displayTextTables,
261 list: [{text: "mytable.id", displayText: "id | Unique ID"},
262 {text: "mytable.name", displayText: "name | The name"}],
263 from: Pos(0, 7),
264 to: Pos(0, 15)
265 });
266
267 test("alias_complete", {
268 value: "SELECT t. FROM users t",
269 cursor: Pos(0, 9),
270 tables: simpleTables,
271 list: ["t.name", "t.score", "t.birthDate"],
272 from: Pos(0, 7),
273 to: Pos(0, 9)
274 });
275
276 test("alias_complete_with_displayText", {
277 value: "SELECT t. FROM mytable t",
278 cursor: Pos(0, 9),
279 tables: displayTextTables,
280 list: [{text: "t.id", displayText: "id | Unique ID"},
281 {text: "t.name", displayText: "name | The name"}],
282 from: Pos(0, 7),
283 to: Pos(0, 9)
284 })
285
286 function deepCompare(a, b) {
287 if (a === b) return true
288 if (!(a && typeof a == "object") ||
289 !(b && typeof b == "object")) return false
290 var array = Array.isArray(a)
291 if (Array.isArray(b) != array) return false
292 if (array) {
293 if (a.length != b.length) return false
294 for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false
295 } else {
296 for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false
297 for (var p in b) if (!(p in a)) return false
298 }
299 return true
300 }
301 })();