Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
OGM-1588 Make MongoDBIndexTest resilient to different versions of Mon…
Browse files Browse the repository at this point in the history
…goDB
  • Loading branch information
jyemin authored and DavideD committed Sep 3, 2024
1 parent 5ac9e00 commit 5863b4a
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public void testSuccessfulIndexCreation() throws Exception {
// on same field set
assertThat( indexMap.size() ).isEqualTo( 5 );

assertJsonEquals( "{ 'v' : 2 , 'key' : { 'author' : 1} , 'name' : 'author_idx' , 'ns' : 'ogm_test_database.T_POEM' , 'background' : true , 'partialFilterExpression' : { 'author' : 'Verlaine'}}",
sanitizeIndexMap( indexMap );

assertJsonEquals( "{ 'v' : 2 , 'key' : { 'author' : 1} , 'name' : 'author_idx' , 'background' : true , 'partialFilterExpression' : { 'author' : 'Verlaine'}}",
indexMap.get( "author_idx" ).toJson( jsonWriterSettings ) );
// TODO OGM-1080: the order should be -1 but we are waiting for ORM 5.2 which exposes this value and allows us to retrieve it
assertJsonEquals( "{ 'v' : 2 , 'key' : { 'name' : 1} , 'name' : 'name_idx' , 'ns' : 'ogm_test_database.T_POEM' , 'expireAfterSeconds' : { '$numberLong' : '10' }}",
assertJsonEquals( "{ 'v' : 2 , 'key' : { 'name' : 1} , 'name' : 'name_idx' , 'expireAfterSeconds' : { '$numberLong' : '10' }}",
indexMap.get( "name_idx" ).toJson( jsonWriterSettings ) );
assertJsonEquals( "{ 'v' : 2 , 'unique' : true , 'key' : { 'author' : 1 , 'name' : 1} , 'name' : 'author_name_idx' , 'ns' : 'ogm_test_database.T_POEM' , 'sparse' : true}",
assertJsonEquals( "{ 'v' : 2 , 'unique' : true , 'key' : { 'author' : 1 , 'name' : 1} , 'name' : 'author_name_idx' , 'sparse' : true}",
indexMap.get( "author_name_idx" ).toJson( jsonWriterSettings ) );

session.close();
Expand All @@ -61,7 +63,9 @@ public void testSuccessfulTextIndexCreation() throws Exception {
// on same field set
assertThat( indexMap.size() ).isEqualTo( 5 );

assertJsonEquals( "{ 'v' : 2 , 'key' : { '_fts' : 'text' , '_ftsx' : 1} , 'name' : 'author_name_text_idx' , 'ns' : 'ogm_test_database.T_POEM' , 'weights' : { 'author' : 2, 'name' : 5} , 'default_language' : 'fr' , 'language_override' : 'language' , 'textIndexVersion' : 3}",
sanitizeIndexMap( indexMap );

assertJsonEquals( "{ 'v' : 2 , 'key' : { '_fts' : 'text' , '_ftsx' : 1} , 'name' : 'author_name_text_idx' , 'weights' : { 'author' : 2, 'name' : 5} , 'default_language' : 'fr' , 'language_override' : 'language' , 'textIndexVersion' : 3}",
indexMap.get( "author_name_text_idx" ).toJson( jsonWriterSettings ) );

session.close();
Expand All @@ -74,7 +78,9 @@ public void testSuccessfulTextIndexWithTypeCreation() throws Exception {
Map<String, Document> indexMap = getIndexes( session.getSessionFactory(), OscarWildePoem.COLLECTION_NAME );
assertThat( indexMap.size() ).isEqualTo( 3 );

assertJsonEquals( "{ 'v' : 2 , 'key' : { '_fts' : 'text' , '_ftsx' : 1} , 'name' : 'name_text_idx' , 'ns' : 'ogm_test_database.T_OSCAR_WILDE_POEM', 'default_language' : 'fr' , 'language_override' : 'language' , weights : { name: 5 } , 'textIndexVersion' : 3}",
sanitizeIndexMap( indexMap );

assertJsonEquals( "{ 'v' : 2 , 'key' : { '_fts' : 'text' , '_ftsx' : 1} , 'name' : 'name_text_idx' , 'default_language' : 'fr' , 'language_override' : 'language' , weights : { name: 5 } , 'textIndexVersion' : 3}",
indexMap.get( "name_text_idx" ).toJson( jsonWriterSettings ) );

session.close();
Expand All @@ -87,12 +93,24 @@ public void testSuccessfulSpatialIndexCreation() throws Exception {
Map<String, Document> indexMap = getIndexes( session.getSessionFactory(), Restaurant.COLLECTION_NAME );
assertThat( indexMap.size() ).isEqualTo( 2 );

assertJsonEquals( "{ 'v' : 2 , 'key' : { 'location' : '2dsphere'} , 'name' : 'location_spatial_idx' , 'ns' : 'ogm_test_database.T_RESTAURANT' , 2dsphereIndexVersion=3}",
sanitizeIndexMap( indexMap );

assertJsonEquals( "{ 'v' : 2 , 'key' : { 'location' : '2dsphere'} , 'name' : 'location_spatial_idx' , 2dsphereIndexVersion=3}",
indexMap.get( "location_spatial_idx" ).toJson( jsonWriterSettings ) );

session.close();
}

// Normalize index map to account for difference in output in various versions of MongoDB
private static void sanitizeIndexMap(Map<String, Document> indexMap) {
indexMap.values().forEach( document -> {
document.remove( "ns" );
if ( document.containsKey( "expireAfterSeconds" ) ) {
document.put( "expireAfterSeconds", document.toBsonDocument().getNumber( "expireAfterSeconds" ).longValue() );
}
} );
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Poem.class, OscarWildePoem.class, Restaurant.class };
Expand Down

0 comments on commit 5863b4a

Please sign in to comment.