datasource_specs.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 { PostgresDatasource } from '../datasource';
  11. import { CustomVariable } from 'app/features/templating/custom_variable';
  12. describe('PostgreSQLDatasource', function() {
  13. var ctx = new helpers.ServiceTestContext();
  14. var instanceSettings = { name: 'postgresql' };
  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(PostgresDatasource, {
  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, title, 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: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
  49. rows: [
  50. [1432288355, 'some text', 'TagA,TagB'],
  51. [1432288390, 'some text2', ' TagB , TagC'],
  52. [1432288400, 'some text3'],
  53. ],
  54. },
  55. ],
  56. },
  57. },
  58. };
  59. beforeEach(function() {
  60. ctx.backendSrv.datasourceRequest = function(options) {
  61. return ctx.$q.when({ data: response, status: 200 });
  62. };
  63. ctx.ds.annotationQuery(options).then(function(data) {
  64. results = data;
  65. });
  66. ctx.$rootScope.$apply();
  67. });
  68. it('should return annotation list', function() {
  69. expect(results.length).to.be(3);
  70. expect(results[0].text).to.be('some text');
  71. expect(results[0].tags[0]).to.be('TagA');
  72. expect(results[0].tags[1]).to.be('TagB');
  73. expect(results[1].tags[0]).to.be('TagB');
  74. expect(results[1].tags[1]).to.be('TagC');
  75. expect(results[2].tags.length).to.be(0);
  76. });
  77. });
  78. describe('When performing metricFindQuery', function() {
  79. let results;
  80. const query = 'select * from atable';
  81. const response = {
  82. results: {
  83. tempvar: {
  84. meta: {
  85. rowCount: 3,
  86. },
  87. refId: 'tempvar',
  88. tables: [
  89. {
  90. columns: [{ text: 'title' }, { text: 'text' }],
  91. rows: [
  92. ['aTitle', 'some text'],
  93. ['aTitle2', 'some text2'],
  94. ['aTitle3', 'some text3'],
  95. ],
  96. },
  97. ],
  98. },
  99. },
  100. };
  101. beforeEach(function() {
  102. ctx.backendSrv.datasourceRequest = function(options) {
  103. return ctx.$q.when({ data: response, status: 200 });
  104. };
  105. ctx.ds.metricFindQuery(query).then(function(data) {
  106. results = data;
  107. });
  108. ctx.$rootScope.$apply();
  109. });
  110. it('should return list of all column values', function() {
  111. expect(results.length).to.be(6);
  112. expect(results[0].text).to.be('aTitle');
  113. expect(results[5].text).to.be('some text3');
  114. });
  115. });
  116. describe('When performing metricFindQuery with key, value columns', function() {
  117. let results;
  118. const query = 'select * from atable';
  119. const response = {
  120. results: {
  121. tempvar: {
  122. meta: {
  123. rowCount: 3,
  124. },
  125. refId: 'tempvar',
  126. tables: [
  127. {
  128. columns: [{ text: '__value' }, { text: '__text' }],
  129. rows: [
  130. ['value1', 'aTitle'],
  131. ['value2', 'aTitle2'],
  132. ['value3', 'aTitle3'],
  133. ],
  134. },
  135. ],
  136. },
  137. },
  138. };
  139. beforeEach(function() {
  140. ctx.backendSrv.datasourceRequest = function(options) {
  141. return ctx.$q.when({ data: response, status: 200 });
  142. };
  143. ctx.ds.metricFindQuery(query).then(function(data) {
  144. results = data;
  145. });
  146. ctx.$rootScope.$apply();
  147. });
  148. it('should return list of as text, value', function() {
  149. expect(results.length).to.be(3);
  150. expect(results[0].text).to.be('aTitle');
  151. expect(results[0].value).to.be('value1');
  152. expect(results[2].text).to.be('aTitle3');
  153. expect(results[2].value).to.be('value3');
  154. });
  155. });
  156. describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() {
  157. let results;
  158. const query = 'select * from atable';
  159. const response = {
  160. results: {
  161. tempvar: {
  162. meta: {
  163. rowCount: 3,
  164. },
  165. refId: 'tempvar',
  166. tables: [
  167. {
  168. columns: [{ text: '__text' }, { text: '__value' }],
  169. rows: [
  170. ['aTitle', 'same'],
  171. ['aTitle', 'same'],
  172. ['aTitle', 'diff'],
  173. ],
  174. },
  175. ],
  176. },
  177. },
  178. };
  179. beforeEach(function() {
  180. ctx.backendSrv.datasourceRequest = function(options) {
  181. return ctx.$q.when({ data: response, status: 200 });
  182. };
  183. ctx.ds.metricFindQuery(query).then(function(data) {
  184. results = data;
  185. });
  186. ctx.$rootScope.$apply();
  187. });
  188. it('should return list of unique keys', function() {
  189. expect(results.length).to.be(1);
  190. expect(results[0].text).to.be('aTitle');
  191. expect(results[0].value).to.be('same');
  192. });
  193. });
  194. describe('When interpolating variables', () => {
  195. beforeEach(function() {
  196. ctx.variable = new CustomVariable({}, {});
  197. });
  198. describe('and value is a string', () => {
  199. it('should return an unquoted value', () => {
  200. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql('abc');
  201. });
  202. });
  203. describe('and value is a number', () => {
  204. it('should return an unquoted value', () => {
  205. expect(ctx.ds.interpolateVariable(1000, ctx.variable)).to.eql(1000);
  206. });
  207. });
  208. describe('and value is an array of strings', () => {
  209. it('should return comma separated quoted values', () => {
  210. expect(
  211. ctx.ds.interpolateVariable(['a', 'b', 'c'], ctx.variable)
  212. ).to.eql("'a','b','c'");
  213. });
  214. });
  215. describe('and variable allows multi-value and is a string', () => {
  216. it('should return a quoted value', () => {
  217. ctx.variable.multi = true;
  218. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql("'abc'");
  219. });
  220. });
  221. describe('and variable allows all and is a string', () => {
  222. it('should return a quoted value', () => {
  223. ctx.variable.includeAll = true;
  224. expect(ctx.ds.interpolateVariable('abc', ctx.variable)).to.eql("'abc'");
  225. });
  226. });
  227. });
  228. });