Skip to content

Commit 690f68a

Browse files
author
Devdutt Shenoi
authored
test: relocate to utils/json (#1253)
Signed-off-by: Devdutt Shenoi <[email protected]>
1 parent 283aa21 commit 690f68a

File tree

2 files changed

+142
-46
lines changed

2 files changed

+142
-46
lines changed

src/handlers/http/ingest.rs

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ mod tests {
395395
use crate::{
396396
event::format::{json, EventFormat},
397397
metadata::SchemaVersion,
398-
utils::json::{convert_array_to_object, flatten::convert_to_array},
399398
};
400399

401400
trait TestExt {
@@ -560,21 +559,6 @@ mod tests {
560559
assert_eq!(rb.num_columns(), 1);
561560
}
562561

563-
#[test]
564-
fn non_object_arr_is_err() {
565-
let json = json!([1]);
566-
567-
assert!(convert_array_to_object(
568-
json,
569-
None,
570-
None,
571-
None,
572-
SchemaVersion::V0,
573-
&crate::event::format::LogSource::default()
574-
)
575-
.is_err())
576-
}
577-
578562
#[test]
579563
fn array_into_recordbatch_inffered_schema() {
580564
let json = json!([
@@ -770,28 +754,17 @@ mod tests {
770754
{
771755
"a": 1,
772756
"b": "hello",
773-
"c": [{"a": 1}]
757+
"c_a": [1],
774758
},
775759
{
776760
"a": 1,
777761
"b": "hello",
778-
"c": [{"a": 1, "b": 2}]
762+
"c_a": [1],
763+
"c_b": [2],
779764
},
780765
]);
781-
let flattened_json = convert_to_array(
782-
convert_array_to_object(
783-
json,
784-
None,
785-
None,
786-
None,
787-
SchemaVersion::V0,
788-
&crate::event::format::LogSource::default(),
789-
)
790-
.unwrap(),
791-
)
792-
.unwrap();
793766

794-
let (rb, _) = json::Event::new(flattened_json)
767+
let (rb, _) = json::Event::new(json)
795768
.into_recordbatch(
796769
&HashMap::default(),
797770
false,
@@ -859,28 +832,17 @@ mod tests {
859832
{
860833
"a": 1,
861834
"b": "hello",
862-
"c": [{"a": 1}]
835+
"c_a": 1,
863836
},
864837
{
865838
"a": 1,
866839
"b": "hello",
867-
"c": [{"a": 1, "b": 2}]
840+
"c_a": 1,
841+
"c_b": 2,
868842
},
869843
]);
870-
let flattened_json = convert_to_array(
871-
convert_array_to_object(
872-
json,
873-
None,
874-
None,
875-
None,
876-
SchemaVersion::V1,
877-
&crate::event::format::LogSource::default(),
878-
)
879-
.unwrap(),
880-
)
881-
.unwrap();
882844

883-
let (rb, _) = json::Event::new(flattened_json)
845+
let (rb, _) = json::Event::new(json)
884846
.into_recordbatch(
885847
&HashMap::default(),
886848
false,

src/utils/json/mod.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,138 @@ mod tests {
278278
assert_eq!(deserialized.value, original.value);
279279
assert_eq!(deserialized.other_field, original.other_field);
280280
}
281+
282+
#[test]
283+
fn non_object_arr_is_err() {
284+
let json = json!([1]);
285+
286+
assert!(flatten_json_body(
287+
json,
288+
None,
289+
None,
290+
None,
291+
SchemaVersion::V0,
292+
false,
293+
&crate::event::format::LogSource::default()
294+
)
295+
.is_err())
296+
}
297+
298+
#[test]
299+
fn arr_obj_with_nested_type() {
300+
let json = json!([
301+
{
302+
"a": 1,
303+
"b": "hello",
304+
},
305+
{
306+
"a": 1,
307+
"b": "hello",
308+
},
309+
{
310+
"a": 1,
311+
"b": "hello",
312+
"c": [{"a": 1}]
313+
},
314+
{
315+
"a": 1,
316+
"b": "hello",
317+
"c": [{"a": 1, "b": 2}]
318+
},
319+
]);
320+
let flattened_json = flatten_json_body(
321+
json,
322+
None,
323+
None,
324+
None,
325+
SchemaVersion::V0,
326+
false,
327+
&crate::event::format::LogSource::default(),
328+
)
329+
.unwrap();
330+
331+
assert_eq!(
332+
json!([
333+
{
334+
"a": 1,
335+
"b": "hello",
336+
},
337+
{
338+
"a": 1,
339+
"b": "hello",
340+
},
341+
{
342+
"a": 1,
343+
"b": "hello",
344+
"c_a": [1],
345+
},
346+
{
347+
"a": 1,
348+
"b": "hello",
349+
"c_a": [1],
350+
"c_b": [2],
351+
},
352+
]),
353+
flattened_json
354+
);
355+
}
356+
357+
#[test]
358+
fn arr_obj_with_nested_type_v1() {
359+
let json = json!([
360+
{
361+
"a": 1,
362+
"b": "hello",
363+
},
364+
{
365+
"a": 1,
366+
"b": "hello",
367+
},
368+
{
369+
"a": 1,
370+
"b": "hello",
371+
"c": [{"a": 1}]
372+
},
373+
{
374+
"a": 1,
375+
"b": "hello",
376+
"c": [{"a": 1, "b": 2}]
377+
},
378+
]);
379+
let flattened_json = flatten_json_body(
380+
json,
381+
None,
382+
None,
383+
None,
384+
SchemaVersion::V1,
385+
false,
386+
&crate::event::format::LogSource::default(),
387+
)
388+
.unwrap();
389+
390+
assert_eq!(
391+
json!([
392+
{
393+
"a": 1,
394+
"b": "hello",
395+
},
396+
{
397+
"a": 1,
398+
"b": "hello",
399+
},
400+
{
401+
"a": 1,
402+
"b": "hello",
403+
"c_a": 1,
404+
},
405+
{
406+
"a": 1,
407+
"b": "hello",
408+
"c_a": 1,
409+
"c_b": 2,
410+
},
411+
]),
412+
flattened_json
413+
);
414+
}
281415
}

0 commit comments

Comments
 (0)