sql-hint-test.js 6.3 KB

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