Skip to content

Commit

Permalink
feat: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin-Ray committed Jun 18, 2024
1 parent a2a04a1 commit 0e045bc
Show file tree
Hide file tree
Showing 13 changed files with 630 additions and 154 deletions.
46 changes: 43 additions & 3 deletions crates/proof-of-sql/src/base/commitment/column_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ impl ColumnBounds {
(ColumnBounds::Int128(bounds_a), ColumnBounds::Int128(bounds_b)) => {
Ok(ColumnBounds::Int128(bounds_a.difference(bounds_b)))
}

(ColumnBounds::TimestampTZ(bounds_a), ColumnBounds::TimestampTZ(bounds_b)) => {
Ok(ColumnBounds::TimestampTZ(bounds_a.difference(bounds_b)))
}
(_, _) => Err(ColumnBoundsMismatch(Box::new(self), Box::new(other))),
}
}
Expand All @@ -286,7 +288,12 @@ impl ColumnBounds {
#[cfg(test)]
mod tests {
use super::*;
use crate::base::{database::OwnedColumn, math::decimal::Precision, scalar::Curve25519Scalar};
use crate::base::{
database::OwnedColumn,
math::decimal::Precision,
scalar::Curve25519Scalar,
time::timestamp::{PoSQLTimeUnit, PoSQLTimeZone},
};
use itertools::Itertools;

#[test]
Expand Down Expand Up @@ -526,8 +533,19 @@ mod tests {
);
let committable_decimal75_column = CommittableColumn::from(&decimal75_column);
let decimal75_column_bounds = ColumnBounds::from_column(&committable_decimal75_column);

assert_eq!(decimal75_column_bounds, ColumnBounds::NoOrder);

let timestamp_column = OwnedColumn::<Curve25519Scalar>::TimestampTZ(
PoSQLTimeUnit::Second,
PoSQLTimeZone::UTC,
vec![1_i64, 2, 3, 4],
);
let committable_timestamp_column = CommittableColumn::from(&timestamp_column);
let timestamp_column_bounds = ColumnBounds::from_column(&committable_timestamp_column);
assert_eq!(
timestamp_column_bounds,
ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 1, max: 4 }))
);
}

#[test]
Expand Down Expand Up @@ -569,6 +587,14 @@ mod tests {
int128_a.try_union(int128_b).unwrap(),
ColumnBounds::Int128(Bounds::Bounded(BoundsInner { min: 1, max: 6 }))
);

let timestamp_a = ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let timestamp_b =
ColumnBounds::TimestampTZ(Bounds::Bounded(BoundsInner { min: 4, max: 6 }));
assert_eq!(
timestamp_a.try_union(timestamp_b).unwrap(),
ColumnBounds::TimestampTZ(Bounds::Bounded(BoundsInner { min: 1, max: 6 }))
);
}

#[test]
Expand All @@ -578,13 +604,15 @@ mod tests {
let int = ColumnBounds::Int(Bounds::Sharp(BoundsInner { min: -10, max: 10 }));
let bigint = ColumnBounds::BigInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let int128 = ColumnBounds::Int128(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
let timestamp = ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));

let bounds = [
(no_order, "NoOrder"),
(smallint, "SmallInt"),
(int, "Int"),
(bigint, "BigInt"),
(int128, "Int128"),
(timestamp, "Timestamp"),
];

for ((bound_a, name_a), (bound_b, name_b)) in bounds.iter().tuple_combinations() {
Expand Down Expand Up @@ -626,13 +654,22 @@ mod tests {
int128_a.try_difference(int128_b).unwrap(),
ColumnBounds::Int128(Bounds::Bounded(BoundsInner { min: 1, max: 4 }))
);

let timestamp_a = ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 1, max: 4 }));
let timestamp_b = ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 3, max: 6 }));
assert_eq!(
timestamp_a.try_difference(timestamp_b).unwrap(),
ColumnBounds::TimestampTZ(Bounds::Bounded(BoundsInner { min: 1, max: 4 }))
);
}

#[test]
fn we_cannot_difference_mismatched_column_bounds() {
let no_order = ColumnBounds::NoOrder;
let bigint = ColumnBounds::BigInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));
let int128 = ColumnBounds::Int128(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
let timestamp = ColumnBounds::TimestampTZ(Bounds::Sharp(BoundsInner { min: 4, max: 6 }));
let smallint = ColumnBounds::SmallInt(Bounds::Sharp(BoundsInner { min: 1, max: 3 }));

assert!(no_order.try_difference(bigint).is_err());
assert!(bigint.try_difference(no_order).is_err());
Expand All @@ -642,5 +679,8 @@ mod tests {

assert!(bigint.try_difference(int128).is_err());
assert!(int128.try_difference(bigint).is_err());

assert!(smallint.try_difference(timestamp).is_err());
assert!(timestamp.try_difference(smallint).is_err());
}
}
149 changes: 148 additions & 1 deletion crates/proof-of-sql/src/base/commitment/column_commitment_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ impl ColumnCommitmentMetadata {
mod tests {
use super::*;
use crate::base::{
commitment::column_bounds::Bounds, database::OwnedColumn, math::decimal::Precision,
commitment::column_bounds::Bounds,
database::OwnedColumn,
math::decimal::Precision,
scalar::Curve25519Scalar,
time::timestamp::{PoSQLTimeUnit, PoSQLTimeZone},
};

#[test]
Expand Down Expand Up @@ -224,6 +227,18 @@ mod tests {
}
);

assert_eq!(
ColumnCommitmentMetadata::try_new(
ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::UTC),
ColumnBounds::TimestampTZ(Bounds::Empty),
)
.unwrap(),
ColumnCommitmentMetadata {
column_type: ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::UTC),
bounds: ColumnBounds::TimestampTZ(Bounds::Empty),
}
);

assert_eq!(
ColumnCommitmentMetadata::try_new(
ColumnType::Int128,
Expand Down Expand Up @@ -354,6 +369,26 @@ mod tests {
);
assert_eq!(decimal_metadata.bounds(), &ColumnBounds::NoOrder);

let timestamp_column: OwnedColumn<Curve25519Scalar> =
OwnedColumn::<Curve25519Scalar>::TimestampTZ(
PoSQLTimeUnit::Second,
PoSQLTimeZone::UTC,
[1i64, 2, 3, 4, 5].to_vec(),
);
let committable_timestamp_column = CommittableColumn::from(&timestamp_column);
let timestamp_metadata =
ColumnCommitmentMetadata::from_column(&committable_timestamp_column);
assert_eq!(
timestamp_metadata.column_type(),
&ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::UTC)
);
if let ColumnBounds::TimestampTZ(Bounds::Sharp(bounds)) = timestamp_metadata.bounds() {
assert_eq!(bounds.min(), &1);
assert_eq!(bounds.max(), &5);
} else {
panic!("Bounds constructed from nonempty TimestampTZ column should be ColumnBounds::BigInt(Bounds::Sharp(_))");
}

let varchar_column = OwnedColumn::<Curve25519Scalar>::VarChar(
["Lorem", "ipsum", "dolor", "sit", "amet"]
.map(String::from)
Expand Down Expand Up @@ -489,6 +524,80 @@ mod tests {
bigint_metadata_a.try_union(bigint_metadata_b).unwrap(),
bigint_metadata_c
);

// Ordered case for TimestampTZ
// Example Unix epoch times
let times = [
1_625_072_400,
1_625_076_000,
1_625_079_600,
1_625_072_400,
1_625_065_000,
];
let timezone = PoSQLTimeZone::UTC;
let timeunit = PoSQLTimeUnit::Second;
let timestamp_column_a = CommittableColumn::TimestampTZ(timeunit, timezone, &times[..2]);
let timestamp_metadata_a = ColumnCommitmentMetadata::from_column(&timestamp_column_a);
let timestamp_column_b = CommittableColumn::TimestampTZ(timeunit, timezone, &times[2..]);
let timestamp_metadata_b = ColumnCommitmentMetadata::from_column(&timestamp_column_b);
let timestamp_column_c = CommittableColumn::TimestampTZ(timeunit, timezone, &times);
let timestamp_metadata_c = ColumnCommitmentMetadata::from_column(&timestamp_column_c);
assert_eq!(
timestamp_metadata_a
.try_union(timestamp_metadata_b)
.unwrap(),
timestamp_metadata_c
);
}

#[test]
fn we_can_difference_timestamp_tz_matching_metadata() {
// Ordered case
let times = [
1_625_072_400,
1_625_076_000,
1_625_079_600,
1_625_072_400,
1_625_065_000,
];
let timezone = PoSQLTimeZone::UTC;
let timeunit = PoSQLTimeUnit::Second;

let timestamp_column_a = CommittableColumn::TimestampTZ(timeunit, timezone, &times[..2]);
let timestamp_metadata_a = ColumnCommitmentMetadata::from_column(&timestamp_column_a);
let timestamp_column_b = CommittableColumn::TimestampTZ(timeunit, timezone, &times);
let timestamp_metadata_b = ColumnCommitmentMetadata::from_column(&timestamp_column_b);

let b_difference_a = timestamp_metadata_b
.try_difference(timestamp_metadata_a)
.unwrap();
assert_eq!(
b_difference_a.column_type,
ColumnType::TimestampTZ(timeunit, timezone)
);
if let ColumnBounds::TimestampTZ(Bounds::Bounded(bounds)) = b_difference_a.bounds {
assert_eq!(bounds.min(), &1_625_065_000);
assert_eq!(bounds.max(), &1_625_079_600);
} else {
panic!("difference of overlapping bounds should be Bounded");
}

let timestamp_column_empty = CommittableColumn::TimestampTZ(timeunit, timezone, &[]);
let timestamp_metadata_empty =
ColumnCommitmentMetadata::from_column(&timestamp_column_empty);

assert_eq!(
timestamp_metadata_b
.try_difference(timestamp_metadata_empty)
.unwrap(),
timestamp_metadata_b
);
assert_eq!(
timestamp_metadata_empty
.try_difference(timestamp_metadata_b)
.unwrap(),
timestamp_metadata_empty
);
}

#[test]
Expand Down Expand Up @@ -746,5 +855,43 @@ mod tests {
assert!(different_decimal75_metadata
.try_union(decimal75_metadata)
.is_err());

let timestamp_tz_metadata_a = ColumnCommitmentMetadata {
column_type: ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::UTC),
bounds: ColumnBounds::TimestampTZ(Bounds::Empty),
};

let timestamp_tz_metadata_b = ColumnCommitmentMetadata {
column_type: ColumnType::TimestampTZ(PoSQLTimeUnit::Millisecond, PoSQLTimeZone::UTC),
bounds: ColumnBounds::TimestampTZ(Bounds::Empty),
};

// Tests for union operations
assert!(timestamp_tz_metadata_a.try_union(varchar_metadata).is_err());
assert!(varchar_metadata.try_union(timestamp_tz_metadata_a).is_err());

// Tests for difference operations
assert!(timestamp_tz_metadata_a
.try_difference(scalar_metadata)
.is_err());
assert!(scalar_metadata
.try_difference(timestamp_tz_metadata_a)
.is_err());

// Tests for different time units within the same type
assert!(timestamp_tz_metadata_a
.try_union(timestamp_tz_metadata_b)
.is_err());
assert!(timestamp_tz_metadata_b
.try_union(timestamp_tz_metadata_a)
.is_err());

// Difference with different time units
assert!(timestamp_tz_metadata_a
.try_difference(timestamp_tz_metadata_b)
.is_err());
assert!(timestamp_tz_metadata_b
.try_difference(timestamp_tz_metadata_a)
.is_err());
}
}
Loading

0 comments on commit 0e045bc

Please sign in to comment.