|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { interpolateEsqlQuery } from './interpolate_query'; |
| 9 | + |
| 10 | +describe('interpolateEsqlQuery', () => { |
| 11 | + it('should correctly interpolate string and number parameters', () => { |
| 12 | + const template = 'FROM support_ticket | WHERE priority == ?priority AND assigne_id == ?user_id'; |
| 13 | + const params = { priority: 'high', user_id: 45 }; |
| 14 | + const expected = 'FROM support_ticket | WHERE priority == "high" AND assigne_id == 45'; |
| 15 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should correctly interpolate boolean parameters', () => { |
| 19 | + const template = 'FROM nodes | WHERE is_active == ?active AND is_primary == ?primary'; |
| 20 | + const params = { active: true, primary: false }; |
| 21 | + const expected = 'FROM nodes | WHERE is_active == true AND is_primary == false'; |
| 22 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should replace all occurrences of a placeholder', () => { |
| 26 | + const template = 'FROM logs | WHERE user.id == ?user_id OR target.id == ?user_id'; |
| 27 | + const params = { user_id: 101 }; |
| 28 | + const expected = 'FROM logs | WHERE user.id == 101 OR target.id == 101'; |
| 29 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should handle an empty parameters object without changing the template', () => { |
| 33 | + const template = 'FROM events | WHERE event.code == ?code'; |
| 34 | + const params = {}; |
| 35 | + expect(interpolateEsqlQuery(template, params)).toBe(template); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should ignore extra parameters that are not in the template', () => { |
| 39 | + const template = 'FROM metrics | WHERE host == ?host'; |
| 40 | + const params = { host: 'server-alpha', unused_param: 'ignore' }; |
| 41 | + const expected = 'FROM metrics | WHERE host == "server-alpha"'; |
| 42 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not partially replace longer placeholders (word boundary test)', () => { |
| 46 | + const template = 'FROM users | WHERE user_name == ?user_name AND user_id == ?user_id'; |
| 47 | + const params = { user: 'test', user_id: 99 }; |
| 48 | + const expected = 'FROM users | WHERE user_name == ?user_name AND user_id == 99'; |
| 49 | + // Only ?user_id should be replaced, ?user should not affect ?user_name |
| 50 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should correctly handle an empty string parameter', () => { |
| 54 | + const template = 'FROM products | WHERE category == ?category'; |
| 55 | + const params = { category: '' }; |
| 56 | + const expected = 'FROM products | WHERE category == ""'; |
| 57 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should correctly handle the number 0 as a parameter', () => { |
| 61 | + const template = 'FROM tasks | WHERE status_code == ?status'; |
| 62 | + const params = { status: 0 }; |
| 63 | + const expected = 'FROM tasks | WHERE status_code == 0'; |
| 64 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle a mix of all data types in one query', () => { |
| 68 | + const template = |
| 69 | + 'FROM telemetry | WHERE device_id == ?device AND temp > ?temp AND enabled == ?enabled'; |
| 70 | + const params = { device: 'sensor-3-14', temp: 25.5, enabled: true }; |
| 71 | + const expected = |
| 72 | + 'FROM telemetry | WHERE device_id == "sensor-3-14" AND temp > 25.5 AND enabled == true'; |
| 73 | + expect(interpolateEsqlQuery(template, params)).toBe(expected); |
| 74 | + }); |
| 75 | +}); |
0 commit comments