Skip to content

Commit afadcd2

Browse files
committed
Fix test pattern inconsistencies
- Remove @nullable from NullableJavaTimePOJO constructor params (matching existing NullablePOJO pattern) - Use getLogicalTypeValue() instead of getValue() in nullable toRow test (matching testJavaTimeToRow pattern) - Add testJavaTimeToRow/testJavaTimeFromRow to JavaBeanSchemaTest (matching existing testToRow/testFromRow coverage)
1 parent 81d6b17 commit afadcd2

3 files changed

Lines changed: 53 additions & 10 deletions

File tree

sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/JavaBeanSchemaTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,49 @@ public void testJavaTimeSchema() throws NoSuchSchemaException {
203203
assertTrue(schema.assignableToIgnoreNullable(icebergStyleSchema));
204204
}
205205

206+
@Test
207+
public void testJavaTimeToRow() throws NoSuchSchemaException {
208+
SchemaRegistry registry = SchemaRegistry.createDefault();
209+
JavaTimeBean bean = new JavaTimeBean();
210+
bean.setLocalDate(LocalDate.of(2024, 1, 15));
211+
bean.setLocalTime(LocalTime.of(10, 30, 45));
212+
bean.setLocalDateTime(LocalDateTime.of(2024, 1, 15, 10, 30, 45));
213+
bean.setInstant(java.time.Instant.ofEpochSecond(1_705_315_845L, 123_456_789L));
214+
bean.setUuid(UUID.fromString("11111111-2222-3333-4444-555555555555"));
215+
216+
Row row = registry.getToRowFunction(JavaTimeBean.class).apply(bean);
217+
218+
assertEquals(5, row.getFieldCount());
219+
assertEquals(bean.getLocalDate(), row.getLogicalTypeValue("localDate", LocalDate.class));
220+
assertEquals(bean.getLocalTime(), row.getLogicalTypeValue("localTime", LocalTime.class));
221+
assertEquals(
222+
bean.getLocalDateTime(), row.getLogicalTypeValue("localDateTime", LocalDateTime.class));
223+
assertEquals(bean.getInstant(), row.getLogicalTypeValue("instant", java.time.Instant.class));
224+
assertEquals(bean.getUuid(), row.getLogicalTypeValue("uuid", UUID.class));
225+
}
226+
227+
@Test
228+
public void testJavaTimeFromRow() throws NoSuchSchemaException {
229+
SchemaRegistry registry = SchemaRegistry.createDefault();
230+
LocalDate localDate = LocalDate.of(2024, 1, 15);
231+
LocalTime localTime = LocalTime.of(10, 30, 45);
232+
LocalDateTime localDateTime = LocalDateTime.of(2024, 1, 15, 10, 30, 45);
233+
java.time.Instant instant = java.time.Instant.ofEpochSecond(1_705_315_845L, 123_456_789L);
234+
UUID uuid = UUID.fromString("11111111-2222-3333-4444-555555555555");
235+
Row row =
236+
Row.withSchema(JAVA_TIME_BEAN_SCHEMA)
237+
.addValues(localDate, localTime, localDateTime, instant, uuid)
238+
.build();
239+
240+
JavaTimeBean bean = registry.getFromRowFunction(JavaTimeBean.class).apply(row);
241+
242+
assertEquals(localDate, bean.getLocalDate());
243+
assertEquals(localTime, bean.getLocalTime());
244+
assertEquals(localDateTime, bean.getLocalDateTime());
245+
assertEquals(instant, bean.getInstant());
246+
assertEquals(uuid, bean.getUuid());
247+
}
248+
206249
@Test
207250
public void testJavaTimeRoundTrip() throws NoSuchSchemaException {
208251
SchemaRegistry registry = SchemaRegistry.createDefault();

sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/JavaFieldSchemaTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ public void testNullableJavaTimeToRow() throws NoSuchSchemaException {
330330
Row row = registry.getToRowFunction(NullableJavaTimePOJO.class).apply(pojo);
331331

332332
assertEquals(5, row.getFieldCount());
333-
assertNull(row.getValue("localDate"));
334-
assertNull(row.getValue("localTime"));
335-
assertNull(row.getValue("localDateTime"));
336-
assertNull(row.getValue("instant"));
337-
assertNull(row.getValue("uuid"));
333+
assertNull(row.getLogicalTypeValue("localDate", LocalDate.class));
334+
assertNull(row.getLogicalTypeValue("localTime", LocalTime.class));
335+
assertNull(row.getLogicalTypeValue("localDateTime", LocalDateTime.class));
336+
assertNull(row.getLogicalTypeValue("instant", java.time.Instant.class));
337+
assertNull(row.getLogicalTypeValue("uuid", UUID.class));
338338
}
339339

340340
@Test

sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/utils/TestPOJOs.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,11 @@ public static class NullableJavaTimePOJO {
13491349
public NullableJavaTimePOJO() {}
13501350

13511351
public NullableJavaTimePOJO(
1352-
@Nullable LocalDate localDate,
1353-
@Nullable LocalTime localTime,
1354-
@Nullable LocalDateTime localDateTime,
1355-
java.time.@Nullable Instant instant,
1356-
@Nullable UUID uuid) {
1352+
LocalDate localDate,
1353+
LocalTime localTime,
1354+
LocalDateTime localDateTime,
1355+
java.time.Instant instant,
1356+
UUID uuid) {
13571357
this.localDate = localDate;
13581358
this.localTime = localTime;
13591359
this.localDateTime = localDateTime;

0 commit comments

Comments
 (0)