Skip to content

Commit

Permalink
SQL: improve timezone sensitive JDBC tests to account for offset diff…
Browse files Browse the repository at this point in the history
…erences (#51592)

* Changed one test to use fixed offsets
* Changed several tests to compare only the relevant parts of the
conversion failure error message and ignore the actual datetime String
from the error message itself
  • Loading branch information
astefan committed Jan 29, 2020
1 parent e89ec30 commit fe254a2
Showing 1 changed file with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.sql.Types;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -63,6 +64,8 @@
import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.asDate;
import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.asTime;
import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.of;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.startsWith;

public class ResultSetTestCase extends JdbcIntegrationTestCase {

Expand Down Expand Up @@ -271,11 +274,12 @@ public void testGettingInvalidByte() throws Exception {
sqle.getMessage());

sqle = expectThrows(SQLException.class, () -> results.getByte("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Byte]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Byte]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Byte.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Byte]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Byte]"));
});
}

Expand Down Expand Up @@ -394,11 +398,12 @@ public void testGettingInvalidShort() throws Exception {
sqle.getMessage());

sqle = expectThrows(SQLException.class, () -> results.getShort("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Short]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Short]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Short.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Short]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Short]"));
});
}

Expand Down Expand Up @@ -509,11 +514,12 @@ public void testGettingInvalidInteger() throws Exception {
sqle.getMessage());

sqle = expectThrows(SQLException.class, () -> results.getInt("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Integer]",
asDateString(randomDate)), sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Integer]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Integer.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Integer]",
asDateString(randomDate)), sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Integer]"));
});
}

Expand Down Expand Up @@ -611,11 +617,12 @@ public void testGettingInvalidLong() throws Exception {
sqle.getMessage());

sqle = expectThrows(SQLException.class, () -> results.getLong("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Long]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Long]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Long.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Long]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Long]"));
});
}

Expand Down Expand Up @@ -694,11 +701,12 @@ public void testGettingInvalidDouble() throws Exception {
sqle.getMessage());

sqle = expectThrows(SQLException.class, () -> results.getDouble("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Double]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Double]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Double.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Double]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Double]"));
});
}

Expand Down Expand Up @@ -777,11 +785,12 @@ public void testGettingInvalidFloat() throws Exception {
sqle.getMessage());

sqle = expectThrows(SQLException.class, () -> results.getFloat("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Float]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Float]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Float.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Float]", asDateString(randomDate)),
sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Float]"));
});
}

Expand Down Expand Up @@ -838,8 +847,8 @@ public void testGettingBooleanValues() throws Exception {
assertEquals("Expected: <true> but was: <false> for field " + fld, true, results.getObject(fld, Boolean.class));
}
SQLException sqle = expectThrows(SQLException.class, () -> results.getBoolean("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Boolean]",
asDateString(randomDate1)), sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Boolean]"));

results.next();
assertEquals(false, results.getBoolean("test_boolean"));
Expand All @@ -848,12 +857,12 @@ public void testGettingBooleanValues() throws Exception {
assertEquals("Expected: <false> but was: <true> for field " + fld, false, results.getObject(fld, Boolean.class));
}
sqle = expectThrows(SQLException.class, () -> results.getBoolean("test_date"));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Boolean]",
asDateString(randomDate2)), sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Boolean]"));

sqle = expectThrows(SQLException.class, () -> results.getObject("test_date", Boolean.class));
assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [DATETIME] to [Boolean]",
asDateString(randomDate2)), sqle.getMessage());
assertThat(sqle.getMessage(), startsWith("Unable to convert value"));
assertThat(sqle.getMessage(), endsWith("of type [DATETIME] to [Boolean]"));

results.next();
for(String fld : fieldsNames.stream()
Expand All @@ -868,7 +877,6 @@ public void testGettingBooleanValues() throws Exception {
});
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/50035")
public void testGettingDateWithoutCalendar() throws Exception {
createIndex("test");
updateMappingForNumericValuesTests("test");
Expand All @@ -879,10 +887,23 @@ public void testGettingDateWithoutCalendar() throws Exception {
Long randomLongDate = randomNonNegativeLong();
indexSimpleDocumentWithTrueValues(randomLongDate);

doWithQuery(SELECT_ALL_FIELDS, (results) -> {
/*
* Create simple timezones (in the form of Z/GMT+/-x so that timezones offsets differences between Java versions (the one
* server runs on and the one the test runs on) will not skew the tests' results.
* -18/+18 is the maximum range of offsets accepted by ZoneOffset, even if in reality this range is more like [-12, +14].
*/
int hoursOffset = randomIntBetween(0, 18);
int subHours = hoursOffset != 18 ? randomFrom(0, 15, 30, 45) : 0;
int plusMinus = randomBoolean() ? 1 : -1;
int totalSeconds = (hoursOffset * 3600 + subHours * 60) * plusMinus;

ZoneOffset randomFixedZone = ZoneOffset.ofTotalSeconds(totalSeconds);
String zoneString = (hoursOffset == 0 && subHours == 0) ? "Z" : "GMT" + randomFixedZone.getId();

doWithQueryAndTimezone(SELECT_ALL_FIELDS, zoneString, (results) -> {
results.next();

java.sql.Date expectedDate = asDate(randomLongDate, getZoneFromOffset(randomLongDate));
java.sql.Date expectedDate = asDate(randomLongDate, ZoneId.of(zoneString));

assertEquals(expectedDate, results.getDate("test_date"));
assertEquals(expectedDate, results.getDate(9));
Expand Down

0 comments on commit fe254a2

Please sign in to comment.