@@ -222,12 +222,42 @@ mod tests {
222
222
use super :: parse_decimal;
223
223
use crate :: core:: avro:: avro_schema:: AvroSchema ;
224
224
use crate :: core:: avro:: avro_schema:: RecordField ;
225
+ use crate :: core:: avro:: error:: AvroResult ;
225
226
use crate :: core:: avro:: AvroError ;
227
+ use crate :: core:: avro:: AvroParser ;
228
+ use crate :: core:: avro:: ResolvedAvroSchema ;
229
+ use crate :: core:: avro:: SchemaProvider ;
230
+ use apache_avro:: Schema ;
226
231
227
232
use apache_avro:: types:: Value as AvroValue ;
233
+ use async_trait:: async_trait;
228
234
use serde_json:: json;
229
235
use serde_json:: Value as JsonValue ;
230
236
237
+ struct MockSchemaProvider { }
238
+ #[ async_trait]
239
+ impl SchemaProvider for MockSchemaProvider {
240
+ async fn get_schema_by_id ( & self , id : i32 ) -> AvroResult < ResolvedAvroSchema > {
241
+ let json_schema = & get_test_avro_schema ( ) ;
242
+ let schema = Schema :: parse_str ( json_schema) . expect ( "invalid test schema" ) ;
243
+ Ok ( ResolvedAvroSchema :: from ( id, & schema) )
244
+ }
245
+ async fn get_schema_by_name ( & self , _: & str ) -> AvroResult < ResolvedAvroSchema > {
246
+ let json_schema = & get_test_avro_schema ( ) ;
247
+ let schema = Schema :: parse_str ( json_schema) . expect ( "invalid test schema" ) ;
248
+ Ok ( ResolvedAvroSchema :: from ( 123 , & schema) )
249
+ }
250
+ }
251
+
252
+ #[ tokio:: test]
253
+ async fn test_parse_nested_records_with_implicit_namespace ( ) {
254
+ let mock_provider = MockSchemaProvider { } ;
255
+ let sut = AvroParser :: new ( mock_provider. into ( ) ) ;
256
+ let sample_json = & get_test_avro_message ( ) ;
257
+ let res = sut. json_to_avro ( sample_json, "sample" ) . await ;
258
+ assert ! ( res. is_ok( ) )
259
+ }
260
+
231
261
#[ test]
232
262
fn test_decimal ( ) {
233
263
// happy path
@@ -311,4 +341,49 @@ mod tests {
311
341
schema : schema,
312
342
}
313
343
}
344
+
345
+ fn get_test_avro_message ( ) -> String {
346
+ r#"{
347
+ "outer_field_1": {
348
+ "middle_field_1": {
349
+ "inner_field_1": 1.7
350
+ },
351
+ "middle_field_2": {
352
+ "inner_field_1": 1.8
353
+ }
354
+ }
355
+ }"#
356
+ . to_string ( )
357
+ }
358
+ fn get_test_avro_schema ( ) -> String {
359
+ r#"{
360
+ "name": "record_name",
361
+ "namespace": "space",
362
+ "type": "record",
363
+ "fields": [
364
+ {
365
+ "name": "outer_field_1",
366
+ "type": {
367
+ "type": "record",
368
+ "name": "middle_record_name",
369
+ "namespace": "middle_namespace",
370
+ "fields": [
371
+ {
372
+ "name": "middle_field_1",
373
+ "type": {
374
+ "type": "record",
375
+ "name": "inner_record_name",
376
+ "fields": [
377
+ { "name": "inner_field_1", "type": "double" }
378
+ ]
379
+ }
380
+ },
381
+ { "name": "middle_field_2", "type": "inner_record_name" }
382
+ ]
383
+ }
384
+ }
385
+ ]
386
+ }"#
387
+ . to_string ( )
388
+ }
314
389
}
0 commit comments