table.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 { interceptChart } from 'cypress/utils';
  20. import {
  21. FORM_DATA_DEFAULTS,
  22. NUM_METRIC,
  23. MAX_DS,
  24. MAX_STATE,
  25. SIMPLE_FILTER,
  26. } from './shared.helper';
  27. // Table
  28. describe('Visualization > Table', () => {
  29. beforeEach(() => {
  30. interceptChart({ legacy: false }).as('chartData');
  31. });
  32. const VIZ_DEFAULTS = {
  33. ...FORM_DATA_DEFAULTS,
  34. viz_type: 'table',
  35. row_limit: 1000,
  36. };
  37. const PERCENT_METRIC = {
  38. expressionType: 'SQL',
  39. sqlExpression: 'CAST(SUM(num_girls) AS FLOAT)/SUM(num)',
  40. column: null,
  41. aggregate: null,
  42. hasCustomLabel: true,
  43. label: 'Girls',
  44. optionName: 'metric_6qwzgc8bh2v_zox7hil1mzs',
  45. };
  46. it('Use default time column', () => {
  47. cy.visitChartByParams({
  48. ...VIZ_DEFAULTS,
  49. granularity_sqla: undefined,
  50. metrics: ['count'],
  51. });
  52. cy.get('[data-test=adhoc_filters]').contains('ds');
  53. });
  54. it('Format non-numeric metrics correctly', () => {
  55. cy.visitChartByParams({
  56. ...VIZ_DEFAULTS,
  57. include_time: true,
  58. granularity_sqla: 'ds',
  59. time_grain_sqla: 'P3M',
  60. metrics: [NUM_METRIC, MAX_DS, MAX_STATE],
  61. });
  62. // when format with smart_date, time column use format by granularity
  63. cy.get('.chart-container td:nth-child(1)').contains('2008 Q1');
  64. // other column with timestamp use adaptive formatting
  65. cy.get('.chart-container td:nth-child(3)').contains('2008');
  66. cy.get('.chart-container td:nth-child(4)').contains('TX');
  67. });
  68. it('Format with table_timestamp_format', () => {
  69. cy.visitChartByParams({
  70. ...VIZ_DEFAULTS,
  71. include_time: true,
  72. granularity_sqla: 'ds',
  73. time_grain_sqla: 'P3M',
  74. table_timestamp_format: '%Y-%m-%d %H:%M',
  75. metrics: [NUM_METRIC, MAX_DS, MAX_STATE],
  76. });
  77. // time column and MAX(ds) metric column both use UTC time
  78. cy.get('.chart-container td:nth-child(1)').contains('2008-01-01 00:00');
  79. cy.get('.chart-container td:nth-child(3)').contains('2008-01-01 00:00');
  80. cy.get('.chart-container td')
  81. .contains('2008-01-01 08:00')
  82. .should('not.exist');
  83. // time column should not use time granularity when timestamp format is set
  84. cy.get('.chart-container td').contains('2008 Q1').should('not.exist');
  85. // other num numeric metric column should stay as string
  86. cy.get('.chart-container td').contains('TX');
  87. });
  88. it('Test table with groupby', () => {
  89. cy.visitChartByParams({
  90. ...VIZ_DEFAULTS,
  91. metrics: [NUM_METRIC, MAX_DS],
  92. groupby: ['name'],
  93. });
  94. cy.verifySliceSuccess({
  95. waitAlias: '@chartData',
  96. querySubstring: /GROUP BY.*name/i,
  97. chartSelector: 'table',
  98. });
  99. });
  100. it('Test table with groupby + time column', () => {
  101. cy.visitChartByParams({
  102. ...VIZ_DEFAULTS,
  103. include_time: true,
  104. granularity_sqla: 'ds',
  105. time_grain_sqla: 'P3M',
  106. metrics: [NUM_METRIC, MAX_DS],
  107. groupby: ['name'],
  108. });
  109. cy.wait('@chartData').then(({ response }) => {
  110. cy.verifySliceContainer('table');
  111. const records = response?.body.result[0].data;
  112. // should sort by first metric when no sort by metric is set
  113. expect(records[0][NUM_METRIC.label]).greaterThan(
  114. records[1][NUM_METRIC.label],
  115. );
  116. });
  117. // should handle frontend sorting correctly
  118. cy.get('.chart-container th').contains('name').click();
  119. cy.get('.chart-container td:nth-child(2):eq(0)').contains('Adam');
  120. cy.get('.chart-container th').contains('ds').click();
  121. cy.get('.chart-container th').contains('ds').click();
  122. cy.get('.chart-container td:nth-child(1):eq(0)').contains('2008');
  123. });
  124. it('Test table with percent metrics and groupby', () => {
  125. cy.visitChartByParams({
  126. ...VIZ_DEFAULTS,
  127. percent_metrics: PERCENT_METRIC,
  128. metrics: [],
  129. groupby: ['name'],
  130. });
  131. cy.verifySliceSuccess({ waitAlias: '@chartData', chartSelector: 'table' });
  132. });
  133. it('Test table with groupby order desc', () => {
  134. cy.visitChartByParams({
  135. ...VIZ_DEFAULTS,
  136. metrics: NUM_METRIC,
  137. groupby: ['name'],
  138. order_desc: true,
  139. });
  140. cy.verifySliceSuccess({ waitAlias: '@chartData', chartSelector: 'table' });
  141. });
  142. it('Test table with groupby + order by + no metric', () => {
  143. cy.visitChartByParams({
  144. ...VIZ_DEFAULTS,
  145. metrics: [],
  146. groupby: ['name'],
  147. timeseries_limit_metric: NUM_METRIC,
  148. order_desc: true,
  149. });
  150. // should contain only the group by column
  151. cy.get('.chart-container th').its('length').should('eq', 1);
  152. // should order correctly
  153. cy.get('.chart-container td:eq(0)').contains('Michael');
  154. cy.verifySliceSuccess({ waitAlias: '@chartData', chartSelector: 'table' });
  155. });
  156. it('Test table with groupby and limit', () => {
  157. const limit = 10;
  158. const formData = {
  159. ...VIZ_DEFAULTS,
  160. metrics: NUM_METRIC,
  161. groupby: ['name'],
  162. row_limit: limit,
  163. };
  164. cy.visitChartByParams(formData);
  165. cy.wait('@chartData').then(({ response }) => {
  166. cy.verifySliceContainer('table');
  167. expect(response?.body.result[0].data.length).to.eq(limit);
  168. });
  169. cy.get('[data-test="row-count-label"]').contains('10 rows');
  170. });
  171. it('Test table with columns and row limit', () => {
  172. cy.visitChartByParams({
  173. ...VIZ_DEFAULTS,
  174. // should still work when query_mode is not-set/invalid
  175. query_mode: undefined,
  176. all_columns: ['state'],
  177. metrics: [],
  178. row_limit: 100,
  179. });
  180. // should display in raw records mode
  181. cy.get(
  182. 'div[data-test="query_mode"] .ant-radio-button-wrapper-checked',
  183. ).contains('Raw records');
  184. cy.get('div[data-test="all_columns"]').should('be.visible');
  185. cy.get('div[data-test="groupby"]').should('not.exist');
  186. cy.verifySliceSuccess({ waitAlias: '@chartData', chartSelector: 'table' });
  187. cy.get('[data-test="row-count-label"]').contains('100 rows');
  188. // should allow switch back to aggregate mode
  189. cy.get('div[data-test="query_mode"] .ant-radio-button-wrapper')
  190. .contains('Aggregate')
  191. .click();
  192. cy.get(
  193. 'div[data-test="query_mode"] .ant-radio-button-wrapper-checked',
  194. ).contains('Aggregate');
  195. cy.get('div[data-test="all_columns"]').should('not.exist');
  196. cy.get('div[data-test="groupby"]').should('be.visible');
  197. });
  198. it('Test table with columns, ordering, and row limit', () => {
  199. const limit = 10;
  200. const formData = {
  201. ...VIZ_DEFAULTS,
  202. query_mode: 'raw',
  203. all_columns: ['name', 'state', 'ds', 'num'],
  204. metrics: [],
  205. row_limit: limit,
  206. order_by_cols: ['["num", false]'],
  207. };
  208. cy.visitChartByParams(formData);
  209. cy.wait('@chartData').then(({ response }) => {
  210. cy.verifySliceContainer('table');
  211. const records = response?.body.result[0].data;
  212. expect(records[0].num).greaterThan(records[records.length - 1].num);
  213. });
  214. });
  215. it('Test table with simple filter', () => {
  216. const metrics = ['count'];
  217. const filters = [SIMPLE_FILTER];
  218. const formData = { ...VIZ_DEFAULTS, metrics, adhoc_filters: filters };
  219. cy.visitChartByParams(formData);
  220. cy.verifySliceSuccess({ waitAlias: '@chartData', chartSelector: 'table' });
  221. });
  222. it('Tests table number formatting with % in metric name', () => {
  223. const formData = {
  224. ...VIZ_DEFAULTS,
  225. percent_metrics: PERCENT_METRIC,
  226. groupby: ['state'],
  227. };
  228. cy.visitChartByParams(formData);
  229. cy.verifySliceSuccess({
  230. waitAlias: '@chartData',
  231. querySubstring: /GROUP BY.*state/i,
  232. chartSelector: 'table',
  233. });
  234. cy.get('td').contains(/\d*%/);
  235. });
  236. it('Test row limit with server pagination toggle', () => {
  237. const serverPaginationSelector =
  238. '[data-test="server_pagination-header"] div.pull-left [type="checkbox"]';
  239. cy.visitChartByParams({
  240. ...VIZ_DEFAULTS,
  241. metrics: ['count'],
  242. row_limit: 100,
  243. });
  244. // Enable server pagination
  245. cy.get(serverPaginationSelector).click();
  246. // Click row limit control and select high value (200k)
  247. cy.get('div[aria-label="Row limit"]').click();
  248. // Type 200000 and press enter to select the option
  249. cy.get('div[aria-label="Row limit"]')
  250. .find('.ant-select-selection-search-input:visible')
  251. .type('200000{enter}');
  252. // Verify that there is no error tooltip when server pagination is enabled
  253. cy.get('[data-test="error-tooltip"]').should('not.exist');
  254. // Disable server pagination
  255. cy.get(serverPaginationSelector).click();
  256. // Verify error tooltip appears
  257. cy.get('[data-test="error-tooltip"]').should('be.visible');
  258. // Trigger mouseover and verify tooltip text
  259. cy.get('[data-test="error-tooltip"]').trigger('mouseover');
  260. // Verify tooltip content
  261. cy.get('.ant-tooltip-inner').should('be.visible');
  262. cy.get('.ant-tooltip-inner').should(
  263. 'contain',
  264. 'Server pagination needs to be enabled for values over',
  265. );
  266. // Hide the tooltip by adding display:none style
  267. cy.get('.ant-tooltip').invoke('attr', 'style', 'display: none');
  268. // Enable server pagination again
  269. cy.get(serverPaginationSelector).click();
  270. cy.get('[data-test="error-tooltip"]').should('not.exist');
  271. cy.get('div[aria-label="Row limit"]').click();
  272. // Type 1000000
  273. cy.get('div[aria-label="Row limit"]')
  274. .find('.ant-select-selection-search-input:visible')
  275. .type('1000000');
  276. // Wait for 1 second
  277. cy.wait(1000);
  278. // Press enter
  279. cy.get('div[aria-label="Row limit"]')
  280. .find('.ant-select-selection-search-input:visible')
  281. .type('{enter}');
  282. // Wait for error tooltip to appear and verify its content
  283. cy.get('[data-test="error-tooltip"]')
  284. .should('be.visible')
  285. .trigger('mouseover');
  286. // Wait for tooltip content and verify
  287. cy.get('.ant-tooltip-inner').should('exist');
  288. cy.get('.ant-tooltip-inner').should('be.visible');
  289. // Verify tooltip content separately
  290. cy.get('.ant-tooltip-inner').should('contain', 'Value cannot exceed');
  291. });
  292. it('Test sorting with server pagination enabled', () => {
  293. cy.visitChartByParams({
  294. ...VIZ_DEFAULTS,
  295. metrics: ['count'],
  296. groupby: ['name'],
  297. row_limit: 100000,
  298. server_pagination: true, // Enable server pagination
  299. });
  300. // Wait for the initial data load
  301. cy.wait('@chartData');
  302. // Get the first column header (name)
  303. cy.get('.chart-container th').contains('name').as('nameHeader');
  304. // Click to sort ascending
  305. cy.get('@nameHeader').click();
  306. cy.wait('@chartData');
  307. // Verify first row starts with 'A'
  308. cy.get('.chart-container td:first').invoke('text').should('match', /^[Aa]/);
  309. // Click again to sort descending
  310. cy.get('@nameHeader').click();
  311. cy.wait('@chartData');
  312. // Verify first row starts with 'Z'
  313. cy.get('.chart-container td:first').invoke('text').should('match', /^[Zz]/);
  314. // Test numeric sorting
  315. cy.get('.chart-container th').contains('COUNT').as('countHeader');
  316. // Click to sort ascending by count
  317. cy.get('@countHeader').click();
  318. cy.wait('@chartData');
  319. // Get first two count values and verify ascending order
  320. cy.get('.chart-container td:nth-child(2)').then($cells => {
  321. const first = parseFloat($cells[0].textContent || '0');
  322. const second = parseFloat($cells[1].textContent || '0');
  323. expect(first).to.be.at.most(second);
  324. });
  325. // Click again to sort descending
  326. cy.get('@countHeader').click();
  327. cy.wait('@chartData');
  328. // Get first two count values and verify descending order
  329. cy.get('.chart-container td:nth-child(2)').then($cells => {
  330. const first = parseFloat($cells[0].textContent || '0');
  331. const second = parseFloat($cells[1].textContent || '0');
  332. expect(first).to.be.at.least(second);
  333. });
  334. });
  335. it('Test search with server pagination enabled', () => {
  336. cy.visitChartByParams({
  337. ...VIZ_DEFAULTS,
  338. metrics: ['count'],
  339. groupby: ['name', 'state'],
  340. row_limit: 100000,
  341. server_pagination: true,
  342. include_search: true,
  343. });
  344. cy.wait('@chartData');
  345. const searchInputSelector = '.dt-global-filter input';
  346. // Basic search test
  347. cy.get(searchInputSelector).should('be.visible');
  348. cy.get(searchInputSelector).type('John');
  349. cy.wait('@chartData');
  350. cy.get('.chart-container tbody tr').each($row => {
  351. cy.wrap($row).contains(/John/i);
  352. });
  353. // Clear and test case-insensitive search
  354. cy.get(searchInputSelector).clear();
  355. cy.wait('@chartData');
  356. cy.get(searchInputSelector).type('mary');
  357. cy.wait('@chartData');
  358. cy.get('.chart-container tbody tr').each($row => {
  359. cy.wrap($row).contains(/Mary/i);
  360. });
  361. // Test special characters
  362. cy.get(searchInputSelector).clear();
  363. cy.get(searchInputSelector).type('Nicole');
  364. cy.wait('@chartData');
  365. cy.get('.chart-container tbody tr').each($row => {
  366. cy.wrap($row).contains(/Nicole/i);
  367. });
  368. // Test no results
  369. cy.get(searchInputSelector).clear();
  370. cy.get(searchInputSelector).type('XYZ123');
  371. cy.wait('@chartData');
  372. cy.get('.chart-container').contains('No records found');
  373. // Test column-specific search
  374. cy.get('.search-select').should('be.visible');
  375. cy.get('.search-select').click();
  376. cy.get('.ant-select-dropdown').should('be.visible');
  377. cy.get('.ant-select-item-option').contains('state').should('be.visible');
  378. cy.get('.ant-select-item-option').contains('state').click();
  379. cy.get(searchInputSelector).clear();
  380. cy.get(searchInputSelector).type('CA');
  381. cy.wait('@chartData');
  382. cy.wait(1000);
  383. cy.get('td[aria-labelledby="header-state"]').should('be.visible');
  384. cy.get('td[aria-labelledby="header-state"]')
  385. .first()
  386. .should('contain', 'CA');
  387. });
  388. });