datasource_specs.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common';
  2. import moment from 'moment';
  3. import helpers from 'test/specs/helpers';
  4. import {MysqlDatasource} from '../datasource';
  5. describe('MySQLDatasource', function() {
  6. var ctx = new helpers.ServiceTestContext();
  7. var instanceSettings = {name: 'mysql'};
  8. beforeEach(angularMocks.module('grafana.core'));
  9. beforeEach(angularMocks.module('grafana.services'));
  10. beforeEach(ctx.providePhase(['backendSrv']));
  11. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  12. ctx.$q = $q;
  13. ctx.$httpBackend = $httpBackend;
  14. ctx.$rootScope = $rootScope;
  15. ctx.ds = $injector.instantiate(MysqlDatasource, {instanceSettings: instanceSettings});
  16. $httpBackend.when('GET', /\.html$/).respond('');
  17. }));
  18. describe('When performing annotationQuery', function() {
  19. let results;
  20. const annotationName = 'MyAnno';
  21. const options = {
  22. annotation: {
  23. name: annotationName,
  24. rawQuery: 'select time_sec, text, tags from table;'
  25. },
  26. range: {
  27. from: moment(1432288354),
  28. to: moment(1432288401)
  29. }
  30. };
  31. const response = {
  32. results: {
  33. MyAnno: {
  34. refId: annotationName,
  35. tables: [
  36. {
  37. columns: [{text: 'time_sec'}, {text: 'text'}, {text: 'tags'}],
  38. rows: [
  39. [1432288355, 'some text', 'TagA,TagB'],
  40. [1432288390, 'some text2', ' TagB , TagC'],
  41. [1432288400, 'some text3']
  42. ]
  43. }
  44. ]
  45. }
  46. }
  47. };
  48. beforeEach(function() {
  49. ctx.backendSrv.datasourceRequest = function(options) {
  50. return ctx.$q.when({data: response, status: 200});
  51. };
  52. ctx.ds.annotationQuery(options).then(function(data) { results = data; });
  53. ctx.$rootScope.$apply();
  54. });
  55. it('should return annotation list', function() {
  56. expect(results.length).to.be(3);
  57. expect(results[0].text).to.be('some text');
  58. expect(results[0].tags[0]).to.be('TagA');
  59. expect(results[0].tags[1]).to.be('TagB');
  60. expect(results[1].tags[0]).to.be('TagB');
  61. expect(results[1].tags[1]).to.be('TagC');
  62. expect(results[2].tags.length).to.be(0);
  63. });
  64. });
  65. describe('When performing metricFindQuery', function() {
  66. let results;
  67. const query = 'select * from atable';
  68. const response = {
  69. results: {
  70. tempvar: {
  71. meta: {
  72. rowCount: 3
  73. },
  74. refId: 'tempvar',
  75. tables: [
  76. {
  77. columns: [{text: 'title'}, {text: 'text'}],
  78. rows: [
  79. ['aTitle', 'some text'],
  80. ['aTitle2', 'some text2'],
  81. ['aTitle3', 'some text3']
  82. ]
  83. }
  84. ]
  85. }
  86. }
  87. };
  88. beforeEach(function() {
  89. ctx.backendSrv.datasourceRequest = function(options) {
  90. return ctx.$q.when({data: response, status: 200});
  91. };
  92. ctx.ds.metricFindQuery(query).then(function(data) { results = data; });
  93. ctx.$rootScope.$apply();
  94. });
  95. it('should return list of all column values', function() {
  96. expect(results.length).to.be(6);
  97. expect(results[0].text).to.be('aTitle');
  98. expect(results[5].text).to.be('some text3');
  99. });
  100. });
  101. describe('When performing metricFindQuery with key, value columns', function() {
  102. let results;
  103. const query = 'select * from atable';
  104. const response = {
  105. results: {
  106. tempvar: {
  107. meta: {
  108. rowCount: 3
  109. },
  110. refId: 'tempvar',
  111. tables: [
  112. {
  113. columns: [{text: '__value'}, {text: '__text'}],
  114. rows: [
  115. ['value1', 'aTitle'],
  116. ['value2', 'aTitle2'],
  117. ['value3', 'aTitle3']
  118. ]
  119. }
  120. ]
  121. }
  122. }
  123. };
  124. beforeEach(function() {
  125. ctx.backendSrv.datasourceRequest = function(options) {
  126. return ctx.$q.when({data: response, status: 200});
  127. };
  128. ctx.ds.metricFindQuery(query).then(function(data) { results = data; });
  129. ctx.$rootScope.$apply();
  130. });
  131. it('should return list of as text, value', function() {
  132. expect(results.length).to.be(3);
  133. expect(results[0].text).to.be('aTitle');
  134. expect(results[0].value).to.be('value1');
  135. expect(results[2].text).to.be('aTitle3');
  136. expect(results[2].value).to.be('value3');
  137. });
  138. });
  139. describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() {
  140. let results;
  141. const query = 'select * from atable';
  142. const response = {
  143. results: {
  144. tempvar: {
  145. meta: {
  146. rowCount: 3
  147. },
  148. refId: 'tempvar',
  149. tables: [
  150. {
  151. columns: [{text: '__text'}, {text: '__value'}],
  152. rows: [
  153. ['aTitle', 'same'],
  154. ['aTitle', 'same'],
  155. ['aTitle', 'diff']
  156. ]
  157. }
  158. ]
  159. }
  160. }
  161. };
  162. beforeEach(function() {
  163. ctx.backendSrv.datasourceRequest = function(options) {
  164. return ctx.$q.when({data: response, status: 200});
  165. };
  166. ctx.ds.metricFindQuery(query).then(function(data) { results = data; });
  167. ctx.$rootScope.$apply();
  168. });
  169. it('should return list of unique keys', function() {
  170. expect(results.length).to.be(1);
  171. expect(results[0].text).to.be('aTitle');
  172. expect(results[0].value).to.be('same');
  173. });
  174. });
  175. });