Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
OGM-1588 Fix test for $explain with native queries
Browse files Browse the repository at this point in the history
Now it checks the result of the operation and it's compatible with
MongoDB 7.0 and 3.6
  • Loading branch information
DavideD committed Sep 3, 2024
1 parent 6309e37 commit 9757e5f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 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 @@ -726,10 +727,9 @@ public void testFindWithModifiersWithEntity() {

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 @@ -743,12 +743,17 @@ public void testFindWithExplain() {
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();
Object[] result = (Object[]) session.createNativeQuery( nativeQuery ).uniqueResult();
assertThat( result ).isNotEmpty();
// In MongoDB 7.0, the first entry of the result is "1" (I don't know why).
int index = result[0].equals( "1" ) ? 1 : 0;
assertThat( result[index] ).isInstanceOf( Document.class );
assertThat( ( (Document) result[index] ).getString( "namespace" ) ).isEqualTo( "ogm_test_database." + OscarWildePoem.TABLE_NAME );

index++;
assertThat( result[index] ).isInstanceOf( Document.class );
assertThat( ( (Document) result[index] ).getBoolean( "executionSuccess" ) ).isEqualTo( true );
assertThat( ( (Document) result[index] ).getInteger( "nReturned" ) ).isEqualTo( 1 );
} );
}

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 9757e5f

Please sign in to comment.