datasource.jest.ts 6.2 KB

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