Skip to content

Commit c9dbf05

Browse files
committed
Modify tests for ISO 8601 timestamp input
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent a4a8d6e commit c9dbf05

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

core/src/test/java/org/opensearch/sql/data/model/DateTimeValueTest.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.time.LocalTime;
1717
import java.time.ZoneOffset;
1818
import java.time.ZonedDateTime;
19+
import java.time.format.DateTimeFormatter;
1920
import org.junit.jupiter.api.Test;
2021
import org.opensearch.sql.exception.ExpressionEvaluationException;
2122
import org.opensearch.sql.expression.function.FunctionProperties;
@@ -112,15 +113,21 @@ public void timeInUnsupportedFormat() {
112113
}
113114

114115
@Test
115-
public void timestampInUnsupportedFormat() {
116-
Throwable exception =
117-
assertThrows(
118-
ExpressionEvaluationException.class,
119-
() -> new ExprTimestampValue("2020-07-07T01:01:01Z"));
116+
public void timestampInISO8601Format() {
117+
ExprTimestampValue timestampValue = new ExprTimestampValue("2020-07-07T01:01:01Z");
120118
assertEquals(
121-
"timestamp:2020-07-07T01:01:01Z in unsupported format, "
122-
+ "please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
123-
exception.getMessage());
119+
LocalDateTime.parse("2020-07-07T01:01:01Z", DateTimeFormatter.ISO_DATE_TIME)
120+
.toInstant(ZoneOffset.UTC),
121+
timestampValue.timestampValue());
122+
}
123+
124+
@Test
125+
public void timestampInISO8601FormatWithTimeZone() {
126+
ExprTimestampValue timestampValue = new ExprTimestampValue("2020-07-07T01:01:01-01:00");
127+
assertEquals(
128+
LocalDateTime.parse("2020-07-07T02:01:01Z", DateTimeFormatter.ISO_DATE_TIME)
129+
.toInstant(ZoneOffset.UTC),
130+
timestampValue.timestampValue());
124131
}
125132

126133
@Test
@@ -134,13 +141,10 @@ public void stringTimestampValue() {
134141
assertEquals(LocalTime.parse("19:44:00"), stringValue.timeValue());
135142
assertEquals("\"2020-08-17 19:44:00\"", stringValue.toString());
136143

137-
Throwable exception =
138-
assertThrows(
139-
ExpressionEvaluationException.class,
140-
() -> new ExprStringValue("2020-07-07T01:01:01Z").timestampValue());
144+
ExprValue stringValueWithIsoTimestamp = new ExprStringValue("2020-07-07T01:01:01Z");
141145
assertEquals(
142-
"date:2020-07-07T01:01:01Z in unsupported format, " + "please use 'yyyy-MM-dd'",
143-
exception.getMessage());
146+
LocalDateTime.parse("2020-07-07T01:01:01Z", DateTimeFormatter.ISO_DATE_TIME).toInstant(ZoneOffset.UTC),
147+
stringValueWithIsoTimestamp.timestampValue());
144148
}
145149

146150
@Test
@@ -221,7 +225,7 @@ public void timestampOverMaxNanoPrecision() {
221225
() -> new ExprTimestampValue("2020-07-07 01:01:01.1234567890"));
222226
assertEquals(
223227
"timestamp:2020-07-07 01:01:01.1234567890 in unsupported format, please use "
224-
+ "'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
228+
+ "'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]' or ISO 8601 format",
225229
exception.getMessage());
226230
}
227231

core/src/test/java/org/opensearch/sql/expression/datetime/TimestampTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public void timestamp_one_arg_string_invalid_format(String value, String testNam
6464
() -> DSL.timestamp(functionProperties, DSL.literal(value)).valueOf());
6565
assertEquals(
6666
String.format(
67-
"timestamp:%s in unsupported format, please " + "use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
67+
"timestamp:%s in unsupported format, please "
68+
+ "use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]' or ISO 8601 format",
6869
value),
6970
exception.getMessage());
7071
}

0 commit comments

Comments
 (0)