Skip to content

Commit

Permalink
OGM-1588 Fix test for $explain with native queries
Browse files Browse the repository at this point in the history
It checks the result of the `$explain` operation, and
it's compatible with MongoDB 7.0 and 3.6
  • Loading branch information
DavideD committed Sep 5, 2024
1 parent 6309e37 commit 5bc01d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.rules.ExpectedException;

import com.mongodb.BasicDBList;
import org.bson.Document;
import org.fest.assertions.Fail;
import org.fest.assertions.MapAssert;

Expand Down Expand Up @@ -719,17 +720,18 @@ public void testFindWithModifiersWithEntity() {
queryWithModifiers.append( "'$query': { } " );
queryWithModifiers.append( ", '$max': { 'year' : 1881 } " );
queryWithModifiers.append( ", '$hint': { 'year' : 1 } " );
// It's important to have at least one test with the `$explain` modifier set to false because
// if something goes wrong it will return a different result type
queryWithModifiers.append( ", '$explain': false " );
queryWithModifiers.append( ", '$snapshot': false " );
queryWithModifiers.append( ", '$comment': 'Testing comment' " );
queryWithModifiers.append( ", '$maxScan': 11234" );

String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({" + queryWithModifiers + "})";

NativeQuery query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class );
@SuppressWarnings("unchecked")
NativeQuery<OscarWildePoem> query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class );
List<OscarWildePoem> result = query.list();
assertThat( result ).onProperty( "id" ).containsExactly( athanasia.getId() );
assertThat( result ).containsExactly( athanasia );
} );
}

Expand All @@ -741,17 +743,34 @@ public void testFindWithExplain() {
queryWithModifiers.append( ", '$max': { 'year' : 1881 } " );
queryWithModifiers.append( ", '$hint': { 'year' : 1 } " );
queryWithModifiers.append( ", '$explain': true " );
String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({" + queryWithModifiers + "})";

NativeQuery query = session.createNativeQuery( nativeQuery );
@SuppressWarnings("unchecked")
List<Object[]> result = query.list();
// I'm not sure if we can test the content because this is the result of the explain command
// and I believe it might change among versions
assertThat( result.get( 0 ) ).isNotEmpty();
String nativeQuery = "db." + OscarWildePoem.TABLE_NAME + ".find({" + queryWithModifiers + "})";
Object[] result = (Object[]) session.createNativeQuery( nativeQuery ).uniqueResult();
assertThat( explained( result ) )
.as( "Unrecognized `$explain` output: " + Arrays.toString( result ) )
.isTrue();
} );
}

/**
* Different versions of MongoDB return different results for the `$explain` operator.
* But, we expect to have a `namespace` key inside a `Document` somewhere in the result.
*/
private static boolean explained(Object[] result) {
if ( result == null ) {
return false;
}
for ( Object value : result ) {
if ( value instanceof Document ) {
Document document = (Document) value;
if ( document.containsKey( "namespace" ) ) {
return true;
}
}
}
return false;
}

@Test
@TestForIssue(jiraKey = "OGM-1024")
public void testAggregateWithUnwindGroupAndSort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ public boolean equals(Object o) {
return false;
}
OscarWildePoem that = (OscarWildePoem) o;
return Objects.equals( name, that.name ) &&
Objects.equals( author, that.author ) &&
Objects.equals( year, that.year );
return rating == that.rating && Objects.equals( name, that.name ) && Objects.equals(
author,
that.author
) && Objects.equals( year, that.year ) && Objects.equals( copiesSold, that.copiesSold );
}

@Override
public int hashCode() {

return Objects.hash( name, author, year );
return Objects.hash( name, author, rating, year, copiesSold );
}
}

0 comments on commit 5bc01d4

Please sign in to comment.