|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import { createStubDataView } from '@kbn/data-views-plugin/common/stubs'; |
| 11 | +import { getCategorizationField } from './get_categorization_field'; |
| 12 | +import { getCategorizationDataViewField } from './get_categorization_field'; |
| 13 | + |
| 14 | +describe('get_categorization_field utils', () => { |
| 15 | + describe('getCategorizationField', () => { |
| 16 | + it('returns "message" if present', () => { |
| 17 | + const fields = ['foo', 'bar', 'message', 'baz']; |
| 18 | + expect(getCategorizationField(fields)).toBe('message'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns "error.message" if present and "message" is not', () => { |
| 22 | + const fields = ['foo', 'error.message', 'baz']; |
| 23 | + expect(getCategorizationField(fields)).toBe('error.message'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns "event.original" if present and others are not', () => { |
| 27 | + const fields = ['event.original', 'foo', 'bar']; |
| 28 | + expect(getCategorizationField(fields)).toBe('event.original'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns first field if none of the priority fields are present', () => { |
| 32 | + const fields = ['foo', 'bar', 'baz']; |
| 33 | + expect(getCategorizationField(fields)).toBe('foo'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns first field, skipping meta data fields, if none of the priority fields are present', () => { |
| 37 | + const fields = ['_id', 'foo', 'bar', 'baz']; |
| 38 | + expect(getCategorizationField(fields)).toBe('foo'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns the first matching field according to priority', () => { |
| 42 | + const fields = ['event.original', 'error.message', 'message']; |
| 43 | + expect(getCategorizationField(fields)).toBe('message'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('handles empty array', () => { |
| 47 | + expect(getCategorizationField([])).toBeUndefined(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('getCategorizationDataViewField', () => { |
| 52 | + it('returns messageField as "message" if present', () => { |
| 53 | + const dataView = createStubDataView({ |
| 54 | + spec: { |
| 55 | + id: 'test', |
| 56 | + fields: { |
| 57 | + foo: { |
| 58 | + searchable: false, |
| 59 | + aggregatable: false, |
| 60 | + name: 'foo', |
| 61 | + type: 'type', |
| 62 | + esTypes: ['keyword'], |
| 63 | + }, |
| 64 | + message: { |
| 65 | + searchable: true, |
| 66 | + aggregatable: true, |
| 67 | + name: 'message', |
| 68 | + type: 'text', |
| 69 | + esTypes: ['text'], |
| 70 | + }, |
| 71 | + 'error.message': { |
| 72 | + searchable: true, |
| 73 | + aggregatable: true, |
| 74 | + name: 'error.message', |
| 75 | + type: 'text', |
| 76 | + esTypes: ['text'], |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }); |
| 81 | + const result = getCategorizationDataViewField(dataView); |
| 82 | + expect(result.messageField?.name).toBe('message'); |
| 83 | + expect(result.dataViewFields.map((f) => f.name)).toContain('message'); |
| 84 | + expect(result.dataViewFields.length).toBe(2); |
| 85 | + }); |
| 86 | + it('returns messageField as "error.message" if "message" is not present', () => { |
| 87 | + const dataView = createStubDataView({ |
| 88 | + spec: { |
| 89 | + id: 'test2', |
| 90 | + fields: { |
| 91 | + foo: { |
| 92 | + searchable: false, |
| 93 | + aggregatable: false, |
| 94 | + name: 'foo', |
| 95 | + type: 'type', |
| 96 | + esTypes: ['keyword'], |
| 97 | + }, |
| 98 | + 'error.message': { |
| 99 | + searchable: true, |
| 100 | + aggregatable: true, |
| 101 | + name: 'error.message', |
| 102 | + type: 'text', |
| 103 | + esTypes: ['text'], |
| 104 | + }, |
| 105 | + bar: { |
| 106 | + searchable: true, |
| 107 | + aggregatable: true, |
| 108 | + name: 'bar', |
| 109 | + type: 'text', |
| 110 | + esTypes: ['text'], |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }); |
| 115 | + const result = getCategorizationDataViewField(dataView); |
| 116 | + expect(result.messageField?.name).toBe('error.message'); |
| 117 | + expect(result.dataViewFields.map((f) => f.name)).toContain('error.message'); |
| 118 | + expect(result.dataViewFields.length).toBe(2); |
| 119 | + }); |
| 120 | + |
| 121 | + it('handles empty fields array', () => { |
| 122 | + const dataView = createStubDataView({ |
| 123 | + spec: { |
| 124 | + id: 'test6', |
| 125 | + fields: {}, |
| 126 | + }, |
| 127 | + }); |
| 128 | + const result = getCategorizationDataViewField(dataView); |
| 129 | + expect(result.messageField).toBeNull(); |
| 130 | + expect(result.dataViewFields.length).toBe(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('returns the first matching field according to priority', () => { |
| 134 | + const dataView = createStubDataView({ |
| 135 | + spec: { |
| 136 | + id: 'test7', |
| 137 | + fields: { |
| 138 | + 'event.original': { |
| 139 | + searchable: true, |
| 140 | + aggregatable: true, |
| 141 | + name: 'event.original', |
| 142 | + type: 'text', |
| 143 | + esTypes: ['text'], |
| 144 | + }, |
| 145 | + 'error.message': { |
| 146 | + searchable: true, |
| 147 | + aggregatable: true, |
| 148 | + name: 'error.message', |
| 149 | + type: 'text', |
| 150 | + esTypes: ['text'], |
| 151 | + }, |
| 152 | + message: { |
| 153 | + searchable: true, |
| 154 | + aggregatable: true, |
| 155 | + name: 'message', |
| 156 | + type: 'text', |
| 157 | + esTypes: ['text'], |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }); |
| 162 | + const result = getCategorizationDataViewField(dataView); |
| 163 | + expect(result.messageField?.name).toBe('message'); |
| 164 | + expect(result.dataViewFields.map((f) => f.name)).toEqual( |
| 165 | + expect.arrayContaining(['message', 'error.message', 'event.original']) |
| 166 | + ); |
| 167 | + expect(result.dataViewFields.length).toBe(3); |
| 168 | + }); |
| 169 | + it('returns the first field if no priority fields are present', () => { |
| 170 | + const dataView = createStubDataView({ |
| 171 | + spec: { |
| 172 | + id: 'test8', |
| 173 | + fields: { |
| 174 | + foo: { |
| 175 | + searchable: true, |
| 176 | + aggregatable: true, |
| 177 | + name: 'foo', |
| 178 | + type: 'text', |
| 179 | + esTypes: ['text'], |
| 180 | + }, |
| 181 | + bar: { |
| 182 | + searchable: true, |
| 183 | + aggregatable: true, |
| 184 | + name: 'bar', |
| 185 | + type: 'text', |
| 186 | + esTypes: ['text'], |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + }); |
| 191 | + const result = getCategorizationDataViewField(dataView); |
| 192 | + expect(result.messageField?.name).toBe('foo'); |
| 193 | + expect(result.dataViewFields.map((f) => f.name)).toEqual( |
| 194 | + expect.arrayContaining(['foo', 'bar']) |
| 195 | + ); |
| 196 | + expect(result.dataViewFields.length).toBe(2); |
| 197 | + }); |
| 198 | + it('returns null messageField if no text fields are present', () => { |
| 199 | + const dataView = createStubDataView({ |
| 200 | + spec: { |
| 201 | + id: 'test9', |
| 202 | + fields: { |
| 203 | + foo: { |
| 204 | + searchable: true, |
| 205 | + aggregatable: true, |
| 206 | + name: 'foo', |
| 207 | + type: 'keyword', |
| 208 | + esTypes: ['keyword'], |
| 209 | + }, |
| 210 | + bar: { |
| 211 | + searchable: true, |
| 212 | + aggregatable: true, |
| 213 | + name: 'bar', |
| 214 | + type: 'keyword', |
| 215 | + esTypes: ['keyword'], |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + }); |
| 220 | + const result = getCategorizationDataViewField(dataView); |
| 221 | + expect(result.messageField).toBeNull(); |
| 222 | + expect(result.dataViewFields.map((f) => f.name)).toEqual(expect.arrayContaining([])); |
| 223 | + expect(result.dataViewFields.length).toBe(0); |
| 224 | + }); |
| 225 | + }); |
| 226 | +}); |
0 commit comments