buildQuery.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 buildQuery from '../../src/Graph/buildQuery';
  20. describe('Graph buildQuery', () => {
  21. const formData = {
  22. datasource: '5__table',
  23. granularity_sqla: 'ds',
  24. source: 'dummy_source',
  25. target: 'dummy_target',
  26. metrics: ['foo', 'bar'],
  27. viz_type: 'my_chart',
  28. };
  29. it('should build groupby with source and target categories', () => {
  30. const formDataWithCategories = {
  31. ...formData,
  32. source: 'dummy_source',
  33. target: 'dummy_target',
  34. source_category: 'dummy_source_category',
  35. target_category: 'dummy_target_category',
  36. };
  37. const queryContext = buildQuery(formDataWithCategories);
  38. const [query] = queryContext.queries;
  39. expect(query.columns).toEqual([
  40. 'dummy_source',
  41. 'dummy_target',
  42. 'dummy_source_category',
  43. 'dummy_target_category',
  44. ]);
  45. expect(query.metrics).toEqual(['foo', 'bar']);
  46. });
  47. it('should build groupby with source category', () => {
  48. const formDataWithCategories = {
  49. ...formData,
  50. source: 'dummy_source',
  51. target: 'dummy_target',
  52. source_category: 'dummy_source_category',
  53. };
  54. const queryContext = buildQuery(formDataWithCategories);
  55. const [query] = queryContext.queries;
  56. expect(query.columns).toEqual([
  57. 'dummy_source',
  58. 'dummy_target',
  59. 'dummy_source_category',
  60. ]);
  61. expect(query.metrics).toEqual(['foo', 'bar']);
  62. });
  63. it('should build groupby with target category', () => {
  64. const formDataWithCategories = {
  65. ...formData,
  66. source: 'dummy_source',
  67. target: 'dummy_target',
  68. target_category: 'dummy_target_category',
  69. };
  70. const queryContext = buildQuery(formDataWithCategories);
  71. const [query] = queryContext.queries;
  72. expect(query.columns).toEqual([
  73. 'dummy_source',
  74. 'dummy_target',
  75. 'dummy_target_category',
  76. ]);
  77. expect(query.metrics).toEqual(['foo', 'bar']);
  78. });
  79. it('should build groupby without any category', () => {
  80. const formDataWithCategories = {
  81. ...formData,
  82. source: 'dummy_source',
  83. target: 'dummy_target',
  84. };
  85. const queryContext = buildQuery(formDataWithCategories);
  86. const [query] = queryContext.queries;
  87. expect(query.columns).toEqual(['dummy_source', 'dummy_target']);
  88. expect(query.metrics).toEqual(['foo', 'bar']);
  89. });
  90. });