Skip to content

Commit

Permalink
fix SchemaUpdaterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangs18 committed Feb 20, 2024
1 parent 33f1559 commit 850d085
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
25 changes: 25 additions & 0 deletions src/us/kbase/workspace/test/WorkspaceMongoIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package us.kbase.workspace.test;

import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.HashSet;
import java.util.Set;

public class WorkspaceMongoIndex {

public static Set<Document> getAndNormalizeIndexes(final MongoDatabase db, final String collectionName) {
final Set<Document> indexes = new HashSet<>();
for (Document index: db.getCollection(collectionName).listIndexes()) {
// In MongoDB 4.4, the listIndexes and the mongo shell helper method db.collection.getAndNormalizeIndexes()
// no longer returns the namespace ns field in the index specification documents.
index.remove("ns");
// some versions of Mongo return ints, some longs. Convert all to longs.
if (index.containsKey("expireAfterSeconds")) {
index.put("expireAfterSeconds", ((Number) index.get("expireAfterSeconds")).longValue());
}
indexes.add(index);
}
return indexes;
}
}
16 changes: 1 addition & 15 deletions src/us/kbase/workspace/test/database/mongo/MongoStartUpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.fail;
import static us.kbase.common.test.TestCommon.assertExceptionCorrect;
import static us.kbase.common.test.TestCommon.set;
import static us.kbase.workspace.test.WorkspaceMongoIndex.getAndNormalizeIndexes;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -297,21 +298,6 @@ private void createIndexes(final String dbname) throws Exception {
method.invoke(null, wsdb);
}

private Set<Document> getAndNormalizeIndexes(final MongoDatabase db, final String collectionName) {
final Set<Document> indexes = new HashSet<>();
for (Document index: db.getCollection(collectionName).listIndexes()) {
// In MongoDB 4.4, the listIndexes and the mongo shell helper method db.collection.getAndNormalizeIndexes()
// no longer returns the namespace ns field in the index specification documents.
index.remove("ns");
// some versions of Mongo return ints, some longs. Convert all to longs.
if (index.containsKey("expireAfterSeconds")) {
index.put("expireAfterSeconds", ((Number) index.get("expireAfterSeconds")).longValue());
}
indexes.add(index);
}
return indexes;
}

@Test
public void indexesConfig() throws Exception {
final Set<Document> expectedIndexes = set(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
import static us.kbase.workspace.test.WorkspaceTestCommon.basicProv;
import static us.kbase.workspace.test.WorkspaceMongoIndex.getAndNormalizeIndexes;

import java.nio.file.Paths;
import java.time.Instant;
Expand Down Expand Up @@ -256,23 +257,18 @@ private void assertIndexesCreated(final MongoDatabase db, final boolean oldTypeI
* MongoWorkspaceDB method is already thoroughly tested elsewhere we just check a single
* index.
*/
final String ns = db.getName() + "." + COL_WS_VER;
final Document currentIndex = new Document("v", 2)
.append("key", new Document("tyname", 1)
.append("tymaj", 1).append("tymin", 1)
.append("ws", 1).append("id", 1).append("ver", -1))
.append("name", "tyname_1_tymaj_1_tymin_1_ws_1_id_1_ver_-1")
.append("ns", ns);
final Set<Document> indexes = new HashSet<>();
db.getCollection("workspaceObjVersions").listIndexes()
.forEach((Consumer<Document>) indexes::add);
.append("name", "tyname_1_tymaj_1_tymin_1_ws_1_id_1_ver_-1");
final Set<Document> indexes = getAndNormalizeIndexes(db, "workspaceObjVersions");
assertThat("incorrect current index", indexes.contains(currentIndex), is(true));

if (oldTypeIndex) {
final Document typeIndex = new Document("v", 2)
.append("key", new Document("type", 1).append("chksum", 1))
.append("name", "type_1_chksum_1")
.append("ns", ns);
.append("name", "type_1_chksum_1");
assertThat("incorrect old index", indexes.contains(typeIndex), is(true));
}
}
Expand Down

0 comments on commit 850d085

Please sign in to comment.