Skip to content

Commit 1d64540

Browse files
committed
Issue #20: add backend unit tests
1 parent b66cce1 commit 1d64540

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed

pkg/quickwit/timestamp_infos_test.go

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
package quickwit
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestDecodeTimestampFieldInfos(t *testing.T) {
10+
t.Run("Test decode timestam field infos", func(t *testing.T) {
11+
t.Run("Test decode simple fields", func(t *testing.T) {
12+
// Given
13+
query := []byte(`
14+
{
15+
"version": "0.6",
16+
"index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W",
17+
"index_config": {
18+
"version": "0.6",
19+
"index_id": "myindex",
20+
"index_uri": "s3://quickwit-indexes/myindex",
21+
"doc_mapping": {
22+
"field_mappings": [
23+
{
24+
"name": "foo",
25+
"type": "text",
26+
"fast": false,
27+
"fieldnorms": false,
28+
"indexed": true,
29+
"record": "basic",
30+
"stored": true,
31+
"tokenizer": "default"
32+
},
33+
{
34+
"name": "timestamp",
35+
"type": "datetime",
36+
"fast": true,
37+
"fast_precision": "seconds",
38+
"indexed": true,
39+
"input_formats": [
40+
"rfc3339",
41+
"unix_timestamp"
42+
],
43+
"output_format": "rfc3339",
44+
"stored": true
45+
}
46+
],
47+
"tag_fields": [],
48+
"store_source": true,
49+
"index_field_presence": false,
50+
"timestamp_field": "timestamp",
51+
"mode": "dynamic",
52+
"dynamic_mapping": {},
53+
"partition_key": "foo",
54+
"max_num_partitions": 1,
55+
"tokenizers": []
56+
},
57+
"indexing_settings": {},
58+
"search_settings": {
59+
"default_search_fields": [
60+
"foo"
61+
]
62+
},
63+
"retention": null
64+
},
65+
"checkpoint": {},
66+
"create_timestamp": 1701075471,
67+
"sources": []
68+
}
69+
`)
70+
71+
// When
72+
timestampFieldName, timestampFieldFormat, err := DecodeTimestampFieldInfos(200, query)
73+
74+
// Then
75+
require.NoError(t, err)
76+
require.Equal(t, timestampFieldName, "timestamp")
77+
require.Equal(t, timestampFieldFormat, "rfc3339")
78+
})
79+
80+
t.Run("Test decode nested fields", func(t *testing.T) {
81+
// Given
82+
query := []byte(`
83+
{
84+
"version": "0.6",
85+
"index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W",
86+
"index_config": {
87+
"version": "0.6",
88+
"index_id": "myindex",
89+
"index_uri": "s3://quickwit-indexes/myindex",
90+
"doc_mapping": {
91+
"field_mappings": [
92+
{
93+
"name": "foo",
94+
"type": "text",
95+
"fast": false,
96+
"fieldnorms": false,
97+
"indexed": true,
98+
"record": "basic",
99+
"stored": true,
100+
"tokenizer": "default"
101+
},
102+
{
103+
"name": "sub",
104+
"type": "object",
105+
"field_mappings": [
106+
{
107+
"fast": true,
108+
"fast_precision": "seconds",
109+
"indexed": true,
110+
"input_formats": [
111+
"rfc3339",
112+
"unix_timestamp"
113+
],
114+
"name": "timestamp",
115+
"output_format": "rfc3339",
116+
"stored": true,
117+
"type": "datetime"
118+
}
119+
]
120+
}
121+
],
122+
"tag_fields": [],
123+
"store_source": true,
124+
"index_field_presence": false,
125+
"timestamp_field": "sub.timestamp",
126+
"mode": "dynamic",
127+
"dynamic_mapping": {},
128+
"partition_key": "foo",
129+
"max_num_partitions": 1,
130+
"tokenizers": []
131+
},
132+
"indexing_settings": {},
133+
"search_settings": {
134+
"default_search_fields": [
135+
"foo"
136+
]
137+
},
138+
"retention": null
139+
},
140+
"checkpoint": {},
141+
"create_timestamp": 1701075471,
142+
"sources": []
143+
}
144+
`)
145+
146+
// When
147+
timestampFieldName, timestampFieldFormat, err := DecodeTimestampFieldInfos(200, query)
148+
149+
// Then
150+
require.NoError(t, err)
151+
require.Equal(t, timestampFieldName, "sub.timestamp")
152+
require.Equal(t, timestampFieldFormat, "rfc3339")
153+
})
154+
155+
t.Run("The timestamp field is not at the expected path", func(t *testing.T) {
156+
// Given
157+
query := []byte(`
158+
{
159+
"version": "0.6",
160+
"index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W",
161+
"index_config": {
162+
"version": "0.6",
163+
"index_id": "myindex",
164+
"index_uri": "s3://quickwit-indexes/myindex",
165+
"doc_mapping": {
166+
"field_mappings": [
167+
{
168+
"name": "foo",
169+
"type": "text",
170+
"fast": false,
171+
"fieldnorms": false,
172+
"indexed": true,
173+
"record": "basic",
174+
"stored": true,
175+
"tokenizer": "default"
176+
},
177+
{
178+
"name": "sub",
179+
"type": "object",
180+
"field_mappings": [
181+
{
182+
"fast": true,
183+
"fast_precision": "seconds",
184+
"indexed": true,
185+
"input_formats": [
186+
"rfc3339",
187+
"unix_timestamp"
188+
],
189+
"name": "timestamp",
190+
"output_format": "rfc3339",
191+
"stored": true,
192+
"type": "datetime"
193+
}
194+
]
195+
}
196+
],
197+
"tag_fields": [],
198+
"store_source": true,
199+
"index_field_presence": false,
200+
"timestamp_field": "timestamp",
201+
"mode": "dynamic",
202+
"dynamic_mapping": {},
203+
"partition_key": "foo",
204+
"max_num_partitions": 1,
205+
"tokenizers": []
206+
},
207+
"indexing_settings": {},
208+
"search_settings": {
209+
"default_search_fields": [
210+
"foo"
211+
]
212+
},
213+
"retention": null
214+
},
215+
"checkpoint": {},
216+
"create_timestamp": 1701075471,
217+
"sources": []
218+
}
219+
`)
220+
221+
// When
222+
_, _, err := DecodeTimestampFieldInfos(200, query)
223+
224+
// Then
225+
require.Error(t, err)
226+
})
227+
228+
t.Run("The timestamp field has not the right type", func(t *testing.T) {
229+
// Given
230+
query := []byte(`
231+
{
232+
"version": "0.6",
233+
"index_uid": "myindex:01HG7ZZK3ZD7XF6BKQCZJHSJ5W",
234+
"index_config": {
235+
"version": "0.6",
236+
"index_id": "myindex",
237+
"index_uri": "s3://quickwit-indexes/myindex",
238+
"doc_mapping": {
239+
"field_mappings": [
240+
{
241+
"name": "foo",
242+
"type": "text",
243+
"fast": false,
244+
"fieldnorms": false,
245+
"indexed": true,
246+
"record": "basic",
247+
"stored": true,
248+
"tokenizer": "default"
249+
},
250+
{
251+
"name": "sub",
252+
"type": "object",
253+
"field_mappings": [
254+
{
255+
"fast": true,
256+
"fast_precision": "seconds",
257+
"indexed": true,
258+
"input_formats": [
259+
"rfc3339",
260+
"unix_timestamp"
261+
],
262+
"name": "timestamp",
263+
"output_format": "rfc3339",
264+
"stored": true,
265+
"type": "whatever"
266+
}
267+
]
268+
}
269+
],
270+
"tag_fields": [],
271+
"store_source": true,
272+
"index_field_presence": false,
273+
"timestamp_field": "sub.timestamp",
274+
"mode": "dynamic",
275+
"dynamic_mapping": {},
276+
"partition_key": "foo",
277+
"max_num_partitions": 1,
278+
"tokenizers": []
279+
},
280+
"indexing_settings": {},
281+
"search_settings": {
282+
"default_search_fields": [
283+
"foo"
284+
]
285+
},
286+
"retention": null
287+
},
288+
"checkpoint": {},
289+
"create_timestamp": 1701075471,
290+
"sources": []
291+
}
292+
`)
293+
294+
// When
295+
_, _, err := DecodeTimestampFieldInfos(200, query)
296+
297+
// Then
298+
require.Error(t, err)
299+
})
300+
})
301+
}
302+
303+
func TestNewErrorCreationPayload(t *testing.T) {
304+
t.Run("Test marshall creation payload error", func(t *testing.T) {
305+
// When
306+
err := NewErrorCreationPayload(400, "No valid format")
307+
308+
// Then
309+
require.Error(t, err)
310+
require.ErrorContains(t, err, "\"message\":\"No valid format\"")
311+
require.ErrorContains(t, err, "\"status\":400")
312+
})
313+
}

0 commit comments

Comments
 (0)