index_pattern.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ///<amd-dependency path="test/specs/helpers" name="helpers" />
  2. import { toUtc, getLocale, setLocale, dateTime } from '@grafana/data';
  3. import { IndexPattern } from '../index_pattern';
  4. describe('IndexPattern', () => {
  5. const originalLocale = getLocale();
  6. afterEach(() => setLocale(originalLocale));
  7. describe('when getting index for today', () => {
  8. test('should return correct index name', () => {
  9. const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
  10. const expected = 'asd-' + toUtc().format('YYYY.MM.DD');
  11. expect(pattern.getIndexForToday()).toBe(expected);
  12. });
  13. test('should format date using western arabic numerals regardless of locale', () => {
  14. setLocale('ar_SA'); // saudi-arabic, formatting for YYYY.MM.DD looks like "٢٠٢٠.٠٩.٠٣"
  15. const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
  16. const expected = 'asd-' + toUtc().locale('en').format('YYYY.MM.DD');
  17. expect(pattern.getIndexForToday()).toBe(expected);
  18. });
  19. });
  20. describe('when getting index list for time range', () => {
  21. describe('no interval', () => {
  22. test('should return correct index', () => {
  23. const pattern = new IndexPattern('my-metrics');
  24. const from = dateTime(new Date(2015, 4, 30, 1, 2, 3));
  25. const to = dateTime(new Date(2015, 5, 1, 12, 5, 6));
  26. expect(pattern.getIndexList(from, to)).toEqual('my-metrics');
  27. });
  28. });
  29. describe('daily', () => {
  30. test('should return correct index list', () => {
  31. const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
  32. const from = dateTime(new Date(1432940523000));
  33. const to = dateTime(new Date(1433153106000));
  34. const expected = ['asd-2015.05.29', 'asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01'];
  35. expect(pattern.getIndexList(from, to)).toEqual(expected);
  36. });
  37. test('should format date using western arabic numerals regardless of locale', () => {
  38. setLocale('ar_SA'); // saudi-arabic, formatting for YYYY.MM.DD looks like "٢٠٢٠.٠٩.٠٣"
  39. const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
  40. const from = dateTime(new Date(1432940523000));
  41. const to = dateTime(new Date(1433153106000));
  42. const expected = ['asd-2015.05.29', 'asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01'];
  43. expect(pattern.getIndexList(from, to)).toEqual(expected);
  44. });
  45. });
  46. describe('weekly', () => {
  47. it('should return correct index list', () => {
  48. const pattern = new IndexPattern('[asd-]YYYY.WW', 'Weekly');
  49. // Sunday, February 21, 2021 1:00:00 AM
  50. const from = dateTime(new Date(1613869200000));
  51. // Friday, March 5, 2021 1:00:00 AM
  52. const to = dateTime(new Date(1614906000000));
  53. const expected = ['asd-2021.07', 'asd-2021.08', 'asd-2021.09'];
  54. expect(pattern.getIndexList(from, to)).toEqual(expected);
  55. });
  56. });
  57. });
  58. describe('when getting index list from single date', () => {
  59. it('Should return index matching the starting time and subsequent ones', () => {
  60. const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
  61. const from = dateTime(new Date(1432940523000));
  62. const expected = [
  63. 'asd-2015.05.29',
  64. 'asd-2015.05.30',
  65. 'asd-2015.05.31',
  66. 'asd-2015.06.01',
  67. 'asd-2015.06.02',
  68. 'asd-2015.06.03',
  69. 'asd-2015.06.04',
  70. 'asd-2015.06.05',
  71. ];
  72. expect(pattern.getIndexList(from)).toEqual(expected);
  73. });
  74. it('Should return index matching the starting time and previous ones', () => {
  75. const pattern = new IndexPattern('[asd-]YYYY.MM.DD', 'Daily');
  76. const to = dateTime(new Date(1432940523000));
  77. const expected = [
  78. 'asd-2015.05.22',
  79. 'asd-2015.05.23',
  80. 'asd-2015.05.24',
  81. 'asd-2015.05.25',
  82. 'asd-2015.05.26',
  83. 'asd-2015.05.27',
  84. 'asd-2015.05.28',
  85. 'asd-2015.05.29',
  86. ];
  87. expect(pattern.getIndexList(undefined, to)).toEqual(expected);
  88. });
  89. });
  90. });