diff options
Diffstat (limited to 'src/mongo/s/catalog/type_shard_collection_test.cpp')
-rw-r--r-- | src/mongo/s/catalog/type_shard_collection_test.cpp | 155 |
1 files changed, 42 insertions, 113 deletions
diff --git a/src/mongo/s/catalog/type_shard_collection_test.cpp b/src/mongo/s/catalog/type_shard_collection_test.cpp index 85e3f5636cb..bde4643c186 100644 --- a/src/mongo/s/catalog/type_shard_collection_test.cpp +++ b/src/mongo/s/catalog/type_shard_collection_test.cpp @@ -29,11 +29,10 @@ #include "mongo/platform/basic.h" +#include "mongo/db/s/shard_metadata_util.h" #include "mongo/s/catalog/type_shard_collection.h" -#include "mongo/base/status_with.h" #include "mongo/bson/oid.h" -#include "mongo/s/catalog/type_collection.h" #include "mongo/unittest/unittest.h" #include "mongo/util/time_support.h" @@ -47,133 +46,63 @@ const BSONObj kKeyPattern = BSON("a" << 1); const BSONObj kDefaultCollation = BSON("locale" << "fr_CA"); -TEST(ShardCollectionType, ToFromBSON) { - const OID epoch = OID::gen(); - const UUID uuid = UUID::gen(); - const ChunkVersion lastRefreshedCollectionVersion(2, 0, epoch); - +TEST(ShardCollectionType, FromBSONEmptyShardKeyFails) { BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - uuid.appendToBuilder(&builder, ShardCollectionType::uuid.name()); - builder.append(ShardCollectionType::epoch(), epoch); - builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); - builder.append(ShardCollectionType::defaultCollation(), kDefaultCollation); - builder.append(ShardCollectionType::unique(), true); - builder.append(ShardCollectionType::refreshing(), false); - builder.appendTimestamp(ShardCollectionType::lastRefreshedCollectionVersion(), - lastRefreshedCollectionVersion.toLong()); - BSONObj obj = builder.obj(); - - ShardCollectionType shardCollectionType = assertGet(ShardCollectionType::fromBSON(obj)); - - ASSERT_EQUALS(shardCollectionType.getNss(), kNss); - ASSERT(shardCollectionType.getUUID()); - ASSERT_EQUALS(*shardCollectionType.getUUID(), uuid); - ASSERT_EQUALS(shardCollectionType.getEpoch(), epoch); - ASSERT_BSONOBJ_EQ(shardCollectionType.getKeyPattern().toBSON(), kKeyPattern); - ASSERT_BSONOBJ_EQ(shardCollectionType.getDefaultCollation(), kDefaultCollation); - ASSERT_EQUALS(shardCollectionType.getUnique(), true); - ASSERT_EQUALS(shardCollectionType.getRefreshing(), false); - ASSERT_EQUALS(shardCollectionType.getLastRefreshedCollectionVersion(), - lastRefreshedCollectionVersion); - - ASSERT_BSONOBJ_EQ(obj, shardCollectionType.toBSON()); + builder.append(ShardCollectionType::kNssFieldName, kNss.ns()); + builder.append(ShardCollectionType::kEpochFieldName, OID::gen()); + builder.append(ShardCollectionType::kKeyPatternFieldName, BSONObj()); + builder.append(ShardCollectionType::kUniqueFieldName, true); + + StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); + ASSERT_EQ(status.getStatus().code(), ErrorCodes::ShardKeyNotFound); } -TEST(ShardCollectionType, ToFromShardBSONWithoutOptionals) { - const OID epoch = OID::gen(); +TEST(ShardCollectionType, FromBSONEpochMatchesLastRefreshedCollectionVersionWhenBSONTimestamp) { + OID epoch = OID::gen(); BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - builder.append(ShardCollectionType::epoch(), epoch); - builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); - builder.append(ShardCollectionType::defaultCollation(), kDefaultCollation); - builder.append(ShardCollectionType::unique(), true); - BSONObj obj = builder.obj(); - - ShardCollectionType shardCollectionType = assertGet(ShardCollectionType::fromBSON(obj)); - - ASSERT_EQUALS(shardCollectionType.getNss(), kNss); - ASSERT_EQUALS(shardCollectionType.getEpoch(), epoch); - ASSERT_BSONOBJ_EQ(shardCollectionType.getKeyPattern().toBSON(), kKeyPattern); - ASSERT_BSONOBJ_EQ(shardCollectionType.getDefaultCollation(), kDefaultCollation); - ASSERT_EQUALS(shardCollectionType.getUnique(), true); - ASSERT_FALSE(shardCollectionType.hasRefreshing()); - ASSERT_FALSE(shardCollectionType.hasLastRefreshedCollectionVersion()); - - ASSERT_BSONOBJ_EQ(obj, shardCollectionType.toBSON()); -} + builder.append(ShardCollectionType::kNssFieldName, kNss.ns()); + builder.append(ShardCollectionType::kEpochFieldName, epoch); + builder.append(ShardCollectionType::kKeyPatternFieldName, kKeyPattern); + builder.append(ShardCollectionType::kUniqueFieldName, true); + builder.append(ShardCollectionType::kLastRefreshedCollectionVersionFieldName, Timestamp()); -TEST(ShardCollectionType, FromEmptyBSON) { - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(BSONObj()); - ASSERT_FALSE(status.isOK()); -} + ShardCollectionType shardCollType = assertGet(ShardCollectionType::fromBSON(builder.obj())); -TEST(ShardCollectionType, FromBSONNoUUIDIsOK) { - BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - builder.append(ShardCollectionType::epoch(), OID::gen()); - builder.append(ShardCollectionType::keyPattern(), kKeyPattern); - builder.append(ShardCollectionType::unique(), true); - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); - ASSERT_OK(status.getStatus()); - ASSERT_FALSE(status.getValue().getUUID()); + ASSERT_EQ(epoch, shardCollType.getLastRefreshedCollectionVersion()->epoch()); } -TEST(ShardCollectionType, FromBSONNoNSFails) { - BSONObjBuilder builder; - UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); - builder.append(ShardCollectionType::epoch(), OID::gen()); - builder.append(ShardCollectionType::keyPattern(), kKeyPattern); - builder.append(ShardCollectionType::unique(), true); - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); - ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); - ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::ns()); -} +TEST(ShardCollectionType, FromBSONEpochMatchesLastRefreshedCollectionVersionWhenDate) { + OID epoch = OID::gen(); -TEST(ShardCollectionType, FromBSONNoEpochFails) { BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); - builder.append(ShardCollectionType::keyPattern(), kKeyPattern); - builder.append(ShardCollectionType::unique(), true); - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); - ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); - ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::epoch()); -} + builder.append(ShardCollectionType::kNssFieldName, kNss.ns()); + builder.append(ShardCollectionType::kEpochFieldName, epoch); + builder.append(ShardCollectionType::kKeyPatternFieldName, kKeyPattern); + builder.append(ShardCollectionType::kUniqueFieldName, true); + builder.append(ShardCollectionType::kLastRefreshedCollectionVersionFieldName, Date_t()); -TEST(ShardCollectionType, FromBSONNoShardKeyFails) { - BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); - builder.append(ShardCollectionType::epoch(), OID::gen()); - builder.append(ShardCollectionType::unique(), true); - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); - ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); - ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::keyPattern()); -} + ShardCollectionType shardCollType = assertGet(ShardCollectionType::fromBSON(builder.obj())); -TEST(ShardCollectionType, FromBSONNoUniqueFails) { - BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); - builder.append(ShardCollectionType::epoch(), OID::gen()); - builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); - builder.append(ShardCollectionType::defaultCollation(), kDefaultCollation); - StatusWith<ShardCollectionType> status = ShardCollectionType::fromBSON(builder.obj()); - ASSERT_EQUALS(status.getStatus().code(), ErrorCodes::NoSuchKey); - ASSERT_STRING_CONTAINS(status.getStatus().reason(), ShardCollectionType::unique()); + ASSERT_EQ(epoch, shardCollType.getLastRefreshedCollectionVersion()->epoch()); } -TEST(ShardCollectionType, FromBSONNoDefaultCollationIsOK) { - BSONObjBuilder builder; - builder.append(ShardCollectionType::ns.name(), kNss.ns()); - UUID::gen().appendToBuilder(&builder, ShardCollectionType::uuid.name()); - builder.append(ShardCollectionType::epoch(), OID::gen()); - builder.append(ShardCollectionType::keyPattern.name(), kKeyPattern); - builder.append(ShardCollectionType::unique(), true); +TEST(ShardCollectionType, ToBSONEmptyDefaultCollationNotIncluded) { + ShardCollectionType shardCollType; + shardCollType.setNss(kNss); + shardCollType.setEpoch(OID::gen()); + shardCollType.setKeyPattern(kKeyPattern); + shardCollType.setUnique(true); + + shardCollType.setDefaultCollation(BSONObj()); + BSONObj obj = shardCollType.toBSON(); + + ASSERT_FALSE(obj.hasField(ShardCollectionType::kDefaultCollationFieldName)); + + shardCollType.setDefaultCollation(kDefaultCollation); + obj = shardCollType.toBSON(); - assertGet(ShardCollectionType::fromBSON(builder.obj())); + ASSERT_TRUE(obj.hasField(ShardCollectionType::kDefaultCollationFieldName)); } } // namespace |