|
22 | 22 | import java.io.IOException; |
23 | 23 | import java.math.BigDecimal; |
24 | 24 | import java.math.BigInteger; |
| 25 | +import java.time.ZonedDateTime; |
25 | 26 | import java.util.List; |
26 | 27 | import java.util.Map; |
27 | 28 | import java.util.Random; |
@@ -75,6 +76,14 @@ protected void initTable(String tableName, String createTableSQL, CommandSetting |
75 | 76 | } |
76 | 77 |
|
77 | 78 | private static void assertEqualsKinda(Object actual, Object expected) { |
| 79 | + if (actual instanceof ZonedDateTime) { |
| 80 | + actual = ((ZonedDateTime) actual).toInstant().getEpochSecond(); |
| 81 | + } |
| 82 | + |
| 83 | + if (expected instanceof ZonedDateTime) { |
| 84 | + expected = ((ZonedDateTime) expected).toInstant().getEpochSecond(); |
| 85 | + } |
| 86 | + |
78 | 87 | assertEquals(String.valueOf(actual), String.valueOf(expected)); |
79 | 88 | } |
80 | 89 |
|
@@ -102,12 +111,37 @@ private void writeTest(String tableName, String tableCreate, Field[][] rows) thr |
102 | 111 | Map<String, Object> row = record.getValues(); |
103 | 112 | //Validate data |
104 | 113 | for (Field field : rows[id - 1]) { |
105 | | - assertEqualsKinda(row.get(field.name), field.getValue()); |
| 114 | + assertEqualsKinda(row.get(field.name), field.comparisonValue); |
106 | 115 | } |
107 | 116 | id++; |
108 | 117 | } |
109 | 118 | } |
110 | 119 |
|
| 120 | + private static class Field { |
| 121 | + String name; |
| 122 | + Object value; |
| 123 | + Object comparisonValue; |
| 124 | + |
| 125 | + Field(String name) { |
| 126 | + this.name = name; |
| 127 | + this.value = null; |
| 128 | + this.comparisonValue = null; |
| 129 | + } |
| 130 | + |
| 131 | + Field(String name, Object value) { |
| 132 | + this.name = name; |
| 133 | + this.value = value; |
| 134 | + this.comparisonValue = value; |
| 135 | + } |
| 136 | + |
| 137 | + public Field set(Object comparisonValue) {//For comparison purposes |
| 138 | + this.comparisonValue = comparisonValue; |
| 139 | + return this; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + |
111 | 145 |
|
112 | 146 |
|
113 | 147 | @Test (groups = { "integration" }) |
@@ -223,33 +257,27 @@ public void writeStringsTest() throws Exception { |
223 | 257 | } |
224 | 258 |
|
225 | 259 |
|
226 | | - private static class Field { |
227 | | - String name; |
228 | | - Object value; |
229 | | - Object defaultValue; |
230 | | - |
231 | | - Field(String name) { |
232 | | - this.name = name; |
233 | | - this.value = null; |
234 | | - this.defaultValue = null; |
235 | | - } |
236 | | - |
237 | | - Field(String name, Object value) { |
238 | | - this.name = name; |
239 | | - this.value = value; |
240 | | - } |
241 | | - |
242 | | - public Field set(Object defaultValue) {//For default value for comparison purposes |
243 | | - this.defaultValue = defaultValue; |
244 | | - return this; |
245 | | - } |
| 260 | + @Test (groups = { "integration" }) |
| 261 | + public void writeDatetimeTests() throws Exception { |
| 262 | + String tableName = "rowBinaryFormatWriterTest_writeNumbersTest_" + UUID.randomUUID().toString().replace('-', '_'); |
| 263 | + String tableCreate = "CREATE TABLE \"" + tableName + "\" " + |
| 264 | + " (id Int32, " + |
| 265 | + " datetime DateTime, datetime_nullable Nullable(DateTime), datetime_default DateTime DEFAULT '2020-01-01 00:00:00', " + |
| 266 | + " datetime64 DateTime64, datetime64_nullable Nullable(DateTime64), datetime64_default DateTime64 DEFAULT '2025-01-01 00:00:00', " + |
| 267 | + " date Date, date_nullable Nullable(Date), date_default Date DEFAULT '2020-01-01', " + |
| 268 | + " date32 Date32, date32_nullable Nullable(Date32), date32_default Date32 DEFAULT '2025-01-01', " + |
| 269 | + " ) Engine = MergeTree ORDER BY id"; |
246 | 270 |
|
247 | | - public Object getValue() { |
248 | | - if (value == null && defaultValue != null) { |
249 | | - return defaultValue; |
250 | | - } |
| 271 | + // Insert random (valid) values |
| 272 | + Field[][] rows = new Field[][] {{ |
| 273 | + new Field("id", 1), //Row ID |
| 274 | + new Field("datetime", ZonedDateTime.now()), new Field("datetime_nullable"), new Field("datetime_default").set(ZonedDateTime.parse("2020-01-01T00:00:00+00:00[UTC]")), //DateTime |
| 275 | + new Field("datetime64", ZonedDateTime.now()), new Field("datetime64_nullable"), new Field("datetime64_default").set(ZonedDateTime.parse("2025-01-01T00:00:00+00:00[UTC]")), //DateTime64 |
| 276 | + new Field("date", ZonedDateTime.parse("2021-01-01T00:00:00+00:00[UTC]")), new Field("date_nullable"), new Field("date_default").set(ZonedDateTime.parse("2020-01-01T00:00:00+00:00[UTC]").toEpochSecond()), //Date |
| 277 | + new Field("date32", ZonedDateTime.parse("2021-01-01T00:00:00+00:00[UTC]")), new Field("date32_nullable"), new Field("date32_default").set(ZonedDateTime.parse("2025-01-01T00:00:00+00:00[UTC]").toEpochSecond()) //Date |
| 278 | + } |
| 279 | + }; |
251 | 280 |
|
252 | | - return value; |
253 | | - } |
| 281 | + writeTest(tableName, tableCreate, rows); |
254 | 282 | } |
255 | 283 | } |
0 commit comments