Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin-Ray committed Sep 17, 2024
1 parent 8439c1d commit d6335cc
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 11 deletions.
46 changes: 40 additions & 6 deletions crates/proof-of-sql-parser/src/posql_time/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl fmt::Display for PoSQLTimeZone {

#[cfg(test)]
mod timezone_parsing_tests {
use crate::posql_time::timezone;
use crate::posql_time::{timezone, PoSQLTimeZone, PoSQLTimestamp};

#[test]
fn test_display_fixed_offset_positive() {
Expand All @@ -102,11 +102,6 @@ mod timezone_parsing_tests {
let timezone = timezone::PoSQLTimeZone::Utc;
assert_eq!(format!("{}", timezone), "+00:00");
}
}

#[cfg(test)]
mod timezone_offset_tests {
use crate::posql_time::{timestamp::PoSQLTimestamp, timezone};

#[test]
fn test_utc_timezone() {
Expand Down Expand Up @@ -139,4 +134,43 @@ mod timezone_offset_tests {
let result = PoSQLTimestamp::try_from(input).unwrap();
assert_eq!(result.timezone(), expected_timezone);
}

#[test]
fn test_from_offset() {
// UTC case
let tz = PoSQLTimeZone::from_offset(0);
assert!(matches!(tz, PoSQLTimeZone::Utc));

// Fixed offset case
let tz = PoSQLTimeZone::from_offset(3600); // UTC+1
assert!(matches!(tz, PoSQLTimeZone::FixedOffset(3600)));

// Negative offset case (UTC-5)
let tz = PoSQLTimeZone::from_offset(-18000);
assert!(matches!(tz, PoSQLTimeZone::FixedOffset(-18000)));
}

#[test]
fn test_normalize_to_utc_with_utc() {
let timestamp = 1231006505; // Unix timestamp in seconds
let tz = PoSQLTimeZone::Utc;
let normalized = PoSQLTimeZone::normalize_to_utc(timestamp, &tz);
assert_eq!(normalized, timestamp); // No adjustment for UTC
}

#[test]
fn test_normalize_to_utc_with_positive_offset() {
let timestamp = 1231006505; // Unix timestamp in seconds
let tz = PoSQLTimeZone::FixedOffset(3600); // UTC+1 (3600 seconds offset)
let normalized = PoSQLTimeZone::normalize_to_utc(timestamp, &tz);
assert_eq!(normalized, 1231006505 - 3600); // Adjusted by 1 hour
}

#[test]
fn test_normalize_to_utc_with_negative_offset() {
let timestamp = 1231006505; // Unix timestamp in seconds
let tz = PoSQLTimeZone::FixedOffset(-18000); // UTC-5 (18000 seconds offset)
let normalized = PoSQLTimeZone::normalize_to_utc(timestamp, &tz);
assert_eq!(normalized, 1231006505 + 18000); // Adjusted by 5 hours
}
}
145 changes: 144 additions & 1 deletion crates/proof-of-sql-parser/src/posql_time/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum PoSQLTimeUnit {

impl PoSQLTimeUnit {
/// Convert the numerical unit of one timeunit to another for comparison purposes.
pub fn convert_timeunit(
pub fn normalize_timeunit(
timestamp: i64,
from_unit: &PoSQLTimeUnit,
to_unit: &PoSQLTimeUnit,
Expand Down Expand Up @@ -138,4 +138,147 @@ mod time_unit_tests {
expected.timestamp_nanos_opt().unwrap()
);
}
#[test]
fn test_normalize_timeunit_seconds_to_milliseconds() {
let timestamp = 1231006505; // seconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Second,
&PoSQLTimeUnit::Millisecond,
);
assert_eq!(result, 1231006505000); // converted to milliseconds
}

#[test]
fn test_normalize_timeunit_seconds_to_microseconds() {
let timestamp = 1231006505; // seconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Second,
&PoSQLTimeUnit::Microsecond,
);
assert_eq!(result, 1231006505000000); // converted to microseconds
}

#[test]
fn test_normalize_timeunit_seconds_to_nanoseconds() {
let timestamp = 1231006505; // seconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Second,
&PoSQLTimeUnit::Nanosecond,
);
assert_eq!(result, 1231006505000000000); // converted to nanoseconds
}

#[test]
fn test_normalize_timeunit_milliseconds_to_seconds() {
let timestamp = 1231006505000; // milliseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Millisecond,
&PoSQLTimeUnit::Second,
);
assert_eq!(result, 1231006505); // converted to seconds
}

#[test]
fn test_normalize_timeunit_milliseconds_to_microseconds() {
let timestamp = 1231006505; // milliseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Millisecond,
&PoSQLTimeUnit::Microsecond,
);
assert_eq!(result, 1231006505000); // converted to microseconds
}

#[test]
fn test_normalize_timeunit_milliseconds_to_nanoseconds() {
let timestamp = 1231006505; // milliseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Millisecond,
&PoSQLTimeUnit::Nanosecond,
);
assert_eq!(result, 1231006505000000); // converted to nanoseconds
}

#[test]
fn test_normalize_timeunit_microseconds_to_seconds() {
let timestamp = 1231006505000000; // microseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Microsecond,
&PoSQLTimeUnit::Second,
);
assert_eq!(result, 1231006505); // converted to seconds
}

#[test]
fn test_normalize_timeunit_microseconds_to_milliseconds() {
let timestamp = 1231006505000; // microseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Microsecond,
&PoSQLTimeUnit::Millisecond,
);
assert_eq!(result, 1231006505); // converted to milliseconds
}

#[test]
fn test_normalize_timeunit_microseconds_to_nanoseconds() {
let timestamp = 1231006505; // microseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Microsecond,
&PoSQLTimeUnit::Nanosecond,
);
assert_eq!(result, 1231006505000); // converted to nanoseconds
}

#[test]
fn test_normalize_timeunit_nanoseconds_to_seconds() {
let timestamp = 1231006505000000000; // nanoseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Nanosecond,
&PoSQLTimeUnit::Second,
);
assert_eq!(result, 1231006505); // converted to seconds
}

#[test]
fn test_normalize_timeunit_nanoseconds_to_milliseconds() {
let timestamp = 1231006505000000; // nanoseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Nanosecond,
&PoSQLTimeUnit::Millisecond,
);
assert_eq!(result, 1231006505); // converted to milliseconds
}

#[test]
fn test_normalize_timeunit_nanoseconds_to_microseconds() {
let timestamp = 1231006505000; // nanoseconds
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Nanosecond,
&PoSQLTimeUnit::Microsecond,
);
assert_eq!(result, 1231006505); // converted to microseconds
}

#[test]
fn test_normalize_timeunit_same_units() {
// If from_unit and to_unit are the same, it should return the timestamp as is
let timestamp = 1231006505;
let result = PoSQLTimeUnit::normalize_timeunit(
timestamp,
&PoSQLTimeUnit::Second,
&PoSQLTimeUnit::Second,
);
assert_eq!(result, timestamp); // No conversion needed
}
}
6 changes: 3 additions & 3 deletions crates/proof-of-sql/src/base/database/column_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ pub(crate) fn slice_eq_with_casting_and_timeunit(
) -> Vec<bool> {
let lhs_adjusted: Vec<i64> = lhs
.iter()
.map(|&ts| PoSQLTimeUnit::convert_timeunit(ts, lhs_unit, rhs_unit))
.map(|&ts| PoSQLTimeUnit::normalize_timeunit(ts, lhs_unit, rhs_unit))
.collect();

slice_eq(&lhs_adjusted, rhs)
Expand All @@ -914,7 +914,7 @@ pub(crate) fn slice_le_with_casting_and_timeunit(
) -> Vec<bool> {
let lhs_adjusted: Vec<i64> = lhs
.iter()
.map(|&ts| PoSQLTimeUnit::convert_timeunit(ts, lhs_unit, rhs_unit))
.map(|&ts| PoSQLTimeUnit::normalize_timeunit(ts, lhs_unit, rhs_unit))
.collect();

lhs_adjusted
Expand All @@ -933,7 +933,7 @@ pub(crate) fn slice_ge_with_casting_and_timeunit(
) -> Vec<bool> {
let lhs_adjusted: Vec<i64> = lhs
.iter()
.map(|&ts| PoSQLTimeUnit::convert_timeunit(ts, lhs_unit, rhs_unit))
.map(|&ts| PoSQLTimeUnit::normalize_timeunit(ts, lhs_unit, rhs_unit))
.collect();

lhs_adjusted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ mod test {
}

#[test]
fn we_can_do_le_operation_on_numeric_and_boolean_columns() {
fn we_can_do_le_operation_on_numeric_boolean_and_timestamp_columns() {
// Booleans
let lhs = OwnedColumn::<Curve25519Scalar>::Boolean(vec![true, false, true]);
let rhs = OwnedColumn::<Curve25519Scalar>::Boolean(vec![true, true, false]);
Expand Down

0 comments on commit d6335cc

Please sign in to comment.