Skip to content

Commit 1e25b45

Browse files
pedroerpfacebook-github-bot
authored andcommitted
DateTime unit test tidy up (facebookincubator#10613)
Summary: Pull Request resolved: facebookincubator#10613 Just some unit test hygiene to follow to make them more consistent and less verbose. Reviewed By: amitkdutta Differential Revision: D60486547 fbshipit-source-id: a11c5ad19efbaa85ba2278816203040d0085c7b6
1 parent c8ac8f7 commit 1e25b45

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

velox/functions/prestosql/tests/DateTimeFunctionsTest.cpp

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -121,33 +121,6 @@ class DateTimeFunctionsTest : public functions::test::FunctionBaseTest {
121121
}
122122
};
123123

124-
std::optional<Timestamp> dateParse(
125-
const std::optional<std::string>& input,
126-
const std::optional<std::string>& format) {
127-
auto resultVector = evaluate(
128-
"date_parse(c0, c1)",
129-
makeRowVector(
130-
{makeNullableFlatVector<std::string>({input}),
131-
makeNullableFlatVector<std::string>({format})}));
132-
EXPECT_EQ(1, resultVector->size());
133-
134-
if (resultVector->isNullAt(0)) {
135-
return std::nullopt;
136-
}
137-
return resultVector->as<SimpleVector<Timestamp>>()->valueAt(0);
138-
}
139-
140-
std::optional<std::string> dateFormat(
141-
std::optional<Timestamp> timestamp,
142-
const std::string& format) {
143-
auto resultVector = evaluate(
144-
"date_format(c0, c1)",
145-
makeRowVector(
146-
{makeNullableFlatVector<Timestamp>({timestamp}),
147-
makeNullableFlatVector<std::string>({format})}));
148-
return resultVector->as<SimpleVector<StringView>>()->valueAt(0);
149-
}
150-
151124
template <typename T>
152125
std::optional<T> evaluateWithTimestampWithTimezone(
153126
const std::string& expression,
@@ -3311,14 +3284,14 @@ TEST_F(DateTimeFunctionsTest, formatDateTimeTimezone) {
33113284
}
33123285

33133286
TEST_F(DateTimeFunctionsTest, dateFormat) {
3314-
const auto dateFormatOnce = [&](std::optional<Timestamp> timestamp,
3315-
const std::string& formatString) {
3316-
return evaluateOnce<std::string>(
3317-
fmt::format("date_format(c0, '{}')", formatString), timestamp);
3287+
const auto dateFormat = [&](std::optional<Timestamp> timestamp,
3288+
std::optional<std::string> format) {
3289+
return evaluateOnce<std::string>("date_format(c0, c1)", timestamp, format);
33183290
};
33193291

33203292
// Check null behaviors
3321-
EXPECT_EQ(std::nullopt, dateFormatOnce(std::nullopt, "%Y"));
3293+
EXPECT_EQ(std::nullopt, dateFormat(std::nullopt, "%Y"));
3294+
EXPECT_EQ(std::nullopt, dateFormat(Timestamp(0, 0), std::nullopt));
33223295

33233296
// Normal cases
33243297
EXPECT_EQ("1970-01-01", dateFormat(parseTimestamp("1970-01-01"), "%Y-%m-%d"));
@@ -3766,10 +3739,12 @@ TEST_F(DateTimeFunctionsTest, fromIso8601Timestamp) {
37663739
}
37673740

37683741
TEST_F(DateTimeFunctionsTest, dateParseMonthOfYearText) {
3769-
auto parseAndFormat = [&](const std::string& input) {
3770-
return dateFormat(dateParse(input, "%M_%Y"), "%Y-%m");
3742+
auto parseAndFormat = [&](std::optional<std::string> input) {
3743+
return evaluateOnce<std::string>(
3744+
"date_format(date_parse(c0, '%M_%Y'), '%Y-%m')", input);
37713745
};
37723746

3747+
EXPECT_EQ(parseAndFormat(std::nullopt), std::nullopt);
37733748
EXPECT_EQ(parseAndFormat("jan_2024"), "2024-01");
37743749
EXPECT_EQ(parseAndFormat("JAN_2024"), "2024-01");
37753750
EXPECT_EQ(parseAndFormat("january_2024"), "2024-01");
@@ -3830,6 +3805,11 @@ TEST_F(DateTimeFunctionsTest, dateParseMonthOfYearText) {
38303805
}
38313806

38323807
TEST_F(DateTimeFunctionsTest, dateParse) {
3808+
const auto dateParse = [&](std::optional<std::string> input,
3809+
std::optional<std::string> format) {
3810+
return evaluateOnce<Timestamp>("date_parse(c0, c1)", input, format);
3811+
};
3812+
38333813
// Check null behavior.
38343814
EXPECT_EQ(std::nullopt, dateParse("1970-01-01", std::nullopt));
38353815
EXPECT_EQ(std::nullopt, dateParse(std::nullopt, "YYYY-MM-dd"));

0 commit comments

Comments
 (0)