Skip to content

Commit 5bc01d4

Browse files
committed
OGM-1588 Fix test for $explain with native queries
It checks the result of the `$explain` operation, and it's compatible with MongoDB 7.0 and 3.6
1 parent 6309e37 commit 5bc01d4

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/MongoDBSessionCLIQueryTest.java

+29-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.rules.ExpectedException;
2626

2727
import com.mongodb.BasicDBList;
28+
import org.bson.Document;
2829
import org.fest.assertions.Fail;
2930
import org.fest.assertions.MapAssert;
3031

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

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

729-
NativeQuery query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class );
730-
@SuppressWarnings("unchecked")
732+
NativeQuery<OscarWildePoem> query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class );
731733
List<OscarWildePoem> result = query.list();
732-
assertThat( result ).onProperty( "id" ).containsExactly( athanasia.getId() );
734+
assertThat( result ).containsExactly( athanasia );
733735
} );
734736
}
735737

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

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

755+
/**
756+
* Different versions of MongoDB return different results for the `$explain` operator.
757+
* But, we expect to have a `namespace` key inside a `Document` somewhere in the result.
758+
*/
759+
private static boolean explained(Object[] result) {
760+
if ( result == null ) {
761+
return false;
762+
}
763+
for ( Object value : result ) {
764+
if ( value instanceof Document ) {
765+
Document document = (Document) value;
766+
if ( document.containsKey( "namespace" ) ) {
767+
return true;
768+
}
769+
}
770+
}
771+
return false;
772+
}
773+
755774
@Test
756775
@TestForIssue(jiraKey = "OGM-1024")
757776
public void testAggregateWithUnwindGroupAndSort() {

mongodb/src/test/java/org/hibernate/ogm/datastore/mongodb/test/query/nativequery/OscarWildePoem.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ public boolean equals(Object o) {
151151
return false;
152152
}
153153
OscarWildePoem that = (OscarWildePoem) o;
154-
return Objects.equals( name, that.name ) &&
155-
Objects.equals( author, that.author ) &&
156-
Objects.equals( year, that.year );
154+
return rating == that.rating && Objects.equals( name, that.name ) && Objects.equals(
155+
author,
156+
that.author
157+
) && Objects.equals( year, that.year ) && Objects.equals( copiesSold, that.copiesSold );
157158
}
158159

159160
@Override
160161
public int hashCode() {
161-
162-
return Objects.hash( name, author, year );
162+
return Objects.hash( name, author, rating, year, copiesSold );
163163
}
164164
}

0 commit comments

Comments
 (0)