Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Use macro in test to avoid repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
VasanthakumarV committed Sep 25, 2021
1 parent 1a86a0e commit ae517e7
Showing 1 changed file with 67 additions and 233 deletions.
300 changes: 67 additions & 233 deletions tests/it/compute/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,140 +2,56 @@ use arrow2::array::*;
use arrow2::compute::temporal::*;
use arrow2::datatypes::*;

#[test]
fn temporal_hour() {
for data_type in TestData::available_time_like_types() {
let data = TestData::data(&data_type);
let result = hour(&*data.input).unwrap();

assert_eq!(
result,
data.hour.unwrap(),
"\"hour\" failed on type: {:?}",
data_type
);
}
}

#[test]
fn temporal_minute() {
for data_type in TestData::available_time_like_types() {
let data = TestData::data(&data_type);
let result = minute(&*data.input).unwrap();

assert_eq!(
result,
data.minute.unwrap(),
"\"hour\" failed on type: {:?}",
data_type
);
}
}

#[test]
fn temporal_second() {
for data_type in TestData::available_time_like_types() {
let data = TestData::data(&data_type);
let result = second(&*data.input).unwrap();

assert_eq!(
result,
data.second.unwrap(),
"\"second\" failed on type: {:?}",
data_type
);
}
}

#[test]
fn temporal_nanosecond() {
for data_type in TestData::available_time_like_types() {
let data = TestData::data(&data_type);
let result = nanosecond(&*data.input).unwrap();

assert_eq!(
result,
data.nanosecond.unwrap(),
"\"nanosecond\" failed on type: {:?}",
data_type
);
}
}

#[test]
fn temporal_year() {
for data_type in TestData::available_date_like_types() {
let data = TestData::data(&data_type);
let result = year(&*data.input).unwrap();

assert_eq!(
result,
data.year.unwrap(),
"\"year\" failed on type: {:?}",
data_type
);
}
}

#[test]
fn temporal_month() {
for data_type in TestData::available_date_like_types() {
let data = TestData::data(&data_type);
let result = month(&*data.input).unwrap();

assert_eq!(
result,
data.month.unwrap(),
"\"month\" failed on type: {:?}",
data_type
);
}
}

#[test]
fn temporal_day() {
for data_type in TestData::available_date_like_types() {
let data = TestData::data(&data_type);
let result = day(&*data.input).unwrap();

assert_eq!(
result,
data.day.unwrap(),
"\"day\" failed on type: {:?}",
data_type
);
}
macro_rules! time_like {
($func:ident, $extract:ident) => {
#[test]
fn $func() {
for data_type in TestData::available_time_like_types() {
let data = TestData::data(&data_type);
let result = $extract(&*data.input).unwrap();

assert_eq!(
result,
data.$extract.unwrap(),
"\"{}\" failed on type: {:?}",
stringify!($extract),
data_type
);
}
}
};
}

#[test]
fn temporal_weekday() {
for data_type in TestData::available_date_like_types() {
let data = TestData::data(&data_type);
let result = weekday(&*data.input).unwrap();

assert_eq!(
result,
data.weekday.unwrap(),
"\"weekday\" failed on type: {:?}",
data_type
);
}
time_like!(temporal_hour, hour);
time_like!(temporal_minute, minute);
time_like!(temporal_second, second);
time_like!(temporal_nanosecond, nanosecond);

macro_rules! date_like {
($func:ident, $extract:ident) => {
#[test]
fn $func() {
for data_type in TestData::available_date_like_types() {
let data = TestData::data(&data_type);
let result = $extract(&*data.input).unwrap();

assert_eq!(
result,
data.$extract.unwrap(),
"\"{}\" failed on type: {:?}",
stringify!($extract),
data_type
);
}
}
};
}

#[test]
fn temporal_iso_week() {
for data_type in TestData::available_date_like_types() {
let data = TestData::data(&data_type);
let result = iso_week(&*data.input).unwrap();

assert_eq!(
result,
data.iso_week.unwrap(),
"\"iso_week\" failed on type: {:?}",
data_type
);
}
}
date_like!(temporal_year, year);
date_like!(temporal_month, month);
date_like!(temporal_day, day);
date_like!(temporal_weekday, weekday);
date_like!(temporal_iso_week, iso_week);

struct TestData {
input: Box<dyn Array>,
Expand Down Expand Up @@ -243,113 +159,31 @@ impl TestData {
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_hour() {
let test_data = test_data_tz();

for data in test_data {
let result = hour(&*data.input).unwrap();

assert_eq!(result, data.hour.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_minute() {
let test_data = test_data_tz();

for data in test_data {
let result = minute(&*data.input).unwrap();

assert_eq!(result, data.minute.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_second() {
let test_data = test_data_tz();

for data in test_data {
let result = second(&*data.input).unwrap();

assert_eq!(result, data.second.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_nanosecond() {
let test_data = test_data_tz();

for data in test_data {
let result = nanosecond(&*data.input).unwrap();

assert_eq!(result, data.nanosecond.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_year() {
let test_data = test_data_tz();

for data in test_data {
let result = year(&*data.input).unwrap();

assert_eq!(result, data.year.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_month() {
let test_data = test_data_tz();

for data in test_data {
let result = month(&*data.input).unwrap();

assert_eq!(result, data.month.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_day() {
let test_data = test_data_tz();
macro_rules! temporal_tz {
($func:ident, $extract:ident) => {
#[cfg(feature = "chrono-tz")]
#[test]
fn $func() {
let test_data = test_data_tz();

for data in test_data {
let result = day(&*data.input).unwrap();
for data in test_data {
let result = $extract(&*data.input).unwrap();

assert_eq!(result, data.day.unwrap());
}
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_weekday() {
let test_data = test_data_tz();

for data in test_data {
let result = weekday(&*data.input).unwrap();

assert_eq!(result, data.weekday.unwrap());
}
assert_eq!(result, data.$extract.unwrap());
}
}
};
}

#[cfg(feature = "chrono-tz")]
#[test]
fn temporal_tz_iso_week() {
let test_data = test_data_tz();

for data in test_data {
let result = iso_week(&*data.input).unwrap();

assert_eq!(result, data.iso_week.unwrap());
}
}
temporal_tz!(temporal_tz_hour, hour);
temporal_tz!(temporal_tz_minute, minute);
temporal_tz!(temporal_tz_second, second);
temporal_tz!(temporal_tz_nanosecond, nanosecond);
temporal_tz!(temporal_tz_year, year);
temporal_tz!(temporal_tz_month, month);
temporal_tz!(temporal_tz_day, day);
temporal_tz!(temporal_tz_weekday, weekday);
temporal_tz!(temporal_tz_iso_week, iso_week);

fn test_data_tz() -> Vec<TestData> {
vec![
Expand Down

0 comments on commit ae517e7

Please sign in to comment.