Skip to content

Commit

Permalink
Add some missing unit and doc tests for DeltaTablePartition
Browse files Browse the repository at this point in the history
I was using this for a project today and noticed no tests, oops.
  • Loading branch information
rtyler committed May 7, 2023
1 parent 395d48b commit 005b874
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions rust/src/partitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String>),
default_for_null: &'a str,
Expand All @@ -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());
}
}

0 comments on commit 005b874

Please sign in to comment.