|
| 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 { Parser } from '../../../../parser'; |
| 9 | +import { BasicPrettyPrinter } from '../../../../pretty_print'; |
| 10 | +import * as commands from '..'; |
| 11 | + |
| 12 | +describe('commands.set', () => { |
| 13 | + describe('.list()', () => { |
| 14 | + it('lists all SET header commands', () => { |
| 15 | + const src = 'SET approximation = true; SET unmapped_fields = "DEFAULT"; FROM index'; |
| 16 | + const { root } = Parser.parse(src); |
| 17 | + |
| 18 | + const nodes = [...commands.set.list(root)]; |
| 19 | + |
| 20 | + expect(nodes).toHaveLength(2); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns empty iterator when no SET commands exist', () => { |
| 24 | + const src = 'FROM index | LIMIT 10'; |
| 25 | + const { root } = Parser.parse(src); |
| 26 | + |
| 27 | + const nodes = [...commands.set.list(root)]; |
| 28 | + |
| 29 | + expect(nodes).toHaveLength(0); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('.findBySettingName()', () => { |
| 34 | + it('finds a SET command by setting name', () => { |
| 35 | + const src = 'SET approximation = true; SET unmapped_fields = "DEFAULT"; FROM index'; |
| 36 | + const { root } = Parser.parse(src); |
| 37 | + |
| 38 | + const node = commands.set.findBySettingName(root, 'unmapped_fields'); |
| 39 | + |
| 40 | + expect(node).toMatchObject({ |
| 41 | + type: 'header-command', |
| 42 | + name: 'set', |
| 43 | + args: [ |
| 44 | + { |
| 45 | + type: 'function', |
| 46 | + subtype: 'binary-expression', |
| 47 | + name: '=', |
| 48 | + args: [ |
| 49 | + { type: 'identifier', name: 'unmapped_fields' }, |
| 50 | + { type: 'literal', literalType: 'keyword', valueUnquoted: 'DEFAULT' }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ], |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns undefined when the setting does not exist', () => { |
| 58 | + const src = 'SET approximation = true; FROM index'; |
| 59 | + const { root } = Parser.parse(src); |
| 60 | + |
| 61 | + const node = commands.set.findBySettingName(root, 'unmapped_fields'); |
| 62 | + |
| 63 | + expect(node).toBeUndefined(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('.set()', () => { |
| 68 | + it('modifies the value of an existing setting', () => { |
| 69 | + const src = 'SET unmapped_fields = "DEFAULT"; FROM index'; |
| 70 | + const { root } = Parser.parse(src); |
| 71 | + |
| 72 | + commands.set.set(root, 'unmapped_fields', 'LOAD'); |
| 73 | + const printed = BasicPrettyPrinter.print(root); |
| 74 | + |
| 75 | + expect(printed).toBe('SET unmapped_fields = "LOAD"; FROM index'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns undefined when the setting does not exist', () => { |
| 79 | + const src = 'SET approximation = TRUE; FROM index'; |
| 80 | + const { root } = Parser.parse(src); |
| 81 | + |
| 82 | + const node = commands.set.set(root, 'unmapped_fields', 'LOAD'); |
| 83 | + const printed = BasicPrettyPrinter.print(root); |
| 84 | + expect(printed).toBe('SET approximation = TRUE; FROM index'); |
| 85 | + expect(node).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('only modifies the targeted setting when multiple exist', () => { |
| 89 | + const src = 'SET approximation = TRUE; SET unmapped_fields = "DEFAULT"; FROM index'; |
| 90 | + const { root } = Parser.parse(src); |
| 91 | + |
| 92 | + commands.set.set(root, 'unmapped_fields', 'LOAD'); |
| 93 | + const printed = BasicPrettyPrinter.print(root); |
| 94 | + |
| 95 | + expect(printed).toBe('SET approximation = TRUE; SET unmapped_fields = "LOAD"; FROM index'); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('.upsert()', () => { |
| 100 | + it('modifies the value when the setting already exists', () => { |
| 101 | + const src = 'SET unmapped_fields = "DEFAULT"; FROM index'; |
| 102 | + const { root } = Parser.parse(src); |
| 103 | + |
| 104 | + const node = commands.set.upsert(root, 'unmapped_fields', 'LOAD'); |
| 105 | + const printed = BasicPrettyPrinter.print(root); |
| 106 | + |
| 107 | + expect(printed).toBe('SET unmapped_fields = "LOAD"; FROM index'); |
| 108 | + expect(node).toMatchObject({ |
| 109 | + type: 'header-command', |
| 110 | + name: 'set', |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('adds a new SET command when the setting does not exist', () => { |
| 115 | + const src = 'FROM index | LIMIT 10'; |
| 116 | + const { root } = Parser.parse(src); |
| 117 | + |
| 118 | + const node = commands.set.upsert(root, 'unmapped_fields', 'LOAD'); |
| 119 | + const printed = BasicPrettyPrinter.print(root); |
| 120 | + |
| 121 | + expect(printed).toBe('SET unmapped_fields = "LOAD"; FROM index | LIMIT 10'); |
| 122 | + expect(node).toMatchObject({ |
| 123 | + type: 'header-command', |
| 124 | + name: 'set', |
| 125 | + args: [ |
| 126 | + { |
| 127 | + type: 'function', |
| 128 | + subtype: 'binary-expression', |
| 129 | + name: '=', |
| 130 | + args: [ |
| 131 | + { type: 'identifier', name: 'unmapped_fields' }, |
| 132 | + { type: 'literal', literalType: 'keyword', valueUnquoted: 'LOAD' }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + ], |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('appends alongside existing SET commands', () => { |
| 140 | + const src = 'SET approximation = TRUE; FROM index'; |
| 141 | + const { root } = Parser.parse(src); |
| 142 | + |
| 143 | + commands.set.upsert(root, 'unmapped_fields', 'LOAD'); |
| 144 | + const printed = BasicPrettyPrinter.print(root); |
| 145 | + |
| 146 | + expect(printed).toBe('SET approximation = TRUE; SET unmapped_fields = "LOAD"; FROM index'); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments