|
27 | 27 | * it in the license file. |
28 | 28 | */ |
29 | 29 |
|
30 | | -/** |
31 | | - * This file contains tests for mongo/db/exec/working_set.cpp |
32 | | - */ |
33 | | - |
| 30 | +#include "mongo/platform/basic.h" |
34 | 31 |
|
35 | 32 | #include "mongo/db/exec/working_set.h" |
36 | 33 | #include "mongo/db/jsobj.h" |
37 | 34 | #include "mongo/db/json.h" |
| 35 | +#include "mongo/db/pipeline/document.h" |
38 | 36 | #include "mongo/db/storage/snapshot.h" |
39 | 37 | #include "mongo/unittest/unittest.h" |
40 | 38 | #include "mongo/util/assert_util.h" |
41 | 39 |
|
42 | | -using namespace mongo; |
43 | | - |
44 | | -namespace { |
| 40 | +namespace mongo { |
45 | 41 |
|
46 | 42 | using std::string; |
47 | 43 |
|
@@ -156,4 +152,42 @@ TEST_F(WorkingSetFixture, getDottedFieldFromIndex) { |
156 | 152 | ASSERT_FALSE(member->getFieldDotted("y", &elt)); |
157 | 153 | } |
158 | 154 |
|
159 | | -} // namespace |
| 155 | +TEST_F(WorkingSetFixture, MetadataCanBeCorrectlyTransferredBackAndForthFromDocument) { |
| 156 | + // Add some metadata to the WSM. |
| 157 | + member->metadata().setTextScore(1.2); |
| 158 | + member->metadata().setSearchScore(3.4); |
| 159 | + |
| 160 | + // Test that the metadata can be extracted from the WSM. |
| 161 | + auto releasedMetadata = member->releaseMetadata(); |
| 162 | + ASSERT_FALSE(member->metadata()); |
| 163 | + ASSERT_FALSE(member->metadata().hasTextScore()); |
| 164 | + ASSERT_FALSE(member->metadata().hasSearchScore()); |
| 165 | + ASSERT_TRUE(releasedMetadata); |
| 166 | + ASSERT_TRUE(releasedMetadata.hasTextScore()); |
| 167 | + ASSERT_TRUE(releasedMetadata.hasSearchScore()); |
| 168 | + |
| 169 | + // Test that the extracted metadata can be added to a Document. |
| 170 | + Document document; |
| 171 | + MutableDocument md{std::move(document)}; |
| 172 | + md.setMetadata(std::move(releasedMetadata)); |
| 173 | + document = md.freeze(); |
| 174 | + ASSERT_FALSE(releasedMetadata); |
| 175 | + ASSERT_FALSE(releasedMetadata.hasTextScore()); |
| 176 | + ASSERT_FALSE(releasedMetadata.hasSearchScore()); |
| 177 | + ASSERT_TRUE(document.metadata()); |
| 178 | + ASSERT_TRUE(document.metadata().hasTextScore()); |
| 179 | + ASSERT_TRUE(document.metadata().hasSearchScore()); |
| 180 | + |
| 181 | + // Test that metadata can be transferred back to the WSM. |
| 182 | + MutableDocument md2{std::move(document)}; |
| 183 | + member->setMetadata(md2.releaseMetadata()); |
| 184 | + document = md2.freeze(); |
| 185 | + ASSERT_FALSE(document.metadata()); |
| 186 | + ASSERT_FALSE(document.metadata().hasTextScore()); |
| 187 | + ASSERT_FALSE(document.metadata().hasSearchScore()); |
| 188 | + ASSERT_TRUE(member->metadata()); |
| 189 | + ASSERT_TRUE(member->metadata().hasTextScore()); |
| 190 | + ASSERT_TRUE(member->metadata().hasSearchScore()); |
| 191 | +} |
| 192 | + |
| 193 | +} // namespace mongo |
0 commit comments