forked from mkaminsky11/codeyourcloud
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql-hint-test.js
More file actions
189 lines (167 loc) · 4.5 KB
/
Copy pathsql-hint-test.js
File metadata and controls
189 lines (167 loc) · 4.5 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
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
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
(function() {
var Pos = CodeMirror.Pos;
var simpleTables = {
"users": ["name", "score", "birthDate"],
"xcountries": ["name", "population", "size"]
};
var schemaTables = {
"schema.users": ["name", "score", "birthDate"],
"schema.countries": ["name", "population", "size"]
};
var displayTextTables = [{
text: "mytable",
displayText: "mytable | The main table",
columns: [{text: "id", displayText: "id | Unique ID"},
{text: "name", displayText: "name | The name"}]
}];
namespace = "sql-hint_";
function test(name, spec) {
testCM(name, function(cm) {
cm.setValue(spec.value);
cm.setCursor(spec.cursor);
var completion = CodeMirror.hint.sql(cm, {tables: spec.tables});
if (!deepCompare(completion.list, spec.list))
throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list));
eqPos(completion.from, spec.from);
eqPos(completion.to, spec.to);
}, {
value: spec.value,
mode: "text/x-mysql"
});
}
test("keywords", {
value: "SEL",
cursor: Pos(0, 3),
list: ["SELECT"],
from: Pos(0, 0),
to: Pos(0, 3)
});
test("from", {
value: "SELECT * fr",
cursor: Pos(0, 11),
list: ["FROM"],
from: Pos(0, 9),
to: Pos(0, 11)
});
test("table", {
value: "SELECT xc",
cursor: Pos(0, 9),
tables: simpleTables,
list: ["xcountries"],
from: Pos(0, 7),
to: Pos(0, 9)
});
test("columns", {
value: "SELECT users.",
cursor: Pos(0, 13),
tables: simpleTables,
list: ["users.name", "users.score", "users.birthDate"],
from: Pos(0, 7),
to: Pos(0, 13)
});
test("singlecolumn", {
value: "SELECT users.na",
cursor: Pos(0, 15),
tables: simpleTables,
list: ["users.name"],
from: Pos(0, 7),
to: Pos(0, 15)
});
test("quoted", {
value: "SELECT `users`.`na",
cursor: Pos(0, 18),
tables: simpleTables,
list: ["`users`.`name`"],
from: Pos(0, 7),
to: Pos(0, 18)
});
test("quotedcolumn", {
value: "SELECT users.`na",
cursor: Pos(0, 16),
tables: simpleTables,
list: ["`users`.`name`"],
from: Pos(0, 7),
to: Pos(0, 16)
});
test("schema", {
value: "SELECT schem",
cursor: Pos(0, 12),
tables: schemaTables,
list: ["schema.users", "schema.countries",
"SCHEMA", "SCHEMA_NAME", "SCHEMAS"],
from: Pos(0, 7),
to: Pos(0, 12)
});
test("schemaquoted", {
value: "SELECT `sch",
cursor: Pos(0, 11),
tables: schemaTables,
list: ["`schema`.`users`", "`schema`.`countries`"],
from: Pos(0, 7),
to: Pos(0, 11)
});
test("schemacolumn", {
value: "SELECT schema.users.",
cursor: Pos(0, 20),
tables: schemaTables,
list: ["schema.users.name",
"schema.users.score",
"schema.users.birthDate"],
from: Pos(0, 7),
to: Pos(0, 20)
});
test("schemacolumnquoted", {
value: "SELECT `schema`.`users`.",
cursor: Pos(0, 24),
tables: schemaTables,
list: ["`schema`.`users`.`name`",
"`schema`.`users`.`score`",
"`schema`.`users`.`birthDate`"],
from: Pos(0, 7),
to: Pos(0, 24)
});
test("displayText_table", {
value: "SELECT myt",
cursor: Pos(0, 10),
tables: displayTextTables,
list: displayTextTables,
from: Pos(0, 7),
to: Pos(0, 10)
});
test("displayText_column", {
value: "SELECT mytable.",
cursor: Pos(0, 15),
tables: displayTextTables,
list: [{text: "mytable.id", displayText: "id | Unique ID"},
{text: "mytable.name", displayText: "name | The name"}],
from: Pos(0, 7),
to: Pos(0, 15)
});
test("alias_complete", {
value: "SELECT t. FROM users t",
cursor: Pos(0, 9),
tables: simpleTables,
list: ["t.name", "t.score", "t.birthDate"],
from: Pos(0, 7),
to: Pos(0, 9)
});
test("alias_complete_with_displayText", {
value: "SELECT t. FROM mytable t",
cursor: Pos(0, 9),
tables: displayTextTables,
list: [{text: "t.id", displayText: "id | Unique ID"},
{text: "t.name", displayText: "name | The name"}],
from: Pos(0, 7),
to: Pos(0, 9)
})
function deepCompare(a, b) {
if (!a || typeof a != "object")
return a === b;
if (!b || typeof b != "object")
return false;
for (var prop in a) if (!deepCompare(a[prop], b[prop])) return false;
return true;
}
})();