|
| 1 | +package com.clickhouse.client.api.data_formats; |
| 2 | + |
| 3 | +import com.clickhouse.client.BaseIntegrationTest; |
| 4 | +import com.clickhouse.client.ClickHouseNode; |
| 5 | +import com.clickhouse.client.ClickHouseProtocol; |
| 6 | +import com.clickhouse.client.ClickHouseServerForTest; |
| 7 | +import com.clickhouse.client.api.Client; |
| 8 | +import com.clickhouse.client.api.enums.Protocol; |
| 9 | +import com.clickhouse.client.api.query.GenericRecord; |
| 10 | +import lombok.Data; |
| 11 | +import org.testng.Assert; |
| 12 | +import org.testng.annotations.Test; |
| 13 | + |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Random; |
| 17 | + |
| 18 | +@Test(groups = {"integration"}) |
| 19 | +public class RowBinaryTest extends BaseIntegrationTest { |
| 20 | + |
| 21 | + protected Client.Builder newClient() { |
| 22 | + ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); |
| 23 | + boolean isSecure = isCloud(); |
| 24 | + return new Client.Builder() |
| 25 | + .addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), isSecure) |
| 26 | + .setUsername("default") |
| 27 | + .setPassword(ClickHouseServerForTest.getPassword()) |
| 28 | + .setDefaultDatabase(ClickHouseServerForTest.getDatabase()); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + @Test(groups = {"integration"}) |
| 33 | + void testDefaultWithFunction() { |
| 34 | + final String table = "test_defaults_with_function"; |
| 35 | + final String createTable = "CREATE TABLE " + table +" ( " + |
| 36 | + " name String," + |
| 37 | + " v Int64 DEFAULT 10, " + |
| 38 | + " fingerPrint UInt64 DEFAULT xxHash64(name)," + |
| 39 | + " comments String" + |
| 40 | + ") ENGINE = MergeTree()" + |
| 41 | + "ORDER BY name;"; |
| 42 | + |
| 43 | + try (Client client = newClient().build()){ |
| 44 | + |
| 45 | + client.execute("DROP TABLE IF EXISTS " + table); |
| 46 | + client.execute(createTable); |
| 47 | + |
| 48 | + client.register(DefaultWithFunctionPojo.class, client.getTableSchema(table)); |
| 49 | + |
| 50 | + DefaultWithFunctionPojo entity = new DefaultWithFunctionPojo(); |
| 51 | + entity.setName("test"); |
| 52 | + entity.setComments("test"); |
| 53 | + List<DefaultWithFunctionPojo> data = Collections.singletonList(entity); |
| 54 | + client.insert(table, data); |
| 55 | + |
| 56 | + List<GenericRecord> records = client.queryAll("SELECT * FROM " + table); |
| 57 | + Assert.assertEquals(records.size(), 1); |
| 58 | + GenericRecord record = records.get(0); |
| 59 | + Assert.assertEquals(record.getString("name"), "test"); |
| 60 | + Assert.assertEquals(record.getLong("v"), 10); |
| 61 | + Assert.assertTrue(record.getLong("fingerPrint") > 0); |
| 62 | + Assert.assertEquals(record.getString("comments"), "test"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Data |
| 67 | + public static class DefaultWithFunctionPojo { |
| 68 | + private String name; |
| 69 | + private Long fingerPrint; |
| 70 | + private Long v; |
| 71 | + private String comments; |
| 72 | + } |
| 73 | +} |
0 commit comments