summaryrefslogtreecommitdiff
path: root/src/mongo/db/fts/fts_index_format_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/fts/fts_index_format_test.cpp')
-rw-r--r--src/mongo/db/fts/fts_index_format_test.cpp323
1 files changed, 171 insertions, 152 deletions
diff --git a/src/mongo/db/fts/fts_index_format_test.cpp b/src/mongo/db/fts/fts_index_format_test.cpp
index a15d014e98c..f7c8a5fa432 100644
--- a/src/mongo/db/fts/fts_index_format_test.cpp
+++ b/src/mongo/db/fts/fts_index_format_test.cpp
@@ -42,165 +42,184 @@
namespace mongo {
- namespace fts {
-
- using std::string;
-
- TEST( FTSIndexFormat, Simple1 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" ) ) ) );
- BSONObjSet keys;
- FTSIndexFormat::getKeys( spec, BSON( "data" << "cat sat" ), &keys );
-
- ASSERT_EQUALS( 2U, keys.size() );
- for ( BSONObjSet::const_iterator i = keys.begin(); i != keys.end(); ++i ) {
- BSONObj key = *i;
- ASSERT_EQUALS( 2, key.nFields() );
- ASSERT_EQUALS( String, key.firstElement().type() );
- }
- }
-
- TEST( FTSIndexFormat, ExtraBack1 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" <<
- "x" << 1 ) ) ) );
- BSONObjSet keys;
- FTSIndexFormat::getKeys( spec, BSON( "data" << "cat" << "x" << 5 ), &keys );
-
- ASSERT_EQUALS( 1U, keys.size() );
- BSONObj key = *(keys.begin());
- ASSERT_EQUALS( 3, key.nFields() );
- BSONObjIterator i( key );
- ASSERT_EQUALS( StringData("cat"), i.next().valuestr() );
- ASSERT( i.next().numberDouble() > 0 );
- ASSERT_EQUALS( 5, i.next().numberInt() );
- }
+namespace fts {
+
+using std::string;
+
+TEST(FTSIndexFormat, Simple1) {
+ FTSSpec spec(FTSSpec::fixSpec(BSON("key" << BSON("data"
+ << "text"))));
+ BSONObjSet keys;
+ FTSIndexFormat::getKeys(spec,
+ BSON("data"
+ << "cat sat"),
+ &keys);
+
+ ASSERT_EQUALS(2U, keys.size());
+ for (BSONObjSet::const_iterator i = keys.begin(); i != keys.end(); ++i) {
+ BSONObj key = *i;
+ ASSERT_EQUALS(2, key.nFields());
+ ASSERT_EQUALS(String, key.firstElement().type());
+ }
+}
- /*
- TEST( FTSIndexFormat, ExtraBackArray1 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" <<
- "x.y" << 1 ) ) ) );
- BSONObjSet keys;
- FTSIndexFormat::getKeys( spec,
- BSON( "data" << "cat" <<
- "x" << BSON_ARRAY( BSON( "y" << 1 ) <<
- BSON( "y" << 2 ) ) ),
- &keys );
-
- ASSERT_EQUALS( 1U, keys.size() );
- BSONObj key = *(keys.begin());
- log() << "e: " << key << endl;
- ASSERT_EQUALS( 3, key.nFields() );
- BSONObjIterator i( key );
- ASSERT_EQUALS( StringData("cat"), i.next().valuestr() );
- ASSERT( i.next().numberDouble() > 0 );
- ASSERT_EQUALS( 5, i.next().numberInt() );
- }
- */
-
- TEST( FTSIndexFormat, ExtraFront1 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "x" << 1 <<
- "data" << "text" ) ) ) );
- BSONObjSet keys;
- FTSIndexFormat::getKeys( spec, BSON( "data" << "cat" << "x" << 5 ), &keys );
-
- ASSERT_EQUALS( 1U, keys.size() );
- BSONObj key = *(keys.begin());
- ASSERT_EQUALS( 3, key.nFields() );
- BSONObjIterator i( key );
- ASSERT_EQUALS( 5, i.next().numberInt() );
- ASSERT_EQUALS( StringData("cat"), i.next().valuestr() );
- ASSERT( i.next().numberDouble() > 0 );
- }
+TEST(FTSIndexFormat, ExtraBack1) {
+ FTSSpec spec(FTSSpec::fixSpec(BSON("key" << BSON("data"
+ << "text"
+ << "x" << 1))));
+ BSONObjSet keys;
+ FTSIndexFormat::getKeys(spec,
+ BSON("data"
+ << "cat"
+ << "x" << 5),
+ &keys);
+
+ ASSERT_EQUALS(1U, keys.size());
+ BSONObj key = *(keys.begin());
+ ASSERT_EQUALS(3, key.nFields());
+ BSONObjIterator i(key);
+ ASSERT_EQUALS(StringData("cat"), i.next().valuestr());
+ ASSERT(i.next().numberDouble() > 0);
+ ASSERT_EQUALS(5, i.next().numberInt());
+}
- TEST( FTSIndexFormat, StopWords1 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" ) ) ) );
+/*
+TEST( FTSIndexFormat, ExtraBackArray1 ) {
+ FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" <<
+ "x.y" << 1 ) ) ) );
+ BSONObjSet keys;
+ FTSIndexFormat::getKeys( spec,
+ BSON( "data" << "cat" <<
+ "x" << BSON_ARRAY( BSON( "y" << 1 ) <<
+ BSON( "y" << 2 ) ) ),
+ &keys );
+
+ ASSERT_EQUALS( 1U, keys.size() );
+ BSONObj key = *(keys.begin());
+ log() << "e: " << key << endl;
+ ASSERT_EQUALS( 3, key.nFields() );
+ BSONObjIterator i( key );
+ ASSERT_EQUALS( StringData("cat"), i.next().valuestr() );
+ ASSERT( i.next().numberDouble() > 0 );
+ ASSERT_EQUALS( 5, i.next().numberInt() );
+}
+*/
- BSONObjSet keys1;
- FTSIndexFormat::getKeys( spec, BSON( "data" << "computer" ), &keys1 );
- ASSERT_EQUALS( 1U, keys1.size() );
+TEST(FTSIndexFormat, ExtraFront1) {
+ FTSSpec spec(FTSSpec::fixSpec(BSON("key" << BSON("x" << 1 << "data"
+ << "text"))));
+ BSONObjSet keys;
+ FTSIndexFormat::getKeys(spec,
+ BSON("data"
+ << "cat"
+ << "x" << 5),
+ &keys);
+
+ ASSERT_EQUALS(1U, keys.size());
+ BSONObj key = *(keys.begin());
+ ASSERT_EQUALS(3, key.nFields());
+ BSONObjIterator i(key);
+ ASSERT_EQUALS(5, i.next().numberInt());
+ ASSERT_EQUALS(StringData("cat"), i.next().valuestr());
+ ASSERT(i.next().numberDouble() > 0);
+}
- BSONObjSet keys2;
- FTSIndexFormat::getKeys( spec, BSON( "data" << "any computer" ), &keys2 );
- ASSERT_EQUALS( 1U, keys2.size() );
- }
+TEST(FTSIndexFormat, StopWords1) {
+ FTSSpec spec(FTSSpec::fixSpec(BSON("key" << BSON("data"
+ << "text"))));
+
+ BSONObjSet keys1;
+ FTSIndexFormat::getKeys(spec,
+ BSON("data"
+ << "computer"),
+ &keys1);
+ ASSERT_EQUALS(1U, keys1.size());
+
+ BSONObjSet keys2;
+ FTSIndexFormat::getKeys(spec,
+ BSON("data"
+ << "any computer"),
+ &keys2);
+ ASSERT_EQUALS(1U, keys2.size());
+}
- /**
- * Helper function to compare keys returned in getKeys() result
- * with expected values.
- */
- void assertEqualsIndexKeys( std::set<std::string>& expectedKeys, const BSONObjSet& keys ) {
- ASSERT_EQUALS( expectedKeys.size(), keys.size() );
- for ( BSONObjSet::const_iterator i = keys.begin(); i != keys.end(); ++i ) {
- BSONObj key = *i;
- ASSERT_EQUALS( 2, key.nFields() );
- ASSERT_EQUALS( String, key.firstElement().type() );
- string s = key.firstElement().String();
- std::set<string>::const_iterator j = expectedKeys.find(s);
- if (j == expectedKeys.end()) {
- mongoutils::str::stream ss;
- ss << "unexpected key " << s << " in FTSIndexFormat::getKeys result. "
- << "expected keys:";
- for (std::set<string>::const_iterator k = expectedKeys.begin();
- k != expectedKeys.end(); ++k) {
- ss << "\n " << *k;
- }
- FAIL(ss);
- }
+/**
+ * Helper function to compare keys returned in getKeys() result
+ * with expected values.
+ */
+void assertEqualsIndexKeys(std::set<std::string>& expectedKeys, const BSONObjSet& keys) {
+ ASSERT_EQUALS(expectedKeys.size(), keys.size());
+ for (BSONObjSet::const_iterator i = keys.begin(); i != keys.end(); ++i) {
+ BSONObj key = *i;
+ ASSERT_EQUALS(2, key.nFields());
+ ASSERT_EQUALS(String, key.firstElement().type());
+ string s = key.firstElement().String();
+ std::set<string>::const_iterator j = expectedKeys.find(s);
+ if (j == expectedKeys.end()) {
+ mongoutils::str::stream ss;
+ ss << "unexpected key " << s << " in FTSIndexFormat::getKeys result. "
+ << "expected keys:";
+ for (std::set<string>::const_iterator k = expectedKeys.begin(); k != expectedKeys.end();
+ ++k) {
+ ss << "\n " << *k;
}
+ FAIL(ss);
}
+ }
+}
- /**
- * Tests keys for long terms using text index version 1.
- * Terms that are too long are not truncated in version 1.
- */
- TEST( FTSIndexFormat, LongWordsTextIndexVersion1 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" ) <<
- "textIndexVersion" << 1 ) ) );
- BSONObjSet keys;
- string longPrefix( 1024U, 'a' );
- // "aaa...aaacat"
- string longWordCat = longPrefix + "cat";
- // "aaa...aaasat"
- string longWordSat = longPrefix + "sat";
- string text = mongoutils::str::stream() << longWordCat << " " << longWordSat;
- FTSIndexFormat::getKeys( spec, BSON( "data" << text ), &keys );
-
- // Hard-coded expected computed keys for future-proofing.
- std::set<string> expectedKeys;
- // cat
- expectedKeys.insert( longWordCat );
- // sat
- expectedKeys.insert( longWordSat );
-
- assertEqualsIndexKeys( expectedKeys, keys);
- }
-
- /**
- * Tests keys for long terms using text index version 2.
- * In version 2, long terms (longer than 32 characters)
- * are hashed with murmur3 and appended to the first 32
- * characters of the term to form the index key.
- */
- TEST( FTSIndexFormat, LongWordTextIndexVersion2 ) {
- FTSSpec spec( FTSSpec::fixSpec( BSON( "key" << BSON( "data" << "text" ) <<
- "textIndexVersion" << 2 ) ) );
- BSONObjSet keys;
- string longPrefix( 1024U, 'a' );
- // "aaa...aaacat"
- string longWordCat = longPrefix + "cat";
- // "aaa...aaasat"
- string longWordSat = longPrefix + "sat";
- string text = mongoutils::str::stream() << longWordCat << " " << longWordSat;
- FTSIndexFormat::getKeys( spec, BSON( "data" << text ), &keys );
-
- // Hard-coded expected computed keys for future-proofing.
- std::set<string> expectedKeys;
- // cat
- expectedKeys.insert( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab8e78455d827ebb87cbe87f392bf45f6" );
- // sat
- expectedKeys.insert( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaf2d6f58bb3b81b97e611ae7ccac6dea7" );
-
- assertEqualsIndexKeys( expectedKeys, keys);
- }
+/**
+ * Tests keys for long terms using text index version 1.
+ * Terms that are too long are not truncated in version 1.
+ */
+TEST(FTSIndexFormat, LongWordsTextIndexVersion1) {
+ FTSSpec spec(FTSSpec::fixSpec(BSON("key" << BSON("data"
+ << "text") << "textIndexVersion" << 1)));
+ BSONObjSet keys;
+ string longPrefix(1024U, 'a');
+ // "aaa...aaacat"
+ string longWordCat = longPrefix + "cat";
+ // "aaa...aaasat"
+ string longWordSat = longPrefix + "sat";
+ string text = mongoutils::str::stream() << longWordCat << " " << longWordSat;
+ FTSIndexFormat::getKeys(spec, BSON("data" << text), &keys);
+
+ // Hard-coded expected computed keys for future-proofing.
+ std::set<string> expectedKeys;
+ // cat
+ expectedKeys.insert(longWordCat);
+ // sat
+ expectedKeys.insert(longWordSat);
+
+ assertEqualsIndexKeys(expectedKeys, keys);
+}
- }
+/**
+ * Tests keys for long terms using text index version 2.
+ * In version 2, long terms (longer than 32 characters)
+ * are hashed with murmur3 and appended to the first 32
+ * characters of the term to form the index key.
+ */
+TEST(FTSIndexFormat, LongWordTextIndexVersion2) {
+ FTSSpec spec(FTSSpec::fixSpec(BSON("key" << BSON("data"
+ << "text") << "textIndexVersion" << 2)));
+ BSONObjSet keys;
+ string longPrefix(1024U, 'a');
+ // "aaa...aaacat"
+ string longWordCat = longPrefix + "cat";
+ // "aaa...aaasat"
+ string longWordSat = longPrefix + "sat";
+ string text = mongoutils::str::stream() << longWordCat << " " << longWordSat;
+ FTSIndexFormat::getKeys(spec, BSON("data" << text), &keys);
+
+ // Hard-coded expected computed keys for future-proofing.
+ std::set<string> expectedKeys;
+ // cat
+ expectedKeys.insert("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab8e78455d827ebb87cbe87f392bf45f6");
+ // sat
+ expectedKeys.insert("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaf2d6f58bb3b81b97e611ae7ccac6dea7");
+
+ assertEqualsIndexKeys(expectedKeys, keys);
+}
+}
}