Skip to content

Commit

Permalink
test: refactor and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Dec 7, 2023
1 parent ce8995a commit 9ab9ffc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
18 changes: 14 additions & 4 deletions src/common/time/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ impl Date {

let (months, days, _) = interval.to_month_day_nano();

let naive_date = naive_date.checked_add_months(Months::new(months as u32))?;

naive_date
.checked_add_months(Months::new(months as u32))?
.checked_add_days(Days::new(days as u64))
.map(Into::into)
}
Expand All @@ -109,9 +108,8 @@ impl Date {

let (months, days, _) = interval.to_month_day_nano();

let naive_date = naive_date.checked_sub_months(Months::new(months as u32))?;

naive_date
.checked_sub_months(Months::new(months as u32))?
.checked_sub_days(Days::new(days as u64))
.map(Into::into)
}
Expand Down Expand Up @@ -153,6 +151,18 @@ mod tests {
assert_eq!(now, Date::from_str(&now).unwrap().to_string());
}

#[test]
fn test_add_sub_interval() {
let date = Date::new(1000);

let interval = Interval::from_year_month(3);

let new_date = date.add_interval(interval).unwrap();
assert_eq!(new_date.val(), 1091);

assert_eq!(date, new_date.sub_interval(interval).unwrap());
}

#[test]
pub fn test_min_max() {
let mut date = Date::from_str("9999-12-31").unwrap();
Expand Down
34 changes: 24 additions & 10 deletions src/common/time/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ impl DateTime {
let naive_datetime = self.to_chrono_datetime()?;
let (months, days, nsecs) = interval.to_month_day_nano();

let naive_datetime = naive_datetime.checked_add_months(Months::new(months as u32))?;
let naive_datetime = naive_datetime + Duration::from_nanos(nsecs as u64);
naive_datetime
.checked_add_days(Days::new(days as u64))
.map(Into::into)
let naive_datetime = naive_datetime
.checked_add_months(Months::new(months as u32))?
.checked_add_days(Days::new(days as u64))?
+ Duration::from_nanos(nsecs as u64);

Some(naive_datetime.into())
}

/// Subtracts given Interval to the current datetime.
Expand All @@ -137,11 +138,12 @@ impl DateTime {
let naive_datetime = self.to_chrono_datetime()?;
let (months, days, nsecs) = interval.to_month_day_nano();

let naive_datetime = naive_datetime.checked_sub_months(Months::new(months as u32))?;
let naive_datetime = naive_datetime - Duration::from_nanos(nsecs as u64);
naive_datetime
.checked_sub_days(Days::new(days as u64))
.map(Into::into)
let naive_datetime = naive_datetime
.checked_sub_months(Months::new(months as u32))?
.checked_sub_days(Days::new(days as u64))?
- Duration::from_nanos(nsecs as u64);

Some(naive_datetime.into())
}

/// Convert to [common_time::date].
Expand Down Expand Up @@ -178,6 +180,18 @@ mod tests {
assert_eq!(42, d.val());
}

#[test]
fn test_add_sub_interval() {
let datetime = DateTime::new(1000);

let interval = Interval::from_day_time(1, 200);

let new_datetime = datetime.add_interval(interval).unwrap();
assert_eq!(new_datetime.val(), 1000 + 3600 * 24 * 1000 + 200);

assert_eq!(datetime, new_datetime.sub_interval(interval).unwrap());
}

#[test]
fn test_parse_local_date_time() {
std::env::set_var("TZ", "Asia/Shanghai");
Expand Down
28 changes: 22 additions & 6 deletions src/common/time/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ impl Timestamp {
let naive_datetime = self.to_chrono_datetime()?;
let (months, days, nsecs) = interval.to_month_day_nano();

let naive_datetime = naive_datetime.checked_add_months(Months::new(months as u32))?;
let naive_datetime = naive_datetime.checked_add_days(Days::new(days as u64))?;
let naive_datetime = naive_datetime + Duration::from_nanos(nsecs as u64);
let naive_datetime = naive_datetime
.checked_add_months(Months::new(months as u32))?
.checked_add_days(Days::new(days as u64))?
+ Duration::from_nanos(nsecs as u64);

match Timestamp::from_chrono_datetime(naive_datetime) {
// Have to convert the new timestamp by the current unit.
Some(ts) => ts.convert_to(self.unit),
Expand All @@ -163,10 +165,11 @@ impl Timestamp {
let naive_datetime = self.to_chrono_datetime()?;
let (months, days, nsecs) = interval.to_month_day_nano();

let naive_datetime = naive_datetime.checked_sub_months(Months::new(months as u32))?;
let naive_datetime = naive_datetime
.checked_sub_months(Months::new(months as u32))?
.checked_sub_days(Days::new(days as u64))?
- Duration::from_nanos(nsecs as u64);

let naive_datetime = naive_datetime.checked_sub_days(Days::new(days as u64))?;
let naive_datetime = naive_datetime - Duration::from_nanos(nsecs as u64);
match Timestamp::from_chrono_datetime(naive_datetime) {
// Have to convert the new timestamp by the current unit.
Some(ts) => ts.convert_to(self.unit),
Expand Down Expand Up @@ -613,6 +616,19 @@ mod tests {
Timestamp::new(value, unit)
}

#[test]
fn test_add_sub_interval() {
let ts = Timestamp::new(1000, TimeUnit::Millisecond);

let interval = Interval::from_day_time(1, 200);

let new_ts = ts.add_interval(interval).unwrap();
assert_eq!(new_ts.unit(), TimeUnit::Millisecond);
assert_eq!(new_ts.value(), 1000 + 3600 * 24 * 1000 + 200);

assert_eq!(ts, new_ts.sub_interval(interval).unwrap());
}

#[test]
fn test_timestamp_reflexivity() {
for _ in 0..1000 {
Expand Down

0 comments on commit 9ab9ffc

Please sign in to comment.