result_transformer.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { LogLevel } from 'app/core/logs_model';
  2. import { findCommonLabels, findUncommonLabels, formatLabels, getLogLevel, parseLabels } from './result_transformer';
  3. describe('getLoglevel()', () => {
  4. it('returns no log level on empty line', () => {
  5. expect(getLogLevel('')).toBe(undefined);
  6. });
  7. it('returns no log level on when level is part of a word', () => {
  8. expect(getLogLevel('this is a warning')).toBe(undefined);
  9. });
  10. it('returns log level on line contains a log level', () => {
  11. expect(getLogLevel('warn: it is looking bad')).toBe(LogLevel.warn);
  12. expect(getLogLevel('2007-12-12 12:12:12 [WARN]: it is looking bad')).toBe(LogLevel.warn);
  13. });
  14. it('returns first log level found', () => {
  15. expect(getLogLevel('WARN this could be a debug message')).toBe(LogLevel.warn);
  16. });
  17. });
  18. describe('parseLabels()', () => {
  19. it('returns no labels on emtpy labels string', () => {
  20. expect(parseLabels('')).toEqual({});
  21. expect(parseLabels('{}')).toEqual({});
  22. });
  23. it('returns labels on labels string', () => {
  24. expect(parseLabels('{foo="bar", baz="42"}')).toEqual({ foo: '"bar"', baz: '"42"' });
  25. });
  26. });
  27. describe('formatLabels()', () => {
  28. it('returns no labels on emtpy label set', () => {
  29. expect(formatLabels({})).toEqual('');
  30. expect(formatLabels({}, 'foo')).toEqual('foo');
  31. });
  32. it('returns label string on label set', () => {
  33. expect(formatLabels({ foo: '"bar"', baz: '"42"' })).toEqual('{baz="42", foo="bar"}');
  34. });
  35. });
  36. describe('findCommonLabels()', () => {
  37. it('returns no common labels on empty sets', () => {
  38. expect(findCommonLabels([{}])).toEqual({});
  39. expect(findCommonLabels([{}, {}])).toEqual({});
  40. });
  41. it('returns no common labels on differing sets', () => {
  42. expect(findCommonLabels([{ foo: '"bar"' }, {}])).toEqual({});
  43. expect(findCommonLabels([{}, { foo: '"bar"' }])).toEqual({});
  44. expect(findCommonLabels([{ baz: '42' }, { foo: '"bar"' }])).toEqual({});
  45. expect(findCommonLabels([{ foo: '42', baz: '"bar"' }, { foo: '"bar"' }])).toEqual({});
  46. });
  47. it('returns the single labels set as common labels', () => {
  48. expect(findCommonLabels([{ foo: '"bar"' }])).toEqual({ foo: '"bar"' });
  49. });
  50. });
  51. describe('findUncommonLabels()', () => {
  52. it('returns no uncommon labels on empty sets', () => {
  53. expect(findUncommonLabels({}, {})).toEqual({});
  54. });
  55. it('returns all labels given no common labels', () => {
  56. expect(findUncommonLabels({ foo: '"bar"' }, {})).toEqual({ foo: '"bar"' });
  57. });
  58. it('returns all labels except the common labels', () => {
  59. expect(findUncommonLabels({ foo: '"bar"', baz: '"42"' }, { foo: '"bar"' })).toEqual({ baz: '"42"' });
  60. });
  61. });