Reports.constants.test.ts106 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { generateRegexpWhere } from './Reports.constants' |
| 4 | import type { ReportFilterItem } from './Reports.types' |
| 5 | |
| 6 | describe('generateRegexpWhere', () => { |
| 7 | it('should return empty string when no filters provided', () => { |
| 8 | const result = generateRegexpWhere([]) |
| 9 | expect(result).toBe('') |
| 10 | }) |
| 11 | |
| 12 | it('should generate WHERE clause for single filter', () => { |
| 13 | const filters: ReportFilterItem[] = [ |
| 14 | { |
| 15 | key: 'request.path', |
| 16 | value: '/api/users', |
| 17 | compare: 'is', |
| 18 | }, |
| 19 | ] |
| 20 | const result = generateRegexpWhere(filters, true) |
| 21 | expect(result).toBe("WHERE request.path = '/api/users'") |
| 22 | }) |
| 23 | |
| 24 | it('should generate AND clause for single filter with prepend=false', () => { |
| 25 | const filters: ReportFilterItem[] = [ |
| 26 | { |
| 27 | key: 'request.path', |
| 28 | value: '/api/users', |
| 29 | compare: 'is', |
| 30 | }, |
| 31 | ] |
| 32 | const result = generateRegexpWhere(filters, false) |
| 33 | expect(result).toBe("AND request.path = '/api/users'") |
| 34 | }) |
| 35 | |
| 36 | it('should handle different comparison operators', () => { |
| 37 | const filters: ReportFilterItem[] = [ |
| 38 | { |
| 39 | key: 'request.path', |
| 40 | value: '/api/*', |
| 41 | compare: 'matches', |
| 42 | }, |
| 43 | { |
| 44 | key: 'response.status_code', |
| 45 | value: 404, |
| 46 | compare: 'is', |
| 47 | }, |
| 48 | ] |
| 49 | const result = generateRegexpWhere(filters, true) |
| 50 | expect(result).toBe( |
| 51 | "WHERE REGEXP_CONTAINS(request.path, '/api/*') AND response.status_code = 404" |
| 52 | ) |
| 53 | }) |
| 54 | |
| 55 | it('should handle values with quotes', () => { |
| 56 | const filters: ReportFilterItem[] = [ |
| 57 | { |
| 58 | key: 'request.path', |
| 59 | value: '"/api/users"', |
| 60 | compare: 'is', |
| 61 | }, |
| 62 | ] |
| 63 | |
| 64 | const result = generateRegexpWhere(filters, true) |
| 65 | expect(result).toBe(`WHERE request.path = "/api/users"`) |
| 66 | }) |
| 67 | |
| 68 | it('should handle values without quotes', () => { |
| 69 | const filters: ReportFilterItem[] = [ |
| 70 | { |
| 71 | key: 'request.path', |
| 72 | value: '/api/users', |
| 73 | compare: 'is', |
| 74 | }, |
| 75 | ] |
| 76 | |
| 77 | const result = generateRegexpWhere(filters, true) |
| 78 | expect(result).toBe("WHERE request.path = '/api/users'") |
| 79 | }) |
| 80 | |
| 81 | it('should handle values with quotes and lowercase', () => { |
| 82 | const filters: ReportFilterItem[] = [ |
| 83 | { |
| 84 | key: 'request.path', |
| 85 | value: '"/Api/Users"', |
| 86 | compare: 'is', |
| 87 | }, |
| 88 | ] |
| 89 | |
| 90 | const result = generateRegexpWhere(filters, true) |
| 91 | expect(result).toBe(`WHERE request.path = "/api/users"`) |
| 92 | }) |
| 93 | |
| 94 | it('should handle numbers', () => { |
| 95 | const filters: ReportFilterItem[] = [ |
| 96 | { |
| 97 | key: 'request.status_code', |
| 98 | value: 200, |
| 99 | compare: 'is', |
| 100 | }, |
| 101 | ] |
| 102 | |
| 103 | const result = generateRegexpWhere(filters, true) |
| 104 | expect(result).toBe(`WHERE request.status_code = 200`) |
| 105 | }) |
| 106 | }) |