aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/col_interval_test.go
blob: 6972ff86d83db8d2f9d77280c52a44f40d85966a (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
package proto

import (
	"bytes"
	"io"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

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

func TestInterval_Add(t *testing.T) {
	v := time.Date(2100, 10, 1, 23, 42, 12, 34145, time.UTC)
	for _, tc := range []struct {
		Scale  IntervalScale
		Result time.Time
	}{
		{
			Scale:  IntervalSecond,
			Result: v.Add(time.Second * 2),
		},
		{
			Scale:  IntervalMinute,
			Result: v.Add(time.Minute * 2),
		},
		{
			Scale:  IntervalHour,
			Result: v.Add(time.Hour * 2),
		},
		{
			Scale:  IntervalDay,
			Result: v.AddDate(0, 0, 2),
		},
		{
			Scale:  IntervalWeek,
			Result: v.AddDate(0, 0, 7*2),
		},
		{
			Scale:  IntervalMonth,
			Result: v.AddDate(0, 2, 0),
		},
		{
			Scale:  IntervalQuarter,
			Result: v.AddDate(0, 4*2, 0),
		},
		{
			Scale:  IntervalYear,
			Result: v.AddDate(2, 0, 0),
		},
	} {
		interval := Interval{
			Scale: tc.Scale,
			Value: 2,
		}
		result := interval.Add(v)
		require.True(t, result.Equal(tc.Result), "%s: %s != %s", interval.Scale, result, tc.Result)
	}
}

func TestInterval_String(t *testing.T) {
	for _, tc := range []struct {
		Scale IntervalScale
		One   string
		Many  string
	}{
		{
			Scale: IntervalSecond,
			One:   "1 second",
			Many:  "3 seconds",
		},
		{
			Scale: IntervalQuarter,
			One:   "1 quarter",
			Many:  "3 quarters",
		},
	} {
		require.Equal(t, tc.One, (Interval{Value: 1, Scale: tc.Scale}).String())
		require.Equal(t, tc.Many, (Interval{Value: 3, Scale: tc.Scale}).String())
		require.Equal(t, "-"+tc.Many, (Interval{Value: -3, Scale: tc.Scale}).String())
	}
}

func TestColInterval(t *testing.T) {
	t.Parallel()
	const rows = 50
	var data ColInterval
	for i := 0; i < rows; i++ {
		v := Interval{
			Value: 10,
		}
		data.Append(v)
		require.Equal(t, v, data.Row(i))
	}

	var buf Buffer
	data.EncodeColumn(&buf)
	t.Run("Golden", func(t *testing.T) {
		t.Parallel()
		gold.Bytes(t, buf.Buf, "col_interval")
	})
	t.Run("Ok", func(t *testing.T) {
		br := bytes.NewReader(buf.Buf)
		r := NewReader(br)

		var dec ColInterval
		require.NoError(t, dec.DecodeColumn(r, rows))
		require.Equal(t, data, dec)
		require.Equal(t, rows, dec.Rows())
		dec.Reset()
		require.Equal(t, 0, dec.Rows())
		require.Equal(t, ColumnTypeInterval+"Second", dec.Type())
	})
	t.Run("ZeroRows", func(t *testing.T) {
		r := NewReader(bytes.NewReader(nil))

		var dec ColInterval
		require.NoError(t, dec.DecodeColumn(r, 0))
	})
	t.Run("EOF", func(t *testing.T) {
		r := NewReader(bytes.NewReader(nil))

		var dec ColInterval
		require.ErrorIs(t, dec.DecodeColumn(r, rows), io.EOF)
	})
	t.Run("NoShortRead", func(t *testing.T) {
		var dec ColInterval
		requireNoShortRead(t, buf.Buf, colAware(&dec, rows))
	})
	t.Run("ZeroRowsEncode", func(t *testing.T) {
		var v ColInterval
		v.EncodeColumn(nil) // should be no-op
	})
}