-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathadvancedSearch.ts
More file actions
758 lines (681 loc) · 18.1 KB
/
advancedSearch.ts
File metadata and controls
758 lines (681 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect, Locator, Page } from '@playwright/test';
import { clickOutside } from './common';
import { escapeESReservedCharacters, getEncodedFqn } from './entity';
type EntityFields = {
id: string;
name: string;
skipConditions?: string[];
};
export const FIELDS: EntityFields[] = [
{
id: 'Owners',
name: 'ownerDisplayName',
},
{
id: 'Tags',
name: 'tags.tagFQN',
},
{
id: 'Tier',
name: 'tier.tagFQN',
},
{
id: 'Service',
name: 'service.displayName.keyword',
},
{
id: 'Database',
name: 'database.displayName.keyword',
},
{
id: 'Database Schema',
name: 'databaseSchema.displayName.keyword',
},
{
id: 'Column',
name: 'columns.name.keyword',
},
{
id: 'Display Name',
name: 'displayName.keyword',
skipConditions: ['isNull', 'isNotNull'], // Null and isNotNull conditions are not present for display name
},
{
id: 'Service Type',
name: 'serviceType',
},
{
id: 'Schema Field',
name: 'messageSchema.schemaFields.name.keyword',
},
{
id: 'Container Column',
name: 'dataModel.columns.name.keyword',
},
{
id: 'Data Model Type',
name: 'dataModelType',
},
{
id: 'Field',
name: 'fields.name.keyword',
},
{
id: 'Task',
name: 'tasks.displayName.keyword',
},
{
id: 'Domains',
name: 'domains.displayName.keyword',
},
{
id: 'Name',
name: 'name.keyword',
skipConditions: ['isNull', 'isNotNull'], // Null and isNotNull conditions are not present for name
},
{
id: 'Project',
name: 'project.keyword',
},
{
id: 'Chart',
name: 'charts.displayName.keyword',
},
{
id: 'Response Schema Field',
name: 'responseSchema.schemaFields.name.keyword',
},
{
id: 'Request Schema Field',
name: 'requestSchema.schemaFields.name.keyword',
},
{
id: 'Data Product',
name: 'dataProducts.displayName.keyword',
},
];
export const OPERATOR = {
AND: {
name: 'AND',
index: 1,
},
OR: {
name: 'OR',
index: 2,
},
};
export const CONDITIONS_MUST = {
equalTo: {
name: '==',
filter: 'must',
},
contains: {
name: 'Contains',
filter: 'must',
},
anyIn: {
name: 'Any in',
filter: 'must',
},
};
export const CONDITIONS_MUST_NOT = {
notEqualTo: {
name: '!=',
filter: 'must_not',
},
notIn: {
name: 'Not in',
filter: 'must_not',
},
notContains: {
name: 'Not contains',
filter: 'must_not',
},
};
export const NULL_CONDITIONS = {
isNull: {
name: 'Is null',
filter: 'empty',
},
isNotNull: {
name: 'Is not null',
filter: 'empty',
},
};
export const showAdvancedSearchDialog = async (page: Page) => {
await page.getByTestId('advance-search-button').click();
await expect(page.locator('[role="dialog"].ant-modal')).toBeVisible();
};
export const selectOption = async (
page: Page,
dropdownLocator: Locator,
optionTitle: string,
isSearchable = false
) => {
if (isSearchable) {
// Wait for dropdown to be visible before clicking
const selector = dropdownLocator.locator('.ant-select-selector');
await expect(selector).toBeVisible();
await selector.click();
await dropdownLocator
.locator('.ant-select-arrow-loading svg[data-icon="loading"]')
.waitFor({ state: 'detached' });
// Clear any existing input and type the new value
const combobox = dropdownLocator.getByRole('combobox');
await combobox.clear();
await dropdownLocator
.locator('.ant-select-arrow-loading svg[data-icon="loading"]')
.waitFor({ state: 'detached' });
await combobox.fill(optionTitle);
await dropdownLocator
.locator('.ant-select-arrow-loading svg[data-icon="loading"]')
.waitFor({ state: 'detached' });
} else {
await dropdownLocator.click();
}
await expect(dropdownLocator).toHaveClass(/(^|\s)ant-select-focused(\s|$)/);
await page.locator('.ant-select-dropdown:visible').first().waitFor({
state: 'visible',
});
// CRITICAL: Use :visible selector chain pattern (Rule 4 from deflake guide)
// Use .first() to handle multiple matches (acceptable when scoped to visible dropdown)
const optionLocator = page
.locator('.ant-select-dropdown:visible')
.locator('.ant-select-item-option')
.filter({
hasText: new RegExp(
`^${optionTitle.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&')}$`
),
})
.first();
await expect(optionLocator).toBeVisible();
// Wait for dropdown animations to settle before clicking
// This prevents "element detached from DOM" errors during re-renders
// eslint-disable-next-line playwright/no-wait-for-timeout -- dropdown animation settling
await page.waitForTimeout(100);
await optionLocator.click({ timeout: 10000 });
};
export const selectRange = async (
page: Page,
ruleLocator: Locator,
startDate: string,
endDate: string
) => {
await ruleLocator.locator('.rule--value .ant-picker-range').click();
await page.locator('.ant-picker-dropdown-range').waitFor({
state: 'visible',
});
await page.locator('.ant-picker-input-active input').fill(startDate);
await page.press('.ant-picker-input-active input', 'Enter');
await page.locator('.ant-picker-input-active input').fill(endDate);
await page.press('.ant-picker-input-active input', 'Enter');
};
export const fillRule = async (
page: Page,
{
condition,
field,
searchCriteria,
index,
}: {
condition: string;
field: EntityFields;
searchCriteria?: string;
index: number;
}
) => {
const escapeRegex = (value: string) =>
value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const ruleLocator = page.locator('.rule').nth(index - 1);
// Perform click on rule field
await selectOption(
page,
ruleLocator.locator('.rule--field .ant-select'),
field.id,
true
);
// Perform click on operator
await selectOption(
page,
ruleLocator.locator('.rule--operator .ant-select'),
condition
);
if (searchCriteria) {
const inputElement = ruleLocator.locator(
'.rule--widget--TEXT input[type="text"]'
);
const searchData = searchCriteria.toLowerCase();
if (await inputElement.isVisible()) {
await inputElement.fill(searchData);
} else {
const dropdownInput = ruleLocator.locator(
'.widget--widget > .ant-select > .ant-select-selector input'
);
const aggregateRes1 = page.waitForResponse('/api/v1/search/aggregate?*');
await dropdownInput.click();
await aggregateRes1;
const aggregateRes2 = page.waitForResponse(
`/api/v1/search/aggregate?*${getEncodedFqn(
escapeESReservedCharacters(searchData)
)}*`
);
await dropdownInput.fill(searchData);
await aggregateRes2;
const dropdown = page.locator('.ant-select-dropdown:visible');
const exactTitleMatch = dropdown
.locator('[title]')
.filter({
hasText: new RegExp(`^${escapeRegex(searchData)}$`, 'i'),
})
.first();
const partialTextMatch = dropdown
.locator('.ant-select-item-option-content')
.filter({
hasText: new RegExp(escapeRegex(searchData), 'i'),
})
.first();
if (await exactTitleMatch.count()) {
await exactTitleMatch.click();
} else if (await partialTextMatch.count()) {
await partialTextMatch.click();
} else {
// Some suggestion backends normalize or delay option text; Enter keeps
// the typed criteria and avoids waiting forever on an exact title match.
await dropdownInput.press('Enter');
}
}
await clickOutside(page);
}
};
export const checkMustPaths = async (
page: Page,
{
condition,
field,
searchCriteria,
index,
}: {
condition: string;
field: EntityFields;
searchCriteria: string;
index: number;
}
) => {
const searchData = searchCriteria.toLowerCase();
await fillRule(page, {
condition,
field,
searchCriteria,
index,
});
const searchRes = page.waitForResponse(
`/api/v1/search/query?*index=dataAsset&from=0&size=15*${getEncodedFqn(
searchData,
true
)}*`
);
await page.getByTestId('apply-btn').click();
const res = await searchRes;
expect(res.request().url()).toContain(getEncodedFqn(searchData, true));
const json = await res.json();
expect(JSON.stringify(json.hits.hits)).toContain(searchCriteria);
await expect(
page.getByTestId('advance-search-filter-container')
).toContainText(searchData);
};
export const checkMustNotPaths = async (
page: Page,
{
condition,
field,
searchCriteria,
index,
}: {
condition: string;
field: EntityFields;
searchCriteria: string;
index: number;
}
) => {
const searchData = searchCriteria.toLowerCase();
await fillRule(page, {
condition,
field,
searchCriteria,
index,
});
const searchRes = page.waitForResponse(
`/api/v1/search/query?*index=dataAsset&from=0&size=15*${getEncodedFqn(
searchData,
true
)}*`
);
await page.getByTestId('apply-btn').click();
const res = await searchRes;
expect(res.request().url()).toContain(getEncodedFqn(searchData, true));
if (!['columns.name.keyword'].includes(field.name)) {
const json = await res.json();
expect(JSON.stringify(json.hits.hits)).not.toContain(searchCriteria);
}
await expect(
page.getByTestId('advance-search-filter-container')
).toContainText(searchData);
};
export const checkNullPaths = async (
page: Page,
{
condition,
field,
searchCriteria,
index,
}: {
condition: string;
field: EntityFields;
searchCriteria?: string;
index: number;
}
) => {
await fillRule(page, {
condition,
field,
searchCriteria,
index,
});
const searchRes = page.waitForResponse(
'/api/v1/search/query?*index=dataAsset&from=0&size=15*%22exists%22*'
);
await page.getByTestId('apply-btn').click();
const res = await searchRes;
const urlParams = new URLSearchParams(res.request().url());
const queryFilter = JSON.parse(urlParams.get('query_filter') ?? '');
const resultQuery =
condition === 'Is null'
? {
query: {
bool: {
must: [
{
bool: {
must: [
{
bool: {
must_not: {
exists: { field: field.name },
},
},
},
],
},
},
],
},
},
}
: {
query: {
bool: {
must: [
{
bool: {
must: [{ exists: { field: field.name } }],
},
},
],
},
},
};
expect(JSON.stringify(queryFilter)).toContain(JSON.stringify(resultQuery));
};
export const verifyAllConditions = async (
page: Page,
field: EntityFields,
searchCriteria: string
) => {
// Check for Must conditions
for (const condition of Object.values(CONDITIONS_MUST)) {
await showAdvancedSearchDialog(page);
await checkMustPaths(page, {
condition: condition.name,
field,
searchCriteria: searchCriteria,
index: 1,
});
await page.getByTestId('clear-filters').click();
}
// Check for Must Not conditions
for (const condition of Object.values(CONDITIONS_MUST_NOT)) {
await showAdvancedSearchDialog(page);
await checkMustNotPaths(page, {
condition: condition.name,
field,
searchCriteria: searchCriteria,
index: 1,
});
await page.getByTestId('clear-filters').click();
}
// Don't run null path if it's present in skipConditions
if (
!field.skipConditions?.includes('isNull') ||
!field.skipConditions?.includes('isNotNull')
) {
// Check for Null and Not Null conditions
for (const condition of Object.values(NULL_CONDITIONS)) {
await showAdvancedSearchDialog(page);
await checkNullPaths(page, {
condition: condition.name,
field,
searchCriteria: undefined,
index: 1,
});
await page.getByTestId('clear-filters').click();
}
}
};
export const checkAddRuleOrGroupWithOperator = async (
page: Page,
{
field,
operator,
condition1,
condition2,
searchCriteria1,
searchCriteria2,
}: {
field: EntityFields;
operator: string;
condition1: string;
condition2: string;
searchCriteria1: string;
searchCriteria2: string;
},
isGroupTest = false
) => {
await showAdvancedSearchDialog(page);
await fillRule(page, {
condition: condition1,
field,
searchCriteria: searchCriteria1,
index: 1,
});
if (!isGroupTest) {
await page.getByTestId('advanced-search-add-rule').nth(1).click();
} else {
await page.getByTestId('advanced-search-add-group').first().click();
}
await fillRule(page, {
condition: condition2,
field,
searchCriteria: searchCriteria2,
index: 2,
});
if (operator === 'OR') {
await page
.getByTestId('advanced-search-modal')
.getByRole('button', { name: 'Or' })
.click();
}
// Since the OR operator with must not conditions will result in huge API response
// with huge data, checking the required criteria might not be present on first page
// Hence, checking the criteria only for AND operator
if (field.id === 'Column') {
await page.getByTestId('apply-btn').click();
} else {
const searchRes = page.waitForResponse(
`/api/v1/search/query?*index=dataAsset&from=0&size=15*${getEncodedFqn(
searchCriteria1.toLowerCase(),
true
)}*${getEncodedFqn(searchCriteria2.toLowerCase(), true)}*`
);
await page.getByTestId('apply-btn').click();
const res = await searchRes;
const json = await res.json();
const hits = json.hits.hits;
if (operator === 'AND') {
expect(JSON.stringify(hits)).toContain(searchCriteria1);
expect(JSON.stringify(hits)).not.toContain(searchCriteria2);
} else {
const hitsString = JSON.stringify(hits);
const containsCriteria1 = hitsString.includes(searchCriteria1);
const containsCriteria2 = hitsString.includes(searchCriteria2);
expect(containsCriteria1 || !containsCriteria2).toBe(true);
}
}
};
export const runRuleGroupTests = async (
page: Page,
field: EntityFields,
operator: string,
isGroupTest: boolean,
searchCriteria: Record<string, string[]>
) => {
const searchCriteria1 = searchCriteria[field.name][0];
const searchCriteria2 = searchCriteria[field.name][1];
const testCases = [
{
condition1: CONDITIONS_MUST.equalTo.name,
condition2: CONDITIONS_MUST_NOT.notEqualTo.name,
},
{
condition1: CONDITIONS_MUST.contains.name,
condition2: CONDITIONS_MUST_NOT.notContains.name,
},
{
condition1: CONDITIONS_MUST.anyIn.name,
condition2: CONDITIONS_MUST_NOT.notIn.name,
},
];
for (const { condition1, condition2 } of testCases) {
await checkAddRuleOrGroupWithOperator(
page,
{
field,
operator,
condition1,
condition2,
searchCriteria1,
searchCriteria2,
},
isGroupTest
);
await page.getByTestId('clear-filters').click();
}
};
export const runRuleGroupTestsWithNonExistingValue = async (page: Page) => {
await showAdvancedSearchDialog(page);
const ruleLocator = page.locator('.rule').nth(0);
// Perform click on rule field
await selectOption(
page,
ruleLocator.locator('.rule--field .ant-select'),
'Database',
true
);
await selectOption(
page,
ruleLocator.locator('.rule--operator .ant-select'),
'=='
);
const inputElement = ruleLocator.locator(
'.rule--widget--SELECT .ant-select-selection-search-input'
);
await inputElement.fill('non-existing-value');
const dropdownText = page.locator('.ant-select-item-empty');
await expect(dropdownText).toContainText('Loading...');
// eslint-disable-next-line playwright/no-wait-for-timeout -- search debounce delay
await page.waitForTimeout(1000);
await expect(dropdownText).not.toContainText('Loading...');
};
// For fields backed by hard-coded listValues (no aggregate API call), options are
// rendered immediately — use selectOption directly instead of fillRule which waits
// for a network response that never comes.
export const fillStaticListRule = async (
page: Page,
{
fieldLabel,
condition,
value,
ruleIndex,
}: {
fieldLabel: string;
condition: string;
value: string;
ruleIndex: number;
}
) => {
const ruleLocator = page.locator('.rule').nth(ruleIndex - 1);
await selectOption(
page,
ruleLocator.locator('.rule--field .ant-select'),
fieldLabel,
true
);
await selectOption(
page,
ruleLocator.locator('.rule--operator .ant-select'),
condition
);
await selectOption(
page,
ruleLocator.locator('.widget--widget > .ant-select'),
value
);
};
export const getFieldsSuggestionSearchText = (
fieldLabel: string,
data: Record<string, string>
) => {
switch (fieldLabel) {
case 'Database':
return data.database;
case 'Database Schema':
return data.databaseSchema;
case 'API Collection':
return data.apiCollection;
case 'Glossary':
return data.glossary;
case 'Domains':
return data.domains;
case 'Data Product':
return data.dataProduct;
case 'Tags':
return data.tag;
case 'Certification':
return data.certification;
case 'Tier':
return data.tier;
default:
return '';
}
};