datasource_specs.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. import {CustomVariable} from 'app/features/templating/custom_variable';
  6. describe('MySQLDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. var instanceSettings = {name: 'mysql'};
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(ctx.providePhase(['backendSrv']));
  12. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  13. ctx.$q = $q;
  14. ctx.$httpBackend = $httpBackend;
  15. ctx.$rootScope = $rootScope;
  16. ctx.ds = $injector.instantiate(MysqlDatasource, {instanceSettings: instanceSettings});
  17. $httpBackend.when('GET', /\.html$/).respond('');
  18. }));
  19. describe('When performing annotationQuery', function() {
  20. let results;
  21. const annotationName = 'MyAnno';
  22. const options = {
  23. annotation: {
  24. name: annotationName,
  25. rawQuery: 'select time_sec, text, tags from table;'
  26. },
  27. range: {
  28. from: moment(1432288354),
  29. to: moment(1432288401)
  30. }
  31. };
  32. const response = {
  33. results: {
  34. MyAnno: {
  35. refId: annotationName,
  36. tables: [
  37. {
  38. columns: [{text: 'time_sec'}, {text: 'text'}, {text: 'tags'}],
  39. rows: [
  40. [1432288355, 'some text', 'TagA,TagB'],
  41. [1432288390, 'some text2', ' TagB , TagC'],
  42. [1432288400, 'some text3']
  43. ]
  44. }
  45. ]
  46. }
  47. }
  48. };
  49. beforeEach(function() {
  50. ctx.backendSrv.datasourceRequest = function(options) {
  51. return ctx.$q.when({data: response, status: 200});
  52. };
  53. ctx.ds.annotationQuery(options).then(function(data) { results = data; });
  54. ctx.$rootScope.$apply();
  55. });
  56. it('should return annotation list', function() {
  57. expect(results.length).to.be(3);
  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. describe('When interpolating variables', () => {
  177. beforeEach(function() {
  178. ctx.variable = new CustomVariable({},{});
  179. });
  180. describe('and value is a string', () => {
  181. it('should return an unquoted value', () => {
  182. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('abc');
  183. });
  184. });
  185. describe('and value is a number', () => {
  186. it('should return an unquoted value', () => {
  187. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).to.eql(1000);
  188. });
  189. });
  190. describe('and value is an array of strings', () => {
  191. it('should return comma separated quoted values', () => {
  192. expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).to.eql('\'a\',\'b\',\'c\'');
  193. });
  194. });
  195. describe('and variable allows multi-value and value is a string', () => {
  196. it('should return a quoted value', () => {
  197. ctx.variable.multi = true;
  198. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('\'abc\'');
  199. });
  200. });
  201. describe('and variable allows all and value is a string', () => {
  202. it('should return a quoted value', () => {
  203. ctx.variable.includeAll = true;
  204. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('\'abc\'');
  205. });
  206. });
  207. });
  208. });