summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/document_source_unwind_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline/document_source_unwind_test.cpp')
-rw-r--r--src/mongo/db/pipeline/document_source_unwind_test.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/document_source_unwind_test.cpp b/src/mongo/db/pipeline/document_source_unwind_test.cpp
index aa8a550ec06..c8c7b46f0c3 100644
--- a/src/mongo/db/pipeline/document_source_unwind_test.cpp
+++ b/src/mongo/db/pipeline/document_source_unwind_test.cpp
@@ -734,6 +734,61 @@ TEST_F(UnwindStageTest, UnwindIncludesIndexPathWhenIncludingIndex) {
ASSERT_EQUALS(1U, modifiedPaths.paths.count("arrIndex"));
}
+TEST_F(UnwindStageTest, UnwindIndexPathIsSamePathAsArrayPath) {
+ const bool includeNullIfEmptyOrMissing = false;
+ const boost::optional<std::string> includeArrayIndex = std::string("array");
+ auto unwind = DocumentSourceUnwind::create(
+ getExpCtx(), "array", includeNullIfEmptyOrMissing, includeArrayIndex);
+ auto source = DocumentSourceMock::createForTest(
+ {Document{{"array", vector<Value>{Value(10), Value(20)}}},
+ Document{{"array", vector<Value>{Value(30), Value(40)}}}},
+ getExpCtx());
+
+ unwind->setSource(source.get());
+
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(0));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(1));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(0));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(1));
+}
+
+TEST_F(UnwindStageTest, UnwindIndexPathIsParentOfArrayPath) {
+ const bool includeNullIfEmptyOrMissing = false;
+ const boost::optional<std::string> includeArrayIndex = std::string("obj");
+ auto unwind = DocumentSourceUnwind::create(
+ getExpCtx(), "obj.array", includeNullIfEmptyOrMissing, includeArrayIndex);
+ auto source = DocumentSourceMock::createForTest(
+ {Document{{"obj", Document{{"array", vector<Value>{Value(10), Value(20)}}}}},
+ Document{{"obj", Document{{"array", vector<Value>{Value(30), Value(40)}}}}}},
+ getExpCtx());
+
+ unwind->setSource(source.get());
+
+ Document res;
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["obj"], Value(0));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["obj"], Value(1));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["obj"], Value(0));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["obj"], Value(1));
+}
+
+TEST_F(UnwindStageTest, UnwindIndexPathIsChildOfArrayPath) {
+ const bool includeNullIfEmptyOrMissing = false;
+ const boost::optional<std::string> includeArrayIndex = std::string("array.index");
+ auto unwind = DocumentSourceUnwind::create(
+ getExpCtx(), "array", includeNullIfEmptyOrMissing, includeArrayIndex);
+ auto source = DocumentSourceMock::createForTest(
+ {Document{{"array", vector<Value>{Value(10), Value(20)}}},
+ Document{{"array", vector<Value>{Value(30), Value(40)}}}},
+ getExpCtx());
+
+ unwind->setSource(source.get());
+
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(BSON("index" << 0)));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(BSON("index" << 1)));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(BSON("index" << 0)));
+ ASSERT_VALUE_EQ(unwind->getNext().getDocument()["array"], Value(BSON("index" << 1)));
+}
+
//
// Error cases.
//
@@ -816,6 +871,26 @@ TEST_F(UnwindStageTest, ShouldRejectUnrecognizedOption) {
28811);
}
+TEST_F(UnwindStageTest, Redaction) {
+ auto spec = fromjson(R"({
+ $unwind: {
+ path: "$foo.bar",
+ includeArrayIndex: "foo.baz",
+ preserveNullAndEmptyArrays: true
+ }
+ })");
+ auto docSource = DocumentSourceUnwind::createFromBson(spec.firstElement(), getExpCtx());
+ ASSERT_BSONOBJ_EQ_AUTO( // NOLINT
+ R"({
+ "$unwind": {
+ "path": "$HASH<foo>.HASH<bar>",
+ "preserveNullAndEmptyArrays": "?bool",
+ "includeArrayIndex": "HASH<foo>.HASH<baz>"
+ }
+ })",
+ redact(*docSource));
+}
+
class All : public OldStyleSuiteSpecification {
public:
All() : OldStyleSuiteSpecification("DocumentSourceUnwindTests") {}