Skip to content

Commit 913e773

Browse files
committed
Added test for TableRow() function
1 parent 718c463 commit 913e773

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import org.junit.Assert;
2323
import org.junit.jupiter.api.AfterAll;
24+
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.BeforeAll;
2526
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
2628
import org.junit.jupiter.api.extension.RegisterExtension;
2729
import org.junit.jupiter.params.ParameterizedTest;
2830
import org.junit.jupiter.params.provider.EnumSource;
@@ -33,6 +35,11 @@
3335
import tech.ydb.jdbc.impl.helper.SqlQueries;
3436
import tech.ydb.jdbc.impl.helper.TextSelectAssert;
3537
import tech.ydb.table.values.PrimitiveType;
38+
import tech.ydb.table.values.PrimitiveValue;
39+
import tech.ydb.table.values.StructValue;
40+
import static tech.ydb.table.values.Type.Kind.OPTIONAL;
41+
import static tech.ydb.table.values.Type.Kind.PRIMITIVE;
42+
import tech.ydb.table.values.Value;
3643
import tech.ydb.test.junit5.YdbHelperExtension;
3744

3845
/**
@@ -294,6 +301,75 @@ private void assertRowValues(ResultSet rs, int id) throws SQLException {
294301
Assert.assertTrue(rs.wasNull());
295302
}
296303

304+
private void assertStructMember(PrimitiveValue value, StructValue sv, String name) {
305+
int index = sv.getType().getMemberIndex(name);
306+
Assertions.assertTrue(index >= 0);
307+
Value<?> member = sv.getMemberValue(index);
308+
309+
switch (member.getType().getKind()) {
310+
case OPTIONAL:
311+
if (value == null) {
312+
Assertions.assertFalse(member.asOptional().isPresent());
313+
} else {
314+
Assertions.assertEquals(value, (PrimitiveValue) member.asOptional().get());
315+
}
316+
break;
317+
case PRIMITIVE:
318+
Assertions.assertEquals(value, member.asData());
319+
break;
320+
default:
321+
throw new AssertionError("Unsupported type " + member.getType());
322+
}
323+
}
324+
325+
private void assertTableRow(ResultSet rs, int id) throws SQLException {
326+
Assert.assertTrue(rs.next());
327+
328+
Object obj = rs.getObject(1);
329+
Assertions.assertNotNull(obj);
330+
Assertions.assertFalse(rs.wasNull());
331+
Assertions.assertEquals(obj, rs.getObject("column0")); // default name of column
332+
333+
Assertions.assertTrue(obj instanceof StructValue);
334+
StructValue sv = (StructValue) obj;
335+
336+
Assertions.assertEquals(22, sv.getType().getMembersCount());
337+
338+
assertStructMember(PrimitiveValue.newInt32(id), sv, "key");
339+
assertStructMember(PrimitiveValue.newBool(id % 2 == 0), sv, "c_Bool");
340+
341+
assertStructMember(PrimitiveValue.newInt8((byte)(id + 1)), sv, "c_Int8");
342+
assertStructMember(PrimitiveValue.newInt16((short)(id + 2)), sv, "c_Int16");
343+
assertStructMember(PrimitiveValue.newInt32(id + 3), sv, "c_Int32");
344+
assertStructMember(PrimitiveValue.newInt64(id + 4), sv, "c_Int64");
345+
346+
assertStructMember(PrimitiveValue.newUint8(id + 5), sv, "c_Uint8");
347+
assertStructMember(PrimitiveValue.newUint16(id + 6), sv, "c_Uint16");
348+
assertStructMember(PrimitiveValue.newUint32(id + 7), sv, "c_Uint32");
349+
assertStructMember(PrimitiveValue.newUint64(id + 8), sv, "c_Uint64");
350+
351+
assertStructMember(PrimitiveValue.newFloat(1.5f * id), sv, "c_Float");
352+
assertStructMember(PrimitiveValue.newDouble(2.5d * id), sv, "c_Double");
353+
354+
assertStructMember(PrimitiveValue.newBytes(new byte[] { (byte)id }), sv, "c_Bytes");
355+
assertStructMember(PrimitiveValue.newText("Text_" + id), sv, "c_Text");
356+
assertStructMember(PrimitiveValue.newJson("{\"json\": " + id + "}"), sv, "c_Json");
357+
assertStructMember(PrimitiveValue.newJsonDocument("{\"jsonDoc\":" + id + "}"), sv, "c_JsonDocument");
358+
assertStructMember(PrimitiveValue.newYson(("{yson=" + id + "}").getBytes()), sv, "c_Yson");
359+
360+
361+
Date sqlDate = new Date(TEST_TS.toEpochMilli());
362+
LocalDateTime dateTime = LocalDateTime.ofInstant(TEST_TS, ZoneOffset.UTC).plusMinutes(id)
363+
.truncatedTo(ChronoUnit.SECONDS);
364+
365+
assertStructMember(PrimitiveValue.newDate(sqlDate.toLocalDate()), sv, "c_Date");
366+
assertStructMember(PrimitiveValue.newDatetime(dateTime), sv, "c_Datetime");
367+
assertStructMember(PrimitiveValue.newTimestamp(truncToMicros(TEST_TS.plusSeconds(id))), sv, "c_Timestamp");
368+
assertStructMember(PrimitiveValue.newInterval(Duration.ofMinutes(id)), sv, "c_Interval");
369+
370+
assertStructMember(null, sv, "c_Decimal");
371+
}
372+
297373
@ParameterizedTest(name = "with {0}")
298374
@EnumSource(SqlQueries.JdbcQuery.class)
299375
public void batchUpsertAllTest(SqlQueries.JdbcQuery query) throws SQLException {
@@ -336,6 +412,28 @@ public void batchUpsertAllTest(SqlQueries.JdbcQuery query) throws SQLException {
336412
}
337413
};
338414

415+
@Test
416+
public void tableRowTest() throws SQLException {
417+
String upsert = TEST_TABLE.upsertAll(SqlQueries.JdbcQuery.BATCHED);
418+
String selectTableRow = TEST_TABLE.withTableName("select TableRow() from #tableName");
419+
420+
try (PreparedStatement statement = jdbc.connection().prepareStatement(upsert)) {
421+
fillRowValues(statement, 1);
422+
statement.addBatch();
423+
fillRowValues(statement, 2);
424+
statement.addBatch();
425+
statement.executeBatch();
426+
}
427+
428+
try (Statement statement = jdbc.connection().createStatement()) {
429+
try (ResultSet rs = statement.executeQuery(selectTableRow)) {
430+
assertTableRow(rs, 1);
431+
assertTableRow(rs, 2);
432+
Assert.assertFalse(rs.next());
433+
}
434+
}
435+
};
436+
339437
@ParameterizedTest(name = "with {0}")
340438
@EnumSource(SqlQueries.JdbcQuery.class)
341439
public void int32Test(SqlQueries.JdbcQuery query) throws SQLException {

0 commit comments

Comments
 (0)