datasource.jest.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //import { describe, beforeEach, it, expect, angularMocks } from 'test/lib/common';
  2. import moment from 'moment';
  3. import { TemplateSrvStub } from 'test/specs/helpers';
  4. import { MysqlDatasource } from '../datasource';
  5. import { CustomVariable } from 'app/features/templating/custom_variable';
  6. describe('MySQLDatasource', function() {
  7. let instanceSettings = { name: 'mysql' };
  8. let backendSrv = {};
  9. let templateSrv = new TemplateSrvStub();
  10. let ctx = <any>{
  11. ds: new MysqlDatasource(instanceSettings, backendSrv, {}, templateSrv),
  12. backendSrv
  13. };
  14. describe('When performing annotationQuery', function() {
  15. let results;
  16. const annotationName = 'MyAnno';
  17. const options = {
  18. annotation: {
  19. name: annotationName,
  20. rawQuery: 'select time_sec, text, tags from table;',
  21. },
  22. range: {
  23. from: moment(1432288354),
  24. to: moment(1432288401),
  25. },
  26. };
  27. const response = {
  28. results: {
  29. MyAnno: {
  30. refId: annotationName,
  31. tables: [
  32. {
  33. columns: [{ text: 'time_sec' }, { text: 'text' }, { text: 'tags' }],
  34. rows: [
  35. [1432288355, 'some text', 'TagA,TagB'],
  36. [1432288390, 'some text2', ' TagB , TagC'],
  37. [1432288400, 'some text3'],
  38. ],
  39. },
  40. ],
  41. },
  42. },
  43. };
  44. beforeEach(function() {
  45. ctx.backendSrv.datasourceRequest = jest.fn((options) => {
  46. return Promise.resolve({ data: response, status: 200 });
  47. });
  48. ctx.ds.annotationQuery(options).then(function(data) {
  49. results = data;
  50. });
  51. });
  52. it('should return annotation list', function() {
  53. expect(results.length).toBe(3);
  54. expect(results[0].text).toBe('some text');
  55. expect(results[0].tags[0]).toBe('TagA');
  56. expect(results[0].tags[1]).toBe('TagB');
  57. expect(results[1].tags[0]).toBe('TagB');
  58. expect(results[1].tags[1]).toBe('TagC');
  59. expect(results[2].tags.length).toBe(0);
  60. });
  61. });
  62. describe('When performing metricFindQuery', function() {
  63. let results;
  64. const query = 'select * from atable';
  65. const response = {
  66. results: {
  67. tempvar: {
  68. meta: {
  69. rowCount: 3,
  70. },
  71. refId: 'tempvar',
  72. tables: [
  73. {
  74. columns: [{ text: 'title' }, { text: 'text' }],
  75. rows: [['aTitle', 'some text'], ['aTitle2', 'some text2'], ['aTitle3', 'some text3']],
  76. },
  77. ],
  78. },
  79. },
  80. };
  81. beforeEach(function() {
  82. ctx.backendSrv.datasourceRequest = jest.fn((options) => {
  83. return Promise.resolve({ data: response, status: 200 });
  84. });
  85. ctx.ds.metricFindQuery(query).then(function(data) {
  86. results = data;
  87. });
  88. });
  89. it('should return list of all column values', function() {
  90. expect(results.length).toBe(6);
  91. expect(results[0].text).toBe('aTitle');
  92. expect(results[5].text).toBe('some text3');
  93. });
  94. });
  95. describe('When performing metricFindQuery with key, value columns', function() {
  96. let results;
  97. const query = 'select * from atable';
  98. const response = {
  99. results: {
  100. tempvar: {
  101. meta: {
  102. rowCount: 3,
  103. },
  104. refId: 'tempvar',
  105. tables: [
  106. {
  107. columns: [{ text: '__value' }, { text: '__text' }],
  108. rows: [['value1', 'aTitle'], ['value2', 'aTitle2'], ['value3', 'aTitle3']],
  109. },
  110. ],
  111. },
  112. },
  113. };
  114. beforeEach(function() {
  115. ctx.backendSrv.datasourceRequest = jest.fn((options) => {
  116. return Promise.resolve({ data: response, status: 200 });
  117. });
  118. ctx.ds.metricFindQuery(query).then(function(data) {
  119. results = data;
  120. });
  121. });
  122. it('should return list of as text, value', function() {
  123. expect(results.length).toBe(3);
  124. expect(results[0].text).toBe('aTitle');
  125. expect(results[0].value).toBe('value1');
  126. expect(results[2].text).toBe('aTitle3');
  127. expect(results[2].value).toBe('value3');
  128. });
  129. });
  130. describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() {
  131. let results;
  132. const query = 'select * from atable';
  133. const response = {
  134. results: {
  135. tempvar: {
  136. meta: {
  137. rowCount: 3,
  138. },
  139. refId: 'tempvar',
  140. tables: [
  141. {
  142. columns: [{ text: '__text' }, { text: '__value' }],
  143. rows: [['aTitle', 'same'], ['aTitle', 'same'], ['aTitle', 'diff']],
  144. },
  145. ],
  146. },
  147. },
  148. };
  149. beforeEach(function() {
  150. ctx.backendSrv.datasourceRequest = jest.fn((options) => {
  151. return Promise.resolve({ data: response, status: 200 });
  152. });
  153. ctx.ds.metricFindQuery(query).then(function(data) {
  154. results = data;
  155. });
  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 value 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 value 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. });