datasource_specs.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 { PostgresDatasource } from '../datasource';
  5. import { CustomVariable } from 'app/features/templating/custom_variable';
  6. describe('PostgreSQLDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. var instanceSettings = { name: 'postgresql' };
  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(PostgresDatasource, {
  18. instanceSettings: instanceSettings,
  19. });
  20. $httpBackend.when('GET', /\.html$/).respond('');
  21. })
  22. );
  23. describe('When performing annotationQuery', function() {
  24. let results;
  25. const annotationName = 'MyAnno';
  26. const options = {
  27. annotation: {
  28. name: annotationName,
  29. rawQuery: 'select time, title, text, tags from table;',
  30. },
  31. range: {
  32. from: moment(1432288354),
  33. to: moment(1432288401),
  34. },
  35. };
  36. const response = {
  37. results: {
  38. MyAnno: {
  39. refId: annotationName,
  40. tables: [
  41. {
  42. columns: [{ text: 'time' }, { text: 'text' }, { text: 'tags' }],
  43. rows: [
  44. [1432288355, 'some text', 'TagA,TagB'],
  45. [1432288390, 'some text2', ' TagB , TagC'],
  46. [1432288400, 'some text3'],
  47. ],
  48. },
  49. ],
  50. },
  51. },
  52. };
  53. beforeEach(function() {
  54. ctx.backendSrv.datasourceRequest = function(options) {
  55. return ctx.$q.when({ data: response, status: 200 });
  56. };
  57. ctx.ds.annotationQuery(options).then(function(data) {
  58. results = data;
  59. });
  60. ctx.$rootScope.$apply();
  61. });
  62. it('should return annotation list', function() {
  63. expect(results.length).to.be(3);
  64. expect(results[0].text).to.be('some text');
  65. expect(results[0].tags[0]).to.be('TagA');
  66. expect(results[0].tags[1]).to.be('TagB');
  67. expect(results[1].tags[0]).to.be('TagB');
  68. expect(results[1].tags[1]).to.be('TagC');
  69. expect(results[2].tags.length).to.be(0);
  70. });
  71. });
  72. describe('When performing metricFindQuery', function() {
  73. let results;
  74. const query = 'select * from atable';
  75. const response = {
  76. results: {
  77. tempvar: {
  78. meta: {
  79. rowCount: 3,
  80. },
  81. refId: 'tempvar',
  82. tables: [
  83. {
  84. columns: [{ text: 'title' }, { text: 'text' }],
  85. rows: [['aTitle', 'some text'], ['aTitle2', 'some text2'], ['aTitle3', 'some text3']],
  86. },
  87. ],
  88. },
  89. },
  90. };
  91. beforeEach(function() {
  92. ctx.backendSrv.datasourceRequest = function(options) {
  93. return ctx.$q.when({ data: response, status: 200 });
  94. };
  95. ctx.ds.metricFindQuery(query).then(function(data) {
  96. results = data;
  97. });
  98. ctx.$rootScope.$apply();
  99. });
  100. it('should return list of all column values', function() {
  101. expect(results.length).to.be(6);
  102. expect(results[0].text).to.be('aTitle');
  103. expect(results[5].text).to.be('some text3');
  104. });
  105. });
  106. describe('When performing metricFindQuery with key, value columns', function() {
  107. let results;
  108. const query = 'select * from atable';
  109. const response = {
  110. results: {
  111. tempvar: {
  112. meta: {
  113. rowCount: 3,
  114. },
  115. refId: 'tempvar',
  116. tables: [
  117. {
  118. columns: [{ text: '__value' }, { text: '__text' }],
  119. rows: [['value1', 'aTitle'], ['value2', 'aTitle2'], ['value3', 'aTitle3']],
  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) {
  130. results = data;
  131. });
  132. ctx.$rootScope.$apply();
  133. });
  134. it('should return list of as text, value', function() {
  135. expect(results.length).to.be(3);
  136. expect(results[0].text).to.be('aTitle');
  137. expect(results[0].value).to.be('value1');
  138. expect(results[2].text).to.be('aTitle3');
  139. expect(results[2].value).to.be('value3');
  140. });
  141. });
  142. describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() {
  143. let results;
  144. const query = 'select * from atable';
  145. const response = {
  146. results: {
  147. tempvar: {
  148. meta: {
  149. rowCount: 3,
  150. },
  151. refId: 'tempvar',
  152. tables: [
  153. {
  154. columns: [{ text: '__text' }, { text: '__value' }],
  155. rows: [['aTitle', 'same'], ['aTitle', 'same'], ['aTitle', 'diff']],
  156. },
  157. ],
  158. },
  159. },
  160. };
  161. beforeEach(function() {
  162. ctx.backendSrv.datasourceRequest = function(options) {
  163. return ctx.$q.when({ data: response, status: 200 });
  164. };
  165. ctx.ds.metricFindQuery(query).then(function(data) {
  166. results = data;
  167. });
  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 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 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. });