datasource_specs.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import {
  2. describe,
  3. beforeEach,
  4. it,
  5. expect,
  6. angularMocks,
  7. } from 'test/lib/common';
  8. import moment from 'moment';
  9. import helpers from 'test/specs/helpers';
  10. import { MysqlDatasource } from '../datasource';
  11. import { CustomVariable } from 'app/features/templating/custom_variable';
  12. describe('MySQLDatasource', function() {
  13. var ctx = new helpers.ServiceTestContext();
  14. var instanceSettings = { name: 'mysql' };
  15. beforeEach(angularMocks.module('grafana.core'));
  16. beforeEach(angularMocks.module('grafana.services'));
  17. beforeEach(ctx.providePhase(['backendSrv']));
  18. beforeEach(
  19. angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  20. ctx.$q = $q;
  21. ctx.$httpBackend = $httpBackend;
  22. ctx.$rootScope = $rootScope;
  23. ctx.ds = $injector.instantiate(MysqlDatasource, {
  24. instanceSettings: instanceSettings,
  25. });
  26. $httpBackend.when('GET', /\.html$/).respond('');
  27. })
  28. );
  29. describe('When performing annotationQuery', function() {
  30. let results;
  31. const annotationName = 'MyAnno';
  32. const options = {
  33. annotation: {
  34. name: annotationName,
  35. rawQuery: 'select time_sec, text, tags from table;',
  36. },
  37. range: {
  38. from: moment(1432288354),
  39. to: moment(1432288401),
  40. },
  41. };
  42. const response = {
  43. results: {
  44. MyAnno: {
  45. refId: annotationName,
  46. tables: [
  47. {
  48. columns: [
  49. { text: 'time_sec' },
  50. { text: 'text' },
  51. { text: 'tags' },
  52. ],
  53. rows: [
  54. [1432288355, 'some text', 'TagA,TagB'],
  55. [1432288390, 'some text2', ' TagB , TagC'],
  56. [1432288400, 'some text3'],
  57. ],
  58. },
  59. ],
  60. },
  61. },
  62. };
  63. beforeEach(function() {
  64. ctx.backendSrv.datasourceRequest = function(options) {
  65. return ctx.$q.when({ data: response, status: 200 });
  66. };
  67. ctx.ds.annotationQuery(options).then(function(data) {
  68. results = data;
  69. });
  70. ctx.$rootScope.$apply();
  71. });
  72. it('should return annotation list', function() {
  73. expect(results.length).to.be(3);
  74. expect(results[0].text).to.be('some text');
  75. expect(results[0].tags[0]).to.be('TagA');
  76. expect(results[0].tags[1]).to.be('TagB');
  77. expect(results[1].tags[0]).to.be('TagB');
  78. expect(results[1].tags[1]).to.be('TagC');
  79. expect(results[2].tags.length).to.be(0);
  80. });
  81. });
  82. describe('When performing metricFindQuery', function() {
  83. let results;
  84. const query = 'select * from atable';
  85. const response = {
  86. results: {
  87. tempvar: {
  88. meta: {
  89. rowCount: 3,
  90. },
  91. refId: 'tempvar',
  92. tables: [
  93. {
  94. columns: [{ text: 'title' }, { text: 'text' }],
  95. rows: [
  96. ['aTitle', 'some text'],
  97. ['aTitle2', 'some text2'],
  98. ['aTitle3', 'some text3'],
  99. ],
  100. },
  101. ],
  102. },
  103. },
  104. };
  105. beforeEach(function() {
  106. ctx.backendSrv.datasourceRequest = function(options) {
  107. return ctx.$q.when({ data: response, status: 200 });
  108. };
  109. ctx.ds.metricFindQuery(query).then(function(data) {
  110. results = data;
  111. });
  112. ctx.$rootScope.$apply();
  113. });
  114. it('should return list of all column values', function() {
  115. expect(results.length).to.be(6);
  116. expect(results[0].text).to.be('aTitle');
  117. expect(results[5].text).to.be('some text3');
  118. });
  119. });
  120. describe('When performing metricFindQuery with key, value columns', function() {
  121. let results;
  122. const query = 'select * from atable';
  123. const response = {
  124. results: {
  125. tempvar: {
  126. meta: {
  127. rowCount: 3,
  128. },
  129. refId: 'tempvar',
  130. tables: [
  131. {
  132. columns: [{ text: '__value' }, { text: '__text' }],
  133. rows: [
  134. ['value1', 'aTitle'],
  135. ['value2', 'aTitle2'],
  136. ['value3', 'aTitle3'],
  137. ],
  138. },
  139. ],
  140. },
  141. },
  142. };
  143. beforeEach(function() {
  144. ctx.backendSrv.datasourceRequest = function(options) {
  145. return ctx.$q.when({ data: response, status: 200 });
  146. };
  147. ctx.ds.metricFindQuery(query).then(function(data) {
  148. results = data;
  149. });
  150. ctx.$rootScope.$apply();
  151. });
  152. it('should return list of as text, value', function() {
  153. expect(results.length).to.be(3);
  154. expect(results[0].text).to.be('aTitle');
  155. expect(results[0].value).to.be('value1');
  156. expect(results[2].text).to.be('aTitle3');
  157. expect(results[2].value).to.be('value3');
  158. });
  159. });
  160. describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() {
  161. let results;
  162. const query = 'select * from atable';
  163. const response = {
  164. results: {
  165. tempvar: {
  166. meta: {
  167. rowCount: 3,
  168. },
  169. refId: 'tempvar',
  170. tables: [
  171. {
  172. columns: [{ text: '__text' }, { text: '__value' }],
  173. rows: [
  174. ['aTitle', 'same'],
  175. ['aTitle', 'same'],
  176. ['aTitle', 'diff'],
  177. ],
  178. },
  179. ],
  180. },
  181. },
  182. };
  183. beforeEach(function() {
  184. ctx.backendSrv.datasourceRequest = function(options) {
  185. return ctx.$q.when({ data: response, status: 200 });
  186. };
  187. ctx.ds.metricFindQuery(query).then(function(data) {
  188. results = data;
  189. });
  190. ctx.$rootScope.$apply();
  191. });
  192. it('should return list of unique keys', function() {
  193. expect(results.length).to.be(1);
  194. expect(results[0].text).to.be('aTitle');
  195. expect(results[0].value).to.be('same');
  196. });
  197. });
  198. describe('When interpolating variables', () => {
  199. beforeEach(function() {
  200. ctx.variable = new CustomVariable({}, {});
  201. });
  202. describe('and value is a string', () => {
  203. it('should return an unquoted value', () => {
  204. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('abc');
  205. });
  206. });
  207. describe('and value is a number', () => {
  208. it('should return an unquoted value', () => {
  209. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).to.eql(1000);
  210. });
  211. });
  212. describe('and value is an array of strings', () => {
  213. it('should return comma separated quoted values', () => {
  214. expect(
  215. ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)
  216. ).to.eql("'a','b','c'");
  217. });
  218. });
  219. describe('and variable allows multi-value and value is a string', () => {
  220. it('should return a quoted value', () => {
  221. ctx.variable.multi = true;
  222. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql("'abc'");
  223. });
  224. });
  225. describe('and variable allows all and value is a string', () => {
  226. it('should return a quoted value', () => {
  227. ctx.variable.includeAll = true;
  228. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql("'abc'");
  229. });
  230. });
  231. });
  232. });