query.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { nanoid } from 'nanoid';
  20. import { selectResultsTab, assertSQLLabResultsAreEqual } from './sqllab.helper';
  21. function parseClockStr(node: JQuery) {
  22. return Number.parseFloat(node.text().replace(/:/g, ''));
  23. }
  24. describe('SqlLab query panel', () => {
  25. beforeEach(() => {
  26. cy.visit('/sqllab');
  27. });
  28. it.skip('supports entering and running a query', () => {
  29. // row limit has to be < ~10 for us to be able to determine how many rows
  30. // are fetched below (because React _Virtualized_ does not render all rows)
  31. let clockTime = 0;
  32. cy.intercept({
  33. method: 'POST',
  34. url: '**/api/v1/sqllab/execute/',
  35. }).as('mockSQLResponse');
  36. cy.get('.TableSelector .Select:eq(0)').click();
  37. cy.get('.TableSelector .Select:eq(0) input[type=text]').focus();
  38. cy.focused().type('{enter}');
  39. cy.get('#brace-editor textarea').focus();
  40. cy.focused().clear();
  41. cy.focused().type(`{selectall}{backspace}SELECT 1`);
  42. cy.get('#js-sql-toolbar button:eq(0)').eq(0).click();
  43. // wait for 300 milliseconds
  44. cy.wait(300);
  45. // started timer
  46. cy.get('.sql-toolbar .label-success').then(node => {
  47. clockTime = parseClockStr(node);
  48. // should be longer than 0.2s
  49. expect(clockTime).greaterThan(0.2);
  50. });
  51. cy.wait('@mockSQLResponse');
  52. // timer is increasing
  53. cy.get('.sql-toolbar .label-success').then(node => {
  54. const newClockTime = parseClockStr(node);
  55. expect(newClockTime).greaterThan(0.9);
  56. clockTime = newClockTime;
  57. });
  58. // rerun the query
  59. cy.get('#js-sql-toolbar button:eq(0)').eq(0).click();
  60. // should restart the timer
  61. cy.get('.sql-toolbar .label-success').contains('00:00:00');
  62. cy.wait('@mockSQLResponse');
  63. cy.get('.sql-toolbar .label-success').then(node => {
  64. expect(parseClockStr(node)).greaterThan(0.9);
  65. });
  66. });
  67. it.skip('successfully saves a query', () => {
  68. cy.intercept('**/api/v1/database/**/tables/**').as('getTables');
  69. const query =
  70. 'SELECT ds, gender, name, num FROM main.birth_names ORDER BY name LIMIT 3';
  71. const savedQueryTitle = `CYPRESS TEST QUERY ${nanoid()}`;
  72. // we will assert that the results of the query we save, and the saved query are the same
  73. let initialResultsTable: HTMLElement | null = null;
  74. let savedQueryResultsTable = null;
  75. cy.get('#brace-editor textarea').clear({ force: true });
  76. cy.get('#brace-editor textarea').type(`{selectall}{backspace}${query}`, {
  77. force: true,
  78. });
  79. cy.get('#brace-editor textarea').focus(); // focus => blur is required for updating the query that is to be saved
  80. cy.focused().blur();
  81. // ctrl + r also runs query
  82. cy.get('#brace-editor textarea').type('{ctrl}r', { force: true });
  83. cy.wait('@sqlLabQuery');
  84. // Save results to check against below
  85. selectResultsTab().then(resultsA => {
  86. initialResultsTable = resultsA[0];
  87. });
  88. cy.get('#js-sql-toolbar button')
  89. .eq(1) // save query
  90. .click();
  91. // Enter name + save into modal
  92. cy.get('.modal-sm input').clear({ force: true });
  93. cy.get('.modal-sm input').type(`{selectall}{backspace}${savedQueryTitle}`, {
  94. force: true,
  95. });
  96. cy.get('.modal-sm .modal-body button')
  97. .eq(0) // save
  98. .click();
  99. // first row contains most recent link, follow back to SqlLab
  100. cy.get('table tr:first-child a[href*="savedQueryId"').click();
  101. // will timeout without explicitly waiting here
  102. cy.wait(['@getSavedQuery', '@getTables']);
  103. // run the saved query
  104. cy.get('#js-sql-toolbar button')
  105. .eq(0) // run query
  106. .click();
  107. cy.wait('@sqlLabQuery');
  108. // assert the results of the saved query match the initial results
  109. selectResultsTab().then(resultsB => {
  110. savedQueryResultsTable = resultsB[0];
  111. assertSQLLabResultsAreEqual(initialResultsTable, savedQueryResultsTable);
  112. });
  113. });
  114. it.skip('Create a chart from a query', () => {
  115. cy.intercept('**/api/v1/sqllab/execute/').as('queryFinished');
  116. cy.intercept('**/api/v1/explore/**').as('explore');
  117. cy.intercept('**/api/v1/chart/**').as('chart');
  118. cy.intercept('**/tabstateview/**').as('tabstateview');
  119. // cypress doesn't handle opening a new tab, override window.open to open in the same tab
  120. cy.window().then(win => {
  121. cy.stub(win, 'open', url => {
  122. // eslint-disable-next-line no-param-reassign
  123. win.location.href = url;
  124. });
  125. });
  126. cy.wait('@tabstateview');
  127. const query = 'SELECT gender, name FROM birth_names';
  128. cy.get('.ace_text-input').focus();
  129. cy.focused().clear({ force: true });
  130. cy.focused().type(`{selectall}{backspace}${query}`, { force: true });
  131. cy.get('.sql-toolbar button').contains('Run').click();
  132. cy.wait('@queryFinished');
  133. cy.get(
  134. '.SouthPane .ant-tabs-content > .ant-tabs-tabpane-active > div button:first',
  135. { timeout: 10000 },
  136. ).click();
  137. cy.wait('@explore');
  138. cy.get('[data-test="datasource-control"] .title-select').contains(query);
  139. cy.get('.column-option-label').first().contains('gender');
  140. cy.get('.column-option-label').last().contains('name');
  141. cy.get(
  142. '[data-test="all_columns"] [data-test="dnd-labels-container"] > div:first-child',
  143. ).contains('gender');
  144. cy.get(
  145. '[data-test="all_columns"] [data-test="dnd-labels-container"] > div:nth-child(2)',
  146. ).contains('name');
  147. cy.wait('@chart');
  148. cy.get('[data-test="slice-container"] table > thead th')
  149. .first()
  150. .contains('gender');
  151. cy.get('[data-test="slice-container"] table > thead th')
  152. .last()
  153. .contains('name');
  154. });
  155. });