diff options
author | Tess Avitabile <tess.avitabile@mongodb.com> | 2016-10-19 16:23:00 -0400 |
---|---|---|
committer | Tess Avitabile <tess.avitabile@mongodb.com> | 2016-10-24 11:13:07 -0400 |
commit | 064ed7fab88447bd5dd6bfc19dfef4dbf89d19f9 (patch) | |
tree | 314fbceecbed08cdf005210f2d036d4e123fbbb3 /src/mongo/db/catalog/index_spec_validate_test.cpp | |
parent | 7154b9f7c81fdda492d2536e8ef7c434356d8ac6 (diff) | |
download | mongo-064ed7fab88447bd5dd6bfc19dfef4dbf89d19f9.tar.gz |
SERVER-26514 Create command should take idIndex option
Diffstat (limited to 'src/mongo/db/catalog/index_spec_validate_test.cpp')
-rw-r--r-- | src/mongo/db/catalog/index_spec_validate_test.cpp | 697 |
1 files changed, 560 insertions, 137 deletions
diff --git a/src/mongo/db/catalog/index_spec_validate_test.cpp b/src/mongo/db/catalog/index_spec_validate_test.cpp index 798ee88a399..e2e77d3cfe2 100644 --- a/src/mongo/db/catalog/index_spec_validate_test.cpp +++ b/src/mongo/db/catalog/index_spec_validate_test.cpp @@ -38,11 +38,17 @@ #include "mongo/bson/bsonmisc.h" #include "mongo/bson/bsonobjbuilder.h" #include "mongo/db/namespace_string.h" +#include "mongo/db/query/collation/collator_interface_mock.h" +#include "mongo/db/query/query_test_service_context.h" #include "mongo/unittest/unittest.h" namespace mongo { namespace { +using index_key_validate::validateIndexSpec; +using index_key_validate::validateIdIndexSpec; +using index_key_validate::validateIndexSpecCollation; + const NamespaceString kTestNamespace("test", "index_spec_validate"); /** @@ -63,14 +69,22 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfKeyPatternIsNotAnObject) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << 1), kTestNamespace, featureCompatibility)); + validateIndexSpec(BSON("key" << 1 << "name" + << "indexName"), + kTestNamespace, + featureCompatibility)); ASSERT_EQ(ErrorCodes::TypeMismatch, validateIndexSpec(BSON("key" - << "not an object"), + << "not an object" + << "name" + << "indexName"), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSONArray()), kTestNamespace, featureCompatibility)); + validateIndexSpec(BSON("key" << BSONArray() << "name" + << "indexName"), + kTestNamespace, + featureCompatibility)); } TEST(IndexSpecValidateTest, ReturnsAnErrorIfFieldRepeatedInKeyPattern) { @@ -79,12 +93,15 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfFieldRepeatedInKeyPattern) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1 << "field" << 1)), + validateIndexSpec(BSON("key" << BSON("field" << 1 << "field" << 1) << "name" + << "indexName"), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::BadValue, validateIndexSpec(BSON("key" << BSON("field" << 1 << "otherField" << -1 << "field" - << "2dsphere")), + << "2dsphere") + << "name" + << "indexName"), kTestNamespace, featureCompatibility)); } @@ -95,7 +112,31 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfKeyPatternIsNotPresent) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::FailedToParse, - validateIndexSpec(BSONObj(), kTestNamespace, featureCompatibility)); + validateIndexSpec(BSON("name" + << "indexName"), + kTestNamespace, + featureCompatibility)); +} + +TEST(IndexSpecValidateTest, ReturnsAnErrorIfNameIsNotAString) { + ServerGlobalParams::FeatureCompatibility featureCompatibility; + featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); + featureCompatibility.validateFeaturesAsMaster.store(true); + + ASSERT_EQ(ErrorCodes::TypeMismatch, + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" << 1), + kTestNamespace, + featureCompatibility)); +} + +TEST(IndexSpecValidateTest, ReturnsAnErrorIfNameIsNotPresent) { + ServerGlobalParams::FeatureCompatibility featureCompatibility; + featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); + featureCompatibility.validateFeaturesAsMaster.store(true); + + ASSERT_EQ( + ErrorCodes::FailedToParse, + validateIndexSpec(BSON("key" << BSON("field" << 1)), kTestNamespace, featureCompatibility)); } TEST(IndexSpecValidateTest, ReturnsAnErrorIfNamespaceIsNotAString) { @@ -104,11 +145,17 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfNamespaceIsNotAString) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" << 1), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << 1), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" << BSONObj()), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << BSONObj()), kTestNamespace, featureCompatibility)); } @@ -119,7 +166,9 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfNamespaceIsEmptyString) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" << ""), NamespaceString(), featureCompatibility)); @@ -131,7 +180,9 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfNamespaceDoesNotMatch) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" << "some string"), kTestNamespace, featureCompatibility)); @@ -139,7 +190,10 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfNamespaceDoesNotMatch) { // Verify that we reject the index specification when the "ns" field only contains the // collection name. ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.coll()), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.coll()), kTestNamespace, featureCompatibility)); } @@ -149,14 +203,22 @@ TEST(IndexSpecValidateTest, ReturnsIndexSpecWithNamespaceFilledInIfItIsNotPresen featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 1), kTestNamespace, featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 1), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 1)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 1)), + sorted(result.getValue())); // Verify that the index specification we returned is still considered valid. ASSERT_OK(validateIndexSpec(result.getValue(), kTestNamespace, featureCompatibility)); @@ -167,14 +229,20 @@ TEST(IndexSpecValidateTest, ReturnsIndexSpecUnchangedIfNamespaceAndVersionArePre featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 1), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 1), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "ns" + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" << "test.index_spec_validate" << "v" << 1)), @@ -187,12 +255,17 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfVersionIsNotANumber) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" << "not a number"), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << BSONObj()), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << BSONObj()), kTestNamespace, featureCompatibility)); } @@ -203,23 +276,33 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfVersionIsNotRepresentableAsInt) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 2.2), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2.2), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << std::nan("1")), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << std::nan("1")), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" << std::numeric_limits<double>::infinity()), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::BadValue, - validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << std::numeric_limits<long long>::max()), - kTestNamespace, - featureCompatibility)); + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << std::numeric_limits<long long>::max()), + kTestNamespace, + featureCompatibility)); } TEST(IndexSpecValidateTest, ReturnsAnErrorIfVersionIsV0) { @@ -228,7 +311,10 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfVersionIsV0) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::CannotCreateIndex, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 0), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 0), kTestNamespace, featureCompatibility)); } @@ -239,14 +325,21 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfVersionIsUnsupported) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::CannotCreateIndex, - validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 3 << "collation" << BSON("locale" - << "en")), - kTestNamespace, - featureCompatibility)); + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 3 + << "collation" + << BSON("locale" + << "en")), + kTestNamespace, + featureCompatibility)); ASSERT_EQ(ErrorCodes::CannotCreateIndex, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << -3LL), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << -3LL), kTestNamespace, featureCompatibility)); } @@ -257,7 +350,10 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfIndexVersionIsV2AndFeatureCompatibil featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::CannotCreateIndex, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 2), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2), kTestNamespace, featureCompatibility)); } @@ -267,14 +363,22 @@ TEST(IndexSpecValidateTest, AcceptsIndexVersionV2WhenValidateFeaturesAsMasterFal featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k32); featureCompatibility.validateFeaturesAsMaster.store(false); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 2), kTestNamespace, featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2)), + sorted(result.getValue())); } TEST(IndexSpecValidateTest, AcceptsIndexVersionsThatAreAllowedForCreation) { @@ -282,23 +386,39 @@ TEST(IndexSpecValidateTest, AcceptsIndexVersionsThatAreAllowedForCreation) { featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 1), kTestNamespace, featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 1), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 1)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 1)), + sorted(result.getValue())); - result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 2LL), kTestNamespace, featureCompatibility); + result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2LL), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2LL)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2LL)), + sorted(result.getValue())); } TEST(IndexSpecValidateTest, DefaultIndexVersionIsV1IfFeatureCompatibilityVersionIs32) { @@ -306,16 +426,22 @@ TEST(IndexSpecValidateTest, DefaultIndexVersionIsV1IfFeatureCompatibilityVersion featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k32); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns()), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns()), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 1)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 1)), + sorted(result.getValue())); // Verify that the index specification we returned is still considered valid. ASSERT_OK(validateIndexSpec(result.getValue(), kTestNamespace, featureCompatibility)); @@ -327,16 +453,22 @@ TEST(IndexSpecValidateTest, featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k32); featureCompatibility.validateFeaturesAsMaster.store(false); - auto result = - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns()), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns()), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 1)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 1)), + sorted(result.getValue())); // Verify that the index specification we returned is still considered valid. ASSERT_OK(validateIndexSpec(result.getValue(), kTestNamespace, featureCompatibility)); @@ -347,16 +479,22 @@ TEST(IndexSpecValidateTest, DefaultIndexVersionIsV2IfFeatureCompatibilityVersion featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = - validateIndexSpec(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns()), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns()), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2)), + sorted(result.getValue())); // Verify that the index specification we returned is still considered valid. ASSERT_OK(validateIndexSpec(result.getValue(), kTestNamespace, featureCompatibility)); @@ -367,14 +505,22 @@ TEST(IndexSpecValidateTest, AcceptsIndexVersionV1WhenFeatureCompatibilityVersion featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 1), kTestNamespace, featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 1), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 1)), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 1)), + sorted(result.getValue())); } TEST(IndexSpecValidateTest, ReturnsAnErrorIfCollationIsNotAnObject) { @@ -383,77 +529,107 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfCollationIsNotAnObject) { featureCompatibility.validateFeaturesAsMaster.store(true); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "collation" << 1), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "collation" + << 1), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "collation" + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "collation" << "not an object"), kTestNamespace, featureCompatibility)); ASSERT_EQ(ErrorCodes::TypeMismatch, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "collation" << BSONArray()), + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "collation" + << BSONArray()), kTestNamespace, featureCompatibility)); } -TEST(IndexSpecValidateTest, ReturnsAnErrorIfCollationIsPresentAndVersionIsLessThanV2) { +TEST(IndexSpecValidateTest, ReturnsAnErrorIfCollationIsEmpty) { ServerGlobalParams::FeatureCompatibility featureCompatibility; featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - ASSERT_EQ( - ErrorCodes::CannotCreateIndex, - validateIndexSpec(BSON("key" << BSON("field" << 1) << "collation" << BSONObj() << "v" << 1), - kTestNamespace, - featureCompatibility)); + ASSERT_EQ(ErrorCodes::BadValue, + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "collation" + << BSONObj()), + kTestNamespace, + featureCompatibility)); } -TEST(IndexSpecValidateTest, AcceptsAnyObjectValueForCollation) { +TEST(IndexSpecValidateTest, ReturnsAnErrorIfCollationIsPresentAndVersionIsLessThanV2) { ServerGlobalParams::FeatureCompatibility featureCompatibility; featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 2 << "collation" << BSON("locale" - << "simple")), - kTestNamespace, - featureCompatibility); - ASSERT_OK(result.getStatus()); + ASSERT_EQ(ErrorCodes::CannotCreateIndex, + validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "collation" + << BSON("locale" + << "simple") + << "v" + << 1), + kTestNamespace, + featureCompatibility)); +} - // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2 - << "collation" - << BSON("locale" - << "simple"))), - sorted(result.getValue())); - - result = - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 2 << "collation" << BSONObj()), - kTestNamespace, - featureCompatibility); +TEST(IndexSpecValidateTest, AcceptsAnyNonEmptyObjectValueForCollation) { + ServerGlobalParams::FeatureCompatibility featureCompatibility; + featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); + featureCompatibility.validateFeaturesAsMaster.store(true); + + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2 + << "collation" + << BSON("locale" + << "simple")), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2 - << "collation" - << BSONObj())), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "simple"))), + sorted(result.getValue())); - result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 2 << "collation" + result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2 + << "collation" << BSON("unknownCollationOption" << true)), kTestNamespace, featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2 - << "collation" - << BSON("unknownCollationOption" << true))), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("unknownCollationOption" << true))), + sorted(result.getValue())); } TEST(IndexSpecValidateTest, AcceptsIndexSpecIfCollationIsPresentAndVersionIsEqualToV2) { @@ -461,20 +637,28 @@ TEST(IndexSpecValidateTest, AcceptsIndexSpecIfCollationIsPresentAndVersionIsEqua featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = validateIndexSpec( - BSON("key" << BSON("field" << 1) << "v" << 2 << "collation" << BSON("locale" - << "en")), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2 + << "collation" + << BSON("locale" + << "en")), + kTestNamespace, + featureCompatibility); ASSERT_OK(result.getStatus()); // We don't care about the order of the fields in the resulting index specification. - ASSERT_BSONOBJ_EQ( - sorted(BSON("key" << BSON("field" << 1) << "ns" << kTestNamespace.ns() << "v" << 2 - << "collation" - << BSON("locale" - << "en"))), - sorted(result.getValue())); + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "en"))), + sorted(result.getValue())); } TEST(IndexSpecValidateTest, ReturnsAnErrorIfUnknownFieldIsPresentInSpecV2) { @@ -482,10 +666,14 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfUnknownFieldIsPresentInSpecV2) { featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 2 << "unknownField" << 1), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 2 + << "unknownField" + << 1), + kTestNamespace, + featureCompatibility); ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, result); } @@ -494,12 +682,247 @@ TEST(IndexSpecValidateTest, ReturnsAnErrorIfUnknownFieldIsPresentInSpecV1) { featureCompatibility.version.store(ServerGlobalParams::FeatureCompatibility::Version::k34); featureCompatibility.validateFeaturesAsMaster.store(true); - auto result = - validateIndexSpec(BSON("key" << BSON("field" << 1) << "v" << 1 << "unknownField" << 1), - kTestNamespace, - featureCompatibility); + auto result = validateIndexSpec(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "v" + << 1 + << "unknownField" + << 1), + kTestNamespace, + featureCompatibility); ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, result); } +TEST(IdIndexSpecValidateTest, ReturnsAnErrorIfKeyPatternIsIncorrectForIdIndex) { + ASSERT_EQ(ErrorCodes::BadValue, + validateIdIndexSpec(BSON("key" << BSON("_id" << -1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2))); + ASSERT_EQ(ErrorCodes::BadValue, + validateIdIndexSpec(BSON("key" << BSON("a" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2))); +} + +TEST(IdIndexSpecValidateTest, ReturnsAnErrorIfNameIsIncorrectForIdIndex) { + ASSERT_EQ(ErrorCodes::BadValue, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2))); +} + +TEST(IdIndexSpecValidateTest, ReturnsOKStatusIfKeyAndNameCorrectForIdIndex) { + ASSERT_OK(validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2))); +} + +TEST(IdIndexSpecValidateTest, ReturnsAnErrorIfFieldNotAllowedForIdIndex) { + ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "background" + << false))); + ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "unique" + << true))); + ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "partialFilterExpression" + << BSON("a" << 5)))); + ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "sparse" + << false))); + ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "expireAfterSeconds" + << 3600))); + ASSERT_EQ(ErrorCodes::InvalidIndexSpecificationOption, + validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "storageEngine" + << BSONObj()))); +} + +TEST(IdIndexSpecValidateTest, ReturnsOKStatusIfAllFieldsAllowedForIdIndex) { + ASSERT_OK(validateIdIndexSpec(BSON("key" << BSON("_id" << 1) << "name" + << "_id_" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "simple")))); +} + +TEST(IndexSpecCollationValidateTest, FillsInFullCollationSpec) { + QueryTestServiceContext serviceContext; + auto txn = serviceContext.makeOperationContext(); + + const CollatorInterface* defaultCollator = nullptr; + + auto result = validateIndexSpecCollation(txn.get(), + BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "mock_reverse_string")), + defaultCollator); + ASSERT_OK(result.getStatus()); + + // We don't care about the order of the fields in the resulting index specification. + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "mock_reverse_string" + << "caseLevel" + << false + << "caseFirst" + << "off" + << "strength" + << 3 + << "numericOrdering" + << false + << "alternate" + << "non-ignorable" + << "maxVariable" + << "punct" + << "normalization" + << false + << "backwards" + << false + << "version" + << "mock_version"))), + sorted(result.getValue())); +} + +TEST(IndexSpecCollationValidateTest, RemovesCollationFieldIfSimple) { + QueryTestServiceContext serviceContext; + auto txn = serviceContext.makeOperationContext(); + + const CollatorInterface* defaultCollator = nullptr; + + auto result = validateIndexSpecCollation(txn.get(), + BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "simple")), + defaultCollator); + ASSERT_OK(result.getStatus()); + + // We don't care about the order of the fields in the resulting index specification. + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2)), + sorted(result.getValue())); +} + +TEST(IndexSpecCollationValidateTest, FillsInCollationFieldWithCollectionDefaultIfNotPresent) { + QueryTestServiceContext serviceContext; + auto txn = serviceContext.makeOperationContext(); + + const CollatorInterfaceMock defaultCollator(CollatorInterfaceMock::MockType::kReverseString); + + auto result = validateIndexSpecCollation(txn.get(), + BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2), + &defaultCollator); + ASSERT_OK(result.getStatus()); + + // We don't care about the order of the fields in the resulting index specification. + ASSERT_BSONOBJ_EQ(sorted(BSON("key" << BSON("field" << 1) << "name" + << "indexName" + << "ns" + << kTestNamespace.ns() + << "v" + << 2 + << "collation" + << BSON("locale" + << "mock_reverse_string" + << "caseLevel" + << false + << "caseFirst" + << "off" + << "strength" + << 3 + << "numericOrdering" + << false + << "alternate" + << "non-ignorable" + << "maxVariable" + << "punct" + << "normalization" + << false + << "backwards" + << false + << "version" + << "mock_version"))), + sorted(result.getValue())); +} + } // namespace } // namespace mongo |