datasource_specs.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {describe, beforeEach, it, sinon, 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, title, 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: 'title'}, {text: 'text'}, {text: 'tags'}],
  38. rows: [
  39. [1432288355, 'aTitle', 'some text', 'TagA,TagB'],
  40. [1432288390, 'aTitle2', 'some text2', ' TagB , TagC'],
  41. [1432288400, 'aTitle3', '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].title).to.be('aTitle');
  58. expect(results[0].text).to.be('some text');
  59. expect(results[0].tags[0]).to.be('TagA');
  60. expect(results[0].tags[1]).to.be('TagB');
  61. expect(results[1].tags[0]).to.be('TagB');
  62. expect(results[1].tags[1]).to.be('TagC');
  63. expect(results[2].tags.length).to.be(0);
  64. });
  65. });
  66. describe('When performing metricFindQuery', function() {
  67. let results;
  68. const query = 'select * from atable';
  69. const response = {
  70. results: {
  71. tempvar: {
  72. meta: {
  73. rowCount: 3
  74. },
  75. refId: 'tempvar',
  76. tables: [
  77. {
  78. columns: [{text: 'title'}, {text: 'text'}],
  79. rows: [
  80. ['aTitle', 'some text'],
  81. ['aTitle2', 'some text2'],
  82. ['aTitle3', 'some text3']
  83. ]
  84. }
  85. ]
  86. }
  87. }
  88. };
  89. beforeEach(function() {
  90. ctx.backendSrv.datasourceRequest = function(options) {
  91. return ctx.$q.when({data: response, status: 200});
  92. };
  93. ctx.ds.metricFindQuery(query).then(function(data) { results = data; });
  94. ctx.$rootScope.$apply();
  95. });
  96. it('should return list of all column values', function() {
  97. expect(results.length).to.be(6);
  98. expect(results[0].text).to.be('aTitle');
  99. expect(results[5].text).to.be('some text3');
  100. });
  101. });
  102. describe('When performing metricFindQuery with key, value columns', function() {
  103. let results;
  104. const query = 'select * from atable';
  105. const response = {
  106. results: {
  107. tempvar: {
  108. meta: {
  109. rowCount: 3
  110. },
  111. refId: 'tempvar',
  112. tables: [
  113. {
  114. columns: [{text: '__value'}, {text: '__text'}],
  115. rows: [
  116. ['value1', 'aTitle'],
  117. ['value2', 'aTitle2'],
  118. ['value3', 'aTitle3']
  119. ]
  120. }
  121. ]
  122. }
  123. }
  124. };
  125. beforeEach(function() {
  126. ctx.backendSrv.datasourceRequest = function(options) {
  127. return ctx.$q.when({data: response, status: 200});
  128. };
  129. ctx.ds.metricFindQuery(query).then(function(data) { results = data; });
  130. ctx.$rootScope.$apply();
  131. });
  132. it('should return list of as text, value', function() {
  133. expect(results.length).to.be(3);
  134. expect(results[0].text).to.be('aTitle');
  135. expect(results[0].value).to.be('value1');
  136. expect(results[2].text).to.be('aTitle3');
  137. expect(results[2].value).to.be('value3');
  138. });
  139. });
  140. describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() {
  141. let results;
  142. const query = 'select * from atable';
  143. const response = {
  144. results: {
  145. tempvar: {
  146. meta: {
  147. rowCount: 3
  148. },
  149. refId: 'tempvar',
  150. tables: [
  151. {
  152. columns: [{text: '__text'}, {text: '__value'}],
  153. rows: [
  154. ['aTitle', 'same'],
  155. ['aTitle', 'same'],
  156. ['aTitle', 'diff']
  157. ]
  158. }
  159. ]
  160. }
  161. }
  162. };
  163. beforeEach(function() {
  164. ctx.backendSrv.datasourceRequest = function(options) {
  165. return ctx.$q.when({data: response, status: 200});
  166. };
  167. ctx.ds.metricFindQuery(query).then(function(data) { results = data; });
  168. ctx.$rootScope.$apply();
  169. });
  170. it('should return list of unique keys', function() {
  171. expect(results.length).to.be(1);
  172. expect(results[0].text).to.be('aTitle');
  173. expect(results[0].value).to.be('same');
  174. });
  175. });
  176. });