From 2352a4ac2e7c22405bd013df1b46f5025fb9e98b Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Wed, 19 Mar 2025 21:12:51 +0530 Subject: [PATCH] test: relocate to `utils/json` --- src/handlers/http/ingest.rs | 54 +++------------ src/utils/json/mod.rs | 134 ++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 46 deletions(-) diff --git a/src/handlers/http/ingest.rs b/src/handlers/http/ingest.rs index 1e5e6d048..07eafc58a 100644 --- a/src/handlers/http/ingest.rs +++ b/src/handlers/http/ingest.rs @@ -385,7 +385,6 @@ mod tests { use crate::{ event::format::{json, EventFormat}, metadata::SchemaVersion, - utils::json::{convert_array_to_object, flatten::convert_to_array}, }; trait TestExt { @@ -538,21 +537,6 @@ mod tests { assert_eq!(rb.num_columns(), 1); } - #[test] - fn non_object_arr_is_err() { - let json = json!([1]); - - assert!(convert_array_to_object( - json, - None, - None, - None, - SchemaVersion::V0, - &crate::event::format::LogSource::default() - ) - .is_err()) - } - #[test] fn array_into_recordbatch_inffered_schema() { let json = json!([ @@ -736,28 +720,17 @@ mod tests { { "a": 1, "b": "hello", - "c": [{"a": 1}] + "c_a": [1], }, { "a": 1, "b": "hello", - "c": [{"a": 1, "b": 2}] + "c_a": [1], + "c_b": [2], }, ]); - let flattened_json = convert_to_array( - convert_array_to_object( - json, - None, - None, - None, - SchemaVersion::V0, - &crate::event::format::LogSource::default(), - ) - .unwrap(), - ) - .unwrap(); - let (rb, _) = json::Event::new(flattened_json) + let (rb, _) = json::Event::new(json) .into_recordbatch(&HashMap::default(), false, None, SchemaVersion::V0) .unwrap(); assert_eq!(rb.num_rows(), 4); @@ -819,28 +792,17 @@ mod tests { { "a": 1, "b": "hello", - "c": [{"a": 1}] + "c_a": 1, }, { "a": 1, "b": "hello", - "c": [{"a": 1, "b": 2}] + "c_a": 1, + "c_b": 2, }, ]); - let flattened_json = convert_to_array( - convert_array_to_object( - json, - None, - None, - None, - SchemaVersion::V1, - &crate::event::format::LogSource::default(), - ) - .unwrap(), - ) - .unwrap(); - let (rb, _) = json::Event::new(flattened_json) + let (rb, _) = json::Event::new(json) .into_recordbatch(&HashMap::default(), false, None, SchemaVersion::V1) .unwrap(); diff --git a/src/utils/json/mod.rs b/src/utils/json/mod.rs index efa9cb2e2..4eeb7465f 100644 --- a/src/utils/json/mod.rs +++ b/src/utils/json/mod.rs @@ -278,4 +278,138 @@ mod tests { assert_eq!(deserialized.value, original.value); assert_eq!(deserialized.other_field, original.other_field); } + + #[test] + fn non_object_arr_is_err() { + let json = json!([1]); + + assert!(flatten_json_body( + json, + None, + None, + None, + SchemaVersion::V0, + false, + &crate::event::format::LogSource::default() + ) + .is_err()) + } + + #[test] + fn arr_obj_with_nested_type() { + let json = json!([ + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + "c": [{"a": 1}] + }, + { + "a": 1, + "b": "hello", + "c": [{"a": 1, "b": 2}] + }, + ]); + let flattened_json = flatten_json_body( + json, + None, + None, + None, + SchemaVersion::V0, + false, + &crate::event::format::LogSource::default(), + ) + .unwrap(); + + assert_eq!( + json!([ + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + "c_a": [1], + }, + { + "a": 1, + "b": "hello", + "c_a": [1], + "c_b": [2], + }, + ]), + flattened_json + ); + } + + #[test] + fn arr_obj_with_nested_type_v1() { + let json = json!([ + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + "c": [{"a": 1}] + }, + { + "a": 1, + "b": "hello", + "c": [{"a": 1, "b": 2}] + }, + ]); + let flattened_json = flatten_json_body( + json, + None, + None, + None, + SchemaVersion::V1, + false, + &crate::event::format::LogSource::default(), + ) + .unwrap(); + + assert_eq!( + json!([ + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + }, + { + "a": 1, + "b": "hello", + "c_a": 1, + }, + { + "a": 1, + "b": "hello", + "c_a": 1, + "c_b": 2, + }, + ]), + flattened_json + ); + } }