Skip to content

Commit 5c7a58d

Browse files
some improvements
1 parent f8edea4 commit 5c7a58d

File tree

8 files changed

+475
-294
lines changed

8 files changed

+475
-294
lines changed

src/integrationTest/java/com/mongodb/hibernate/query/AbstractQueryIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ protected void assertMutationQuery(
158158
if (queryPostProcessor != null) {
159159
queryPostProcessor.accept(query);
160160
}
161-
assertThat(query.executeUpdate()).isEqualTo(expectedMutationCount);
161+
var mutationCount = query.executeUpdate();
162162
assertActualCommand(BsonDocument.parse(expectedMql));
163+
assertThat(mutationCount).isEqualTo(expectedMutationCount);
163164
});
164165
assertThat(collection.find()).containsExactlyElementsOf(expectedDocuments);
165166
}

src/integrationTest/java/com/mongodb/hibernate/query/Book.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import java.math.BigDecimal;
2323

2424
@Entity(name = "Book")
25-
@Table(name = Book.COLLECTION_NAME)
25+
@Table(name = Book.COLLECTION)
2626
public class Book {
27-
public static final String COLLECTION_NAME = "books";
27+
public static final String COLLECTION = "books";
2828

2929
@Id
3030
public int id;
@@ -39,10 +39,6 @@ public class Book {
3939

4040
public Book() {}
4141

42-
public Book(int id, String title, Integer publishYear) {
43-
this(id, title, publishYear, false);
44-
}
45-
4642
public Book(int id, String title, Integer publishYear, Boolean outOfStock) {
4743
this.id = id;
4844
this.title = title;

src/integrationTest/java/com/mongodb/hibernate/query/mutation/AbstractMutationQueryIntegrationTests.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,22 @@
3939
import org.hibernate.testing.orm.junit.Setting;
4040

4141
@ServiceRegistry(
42-
settings = {
43-
@Setting(
44-
name = DIALECT,
45-
value =
46-
"com.mongodb.hibernate.query.mutation.AbstractMutationQueryIntegrationTests$MutationTranslatorAwareDialect"),
47-
})
42+
settings =
43+
@Setting(
44+
name = DIALECT,
45+
value =
46+
"com.mongodb.hibernate.query.mutation.AbstractMutationQueryIntegrationTests$MutationTranslatorAwareDialect"))
4847
class AbstractMutationQueryIntegrationTests extends AbstractQueryIntegrationTests {
4948

50-
void assertAffectedTableNames(String hql, String expectedAffectedTableName) {
51-
assertAffectedTableNames(hql, null, Set.of(expectedAffectedTableName));
49+
void assertAffectedTable(String hql, String expectedAffectedTableName) {
50+
assertAffectedTables(hql, null, Set.of(expectedAffectedTableName));
5251
}
5352

54-
void assertAffectedTableNames(
55-
String hql, Consumer<MutationQuery> queryPostProcessor, String expectedAffectedTableName) {
56-
assertAffectedTableNames(hql, queryPostProcessor, Set.of(expectedAffectedTableName));
53+
void assertAffectedTable(String hql, Consumer<MutationQuery> queryPostProcessor, String expectedAffectedTableName) {
54+
assertAffectedTables(hql, queryPostProcessor, Set.of(expectedAffectedTableName));
5755
}
5856

59-
void assertAffectedTableNames(
57+
void assertAffectedTables(
6058
String hql, Consumer<MutationQuery> queryPostProcessor, Set<String> expectedAffectedTableNames) {
6159
getSessionFactoryScope().inTransaction(session -> {
6260
var query = session.createMutationQuery(hql);

src/integrationTest/java/com/mongodb/hibernate/query/mutation/DeletionIntegrationTests.java

Lines changed: 148 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
@DomainModel(annotatedClasses = Book.class)
2929
class DeletionIntegrationTests extends AbstractMutationQueryIntegrationTests {
3030

31-
@InjectMongoCollection(Book.COLLECTION_NAME)
31+
@InjectMongoCollection(Book.COLLECTION)
3232
private static MongoCollection<BsonDocument> mongoCollection;
3333

3434
private static final List<Book> testingBooks = List.of(
35-
new Book(1, "War and Peace", 1869),
36-
new Book(2, "Crime and Punishment", 1866),
37-
new Book(3, "Anna Karenina", 1877),
38-
new Book(4, "The Brothers Karamazov", 1880),
39-
new Book(5, "War and Peace", 2025));
35+
new Book(1, "War and Peace", 1869, true),
36+
new Book(2, "Crime and Punishment", 1866, false),
37+
new Book(3, "Anna Karenina", 1877, false),
38+
new Book(4, "The Brothers Karamazov", 1880, false),
39+
new Book(5, "War and Peace", 2025, false));
4040

4141
@BeforeEach
4242
void beforeEach() {
@@ -45,71 +45,154 @@ void beforeEach() {
4545
}
4646

4747
@Test
48-
void testSimpleDeletion() {
49-
getSessionFactoryScope()
50-
.inTransaction(
51-
session -> assertMutationQuery(
52-
"delete from Book where title = :title",
53-
q -> q.setParameter("title", "War and Peace"),
54-
2,
48+
void testDeletionWithNonZeroMutationCount() {
49+
assertMutationQuery(
50+
"delete from Book where title = :title",
51+
q -> q.setParameter("title", "War and Peace"),
52+
2,
53+
"""
54+
{
55+
"delete": "books",
56+
"deletes": [
57+
{
58+
"limit": 0,
59+
"q": {
60+
"title": {
61+
"$eq": "War and Peace"
62+
}
63+
}
64+
}
65+
]
66+
}
67+
""",
68+
mongoCollection,
69+
List.of(
70+
BsonDocument.parse(
71+
"""
72+
{
73+
"_id": 2,
74+
"title": "Crime and Punishment",
75+
"outOfStock": false,
76+
"publishYear": 1866,
77+
"isbn13": {"$numberLong": "0"},
78+
"discount": {"$numberDouble": "0"},
79+
"price": {"$numberDecimal": "0.0"}
80+
}
81+
"""),
82+
BsonDocument.parse(
83+
"""
84+
{
85+
"_id": 3,
86+
"title": "Anna Karenina",
87+
"outOfStock": false,
88+
"publishYear": 1877,
89+
"isbn13": {"$numberLong": "0"},
90+
"discount": {"$numberDouble": "0"},
91+
"price": {"$numberDecimal": "0.0"}
92+
}
93+
"""),
94+
BsonDocument.parse(
95+
"""
96+
{
97+
"_id": 4,
98+
"title": "The Brothers Karamazov",
99+
"outOfStock": false,
100+
"publishYear": 1880,
101+
"isbn13": {"$numberLong": "0"},
102+
"discount": {"$numberDouble": "0"},
103+
"price": {"$numberDecimal": "0.0"}
104+
}
105+
""")));
106+
}
107+
108+
@Test
109+
void testDeletionWithZeroMutationCount() {
110+
assertMutationQuery(
111+
"delete from Book where publishYear < :year",
112+
q -> q.setParameter("year", 1850),
113+
0,
114+
"""
115+
{
116+
"delete": "books",
117+
"deletes": [
118+
{
119+
"limit": 0,
120+
"q": {
121+
"publishYear": {
122+
"$lt": 1850
123+
}
124+
}
125+
}
126+
]
127+
}
128+
""",
129+
mongoCollection,
130+
List.of(
131+
BsonDocument.parse(
132+
"""
133+
{
134+
"_id": 1,
135+
"title": "War and Peace",
136+
"outOfStock": true,
137+
"publishYear": 1869,
138+
"isbn13": {"$numberLong": "0"},
139+
"discount": {"$numberDouble": "0"},
140+
"price": {"$numberDecimal": "0.0"}
141+
}
142+
"""),
143+
BsonDocument.parse(
144+
"""
145+
{
146+
"_id": 2,
147+
"title": "Crime and Punishment",
148+
"outOfStock": false,
149+
"publishYear": 1866,
150+
"isbn13": {"$numberLong": "0"},
151+
"discount": {"$numberDouble": "0"},
152+
"price": {"$numberDecimal": "0.0"}
153+
}
154+
"""),
155+
BsonDocument.parse(
156+
"""
157+
{
158+
"_id": 3,
159+
"title": "Anna Karenina",
160+
"outOfStock": false,
161+
"publishYear": 1877,
162+
"isbn13": {"$numberLong": "0"},
163+
"discount": {"$numberDouble": "0"},
164+
"price": {"$numberDecimal": "0.0"}
165+
}
166+
"""),
167+
BsonDocument.parse(
168+
"""
169+
{
170+
"_id": 4,
171+
"title": "The Brothers Karamazov",
172+
"outOfStock": false,
173+
"publishYear": 1880,
174+
"isbn13": {"$numberLong": "0"},
175+
"discount": {"$numberDouble": "0"},
176+
"price": {"$numberDecimal": "0.0"}
177+
}
178+
"""),
179+
BsonDocument.parse(
55180
"""
56181
{
57-
"delete": "books",
58-
"deletes": [
59-
{
60-
"limit": 0,
61-
"q": {
62-
"title": {
63-
"$eq": "War and Peace"
64-
}
65-
}
66-
}
67-
]
182+
"_id": 5,
183+
"title": "War and Peace",
184+
"outOfStock": false,
185+
"publishYear": 2025,
186+
"isbn13": {"$numberLong": "0"},
187+
"discount": {"$numberDouble": "0"},
188+
"price": {"$numberDecimal": "0.0"}
68189
}
69-
""",
70-
mongoCollection,
71-
List.of(
72-
BsonDocument.parse(
73-
"""
74-
{
75-
"_id": 2,
76-
"title": "Crime and Punishment",
77-
"outOfStock": false,
78-
"publishYear": 1866,
79-
"isbn13": {"$numberLong": "0"},
80-
"discount": {"$numberDouble": "0"},
81-
"price": {"$numberDecimal": "0.0"}
82-
}
83-
"""),
84-
BsonDocument.parse(
85-
"""
86-
{
87-
"_id": 3,
88-
"title": "Anna Karenina",
89-
"outOfStock": false,
90-
"publishYear": 1877,
91-
"isbn13": {"$numberLong": "0"},
92-
"discount": {"$numberDouble": "0"},
93-
"price": {"$numberDecimal": "0.0"}
94-
}
95-
"""),
96-
BsonDocument.parse(
97-
"""
98-
{
99-
"_id": 4,
100-
"title": "The Brothers Karamazov",
101-
"outOfStock": false,
102-
"publishYear": 1880,
103-
"isbn13": {"$numberLong": "0"},
104-
"discount": {"$numberDouble": "0"},
105-
"price": {"$numberDecimal": "0.0"}
106-
}
107-
"""))));
190+
""")));
108191
}
109192

110193
@Test
111-
void testAffectedTableNames() {
112-
assertAffectedTableNames(
194+
void testAffectedTable() {
195+
assertAffectedTable(
113196
"""
114197
delete from Book where title = :title
115198
""",

0 commit comments

Comments
 (0)