@@ -46,3 +46,134 @@ fn create_table(results: &[RecordBatch]) -> Result<Table, ArrowError> {
4646
4747 Ok ( table)
4848}
49+
50+ #[ cfg( test) ]
51+ mod tests {
52+ use super :: * ;
53+ use arrow_array:: { ArrayRef , Int32Array , StringArray } ;
54+ use arrow_schema:: { DataType , Field , Schema } ;
55+ use std:: sync:: Arc ;
56+
57+ fn create_test_schema ( ) -> Schema {
58+ Schema :: new ( vec ! [
59+ Field :: new( "id" , DataType :: Int32 , false ) ,
60+ Field :: new( "name" , DataType :: Utf8 , false ) ,
61+ ] )
62+ }
63+
64+ fn create_test_batch ( ) -> RecordBatch {
65+ let schema = Arc :: new ( create_test_schema ( ) ) ;
66+ let id_array: ArrayRef = Arc :: new ( Int32Array :: from ( vec ! [ 1 , 2 , 3 ] ) ) ;
67+ let name_array: ArrayRef = Arc :: new ( StringArray :: from ( vec ! [ "Alice" , "Bob" , "Charlie" ] ) ) ;
68+
69+ RecordBatch :: try_new ( schema, vec ! [ id_array, name_array] ) . unwrap ( )
70+ }
71+
72+ #[ test]
73+ fn test_create_table_empty ( ) {
74+ let results: Vec < RecordBatch > = vec ! [ ] ;
75+ let table = create_table ( & results) . unwrap ( ) ;
76+ // Empty table should be created successfully with no panic
77+ let _table_str = table. to_string ( ) ;
78+ }
79+
80+ #[ test]
81+ fn test_create_table_single_batch ( ) {
82+ let batch = create_test_batch ( ) ;
83+ let results = vec ! [ batch] ;
84+ let table = create_table ( & results) . unwrap ( ) ;
85+ let table_str = table. to_string ( ) ;
86+
87+ // Should contain headers
88+ assert ! ( table_str. contains( "id" ) ) ;
89+ assert ! ( table_str. contains( "name" ) ) ;
90+
91+ // Should contain data
92+ assert ! ( table_str. contains( "1" ) ) ;
93+ assert ! ( table_str. contains( "Alice" ) ) ;
94+ assert ! ( table_str. contains( "2" ) ) ;
95+ assert ! ( table_str. contains( "Bob" ) ) ;
96+ assert ! ( table_str. contains( "3" ) ) ;
97+ assert ! ( table_str. contains( "Charlie" ) ) ;
98+ }
99+
100+ #[ test]
101+ fn test_create_table_multiple_batches ( ) {
102+ let batch1 = create_test_batch ( ) ;
103+ let batch2 = create_test_batch ( ) ;
104+ let results = vec ! [ batch1, batch2] ;
105+ let table = create_table ( & results) . unwrap ( ) ;
106+ let table_str = table. to_string ( ) ;
107+
108+ // Should have data from both batches (6 rows total)
109+ assert ! ( table_str. contains( "Alice" ) ) ;
110+ assert ! ( table_str. contains( "Bob" ) ) ;
111+ assert ! ( table_str. contains( "Charlie" ) ) ;
112+ }
113+
114+ #[ test]
115+ fn test_create_table_mismatched_columns ( ) {
116+ let schema1 = Arc :: new ( Schema :: new ( vec ! [
117+ Field :: new( "id" , DataType :: Int32 , false ) ,
118+ Field :: new( "name" , DataType :: Utf8 , false ) ,
119+ ] ) ) ;
120+
121+ let schema2 = Arc :: new ( Schema :: new ( vec ! [ Field :: new( "id" , DataType :: Int32 , false ) ] ) ) ;
122+
123+ let batch1 = RecordBatch :: try_new (
124+ schema1,
125+ vec ! [
126+ Arc :: new( Int32Array :: from( vec![ 1 ] ) ) ,
127+ Arc :: new( StringArray :: from( vec![ "Alice" ] ) ) ,
128+ ] ,
129+ )
130+ . unwrap ( ) ;
131+
132+ let batch2 =
133+ RecordBatch :: try_new ( schema2, vec ! [ Arc :: new( Int32Array :: from( vec![ 2 ] ) ) ] ) . unwrap ( ) ;
134+
135+ let results = vec ! [ batch1, batch2] ;
136+ let result = create_table ( & results) ;
137+
138+ assert ! ( result. is_err( ) ) ;
139+ let err = result. unwrap_err ( ) ;
140+ assert ! (
141+ err. to_string( )
142+ . contains( "Expected the same number of columns" )
143+ ) ;
144+ }
145+
146+ #[ test]
147+ fn test_print_batches_empty ( ) {
148+ let results: Vec < RecordBatch > = vec ! [ ] ;
149+ // Should not panic on empty results
150+ let result = print_batches ( & results) ;
151+ assert ! ( result. is_ok( ) ) ;
152+ }
153+
154+ #[ test]
155+ fn test_create_table_with_nulls ( ) {
156+ let schema = Arc :: new ( Schema :: new ( vec ! [
157+ Field :: new( "id" , DataType :: Int32 , true ) ,
158+ Field :: new( "name" , DataType :: Utf8 , true ) ,
159+ ] ) ) ;
160+
161+ let id_array: ArrayRef = Arc :: new ( Int32Array :: from ( vec ! [ Some ( 1 ) , None , Some ( 3 ) ] ) ) ;
162+ let name_array: ArrayRef =
163+ Arc :: new ( StringArray :: from ( vec ! [ Some ( "Alice" ) , Some ( "Bob" ) , None ] ) ) ;
164+
165+ let batch = RecordBatch :: try_new ( schema, vec ! [ id_array, name_array] ) . unwrap ( ) ;
166+ let results = vec ! [ batch] ;
167+ let table = create_table ( & results) . unwrap ( ) ;
168+ let table_str = table. to_string ( ) ;
169+
170+ // Should contain headers
171+ assert ! ( table_str. contains( "id" ) ) ;
172+ assert ! ( table_str. contains( "name" ) ) ;
173+
174+ // Should contain data
175+ assert ! ( table_str. contains( "1" ) ) ;
176+ assert ! ( table_str. contains( "Alice" ) ) ;
177+ assert ! ( table_str. contains( "Bob" ) ) ;
178+ }
179+ }
0 commit comments