summaryrefslogtreecommitdiff
path: root/jstests/core/apitest_db.js
blob: f22716490161cbcc6f8725ba4d6ed53248847185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 *   Tests for the db object enhancement
 */

assert( "test" == db, "wrong database currently not test" );

dd = function( x ){
    //print( x );
};

dd( "a" );


dd( "b" ); 

/*
 *  be sure the public collection API is complete
 */
assert(db.createCollection , "createCollection" );
assert(db.getProfilingLevel , "getProfilingLevel" );
assert(db.setProfilingLevel , "setProfilingLevel" );
assert(db.dbEval , "dbEval" );
assert(db.group , "group" );

dd( "c" );

/*
 * test createCollection
 */

db.getCollection( "test" ).drop();
db.getCollectionNames().forEach( function(x) { assert(x != "test"); });

dd( "d" );

db.createCollection("test");
var found = false;
db.getCollectionNames().forEach( function(x) { if (x == "test") found = true; });
assert(found, "found test.test in collection infos");

// storageEngine in collection options must:
// - be a document
// - all fields of the document:
// -- must have names that are registered storage engines
// -- must be objects
db.getCollection('test').drop();
var storageEngineName = db.serverStatus().storageEngine.name;
assert.commandFailed(db.createCollection('test', {storageEngine: 'not a document'}));
assert.commandWorked(db.createCollection('test', {storageEngine: {}}));
assert.commandFailed(db.createCollection('test', {storageEngine: {unknownStorageEngine: {}}}));
var invalidStorageEngineOptions = {};
invalidStorageEngineOptions[storageEngineName] = 12345;
assert.commandFailed(db.createCollection('test', {storageEngine: invalidStorageEngineOptions}));

// Test round trip of storageEngine in collection options.
// Assume that empty document for storageEngine-specific options is acceptable.
var validStorageEngineOptions = {};
validStorageEngineOptions[storageEngineName] = {};
db.getCollection('test').drop();
assert.commandWorked(db.createCollection('test', {storageEngine: validStorageEngineOptions}));

var collectionInfos = db.getCollectionInfos({name: 'test'});
assert.eq(1, collectionInfos.length, "'test' collection not created");
assert.eq('test', collectionInfos[0].name, "'test' collection not created");
assert.docEq(validStorageEngineOptions, collectionInfos[0].options.storageEngine,
             'storage engine options not found in listCommands result');

// The indexOptionDefaults must be a document that contains only a storageEngine field.
db.idxOptions.drop();
assert.commandFailed(db.createCollection('idxOptions', {indexOptionDefaults: 'not a document'}));
assert.commandFailed(db.createCollection('idxOptions', {
    indexOptionDefaults: {unknownOption: true}
}), 'created a collection with an unknown option to indexOptionDefaults');
assert.commandWorked(db.createCollection('idxOptions', {indexOptionDefaults: {}}),
                     'should have been able to specify an empty object for indexOptionDefaults');
assert(db.idxOptions.drop());
assert.commandWorked(db.createCollection('idxOptions', {
    indexOptionDefaults: {storageEngine: {}}
}), 'should have been able to configure zero storage engines');
assert(db.idxOptions.drop());

// The storageEngine subdocument must configure only registered storage engines.
assert.commandFailed(db.createCollection('idxOptions', {
    indexOptionDefaults: {storageEngine: {unknownStorageEngine: {}}}
}), 'configured an unregistered storage engine');

// The storageEngine subdocument must contain valid storage engine options.
assert.commandFailed(db.createCollection('idxOptions', {
    indexOptionDefaults: {storageEngine: invalidStorageEngineOptions}
}), 'configured a storage engine with invalid options');

// Tests that a non-active storage engine can be configured so long as it is registered.
if (db.serverBuildInfo().bits === 64) {
    // wiredTiger is not a registered storage engine on 32-bit systems.
    var indexOptions = {storageEngine: {}};
    if (storageEngineName === 'wiredTiger') {
        indexOptions.storageEngine.mmapv1 = {};
    } else {
        indexOptions.storageEngine.wiredTiger = {};
    }
    assert.commandWorked(db.createCollection('idxOptions', {indexOptionDefaults: indexOptions}),
                         'should have been able to configure a non-active storage engine');
    assert(db.idxOptions.drop());
}

// Tests that the indexOptionDefaults are retrievable from the collection options.
assert.commandWorked(db.createCollection('idxOptions', {
    indexOptionDefaults: {storageEngine: validStorageEngineOptions}
}));

var collectionInfos = db.getCollectionInfos({name: 'idxOptions'});
assert.eq(1, collectionInfos.length, "'idxOptions' collection not created");
assert.docEq({storageEngine: validStorageEngineOptions},
             collectionInfos[0].options.indexOptionDefaults,
             'indexOptionDefaults were not applied: ' + tojson(collectionInfos));

dd( "e" );

/*
 *  profile level
 */ 
 
db.setProfilingLevel(0);
assert(db.getProfilingLevel() == 0, "prof level 0");

db.setProfilingLevel(1);
assert(db.getProfilingLevel() == 1, "p1");

db.setProfilingLevel(2);
assert(db.getProfilingLevel() == 2, "p2");

db.setProfilingLevel(0);
assert(db.getProfilingLevel() == 0, "prof level 0");

dd( "f" );
asserted = false;
try {
    db.setProfilingLevel(10);
    assert(false);
}
catch (e) { 
    asserted = true;
    assert(e.dbSetProfilingException);
}
assert( asserted, "should have asserted" );

dd( "g" );



assert.eq( "foo" , db.getSisterDB( "foo" ).getName() );
assert.eq( "foo" , db.getSiblingDB( "foo" ).getName() );