Skip to content

Commit 98676f7

Browse files
committed
add: tests for validate_field_names
1 parent 225c3e5 commit 98676f7

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/static_schema.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ use serde::{Deserialize, Serialize};
2323
use std::str;
2424

2525
use arrow_schema::{DataType, Field, Schema, TimeUnit};
26-
use std::{collections::{HashMap,HashSet} , sync::Arc};
26+
use std::{
27+
collections::{HashMap, HashSet},
28+
sync::Arc,
29+
};
2730
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2831
pub struct StaticSchema {
2932
fields: Vec<SchemaFields>,
@@ -91,7 +94,6 @@ pub fn convert_static_schema_to_arrow_schema(
9194
let mut existing_field_names: HashSet<String> = HashSet::new();
9295

9396
for mut field in static_schema.fields {
94-
9597
validate_field_names(&field.name, &mut existing_field_names)?;
9698
if !time_partition.is_empty() && field.name == time_partition {
9799
time_partition_exists = true;
@@ -144,7 +146,6 @@ pub fn convert_static_schema_to_arrow_schema(
144146
fn add_parseable_fields_to_static_schema(
145147
parsed_schema: ParsedSchema,
146148
) -> Result<Arc<Schema>, AnyError> {
147-
148149
let mut schema: Vec<Arc<Field>> = Vec::new();
149150
for field in parsed_schema.fields.iter() {
150151
let field = Field::new(field.name.clone(), field.data_type.clone(), field.nullable);
@@ -183,8 +184,10 @@ fn default_dict_is_ordered() -> bool {
183184
false
184185
}
185186

186-
fn validate_field_names(field_name: &str, existing_fields: &mut HashSet<String>) -> Result<(), AnyError> {
187-
187+
fn validate_field_names(
188+
field_name: &str,
189+
existing_fields: &mut HashSet<String>,
190+
) -> Result<(), AnyError> {
188191
if field_name.is_empty() {
189192
return Err(anyhow!("field names should not be empty"));
190193
}
@@ -195,3 +198,21 @@ fn validate_field_names(field_name: &str, existing_fields: &mut HashSet<String>)
195198

196199
Ok(())
197200
}
201+
202+
#[cfg(test)]
203+
mod tests {
204+
use super::*;
205+
use std::collections::HashSet;
206+
#[test]
207+
fn empty_field_names() {
208+
let mut existing_field_names: HashSet<String> = HashSet::new();
209+
assert!(validate_field_names("", &mut existing_field_names).is_err());
210+
}
211+
212+
#[test]
213+
fn duplicate_field_names() {
214+
let mut existing_field_names: HashSet<String> = HashSet::new();
215+
let _ = validate_field_names("test_field", &mut existing_field_names);
216+
assert!(validate_field_names("test_field", &mut existing_field_names).is_err());
217+
}
218+
}

0 commit comments

Comments
 (0)