Skip to content

Commit

Permalink
cqlvalue: make chrono conversions test utility
Browse files Browse the repository at this point in the history
Made `CqlValue`-to-chrono types conversion methods a private
utility methods used only for tests.
  • Loading branch information
muzarski committed May 14, 2024
1 parent 7da18c9 commit 3e366d4
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions scylla-cql/src/frame/response/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ impl CqlValue {
}
}

#[cfg(test)]
#[cfg(feature = "chrono-04")]
pub fn as_naive_date(&self) -> Option<chrono_04::NaiveDate> {
fn as_naive_date_04(&self) -> Option<chrono_04::NaiveDate> {
self.as_cql_date().and_then(|date| date.try_into().ok())
}

Expand All @@ -206,8 +207,9 @@ impl CqlValue {
}
}

#[cfg(test)]
#[cfg(feature = "chrono-04")]
pub fn as_datetime(&self) -> Option<chrono_04::DateTime<chrono_04::Utc>> {
fn as_datetime_04(&self) -> Option<chrono_04::DateTime<chrono_04::Utc>> {
self.as_cql_timestamp().and_then(|ts| ts.try_into().ok())
}

Expand All @@ -224,8 +226,9 @@ impl CqlValue {
}
}

#[cfg(test)]
#[cfg(feature = "chrono-04")]
pub fn as_naive_time(&self) -> Option<chrono_04::NaiveTime> {
fn as_naive_time_04(&self) -> Option<chrono_04::NaiveTime> {
self.as_cql_time().and_then(|ts| ts.try_into().ok())
}

Expand Down Expand Up @@ -1405,7 +1408,7 @@ mod tests {
super::deser_cql_value(&ColumnType::Date, &mut (1u32 << 31).to_be_bytes().as_ref())
.unwrap();

assert_eq!(date.as_naive_date(), Some(unix_epoch));
assert_eq!(date.as_naive_date_04(), Some(unix_epoch));

// 2^31 - 30 when converted to NaiveDate is 1969-12-02
let before_epoch = NaiveDate::from_ymd_opt(1969, 12, 2).unwrap();
Expand All @@ -1415,7 +1418,7 @@ mod tests {
)
.unwrap();

assert_eq!(date.as_naive_date(), Some(before_epoch));
assert_eq!(date.as_naive_date_04(), Some(before_epoch));

// 2^31 + 30 when converted to NaiveDate is 1970-01-31
let after_epoch = NaiveDate::from_ymd_opt(1970, 1, 31).unwrap();
Expand All @@ -1425,20 +1428,20 @@ mod tests {
)
.unwrap();

assert_eq!(date.as_naive_date(), Some(after_epoch));
assert_eq!(date.as_naive_date_04(), Some(after_epoch));

// 0 and u32::MAX are out of NaiveDate range, fails with an error, not panics
assert_eq!(
super::deser_cql_value(&ColumnType::Date, &mut 0_u32.to_be_bytes().as_ref())
.unwrap()
.as_naive_date(),
.as_naive_date_04(),
None
);

assert_eq!(
super::deser_cql_value(&ColumnType::Date, &mut u32::MAX.to_be_bytes().as_ref())
.unwrap()
.as_naive_date(),
.as_naive_date_04(),
None
);
}
Expand Down Expand Up @@ -1527,7 +1530,7 @@ mod tests {
let time =
super::deser_cql_value(&ColumnType::Time, &mut (0i64).to_be_bytes().as_ref()).unwrap();

assert_eq!(time.as_naive_time(), Some(midnight));
assert_eq!(time.as_naive_time_04(), Some(midnight));

// 10:10:30.500,000,001
let (h, m, s, n) = (10, 10, 30, 500_000_001);
Expand All @@ -1540,7 +1543,7 @@ mod tests {
)
.unwrap();

assert_eq!(time.as_naive_time(), Some(midnight));
assert_eq!(time.as_naive_time_04(), Some(midnight));

// 23:59:59.999,999,999
let (h, m, s, n) = (23, 59, 59, 999_999_999);
Expand All @@ -1553,7 +1556,7 @@ mod tests {
)
.unwrap();

assert_eq!(time.as_naive_time(), Some(midnight));
assert_eq!(time.as_naive_time_04(), Some(midnight));
}

#[cfg(feature = "time-03")]
Expand Down Expand Up @@ -1619,7 +1622,7 @@ mod tests {
let date = super::deser_cql_value(&ColumnType::Timestamp, &mut 0i64.to_be_bytes().as_ref())
.unwrap();

assert_eq!(date.as_datetime(), Some(unix_epoch));
assert_eq!(date.as_datetime_04(), Some(unix_epoch));

// When converted to NaiveDateTime, this is 1969-12-01 11:29:29.5
let timestamp: i64 = -((((30 * 24 + 12) * 60 + 30) * 60 + 30) * 1000 + 500);
Expand All @@ -1634,7 +1637,7 @@ mod tests {
)
.unwrap();

assert_eq!(date.as_datetime(), Some(before_epoch));
assert_eq!(date.as_datetime_04(), Some(before_epoch));

// when converted to NaiveDateTime, this is is 1970-01-31 12:30:30.5
let timestamp: i64 = (((30 * 24 + 12) * 60 + 30) * 60 + 30) * 1000 + 500;
Expand All @@ -1649,20 +1652,20 @@ mod tests {
)
.unwrap();

assert_eq!(date.as_datetime(), Some(after_epoch));
assert_eq!(date.as_datetime_04(), Some(after_epoch));

// 0 and u32::MAX are out of NaiveDate range, fails with an error, not panics
assert_eq!(
super::deser_cql_value(&ColumnType::Timestamp, &mut i64::MIN.to_be_bytes().as_ref())
.unwrap()
.as_datetime(),
.as_datetime_04(),
None
);

assert_eq!(
super::deser_cql_value(&ColumnType::Timestamp, &mut i64::MAX.to_be_bytes().as_ref())
.unwrap()
.as_datetime(),
.as_datetime_04(),
None
);
}
Expand Down

0 comments on commit 3e366d4

Please sign in to comment.