Skip to content

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
merged 16 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
4ec960e
Implement visitBooleanExpressionPredicate
NathanQingyangXu Apr 23, 2025
90ff011
refactor testing case out of SimpleSelectQueryIntegrationTests#QueryL…
NathanQingyangXu Apr 29, 2025
b30fb2d
add "unsupported" testing case
NathanQingyangXu Apr 29, 2025
4fa0376
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Ab…
NathanQingyangXu May 2, 2025
a980f21
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Bo…
NathanQingyangXu May 2, 2025
2e325c5
revert back Book.id type refactoring for it is unnecessary
NathanQingyangXu May 2, 2025
aa987b5
rename BooleanExpressionPredicateTranslatingTests to BooleanExpressio…
NathanQingyangXu May 2, 2025
48d2c27
make fields in AbstractSelectionQueryIntegrationTests private and add…
NathanQingyangXu May 2, 2025
b420a2d
introduce query assertion over-loaded methods omitting query post pro…
NathanQingyangXu May 2, 2025
6b12a49
Merge branch 'main' into HIBERNATE-78-new
NathanQingyangXu May 2, 2025
2745f4b
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Ab…
NathanQingyangXu May 5, 2025
8cb0c52
Update src/integrationTest/java/com/mongodb/hibernate/query/select/Ab…
NathanQingyangXu May 5, 2025
42bbc77
Merge branch 'main' into HIBERNATE-78-new
NathanQingyangXu May 5, 2025
10789b4
fix spotless issue; switch to package-private visibility for two acce…
NathanQingyangXu May 5, 2025
b716743
Merge branch 'main' into HIBERNATE-78-new
NathanQingyangXu May 5, 2025
2653a18
resolve conflict manually with latest main
NathanQingyangXu May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

<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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public class Book {
@ObjectIdGenerator
ObjectId id;

public Book() {}
Book() {}

String title;
Boolean outOfStock;
Integer publishYear;
Long isbn13;
Double discount;
BigDecimal price;
String title = "";
Boolean outOfStock = false;
Integer publishYear = 0;
Long isbn13 = 0L;
Double discount = 0.0D;
BigDecimal price = new BigDecimal("0.0");
}
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");
}
}
Loading