Skip to content

Commit 4921df4

Browse files
committed
Add tests capturing current addAddHocFilter behavior
Documents how the = operator wraps array values like ["paperclip"] in quotes, producing phrase queries that don't match indexed array elements. Tests cover all operator types and edge cases. Refs #179
1 parent 51b1f4d commit 4921df4

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

src/modifyQuery.test.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { addAddHocFilter } from './modifyQuery';
2+
3+
describe('addAddHocFilter', () => {
4+
describe('current behavior with array values', () => {
5+
it('wraps equality filter value in quotes (phrase query)', () => {
6+
const result = addAddHocFilter('', {
7+
key: 'attributes.tags',
8+
operator: '=',
9+
value: '["paperclip"]',
10+
});
11+
// Current behavior: generates a phrase query with the stringified array
12+
expect(result).toBe('attributes.tags:"[\\"paperclip\\"]"');
13+
});
14+
15+
it('wraps negated equality filter value in quotes', () => {
16+
const result = addAddHocFilter('', {
17+
key: 'attributes.tags',
18+
operator: '!=',
19+
value: '["paperclip"]',
20+
});
21+
expect(result).toBe('-attributes.tags:"[\\"paperclip\\"]"');
22+
});
23+
24+
it('term operator produces unquoted query', () => {
25+
const result = addAddHocFilter('', {
26+
key: 'attributes.tags',
27+
operator: 'term',
28+
value: 'paperclip',
29+
});
30+
expect(result).toBe('attributes.tags:paperclip');
31+
});
32+
33+
it('not term operator produces negated unquoted query', () => {
34+
const result = addAddHocFilter('', {
35+
key: 'attributes.tags',
36+
operator: 'not term',
37+
value: 'paperclip',
38+
});
39+
expect(result).toBe('-attributes.tags:paperclip');
40+
});
41+
});
42+
43+
describe('scalar value filters', () => {
44+
it('equality on simple string value', () => {
45+
const result = addAddHocFilter('', {
46+
key: 'attributes.controller',
47+
operator: '=',
48+
value: 'BlogController',
49+
});
50+
expect(result).toBe('attributes.controller:"BlogController"');
51+
});
52+
53+
it('appends to existing query with AND', () => {
54+
const result = addAddHocFilter('status:200', {
55+
key: 'attributes.controller',
56+
operator: '=',
57+
value: 'BlogController',
58+
});
59+
expect(result).toBe('status:200 AND attributes.controller:"BlogController"');
60+
});
61+
62+
it('exists operator', () => {
63+
const result = addAddHocFilter('', {
64+
key: 'attributes.tags',
65+
operator: 'exists',
66+
value: '',
67+
});
68+
expect(result).toBe('attributes.tags:*');
69+
});
70+
71+
it('not exists operator', () => {
72+
const result = addAddHocFilter('', {
73+
key: 'attributes.tags',
74+
operator: 'not exists',
75+
value: '',
76+
});
77+
expect(result).toBe('-attributes.tags:*');
78+
});
79+
80+
it('regex operator', () => {
81+
const result = addAddHocFilter('', {
82+
key: 'attributes.controller',
83+
operator: '=~',
84+
value: 'Blog.*',
85+
});
86+
expect(result).toBe('attributes.controller:/Blog.*/');
87+
});
88+
89+
it('greater than operator', () => {
90+
const result = addAddHocFilter('', {
91+
key: 'attributes.duration',
92+
operator: '>',
93+
value: '100',
94+
});
95+
expect(result).toBe('attributes.duration:>100');
96+
});
97+
98+
it('less than operator', () => {
99+
const result = addAddHocFilter('', {
100+
key: 'attributes.duration',
101+
operator: '<',
102+
value: '100',
103+
});
104+
expect(result).toBe('attributes.duration:<100');
105+
});
106+
});
107+
108+
describe('edge cases', () => {
109+
it('returns query unchanged when key is empty', () => {
110+
const result = addAddHocFilter('existing', {
111+
key: '',
112+
operator: '=',
113+
value: 'test',
114+
});
115+
expect(result).toBe('existing');
116+
});
117+
118+
it('returns query unchanged when value is empty for non-exists operators', () => {
119+
const result = addAddHocFilter('existing', {
120+
key: 'field',
121+
operator: '=',
122+
value: '',
123+
});
124+
expect(result).toBe('existing');
125+
});
126+
127+
it('handles multi-element array value with equality', () => {
128+
const result = addAddHocFilter('', {
129+
key: 'attributes.tags',
130+
operator: '=',
131+
value: '["paperclip","stapler"]',
132+
});
133+
// Current behavior: entire stringified array becomes the phrase
134+
expect(result).toBe('attributes.tags:"[\\"paperclip\\",\\"stapler\\"]"');
135+
});
136+
});
137+
});

0 commit comments

Comments
 (0)