From 005b874364c1de2a1cc29f5a87274f670711f630 Mon Sep 17 00:00:00 2001 From: "R. Tyler Croy" Date: Sat, 6 May 2023 22:54:14 -0700 Subject: [PATCH] Add some missing unit and doc tests for DeltaTablePartition I was using this for a project today and noticed no tests, oops. --- rust/src/partitions.rs | 43 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/rust/src/partitions.rs b/rust/src/partitions.rs index c5f263b2e3..47f8a5cd61 100644 --- a/rust/src/partitions.rs +++ b/rust/src/partitions.rs @@ -189,8 +189,20 @@ pub struct DeltaTablePartition<'a> { pub value: &'a str, } -/// Create a DeltaTable partition from a HivePartition string. -/// A HivePartition string is represented by a "key=value" format. +/** + * Create a DeltaTable partition from a HivePartition string. + * + * A HivePartition string is represented by a "key=value" format. + * + * ```rust + * use deltalake::DeltaTablePartition; + * + * let hive_part = "ds=2023-01-01"; + * let partition = DeltaTablePartition::try_from(hive_part).unwrap(); + * assert_eq!("ds", partition.key); + * assert_eq!("2023-01-01", partition.value); + * ``` + */ impl<'a> TryFrom<&'a str> for DeltaTablePartition<'a> { type Error = DeltaTableError; @@ -211,7 +223,20 @@ impl<'a> TryFrom<&'a str> for DeltaTablePartition<'a> { } impl<'a> DeltaTablePartition<'a> { - /// Try to create a DeltaTable partition from a partition value kv pair. + /** + * Try to create a DeltaTable partition from a partition value kv pair. + * + * ```rust + * use deltalake::DeltaTablePartition; + * + * let value = (&"ds".to_string(), &Some("2023-01-01".to_string())); + * let null_default = "1979-01-01"; + * let partition = DeltaTablePartition::from_partition_value(value, null_default); + * + * assert_eq!("ds", partition.key); + * assert_eq!("2023-01-01", partition.value); + * ``` + */ pub fn from_partition_value( partition_value: (&'a String, &'a Option), default_for_null: &'a str, @@ -224,3 +249,15 @@ impl<'a> DeltaTablePartition<'a> { DeltaTablePartition { key: k, value: v } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tryfrom_invalid() { + let buf = "this-is-not-a-partition"; + let partition = DeltaTablePartition::try_from(buf); + assert!(partition.is_err()); + } +}