@@ -462,7 +462,12 @@ fn process_object_value(
462
462
463
463
#[ cfg( test) ]
464
464
mod tests {
465
- use crate :: utils:: json:: flatten:: { flatten_array_objects, generic_flattening, FlattenContext } ;
465
+ use std:: vec;
466
+
467
+ use crate :: utils:: json:: flatten:: {
468
+ create_nested_key, flatten_array_objects, generic_flattening, process_array_value,
469
+ process_json_array, process_json_object, FlattenContext ,
470
+ } ;
466
471
467
472
use super :: { flatten, JsonFlattenError } ;
468
473
use serde_json:: { json, Map , Value } ;
@@ -764,4 +769,107 @@ mod tests {
764
769
expected
765
770
) ;
766
771
}
772
+
773
+ #[ test]
774
+ fn test_process_json_array ( ) {
775
+ let context = FlattenContext {
776
+ current_level : 1 ,
777
+ separator : "_" ,
778
+ time_partition : None ,
779
+ time_partition_limit : None ,
780
+ custom_partition : None ,
781
+ flatten_depth_limit : 3 ,
782
+ } ;
783
+
784
+ let mut input = json ! ( [
785
+ { "name" : "John" , "age" : 30 , "address" : { "city" : "New York" , "state" : "NY" } } ,
786
+ { "name" : "Jane" , "age" : 25 , "address" : { "city" : "New York" , "state" : "NY" } }
787
+ ] ) ;
788
+
789
+ let input_arr = input. as_array_mut ( ) . unwrap ( ) ;
790
+
791
+ let expected = vec ! [
792
+ json!( { "name" : "John" , "age" : 30 , "address_city" : "New York" , "address_state" : "NY" } ) ,
793
+ json!( { "name" : "Jane" , "age" : 25 , "address_city" : "New York" , "address_state" : "NY" } ) ,
794
+ ] ;
795
+
796
+ assert_eq ! (
797
+ process_json_array( input_arr, & context, None ) . unwrap( ) ,
798
+ expected
799
+ ) ;
800
+
801
+ let mut input = json ! ( [
802
+ { "name" : "John" , "age" : 30 , "address" : { "city" : "New York" , "state" : "NY" } , "phone" : [ "123" , "456" ] } ,
803
+ { "name" : "Jane" , "age" : 25 , "address" : { "city" : "New York" , "state" : "NY" } , "phone" : [ "789" , "101" ] }
804
+ ] ) ;
805
+ let input_arr = input. as_array_mut ( ) . unwrap ( ) ;
806
+
807
+ let expected = vec ! [
808
+ json!( { "name" : "John" , "age" : 30 , "address_city" : "New York" , "address_state" : "NY" , "phone" : "123" } ) ,
809
+ json!( { "name" : "John" , "age" : 30 , "address_city" : "New York" , "address_state" : "NY" , "phone" : "456" } ) ,
810
+ json!( { "name" : "Jane" , "age" : 25 , "address_city" : "New York" , "address_state" : "NY" , "phone" : "789" } ) ,
811
+ json!( { "name" : "Jane" , "age" : 25 , "address_city" : "New York" , "address_state" : "NY" , "phone" : "101" } ) ,
812
+ ] ;
813
+
814
+ assert_eq ! (
815
+ process_json_array( input_arr, & context, None ) . unwrap( ) ,
816
+ expected
817
+ ) ;
818
+ }
819
+
820
+ #[ test]
821
+ fn test_process_json_object ( ) {
822
+ let context = FlattenContext {
823
+ current_level : 1 ,
824
+ separator : "_" ,
825
+ time_partition : None ,
826
+ time_partition_limit : None ,
827
+ custom_partition : None ,
828
+ flatten_depth_limit : 3 ,
829
+ } ;
830
+
831
+ let mut input = json ! (
832
+ { "name" : "Jane" , "age" : 25 , "address" : { "city" : "New York" , "state" : "NY" } }
833
+ ) ;
834
+
835
+ let input_map = input. as_object_mut ( ) . unwrap ( ) ;
836
+
837
+ let expected = vec ! [
838
+ json!( { "name" : "Jane" , "age" : 25 , "address_city" : "New York" , "address_state" : "NY" } ) ,
839
+ ] ;
840
+
841
+ assert_eq ! (
842
+ process_json_object( input_map, & context, None ) . unwrap( ) ,
843
+ expected
844
+ ) ;
845
+
846
+ let mut input = json ! (
847
+ { "name" : "Jane" , "age" : 25 , "address" : { "city" : "New York" , "state" : "NY" } , "phone" : [ "123" , "456" ] }
848
+ ) ;
849
+
850
+ let input_map = input. as_object_mut ( ) . unwrap ( ) ;
851
+
852
+ let expected = vec ! [
853
+ json!( { "name" : "Jane" , "age" : 25 , "address_city" : "New York" , "address_state" : "NY" , "phone" : "123" } ) ,
854
+ json!( { "name" : "Jane" , "age" : 25 , "address_city" : "New York" , "address_state" : "NY" , "phone" : "456" } ) ,
855
+ ] ;
856
+
857
+ assert_eq ! (
858
+ process_json_object( input_map, & context, None ) . unwrap( ) ,
859
+ expected
860
+ ) ;
861
+ }
862
+
863
+ #[ test]
864
+ fn test_create_nested_key ( ) {
865
+ let parent_key = Some ( "a" ) ;
866
+ let key = "b" ;
867
+ let separator = "_" ;
868
+ assert_eq ! ( create_nested_key( parent_key, key, separator) , "a_b" ) ;
869
+
870
+ let parent_key = None ;
871
+ let key = "b" ;
872
+ let separator = "_" ;
873
+ assert_eq ! ( create_nested_key( parent_key, key, separator) , "b" ) ;
874
+ }
767
875
}
0 commit comments