summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/expression_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline/expression_test.cpp')
-rw-r--r--src/mongo/db/pipeline/expression_test.cpp42
1 files changed, 41 insertions, 1 deletions
diff --git a/src/mongo/db/pipeline/expression_test.cpp b/src/mongo/db/pipeline/expression_test.cpp
index f4066e10d3a..ae2ed802f91 100644
--- a/src/mongo/db/pipeline/expression_test.cpp
+++ b/src/mongo/db/pipeline/expression_test.cpp
@@ -4904,6 +4904,7 @@ TEST_F(ExpressionDateToStringTest, SerializesToObjectSyntax) {
Document{{"format", "%Y-%m-%d"_sd},
{"date", Document{{"$const", Date_t{}}}},
{"timezone", Document{{"$const", "Europe/London"_sd}}}}}});
+
ASSERT_VALUE_EQ(dateExp->serialize(true), expectedSerialization);
ASSERT_VALUE_EQ(dateExp->serialize(false), expectedSerialization);
}
@@ -4966,9 +4967,48 @@ TEST_F(ExpressionDateToStringTest, OptimizesToConstantIfAllInputsAreConstant) {
dateExp = Expression::parseExpression(expCtx, spec, expCtx->variablesParseState);
ASSERT_FALSE(dynamic_cast<ExpressionConstant*>(dateExp->optimize().get()));
}
-
} // namespace ExpressionDateToStringTest
+namespace ExpressionDateFromStringTest {
+
+// This provides access to an ExpressionContext that has a valid ServiceContext with a
+// TimeZoneDatabase via getExpCtx(), but we'll use a different name for this test suite.
+using ExpressionDateFromStringTest = AggregationContextFixture;
+
+TEST_F(ExpressionDateFromStringTest, SerializesToObjectSyntax) {
+ auto expCtx = getExpCtx();
+
+ // Test that it serializes to the full format if given an object specification.
+ BSONObj spec = BSON("$dateFromString" << BSON("dateString"
+ << "2017-07-04T13:06:44Z"));
+ auto dateExp = Expression::parseExpression(expCtx, spec, expCtx->variablesParseState);
+ auto expectedSerialization = Value(
+ Document{{"$dateFromString",
+ Document{{"dateString", Document{{"$const", "2017-07-04T13:06:44Z"_sd}}}}}});
+
+ ASSERT_VALUE_EQ(dateExp->serialize(true), expectedSerialization);
+ ASSERT_VALUE_EQ(dateExp->serialize(false), expectedSerialization);
+}
+
+TEST_F(ExpressionDateFromStringTest, OptimizesToConstantIfAllInputsAreConstant) {
+ auto expCtx = getExpCtx();
+ auto spec = BSON("$dateFromString" << BSON("dateString"
+ << "2017-07-04T13:09:57Z"));
+ auto dateExp = Expression::parseExpression(expCtx, spec, expCtx->variablesParseState);
+ ASSERT(dynamic_cast<ExpressionConstant*>(dateExp->optimize().get()));
+
+ Date_t dateVal = Date_t::fromMillisSinceEpoch(1499173797000);
+ ASSERT_VALUE_EQ(Value(dateVal), dateExp->evaluate(Document{}));
+
+ // Test that it does *not* become a constant if dateString is not a constant.
+ spec = BSON("$dateFromString" << BSON("dateString"
+ << "$date"));
+ dateExp = Expression::parseExpression(expCtx, spec, expCtx->variablesParseState);
+ ASSERT_FALSE(dynamic_cast<ExpressionConstant*>(dateExp->optimize().get()));
+}
+
+} // namespace ExpressionDateFromStringTest
+
class All : public Suite {
public:
All() : Suite("expression") {}