Skip to content

Commit

Permalink
AVRO-3900: Merge the validators integration tests in one test
Browse files Browse the repository at this point in the history
Just make sure the setup of the custom validators is done before the
parsing of any schema to prevent registering the default validator
(SpecificationValidator)

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
martin-g committed Dec 22, 2023
1 parent 8315922 commit eeb2b47
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 182 deletions.
2 changes: 1 addition & 1 deletion lang/rust/avro/examples/test_interop_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
// under the License.

use apache_avro::Reader;
use std::error::Error;
use std::{
collections::HashMap,
ffi::OsStr,
io::{BufReader, Read},
};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
let mut expected_user_metadata: HashMap<String, Vec<u8>> = HashMap::new();
Expand Down
44 changes: 0 additions & 44 deletions lang/rust/avro/tests/validator_enum_symbol_name.rs

This file was deleted.

49 changes: 0 additions & 49 deletions lang/rust/avro/tests/validator_record_field_name.rs

This file was deleted.

44 changes: 0 additions & 44 deletions lang/rust/avro/tests/validator_schema_name.rs

This file was deleted.

44 changes: 0 additions & 44 deletions lang/rust/avro/tests/validator_schema_namespace.rs

This file was deleted.

84 changes: 84 additions & 0 deletions lang/rust/avro/tests/validators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use apache_avro::schema::Namespace;
use apache_avro::validator::{
set_record_field_name_validator, set_schema_name_validator, set_schema_namespace_validator,
RecordFieldNameValidator, SchemaNameValidator, SchemaNamespaceValidator,
};
use apache_avro::{
validator::{set_enum_symbol_name_validator, EnumSymbolNameValidator},
AvroResult,
};
use apache_avro_test_helper::TestResult;

struct CustomValidator;

#[test]
fn avro_3900_custom_validator_with_spec_invalid_names() -> TestResult {
// Setup the custom validators before the schema is parsed
// because the parsing will trigger the validation and will
// setup the default validator (SpecificationValidator)!
impl SchemaNameValidator for CustomValidator {
fn validate(&self, schema_name: &str) -> AvroResult<(String, Namespace)> {
Ok((schema_name.to_string(), None))
}
}
impl SchemaNamespaceValidator for CustomValidator {
fn validate(&self, _ns: &str) -> AvroResult<()> {
Ok(())
}
}

impl EnumSymbolNameValidator for CustomValidator {
fn validate(&self, _ns: &str) -> AvroResult<()> {
Ok(())
}
}

impl RecordFieldNameValidator for CustomValidator {
fn validate(&self, _ns: &str) -> AvroResult<()> {
Ok(())
}
}

assert!(set_schema_name_validator(Box::new(CustomValidator)).is_ok());
assert!(set_schema_namespace_validator(Box::new(CustomValidator)).is_ok());
assert!(set_enum_symbol_name_validator(Box::new(CustomValidator)).is_ok());
assert!(set_record_field_name_validator(Box::new(CustomValidator)).is_ok());

let schema = r#"{
"name": "invalid-schema-name",
"namespace": "invalid-namespace",
"type": "record",
"fields": [
{
"name": "invalid-field-name",
"type": "int"
},
{
"type": "enum",
"name": "Test",
"symbols": ["A-B", "B-A"]
}
]
}"#;

apache_avro::Schema::parse_str(schema)?;

Ok(())
}

0 comments on commit eeb2b47

Please sign in to comment.