sql-hint-test.js (6008B)
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/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 namespace = "sql-hint_"; 25 26 function test(name, spec) { 27 testCM(name, function(cm) { 28 cm.setValue(spec.value); 29 cm.setCursor(spec.cursor); 30 var completion = CodeMirror.hint.sql(cm, {tables: spec.tables}); 31 if (!deepCompare(completion.list, spec.list)) 32 throw new Failure("Wrong completion results " + JSON.stringify(completion.list) + " vs " + JSON.stringify(spec.list)); 33 eqCharPos(completion.from, spec.from); 34 eqCharPos(completion.to, spec.to); 35 }, { 36 value: spec.value, 37 mode: spec.mode || "text/x-mysql" 38 }); 39 } 40 41 test("keywords", { 42 value: "SEL", 43 cursor: Pos(0, 3), 44 list: ["SELECT"], 45 from: Pos(0, 0), 46 to: Pos(0, 3) 47 }); 48 49 test("from", { 50 value: "SELECT * fr", 51 cursor: Pos(0, 11), 52 list: ["FROM"], 53 from: Pos(0, 9), 54 to: Pos(0, 11) 55 }); 56 57 test("table", { 58 value: "SELECT xc", 59 cursor: Pos(0, 9), 60 tables: simpleTables, 61 list: ["xcountries"], 62 from: Pos(0, 7), 63 to: Pos(0, 9) 64 }); 65 66 test("columns", { 67 value: "SELECT users.", 68 cursor: Pos(0, 13), 69 tables: simpleTables, 70 list: ["users.name", "users.score", "users.birthDate"], 71 from: Pos(0, 7), 72 to: Pos(0, 13) 73 }); 74 75 test("singlecolumn", { 76 value: "SELECT users.na", 77 cursor: Pos(0, 15), 78 tables: simpleTables, 79 list: ["users.name"], 80 from: Pos(0, 7), 81 to: Pos(0, 15) 82 }); 83 84 test("quoted", { 85 value: "SELECT `users`.`na", 86 cursor: Pos(0, 18), 87 tables: simpleTables, 88 list: ["`users`.`name`"], 89 from: Pos(0, 7), 90 to: Pos(0, 18) 91 }); 92 93 test("doublequoted", { 94 value: "SELECT \"users\".\"na", 95 cursor: Pos(0, 18), 96 tables: simpleTables, 97 list: ["\"users\".\"name\""], 98 from: Pos(0, 7), 99 to: Pos(0, 18), 100 mode: "text/x-sqlite" 101 }); 102 103 test("quotedcolumn", { 104 value: "SELECT users.`na", 105 cursor: Pos(0, 16), 106 tables: simpleTables, 107 list: ["`users`.`name`"], 108 from: Pos(0, 7), 109 to: Pos(0, 16) 110 }); 111 112 test("doublequotedcolumn", { 113 value: "SELECT users.\"na", 114 cursor: Pos(0, 16), 115 tables: simpleTables, 116 list: ["\"users\".\"name\""], 117 from: Pos(0, 7), 118 to: Pos(0, 16), 119 mode: "text/x-sqlite" 120 }); 121 122 test("schema", { 123 value: "SELECT schem", 124 cursor: Pos(0, 12), 125 tables: schemaTables, 126 list: ["schema.users", "schema.countries", 127 "SCHEMA", "SCHEMA_NAME", "SCHEMAS"], 128 from: Pos(0, 7), 129 to: Pos(0, 12) 130 }); 131 132 test("schemaquoted", { 133 value: "SELECT `sch", 134 cursor: Pos(0, 11), 135 tables: schemaTables, 136 list: ["`schema`.`users`", "`schema`.`countries`"], 137 from: Pos(0, 7), 138 to: Pos(0, 11) 139 }); 140 141 test("schemadoublequoted", { 142 value: "SELECT \"sch", 143 cursor: Pos(0, 11), 144 tables: schemaTables, 145 list: ["\"schema\".\"users\"", "\"schema\".\"countries\""], 146 from: Pos(0, 7), 147 to: Pos(0, 11), 148 mode: "text/x-sqlite" 149 }); 150 151 test("schemacolumn", { 152 value: "SELECT schema.users.", 153 cursor: Pos(0, 20), 154 tables: schemaTables, 155 list: ["schema.users.name", 156 "schema.users.score", 157 "schema.users.birthDate"], 158 from: Pos(0, 7), 159 to: Pos(0, 20) 160 }); 161 162 test("schemacolumnquoted", { 163 value: "SELECT `schema`.`users`.", 164 cursor: Pos(0, 24), 165 tables: schemaTables, 166 list: ["`schema`.`users`.`name`", 167 "`schema`.`users`.`score`", 168 "`schema`.`users`.`birthDate`"], 169 from: Pos(0, 7), 170 to: Pos(0, 24) 171 }); 172 173 test("schemacolumndoublequoted", { 174 value: "SELECT \"schema\".\"users\".", 175 cursor: Pos(0, 24), 176 tables: schemaTables, 177 list: ["\"schema\".\"users\".\"name\"", 178 "\"schema\".\"users\".\"score\"", 179 "\"schema\".\"users\".\"birthDate\""], 180 from: Pos(0, 7), 181 to: Pos(0, 24), 182 mode: "text/x-sqlite" 183 }); 184 185 test("displayText_table", { 186 value: "SELECT myt", 187 cursor: Pos(0, 10), 188 tables: displayTextTables, 189 list: [{text: "mytable", displayText: "mytable | The main table",}], 190 from: Pos(0, 7), 191 to: Pos(0, 10) 192 }); 193 194 test("displayText_column", { 195 value: "SELECT mytable.", 196 cursor: Pos(0, 15), 197 tables: displayTextTables, 198 list: [{text: "mytable.id", displayText: "id | Unique ID"}, 199 {text: "mytable.name", displayText: "name | The name"}], 200 from: Pos(0, 7), 201 to: Pos(0, 15) 202 }); 203 204 test("alias_complete", { 205 value: "SELECT t. FROM users t", 206 cursor: Pos(0, 9), 207 tables: simpleTables, 208 list: ["t.name", "t.score", "t.birthDate"], 209 from: Pos(0, 7), 210 to: Pos(0, 9) 211 }); 212 213 test("alias_complete_with_displayText", { 214 value: "SELECT t. FROM mytable t", 215 cursor: Pos(0, 9), 216 tables: displayTextTables, 217 list: [{text: "t.id", displayText: "id | Unique ID"}, 218 {text: "t.name", displayText: "name | The name"}], 219 from: Pos(0, 7), 220 to: Pos(0, 9) 221 }) 222 223 function deepCompare(a, b) { 224 if (a === b) return true 225 if (!(a && typeof a == "object") || 226 !(b && typeof b == "object")) return false 227 var array = Array.isArray(a) 228 if (Array.isArray(b) != array) return false 229 if (array) { 230 if (a.length != b.length) return false 231 for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false 232 } else { 233 for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false 234 for (var p in b) if (!(p in a)) return false 235 } 236 return true 237 } 238 })();