Skip to content

Commit

Permalink
Added test case to retrieve server timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
subkanthi committed Nov 17, 2023
1 parent 534c06c commit 9f47a23
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public void testCreateTable() throws Exception {
BaseDbWriter writer = new BaseDbWriter(clickHouseContainer.getHost(), clickHouseContainer.getFirstMappedPort(),
"employees", clickHouseContainer.getUsername(), clickHouseContainer.getPassword(), null);

ResultSet clickHouseTimeZone = writer.executeQueryWithResultSet("select timezone()");
while(clickHouseTimeZone.next()) {
clickHouseTimeZone.getString(1).equalsIgnoreCase("America/Chicago");
}
Map<String, String> decimalTable = writer.getColumnsDataTypesForTable("numeric_types_DECIMAL_65_30");
Map<String, String> dateTimeTable = writer.getColumnsDataTypesForTable("temporal_types_DATETIME6");
Map<String, String> timestampTable = writer.getColumnsDataTypesForTable("temporal_types_TIMESTAMP6");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.ZoneId;
import java.util.TimeZone;

public class DBMetadata {

Expand Down Expand Up @@ -257,4 +259,20 @@ public boolean checkIfNewReplacingMergeTree(String currentClickHouseVersion) thr

return result;
}


/**
* Function to get the ClickHouse server timezone(Defaults to UTC)
*/
public ZoneId getServerTimeZone(ClickHouseConnection conn) {
ZoneId result = ZoneId.of("UTC");
if(conn != null) {
TimeZone serverTimeZone = conn.getServerTimeZone();
if(serverTimeZone != null) {
result = serverTimeZone.toZoneId();
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import org.testcontainers.junit.jupiter.Testcontainers;
import com.altinity.clickhouse.sink.connector.ClickHouseSinkConnectorConfig;
import org.apache.commons.lang3.tuple.MutablePair;
import org.testcontainers.utility.MountableFile;

import java.sql.SQLException;
import java.time.ZoneId;
import java.util.HashMap;

@Testcontainers
Expand All @@ -19,7 +22,8 @@ public class DBMetadataTest {

@Container
private ClickHouseContainer clickHouseContainer = new ClickHouseContainer("clickhouse/clickhouse-server:latest")
.withInitScript("./init_clickhouse.sql");
.withInitScript("./init_clickhouse.sql").withCopyFileToContainer(MountableFile.forClasspathResource("config.xml"), "/etc/clickhouse-server/config.d/config.xml");

@Test
public void testGetSignColumnForCollapsingMergeTree() {

Expand Down Expand Up @@ -112,4 +116,20 @@ public void testIsRMTVersionSupported(String clickhouseVersion, boolean result)
Assert.assertTrue(new DBMetadata().checkIfNewReplacingMergeTree(clickhouseVersion) == result);
}

@Test
public void getTestGetServerTimeZone() {
String dbHostName = clickHouseContainer.getHost();
Integer port = clickHouseContainer.getFirstMappedPort();
String database = "default";
String userName = clickHouseContainer.getUsername();
String password = clickHouseContainer.getPassword();
String tableName = "employees";

DbWriter writer = new DbWriter(dbHostName, port, database, tableName, userName, password,
new ClickHouseSinkConnectorConfig(new HashMap<>()), null);
ZoneId serverTimeZone = new DBMetadata().getServerTimeZone(writer.getConnection());

Assert.assertTrue(serverTimeZone.toString().equalsIgnoreCase("America/Chicago"));

}
}
11 changes: 11 additions & 0 deletions sink-connector/src/test/resources/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<clickhouse replace="true">
<user_directories>
<users_xml>
<path>users.xml</path>
</users_xml>
<local_directory>
<path>/var/lib/clickhouse/access/</path>
</local_directory>
</user_directories>
<timezone>America/Chicago</timezone>
</clickhouse>

0 comments on commit 9f47a23

Please sign in to comment.