aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/block_test.go
blob: 8b28c82e1d1b19b1bc43c1aef5327f08f26f4d7e (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package proto

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/ClickHouse/ch-go/internal/gold"
)

func TestBlockInfo_Encode(t *testing.T) {
	i := BlockInfo{
		Overflows: true,
		BucketNum: 1056,
	}
	var b Buffer
	b.Encode(i)
	require.Equal(t, []byte{0x1, 0x1, 0x2, 0x20, 0x4, 0x0, 0x0, 0x0}, b.Buf)
	t.Run("Decode", func(t *testing.T) {
		var v BlockInfo
		requireDecode(t, b.Buf, &v)
		require.Equal(t, i, v)
		requireNoShortRead(t, b.Buf, &v)
	})
}

func TestBlock_EncodeAware(t *testing.T) {
	Gold(t, Block{
		Info: BlockInfo{
			Overflows: true,
			BucketNum: 2014,
		},
		Columns: 15,
		Rows:    10,
	})
}

type resultAware struct {
	*Block
	out Result
}

func (c resultAware) Decode(r *Reader) error {
	return c.DecodeBlock(r, Version, c.out)
}

func resAware(v *Block, out Results) Decoder {
	return resultAware{
		Block: v,
		out:   out,
	}
}

func colStr(data ...string) ColStr {
	var v ColStr
	for _, s := range data {
		v.Append(s)
	}
	return v
}

func TestBlock_EncodeRawBlock(t *testing.T) {
	b := new(Buffer)
	v := Block{Rows: 2, Columns: 2}
	require.NoError(t, v.EncodeRawBlock(b, Version, []InputColumn{
		{Name: "title", Data: colStr("Foo", "Bar")},
		{Name: "data", Data: ColInt64{1, 2}},
	}), "encode")

	gold.Bytes(t, b.Buf, "block_title_data")

	var (
		title ColStr
		data  ColInt64
	)
	require.NoError(t, v.DecodeRawBlock(b.Reader(), Version, Results{
		{Name: "title", Data: &title},
		{Name: "data", Data: &data},
	}), "decode")
}

func TestBlock_EncodeBlock(t *testing.T) {
	v := Block{
		Info: BlockInfo{
			BucketNum: -1,
		},
		Columns: 2,
		Rows:    5,
	}
	var b Buffer
	require.NoError(t, v.EncodeBlock(&b, Version, []InputColumn{
		{Name: "count", Data: ColInt8{1, 2, 3, 4, 5}},
		{Name: "users", Data: ColUInt64{5467267, 175676, 956105, 18347896, 554714}},
	}))
	gold.Bytes(t, b.Buf, "block_int8_uint64")
	t.Run("Ok", func(t *testing.T) {
		var dec Block
		var (
			countRes ColInt8
			usersRes ColUInt64
		)
		requireDecode(t, b.Buf, resAware(&dec, []ResultColumn{
			{Name: "count", Data: &countRes},
			{Name: "users", Data: &usersRes},
		}))
		require.Equal(t, dec, v)
	})
	t.Run("NoShortRead", func(t *testing.T) {
		var dec Block
		var (
			countRes ColInt8
			usersRes ColUInt64
		)
		requireNoShortRead(t, b.Buf, resAware(&dec, []ResultColumn{
			{Name: "count", Data: &countRes},
			{Name: "users", Data: &usersRes},
		}))
	})
	t.Run("BadColumn", func(t *testing.T) {
		var dec Block
		var (
			countRes ColInt8
			usersRes ColUInt64
		)
		for _, res := range []Results{
			{}, // No columns.
			{
				// Bad name.
				{Name: "counts", Data: &countRes},
				{Name: "users", Data: &usersRes},
			},
			{
				// Bad type.
				{Name: "count", Data: &countRes},
				{Name: "users", Data: new(ColStr)},
			},
		} {
			require.Error(t, dec.DecodeBlock(b.Reader(), Version, res))
		}
	})
}