Skip to content

Commit fdefbd9

Browse files
committed
Test MongoPreparedStatement.setObject
1 parent bff3df1 commit fdefbd9

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

src/integrationTest/java/com/mongodb/hibernate/id/ObjectIdFieldTypeIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ void insert() {
5151
item.v = new ObjectId(2, 0);
5252
sessionFactoryScope.inTransaction(session -> session.persist(item));
5353
assertThat(mongoCollection.find())
54-
.containsExactly(new BsonDocument(ID_FIELD_NAME, new BsonObjectId(item.id))
54+
.containsExactly(new BsonDocument()
55+
.append(ID_FIELD_NAME, new BsonObjectId(item.id))
5556
.append("v", new BsonObjectId(item.v)));
5657
}
5758

src/main/java/com/mongodb/hibernate/jdbc/MongoPreparedStatement.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
189189

190190
@Override
191191
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
192-
// VAKOTODO add tests
193192
checkClosed();
194193
checkParameterIndex(parameterIndex);
195194
BsonValue value;

src/test/java/com/mongodb/hibernate/jdbc/MongoPreparedStatementTests.java

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.mongodb.client.MongoCollection;
3636
import com.mongodb.client.MongoCursor;
3737
import com.mongodb.client.MongoDatabase;
38+
import com.mongodb.hibernate.internal.type.MqlType;
3839
import java.math.BigDecimal;
3940
import java.sql.Array;
4041
import java.sql.Date;
@@ -45,9 +46,16 @@
4546
import java.sql.Timestamp;
4647
import java.sql.Types;
4748
import java.util.Calendar;
49+
import java.util.List;
4850
import java.util.function.Consumer;
51+
import org.bson.BsonArray;
52+
import org.bson.BsonBoolean;
4953
import org.bson.BsonDocument;
54+
import org.bson.BsonInt32;
55+
import org.bson.BsonObjectId;
56+
import org.bson.BsonString;
5057
import org.bson.Document;
58+
import org.bson.types.ObjectId;
5159
import org.junit.jupiter.api.BeforeEach;
5260
import org.junit.jupiter.api.DisplayName;
5361
import org.junit.jupiter.api.Nested;
@@ -79,16 +87,18 @@ private MongoPreparedStatement createMongoPreparedStatement(String mql) throws S
7987
private static final String EXAMPLE_MQL =
8088
"""
8189
{
82-
insert: "books",
90+
insert: "items",
8391
documents: [
8492
{
85-
title: { $undefined: true },
86-
author: { $undefined: true },
87-
publishYear: { $undefined: true },
88-
outOfStock: { $undefined: true },
89-
tags: [
93+
string1: { $undefined: true },
94+
string2: { $undefined: true },
95+
int32: { $undefined: true },
96+
boolean: { $undefined: true },
97+
stringAndObjectId: [
98+
{ $undefined: true },
9099
{ $undefined: true }
91100
]
101+
objectId: { $undefined: true }
92102
}
93103
]
94104
}
@@ -110,33 +120,33 @@ void testSuccess() throws SQLException {
110120

111121
try (var preparedStatement = createMongoPreparedStatement(EXAMPLE_MQL)) {
112122

113-
preparedStatement.setString(1, "War and Peace");
114-
preparedStatement.setString(2, "Leo Tolstoy");
115-
preparedStatement.setInt(3, 1869);
116-
preparedStatement.setBoolean(4, false);
117-
preparedStatement.setString(5, "classic");
123+
preparedStatement.setString(1, "s1");
124+
preparedStatement.setString(2, "s2");
125+
preparedStatement.setInt(3, 1);
126+
preparedStatement.setBoolean(4, true);
127+
preparedStatement.setString(5, "array element");
128+
preparedStatement.setObject(6, new ObjectId(1, 2), MqlType.OBJECT_ID.getVendorTypeNumber());
129+
preparedStatement.setObject(7, new ObjectId(2, 0), MqlType.OBJECT_ID.getVendorTypeNumber());
118130

119131
preparedStatement.executeUpdate();
120132

121133
verify(mongoDatabase).runCommand(eq(clientSession), commandCaptor.capture());
122134
var command = commandCaptor.getValue();
123-
var expectedDoc = BsonDocument.parse(
124-
"""
125-
{
126-
insert: "books",
127-
documents: [
128-
{
129-
title: "War and Peace",
130-
author: "Leo Tolstoy",
131-
publishYear: 1869,
132-
outOfStock: false,
133-
tags: [
134-
"classic"
135-
]
136-
}
137-
]
138-
}
139-
""");
135+
var expectedDoc = new BsonDocument()
136+
.append("insert", new BsonString("items"))
137+
.append(
138+
"documents",
139+
new BsonArray(List.of(new BsonDocument()
140+
.append("string1", new BsonString("s1"))
141+
.append("string2", new BsonString("s2"))
142+
.append("int32", new BsonInt32(1))
143+
.append("boolean", BsonBoolean.TRUE)
144+
.append(
145+
"stringAndObjectId",
146+
new BsonArray(List.of(
147+
new BsonString("array element"),
148+
new BsonObjectId(new ObjectId(1, 2)))))
149+
.append("objectId", new BsonObjectId(new ObjectId(2, 0))))));
140150
assertEquals(expectedDoc, command);
141151
}
142152
}
@@ -151,7 +161,7 @@ void testParameterIndexUnderflow() throws SQLSyntaxErrorException {
151161
@Test
152162
void testParameterIndexOverflow() throws SQLSyntaxErrorException {
153163
var mongoPreparedStatement = createMongoPreparedStatement(EXAMPLE_MQL);
154-
checkSetterMethods(mongoPreparedStatement, 6, MongoPreparedStatementTests::assertThrowsOutOfRangeException);
164+
checkSetterMethods(mongoPreparedStatement, 8, MongoPreparedStatementTests::assertThrowsOutOfRangeException);
155165
}
156166

157167
@Nested

0 commit comments

Comments
 (0)