Skip to content

Commit c4b7f8b

Browse files
test: add tests for time and time64 with setup and protocol wrappers
1 parent ed3c9f3 commit c4b7f8b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/time_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tests
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
"github.com/ClickHouse/clickhouse-go/v2"
10+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func setupTimeTest(t *testing.T) (context.Context, func(), clickhouse.Conn) {
15+
ctx := context.Background()
16+
conn := clickhouse_tests.GetNativeConnection(t, map[string]any{
17+
"enable_time_timet64_type": 1,
18+
})
19+
if !clickhouse_tests.CheckMinServerServerVersion(conn, 24, 6, 1) {
20+
t.Skip("Time/Time64 not supported on this ClickHouse version")
21+
}
22+
return ctx, func() {}, conn
23+
}
24+
25+
func TestTimeAndTime64(t *testing.T) {
26+
clickhouse_tests.TestProtocols(t, func(t *testing.T, protocol string) {
27+
ctx, cleanup, conn := setupTimeTest(t)
28+
defer cleanup()
29+
tableName := fmt.Sprintf("test_time_types_%d", time.Now().UnixNano())
30+
require.NoError(t, conn.Exec(ctx, fmt.Sprintf(`
31+
CREATE TABLE %s (
32+
t1 Time,
33+
t2 Time64(9),
34+
t3 Array(Time),
35+
t4 Array(Time64(9))
36+
) ENGINE = MergeTree() ORDER BY tuple()`, tableName)))
37+
defer conn.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName))
38+
39+
t1 := time.Date(0, 1, 1, 12, 34, 56, 0, time.UTC)
40+
t2 := time.Date(0, 1, 1, 23, 59, 59, 123456789, time.UTC)
41+
t3 := []time.Time{t1, t2}
42+
t4 := []time.Time{t2, t1}
43+
batch, err := conn.PrepareBatch(ctx, fmt.Sprintf("INSERT INTO %s (t1, t2, t3, t4)", tableName))
44+
require.NoError(t, err)
45+
require.NoError(t, batch.Append(t1, t2, t3, t4))
46+
require.NoError(t, batch.Send())
47+
48+
var (
49+
outT1 time.Time
50+
outT2 time.Time
51+
outT3 []time.Time
52+
outT4 []time.Time
53+
)
54+
row := conn.QueryRow(ctx, fmt.Sprintf("SELECT t1, t2, t3, t4 FROM %s", tableName))
55+
require.NoError(t, row.Scan(&outT1, &outT2, &outT3, &outT4))
56+
require.Equal(t, t1, outT1)
57+
require.Equal(t, t2, outT2)
58+
require.Equal(t, t3, outT3)
59+
require.Equal(t, t4, outT4)
60+
})
61+
}

0 commit comments

Comments
 (0)