|
| 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 { EsqlQuery } from '../../composer/query'; |
| 9 | +import { Walker } from '../../ast/walker'; |
| 10 | +import type { ESQLAstRegisteredDomainCommand, ESQLFunction } from '../../types'; |
| 11 | + |
| 12 | +describe('REGISTERED_DOMAIN', () => { |
| 13 | + describe('correctly formatted', () => { |
| 14 | + it('parses basic example', () => { |
| 15 | + const src = `FROM index | REGISTERED_DOMAIN parts = host`; |
| 16 | + const { ast, errors } = EsqlQuery.fromSrc(src); |
| 17 | + const registeredDomain = Walker.match(ast, { |
| 18 | + type: 'command', |
| 19 | + name: 'registered_domain', |
| 20 | + }) as ESQLAstRegisteredDomainCommand; |
| 21 | + |
| 22 | + expect(errors.length).toBe(0); |
| 23 | + expect(registeredDomain).toMatchObject({ |
| 24 | + type: 'command', |
| 25 | + name: 'registered_domain', |
| 26 | + incomplete: false, |
| 27 | + }); |
| 28 | + expect(registeredDomain.targetField).toMatchObject({ |
| 29 | + type: 'column', |
| 30 | + name: 'parts', |
| 31 | + }); |
| 32 | + expect(registeredDomain.expression).toMatchObject({ |
| 33 | + type: 'column', |
| 34 | + name: 'host', |
| 35 | + }); |
| 36 | + expect(registeredDomain.args).toHaveLength(1); |
| 37 | + expect(registeredDomain.args[0]).toMatchObject({ |
| 38 | + type: 'function', |
| 39 | + subtype: 'binary-expression', |
| 40 | + name: '=', |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('parses the assignment structure correctly', () => { |
| 45 | + const src = `FROM index | REGISTERED_DOMAIN parts = host`; |
| 46 | + const { ast } = EsqlQuery.fromSrc(src); |
| 47 | + const registeredDomain = ast.commands[1] as ESQLAstRegisteredDomainCommand; |
| 48 | + |
| 49 | + const assignment = registeredDomain.args[0] as ESQLFunction; |
| 50 | + expect(assignment.args[0]).toMatchObject({ |
| 51 | + type: 'column', |
| 52 | + name: 'parts', |
| 53 | + }); |
| 54 | + expect(assignment.args[1]).toMatchObject({ |
| 55 | + type: 'column', |
| 56 | + name: 'host', |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('parses with dotted qualified name', () => { |
| 61 | + const src = `FROM index | REGISTERED_DOMAIN my.prefix = fqdn`; |
| 62 | + const { ast, errors } = EsqlQuery.fromSrc(src); |
| 63 | + const registeredDomain = Walker.match(ast, { |
| 64 | + type: 'command', |
| 65 | + name: 'registered_domain', |
| 66 | + }) as ESQLAstRegisteredDomainCommand; |
| 67 | + |
| 68 | + expect(errors.length).toBe(0); |
| 69 | + expect(registeredDomain.targetField).toMatchObject({ |
| 70 | + type: 'column', |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('parses with function expression as source', () => { |
| 75 | + const src = `FROM index | REGISTERED_DOMAIN parts = CONCAT(host, ".com")`; |
| 76 | + const { ast, errors } = EsqlQuery.fromSrc(src); |
| 77 | + const registeredDomain = Walker.match(ast, { |
| 78 | + type: 'command', |
| 79 | + name: 'registered_domain', |
| 80 | + }) as ESQLAstRegisteredDomainCommand; |
| 81 | + |
| 82 | + expect(errors.length).toBe(0); |
| 83 | + expect(registeredDomain.expression).toMatchObject({ |
| 84 | + type: 'function', |
| 85 | + name: 'concat', |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('parses with quoted target prefix', () => { |
| 90 | + const src = 'FROM index | REGISTERED_DOMAIN `my-prefix` = fqdn'; |
| 91 | + const { ast, errors } = EsqlQuery.fromSrc(src); |
| 92 | + const registeredDomain = ast.commands[1] as ESQLAstRegisteredDomainCommand; |
| 93 | + |
| 94 | + expect(errors.length).toBe(0); |
| 95 | + expect(registeredDomain.targetField).toMatchObject({ |
| 96 | + type: 'column', |
| 97 | + name: 'my-prefix', |
| 98 | + quoted: true, |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('parses with parameter expression as source', () => { |
| 103 | + const src = 'FROM index | REGISTERED_DOMAIN parts = ?fqdn_param'; |
| 104 | + const { ast, errors } = EsqlQuery.fromSrc(src); |
| 105 | + const registeredDomain = ast.commands[1] as ESQLAstRegisteredDomainCommand; |
| 106 | + |
| 107 | + expect(errors.length).toBe(0); |
| 108 | + expect(registeredDomain.expression).toMatchObject({ |
| 109 | + type: 'literal', |
| 110 | + literalType: 'param', |
| 111 | + value: 'fqdn_param', |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('incorrectly formatted', () => { |
| 117 | + it('errors on just the command keyword', () => { |
| 118 | + const { ast, errors } = EsqlQuery.fromSrc(`FROM index | REGISTERED_DOMAIN`); |
| 119 | + const registeredDomain = ast.commands[1] as ESQLAstRegisteredDomainCommand; |
| 120 | + |
| 121 | + expect(errors.length).toBeGreaterThan(0); |
| 122 | + expect(registeredDomain).toMatchObject({ |
| 123 | + name: 'registered_domain', |
| 124 | + incomplete: true, |
| 125 | + }); |
| 126 | + expect(registeredDomain.targetField).toMatchObject({ |
| 127 | + type: 'column', |
| 128 | + name: '', |
| 129 | + incomplete: true, |
| 130 | + }); |
| 131 | + expect(registeredDomain.expression).toBeUndefined(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('errors on missing assignment', () => { |
| 135 | + const { ast, errors } = EsqlQuery.fromSrc(`FROM index | REGISTERED_DOMAIN parts`); |
| 136 | + const registeredDomain = ast.commands[1] as ESQLAstRegisteredDomainCommand; |
| 137 | + |
| 138 | + expect(errors.length).toBeGreaterThan(0); |
| 139 | + expect(registeredDomain).toMatchObject({ |
| 140 | + name: 'registered_domain', |
| 141 | + incomplete: true, |
| 142 | + }); |
| 143 | + expect(registeredDomain.targetField).toMatchObject({ |
| 144 | + type: 'column', |
| 145 | + name: 'parts', |
| 146 | + }); |
| 147 | + expect(registeredDomain.expression).toBeUndefined(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('errors on missing expression after assignment', () => { |
| 151 | + const { ast, errors } = EsqlQuery.fromSrc(`FROM index | REGISTERED_DOMAIN parts =`); |
| 152 | + const registeredDomain = ast.commands[1] as ESQLAstRegisteredDomainCommand; |
| 153 | + |
| 154 | + expect(errors.length).toBeGreaterThan(0); |
| 155 | + expect(registeredDomain).toMatchObject({ |
| 156 | + name: 'registered_domain', |
| 157 | + incomplete: true, |
| 158 | + }); |
| 159 | + expect(registeredDomain.targetField).toMatchObject({ |
| 160 | + type: 'column', |
| 161 | + name: 'parts', |
| 162 | + }); |
| 163 | + expect(registeredDomain.expression).toMatchObject({ |
| 164 | + type: 'unknown', |
| 165 | + incomplete: true, |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments