-
Notifications
You must be signed in to change notification settings - Fork 5
Implement visitBooleanExpressionPredicate #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NathanQingyangXu
merged 16 commits into
mongodb:main
from
NathanQingyangXu:HIBERNATE-78-new
May 5, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4ec960e
Implement visitBooleanExpressionPredicate
NathanQingyangXu 90ff011
refactor testing case out of SimpleSelectQueryIntegrationTests#QueryL…
NathanQingyangXu b30fb2d
add "unsupported" testing case
NathanQingyangXu 4fa0376
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Ab…
NathanQingyangXu a980f21
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Bo…
NathanQingyangXu 2e325c5
revert back Book.id type refactoring for it is unnecessary
NathanQingyangXu aa987b5
rename BooleanExpressionPredicateTranslatingTests to BooleanExpressio…
NathanQingyangXu 48d2c27
make fields in AbstractSelectionQueryIntegrationTests private and add…
NathanQingyangXu b420a2d
introduce query assertion over-loaded methods omitting query post pro…
NathanQingyangXu 6b12a49
Merge branch 'main' into HIBERNATE-78-new
NathanQingyangXu 2745f4b
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Ab…
NathanQingyangXu 8cb0c52
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Ab…
NathanQingyangXu 42bbc77
Merge branch 'main' into HIBERNATE-78-new
NathanQingyangXu 10789b4
fix spotless issue; switch to package-private visibility for two acce…
NathanQingyangXu b716743
Merge branch 'main' into HIBERNATE-78-new
NathanQingyangXu 2653a18
resolve conflict manually with latest main
NathanQingyangXu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...nTest/java/com/mongodb/hibernate/query/select/AbstractSelectionQueryIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright 2025-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.hibernate.query.select; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import com.mongodb.hibernate.TestCommandListener; | ||
import com.mongodb.hibernate.junit.MongoExtension; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.bson.BsonDocument; | ||
import org.hibernate.query.SelectionQuery; | ||
import org.hibernate.testing.orm.junit.ServiceRegistryScope; | ||
import org.hibernate.testing.orm.junit.ServiceRegistryScopeAware; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScopeAware; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@SessionFactory(exportSchema = false) | ||
@ExtendWith(MongoExtension.class) | ||
abstract class AbstractSelectionQueryIntegrationTests implements SessionFactoryScopeAware, ServiceRegistryScopeAware { | ||
|
||
private SessionFactoryScope sessionFactoryScope; | ||
|
||
private TestCommandListener testCommandListener; | ||
|
||
SessionFactoryScope getSessionFactoryScope() { | ||
return sessionFactoryScope; | ||
} | ||
|
||
TestCommandListener getTestCommandListener() { | ||
return testCommandListener; | ||
} | ||
|
||
@Override | ||
public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) { | ||
this.sessionFactoryScope = sessionFactoryScope; | ||
} | ||
|
||
@Override | ||
public void injectServiceRegistryScope(ServiceRegistryScope serviceRegistryScope) { | ||
this.testCommandListener = serviceRegistryScope.getRegistry().requireService(TestCommandListener.class); | ||
} | ||
|
||
<T> void assertSelectionQuery( | ||
String hql, | ||
Class<T> resultType, | ||
Consumer<SelectionQuery<T>> queryPostProcessor, | ||
String expectedMql, | ||
List<T> expectedResultList) { | ||
assertSelectionQuery(hql, resultType, queryPostProcessor, expectedMql, resultList -> assertThat(resultList) | ||
.usingRecursiveFieldByFieldElementComparator() | ||
.containsExactlyElementsOf(expectedResultList)); | ||
} | ||
|
||
<T> void assertSelectionQuery(String hql, Class<T> resultType, String expectedMql, List<T> expectedResultList) { | ||
assertSelectionQuery(hql, resultType, null, expectedMql, expectedResultList); | ||
} | ||
|
||
<T> void assertSelectionQuery( | ||
String hql, | ||
Class<T> resultType, | ||
Consumer<SelectionQuery<T>> queryPostProcessor, | ||
String expectedMql, | ||
Consumer<List<T>> resultListVerifier) { | ||
sessionFactoryScope.inTransaction(session -> { | ||
var selectionQuery = session.createSelectionQuery(hql, resultType); | ||
if (queryPostProcessor != null) { | ||
queryPostProcessor.accept(selectionQuery); | ||
} | ||
var resultList = selectionQuery.getResultList(); | ||
|
||
assertActualCommand(BsonDocument.parse(expectedMql)); | ||
|
||
resultListVerifier.accept(resultList); | ||
}); | ||
} | ||
|
||
<T> void assertSelectionQuery( | ||
String hql, Class<T> resultType, String expectedMql, Consumer<List<T>> resultListVerifier) { | ||
assertSelectionQuery(hql, resultType, null, expectedMql, resultListVerifier); | ||
} | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<T> void assertSelectQueryFailure( | ||
String hql, | ||
Class<T> resultType, | ||
Consumer<SelectionQuery<T>> queryPostProcessor, | ||
Class<? extends Exception> expectedExceptionType, | ||
String expectedExceptionMessage, | ||
Object... expectedExceptionMessageParameters) { | ||
sessionFactoryScope.inTransaction(session -> assertThatThrownBy(() -> { | ||
var selectionQuery = session.createSelectionQuery(hql, resultType); | ||
if (queryPostProcessor != null) { | ||
queryPostProcessor.accept(selectionQuery); | ||
} | ||
selectionQuery.getResultList(); | ||
}) | ||
.isInstanceOf(expectedExceptionType) | ||
.hasMessage(expectedExceptionMessage, expectedExceptionMessageParameters)); | ||
} | ||
|
||
<T> void assertSelectQueryFailure( | ||
String hql, | ||
Class<T> resultType, | ||
Class<? extends Exception> expectedExceptionType, | ||
String expectedExceptionMessage, | ||
Object... expectedExceptionMessageParameters) { | ||
assertSelectQueryFailure( | ||
hql, | ||
resultType, | ||
null, | ||
expectedExceptionType, | ||
expectedExceptionMessage, | ||
expectedExceptionMessageParameters); | ||
} | ||
|
||
void assertActualCommand(BsonDocument expectedCommand) { | ||
var capturedCommands = testCommandListener.getStartedCommands(); | ||
|
||
assertThat(capturedCommands) | ||
.singleElement() | ||
.asInstanceOf(InstanceOfAssertFactories.MAP) | ||
.containsAllEntriesOf(expectedCommand); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...java/com/mongodb/hibernate/query/select/BooleanExpressionWhereClauseIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2025-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.hibernate.query.select; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.mongodb.hibernate.internal.FeatureNotSupportedException; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
@DomainModel(annotatedClasses = Book.class) | ||
class BooleanExpressionWhereClauseIntegrationTests extends AbstractSelectionQueryIntegrationTests { | ||
|
||
private Book bookOutOfStock; | ||
private Book bookInStock; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
bookOutOfStock = new Book(); | ||
bookOutOfStock.outOfStock = true; | ||
|
||
bookInStock = new Book(); | ||
bookInStock.outOfStock = false; | ||
|
||
getSessionFactoryScope().inTransaction(session -> { | ||
session.persist(bookOutOfStock); | ||
session.persist(bookInStock); | ||
}); | ||
|
||
getTestCommandListener().clear(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {true, false}) | ||
void testBooleanFieldPathExpression(boolean negated) { | ||
assertSelectionQuery( | ||
"from Book where" + (negated ? " not " : " ") + "outOfStock", | ||
Book.class, | ||
"{'aggregate': 'books', 'pipeline': [{'$match': {'outOfStock': {'$eq': " | ||
+ (negated ? "false" : "true") | ||
+ "}}}, {'$project': {'_id': true, 'discount': true, 'isbn13': true, 'outOfStock': true, 'price': true, 'publishYear': true, 'title': true}}]}", | ||
negated ? singletonList(bookInStock) : singletonList(bookOutOfStock)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(booleans = {true, false}) | ||
void testNonFieldPathExpressionNotSupported(final boolean booleanLiteral) { | ||
assertSelectQueryFailure( | ||
"from Book where " + booleanLiteral, | ||
Book.class, | ||
FeatureNotSupportedException.class, | ||
"Expression not of field path not supported"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.