datasource.jest.ts 6.3 KB

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