aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/column_test.go
blob: 639d730926617a26b56a5cb8c46691f96e2b7e62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package proto

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func requireEqual[T any](t *testing.T, a, b ColumnOf[T]) {
	t.Helper()
	require.Equal(t, a.Rows(), b.Rows(), "rows count should match")
	for i := 0; i < a.Rows(); i++ {
		require.Equalf(t, a.Row(i), b.Row(i), "[%d]", i)
	}
}

func TestColumnType_Elem(t *testing.T) {
	t.Run("Array", func(t *testing.T) {
		v := ColumnTypeInt16.Array()
		assert.Equal(t, ColumnType("Array(Int16)"), v)
		assert.True(t, v.IsArray())
		assert.Equal(t, ColumnTypeInt16, v.Elem())
	})
	t.Run("Simple", func(t *testing.T) {
		assert.Equal(t, ColumnTypeNone, ColumnTypeFloat32.Elem())
		assert.False(t, ColumnTypeInt32.IsArray())
	})
	t.Run("Conflict", func(t *testing.T) {
		t.Run("Compatible", func(t *testing.T) {
			for _, tt := range []struct {
				A, B ColumnType
			}{
				{}, // blank
				{A: ColumnTypeInt32, B: ColumnTypeInt32},
				{A: ColumnTypeDateTime, B: ColumnTypeDateTime},
				{A: ColumnTypeArray.Sub(ColumnTypeInt32), B: ColumnTypeArray.Sub(ColumnTypeInt32)},
				{A: ColumnTypeDateTime.With("Europe/Moscow"), B: ColumnTypeDateTime.With("UTC")},
				{A: ColumnTypeDateTime.With("Europe/Moscow"), B: ColumnTypeDateTime},
				{A: "Map(String,String)", B: "Map(String, String)"},
				{A: "Enum8('increment' = 1, 'gauge' = 2)", B: "Int8"},
				{A: "Int8", B: "Enum8('increment' = 1, 'gauge' = 2)"},
			} {
				assert.False(t, tt.A.Conflicts(tt.B),
					"%s ~ %s", tt.A, tt.B,
				)
			}
		})
		t.Run("Incompatible", func(t *testing.T) {
			for _, tt := range []struct {
				A, B ColumnType
			}{
				{A: ColumnTypeInt64}, // blank
				{A: ColumnTypeInt32, B: ColumnTypeInt64},
				{A: ColumnTypeDateTime, B: ColumnTypeInt32},
				{A: ColumnTypeArray.Sub(ColumnTypeInt32), B: ColumnTypeArray.Sub(ColumnTypeInt64)},
				{A: "Map(String,String)", B: "Map(String,Int32)"},
				{A: "Enum16('increment' = 1, 'gauge' = 2)", B: "Int8"},
			} {
				assert.True(t, tt.A.Conflicts(tt.B),
					"%s !~ %s", tt.A, tt.B,
				)
			}
		})
	})
}