2
* Copyright (C) 2012 10gen Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Affero General Public License, version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Affero General Public License for more details.
13
* You should have received a copy of the GNU Affero General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "mongo/pch.h"
19
#include "mongo/db/jsobj.h"
20
#include "mongo/s/chunk_version.h"
21
#include "mongo/unittest/unittest.h"
27
* Tests parsing of BSON for versions. In version 2.2, this parsing is meant to be very
28
* flexible so different formats can be tried and enforced later.
32
* A) { vFieldName : <TSTYPE>, [ vFieldNameEpoch : <OID> ], ... }
33
* B) { fieldName : [ <TSTYPE>, <OID> ], ... }
35
* vFieldName is a specifyable name - usually "version" (default) or "lastmod". <TSTYPE> is a
36
* type convertible to Timestamp, ideally Timestamp but also numeric.
37
* <OID> is a value of type OID.
41
TEST(Compatibility, LegacyFormatA) {
42
BSONObjBuilder versionObjB;
43
versionObjB.appendTimestamp( "testVersion",
44
ChunkVersion( 1, 1, OID() ).toLong() );
45
versionObjB.append( "testVersionEpoch", OID::gen() );
46
BSONObj versionObj = versionObjB.obj();
49
ChunkVersion::fromBSON( versionObj[ "testVersion" ] );
51
ASSERT( ChunkVersion::canParseBSON( versionObj[ "testVersion" ] ) );
52
ASSERT( parsed.majorVersion() == 1 );
53
ASSERT( parsed.minorVersion() == 1 );
54
ASSERT( ! parsed.epoch().isSet() );
56
parsed = ChunkVersion::fromBSON( versionObj, "testVersion" );
58
ASSERT( ChunkVersion::canParseBSON( versionObj, "testVersion" ) );
59
ASSERT( parsed.majorVersion() == 1 );
60
ASSERT( parsed.minorVersion() == 1 );
61
ASSERT( parsed.epoch().isSet() );
64
TEST(Compatibility, SubArrayFormatB) {
65
BSONObjBuilder tsObjB;
66
tsObjB.appendTimestamp( "ts", ChunkVersion( 1, 1, OID() ).toLong() );
67
BSONObj tsObj = tsObjB.obj();
69
BSONObjBuilder versionObjB;
70
BSONArrayBuilder subArrB( versionObjB.subarrayStart( "testVersion" ) );
71
// Append this weird way so we're sure we get a timestamp type
72
subArrB.append( tsObj.firstElement() );
73
subArrB.append( OID::gen() );
75
BSONObj versionObj = versionObjB.obj();
78
ChunkVersion::fromBSON( versionObj[ "testVersion" ] );
80
ASSERT( ChunkVersion::canParseBSON( versionObj[ "testVersion" ] ) );
81
ASSERT( ChunkVersion::canParseBSON( BSONArray( versionObj[ "testVersion" ].Obj() ) ) );
82
ASSERT( parsed.majorVersion() == 1 );
83
ASSERT( parsed.minorVersion() == 1 );
84
ASSERT( parsed.epoch().isSet() );
87
} // unnamed namespace