datasource_specs.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 { MssqlDatasource } from '../datasource';
  5. import { CustomVariable } from 'app/features/templating/custom_variable';
  6. describe('MSSQLDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. var instanceSettings = { name: 'mssql' };
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.services'));
  11. beforeEach(ctx.providePhase(['backendSrv']));
  12. beforeEach(
  13. angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  14. ctx.$q = $q;
  15. ctx.$httpBackend = $httpBackend;
  16. ctx.$rootScope = $rootScope;
  17. ctx.ds = $injector.instantiate(MssqlDatasource, { instanceSettings: instanceSettings });
  18. $httpBackend.when('GET', /\.html$/).respond('');
  19. })
  20. );
  21. describe('When performing annotationQuery', function() {
  22. let results;
  23. const annotationName = 'MyAnno';
  24. const options = {
  25. annotation: {
  26. name: annotationName,
  27. rawQuery: 'select time, text, tags from table;',
  28. },
  29. range: {
  30. from: moment(1432288354),
  31. to: moment(1432288401),
  32. },
  33. };
  34. const response = {
  35. results: {
  36. MyAnno: {
  37. refId: annotationName,
  38. tables: [
  39. {
  40. columns: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
  41. rows: [
  42. [1521546171129, 'some text', 'TagA,TagB'],
  43. [1521546531404, 'some text2', ' TagB , TagC'],
  44. [1521546901702, 'some text3'],
  45. ],
  46. },
  47. ],
  48. },
  49. },
  50. };
  51. beforeEach(function() {
  52. ctx.backendSrv.datasourceRequest = function(options) {
  53. return ctx.$q.when({ data: response, status: 200 });
  54. };
  55. ctx.ds.annotationQuery(options).then(function(data) {
  56. results = data;
  57. });
  58. ctx.$rootScope.$apply();
  59. });
  60. it('should return annotation list', function() {
  61. expect(results.length).to.be(3);
  62. expect(results[0].text).to.be('some text');
  63. expect(results[0].tags[0]).to.be('TagA');
  64. expect(results[0].tags[1]).to.be('TagB');
  65. expect(results[1].tags[0]).to.be('TagB');
  66. expect(results[1].tags[1]).to.be('TagC');
  67. expect(results[2].tags.length).to.be(0);
  68. });
  69. });
  70. describe('When performing metricFindQuery', function() {
  71. let results;
  72. const query = 'select * from atable';
  73. const response = {
  74. results: {
  75. tempvar: {
  76. meta: {
  77. rowCount: 3,
  78. },
  79. refId: 'tempvar',
  80. tables: [
  81. {
  82. columns: [{ text: 'title' }, { text: 'text' }],
  83. rows: [['aTitle', 'some text'], ['aTitle2', 'some text2'], ['aTitle3', 'some text3']],
  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) {
  94. results = data;
  95. });
  96. ctx.$rootScope.$apply();
  97. });
  98. it('should return list of all column values', function() {
  99. expect(results.length).to.be(6);
  100. expect(results[0].text).to.be('aTitle');
  101. expect(results[5].text).to.be('some text3');
  102. });
  103. });
  104. describe('When performing metricFindQuery with key, value columns', function() {
  105. let results;
  106. const query = 'select * from atable';
  107. const response = {
  108. results: {
  109. tempvar: {
  110. meta: {
  111. rowCount: 3,
  112. },
  113. refId: 'tempvar',
  114. tables: [
  115. {
  116. columns: [{ text: '__value' }, { text: '__text' }],
  117. rows: [['value1', 'aTitle'], ['value2', 'aTitle2'], ['value3', 'aTitle3']],
  118. },
  119. ],
  120. },
  121. },
  122. };
  123. beforeEach(function() {
  124. ctx.backendSrv.datasourceRequest = function(options) {
  125. return ctx.$q.when({ data: response, status: 200 });
  126. };
  127. ctx.ds.metricFindQuery(query).then(function(data) {
  128. results = data;
  129. });
  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: [['aTitle', 'same'], ['aTitle', 'same'], ['aTitle', 'diff']],
  154. },
  155. ],
  156. },
  157. },
  158. };
  159. beforeEach(function() {
  160. ctx.backendSrv.datasourceRequest = function(options) {
  161. return ctx.$q.when({ data: response, status: 200 });
  162. };
  163. ctx.ds.metricFindQuery(query).then(function(data) {
  164. results = data;
  165. });
  166. ctx.$rootScope.$apply();
  167. });
  168. it('should return list of unique keys', function() {
  169. expect(results.length).to.be(1);
  170. expect(results[0].text).to.be('aTitle');
  171. expect(results[0].value).to.be('same');
  172. });
  173. });
  174. describe('When interpolating variables', () => {
  175. beforeEach(function() {
  176. ctx.variable = new CustomVariable({}, {});
  177. });
  178. describe('and value is a string', () => {
  179. it('should return an unquoted value', () => {
  180. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('abc');
  181. });
  182. });
  183. describe('and value is a number', () => {
  184. it('should return an unquoted value', () => {
  185. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).to.eql(1000);
  186. });
  187. });
  188. describe('and value is an array of strings', () => {
  189. it('should return comma separated quoted values', () => {
  190. expect(ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)).to.eql("'a','b','c'");
  191. });
  192. });
  193. describe('and variable allows multi-value and value is a string', () => {
  194. it('should return a quoted value', () => {
  195. ctx.variable.multi = true;
  196. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql("'abc'");
  197. });
  198. });
  199. describe('and variable allows all and value is a string', () => {
  200. it('should return a quoted value', () => {
  201. ctx.variable.includeAll = true;
  202. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql("'abc'");
  203. });
  204. });
  205. });
  206. });