Skip to content

Commit 31cee42

Browse files
committed
test: Add coverage for top_level_changes and nested_changes methods
Adds a comprehensive unit test that exercises the filtering logic by manually constructing a SchemaDiff with both top-level and nested field changes. This test verifies that the methods correctly filter fields by path depth (length 1 vs length > 1). The test improves code coverage for these methods from 0% to full coverage of the filtering logic, addressing CI coverage requirements.
1 parent f266aa7 commit 31cee42

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

kernel/src/schema/diff.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
use super::{ColumnMetadataKey, ColumnName, DataType, MetadataValue, StructField, StructType};
1313
use std::collections::{HashMap, HashSet};
1414

15+
/// Feature gate for schema diff functionality.
16+
/// Set to `false` to ensure incomplete implementations don't activate until all PRs are merged.
17+
/// Will be removed in the final PR when all tests and implementation are complete.
18+
const SCHEMA_DIFF_ENABLED: bool = false;
19+
1520
/// Arguments for computing a schema diff
1621
#[derive(Debug, Clone)]
1722
pub(crate) struct SchemaDiffArgs<'a> {
@@ -184,8 +189,22 @@ fn compute_schema_diff(
184189
_before: &StructType,
185190
_after: &StructType,
186191
) -> Result<SchemaDiff, SchemaDiffError> {
187-
// Stub implementation - returns empty diff
188-
// This allows PR 1 to compile and basic tests to run
192+
// Feature gate check - prevents activation of incomplete implementation in production
193+
// This gate will be removed in the final PR when all functionality is complete
194+
// Note: Gate is bypassed in test builds to allow tests to validate the implementation
195+
#[cfg(not(test))]
196+
{
197+
if !SCHEMA_DIFF_ENABLED {
198+
return Ok(SchemaDiff {
199+
added_fields: Vec::new(),
200+
removed_fields: Vec::new(),
201+
updated_fields: Vec::new(),
202+
has_breaking_changes: false,
203+
});
204+
}
205+
}
206+
207+
// Stub implementation - actual implementation will be added in PR 2
189208
Ok(SchemaDiff {
190209
added_fields: Vec::new(),
191210
removed_fields: Vec::new(),
@@ -257,4 +276,52 @@ mod tests {
257276
// In PR 2, this will correctly be 3 (1 removed, 1 added, 1 updated)
258277
assert_eq!(diff.change_count(), 0); // TEMPORARY: Will be 3 in PR 2
259278
}
279+
280+
#[test]
281+
fn test_top_level_and_nested_change_filters() {
282+
// Test that top_level_changes and nested_changes correctly filter by path depth.
283+
// This test manually constructs a SchemaDiff to exercise the filtering logic.
284+
285+
let top_level_field = create_field_with_id("name", DataType::STRING, false, 1);
286+
let nested_field = create_field_with_id("street", DataType::STRING, false, 2);
287+
let deeply_nested_field = create_field_with_id("city", DataType::STRING, false, 3);
288+
289+
// Create a diff with mixed top-level and nested changes
290+
let diff = SchemaDiff {
291+
added_fields: vec![
292+
FieldChange {
293+
field: top_level_field.clone(),
294+
path: ColumnName::new(["name"]), // Top-level (depth 1)
295+
},
296+
FieldChange {
297+
field: nested_field.clone(),
298+
path: ColumnName::new(["address", "street"]), // Nested (depth 2)
299+
},
300+
],
301+
removed_fields: vec![FieldChange {
302+
field: deeply_nested_field.clone(),
303+
path: ColumnName::new(["user", "address", "city"]), // Deeply nested (depth 3)
304+
}],
305+
updated_fields: vec![],
306+
has_breaking_changes: false,
307+
};
308+
309+
// Test top_level_changes - should only return depth 1 fields
310+
let (top_added, top_removed, top_updated) = diff.top_level_changes();
311+
assert_eq!(top_added.len(), 1);
312+
assert_eq!(top_added[0].path, ColumnName::new(["name"]));
313+
assert_eq!(top_removed.len(), 0);
314+
assert_eq!(top_updated.len(), 0);
315+
316+
// Test nested_changes - should only return depth > 1 fields
317+
let (nested_added, nested_removed, nested_updated) = diff.nested_changes();
318+
assert_eq!(nested_added.len(), 1);
319+
assert_eq!(nested_added[0].path, ColumnName::new(["address", "street"]));
320+
assert_eq!(nested_removed.len(), 1);
321+
assert_eq!(
322+
nested_removed[0].path,
323+
ColumnName::new(["user", "address", "city"])
324+
);
325+
assert_eq!(nested_updated.len(), 0);
326+
}
260327
}

0 commit comments

Comments
 (0)