Skip to content

Commit e493560

Browse files
committed
Adds Tests for dateTimeOverflow check.
1 parent a561831 commit e493560

File tree

6 files changed

+112
-28
lines changed

6 files changed

+112
-28
lines changed

lib/column/date_helpers.go

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ import (
2525
const secInDay = 24 * 60 * 60
2626

2727
func dateOverflow(min, max, v time.Time, format string) error {
28+
if v.Before(min) || v.After(max) {
29+
return &DateOverflowError{
30+
Min: min,
31+
Max: max,
32+
Value: v,
33+
Format: format,
34+
}
35+
}
36+
return nil
37+
}
38+
39+
func dateTimeOverflow(min, max, v time.Time, format string) error {
2840
if !v.IsZero() && (v.Before(min) || v.After(max)) {
2941
return &DateOverflowError{
3042
Min: min,

lib/column/date_helpers_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package column
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestDateTimeOverflow(t *testing.T) {
9+
t.Parallel()
10+
zeroTime := time.Time{}
11+
tests := []struct {
12+
v time.Time
13+
name string
14+
err bool
15+
}{
16+
{
17+
name: "valid date",
18+
v: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
19+
err: false,
20+
},
21+
{
22+
name: "before min date",
23+
v: minDate.Add(-time.Second),
24+
err: true,
25+
},
26+
{
27+
name: "after max date",
28+
v: maxDate.Add(time.Second),
29+
err: true,
30+
},
31+
{
32+
name: "zero value date",
33+
v: zeroTime,
34+
err: false,
35+
},
36+
{
37+
name: "non-zero value equal to zero date",
38+
v: time.UnixMilli(zeroTime.UnixMilli()),
39+
err: false,
40+
},
41+
}
42+
43+
for _, test := range tests {
44+
test := test
45+
t.Run(test.name, func(t *testing.T) {
46+
t.Parallel()
47+
err := dateTimeOverflow(minDateTime, maxDateTime, test.v, defaultDateFormatNoZone)
48+
if (err != nil) != test.err {
49+
t.Errorf("expected error: %v, got: %v", test.err, err)
50+
}
51+
})
52+
}
53+
}

lib/column/datetime.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"database/sql"
2222
"database/sql/driver"
2323
"fmt"
24-
"github.com/ClickHouse/ch-go/proto"
2524
"reflect"
2625
"strings"
2726
"time"
2827

28+
"github.com/ClickHouse/ch-go/proto"
2929
"github.com/ClickHouse/clickhouse-go/v2/lib/timezone"
3030
)
3131

@@ -131,7 +131,7 @@ func (col *DateTime) Append(v any) (nulls []uint8, err error) {
131131
case []time.Time:
132132
nulls = make([]uint8, len(v))
133133
for i := range v {
134-
if err := dateOverflow(minDateTime, maxDateTime, v[i], defaultDateTimeFormatNoZone); err != nil {
134+
if err := dateTimeOverflow(minDateTime, maxDateTime, v[i], defaultDateTimeFormatNoZone); err != nil {
135135
return nil, err
136136
}
137137
col.col.Append(v[i])
@@ -142,7 +142,7 @@ func (col *DateTime) Append(v any) (nulls []uint8, err error) {
142142
for i := range v {
143143
switch {
144144
case v[i] != nil:
145-
if err := dateOverflow(minDateTime, maxDateTime, *v[i], defaultDateTimeFormatNoZone); err != nil {
145+
if err := dateTimeOverflow(minDateTime, maxDateTime, *v[i], defaultDateTimeFormatNoZone); err != nil {
146146
return nil, err
147147
}
148148
col.col.Append(*v[i])
@@ -223,14 +223,14 @@ func (col *DateTime) AppendRow(v any) error {
223223
col.col.Append(time.Time{})
224224
}
225225
case time.Time:
226-
if err := dateOverflow(minDateTime, maxDateTime, v, defaultDateTimeFormatNoZone); err != nil {
226+
if err := dateTimeOverflow(minDateTime, maxDateTime, v, defaultDateTimeFormatNoZone); err != nil {
227227
return err
228228
}
229229
col.col.Append(v)
230230
case *time.Time:
231231
switch {
232232
case v != nil:
233-
if err := dateOverflow(minDateTime, maxDateTime, *v, defaultDateTimeFormatNoZone); err != nil {
233+
if err := dateTimeOverflow(minDateTime, maxDateTime, *v, defaultDateTimeFormatNoZone); err != nil {
234234
return err
235235
}
236236
col.col.Append(*v)
@@ -311,7 +311,7 @@ func (col *DateTime) row(i int) time.Time {
311311
func (col *DateTime) parseDateTime(value string) (tv time.Time, err error) {
312312
defer func() {
313313
if err == nil {
314-
err = dateOverflow(minDateTime, maxDateTime, tv, defaultDateFormatNoZone)
314+
err = dateTimeOverflow(minDateTime, maxDateTime, tv, defaultDateFormatNoZone)
315315
}
316316
}()
317317

lib/column/datetime64.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
"database/sql"
2222
"database/sql/driver"
2323
"fmt"
24-
"github.com/ClickHouse/ch-go/proto"
2524
"math"
2625
"reflect"
2726
"strconv"
2827
"strings"
2928
"time"
3029

30+
"github.com/ClickHouse/ch-go/proto"
3131
"github.com/ClickHouse/clickhouse-go/v2/lib/timezone"
3232
)
3333

@@ -152,7 +152,7 @@ func (col *DateTime64) Append(v any) (nulls []uint8, err error) {
152152
case []time.Time:
153153
nulls = make([]uint8, len(v))
154154
for i := range v {
155-
if err := dateOverflow(minDateTime64, maxDateTime64, v[i], "2006-01-02 15:04:05"); err != nil {
155+
if err := dateTimeOverflow(minDateTime64, maxDateTime64, v[i], "2006-01-02 15:04:05"); err != nil {
156156
return nil, err
157157
}
158158
col.col.Append(v[i])
@@ -162,7 +162,7 @@ func (col *DateTime64) Append(v any) (nulls []uint8, err error) {
162162
for i := range v {
163163
switch {
164164
case v[i] != nil:
165-
if err := dateOverflow(minDateTime64, maxDateTime64, *v[i], "2006-01-02 15:04:05"); err != nil {
165+
if err := dateTimeOverflow(minDateTime64, maxDateTime64, *v[i], "2006-01-02 15:04:05"); err != nil {
166166
return nil, err
167167
}
168168
col.col.Append(*v[i])
@@ -227,14 +227,14 @@ func (col *DateTime64) AppendRow(v any) error {
227227
col.col.Append(time.Time{})
228228
}
229229
case time.Time:
230-
if err := dateOverflow(minDateTime64, maxDateTime64, v, "2006-01-02 15:04:05"); err != nil {
230+
if err := dateTimeOverflow(minDateTime64, maxDateTime64, v, "2006-01-02 15:04:05"); err != nil {
231231
return err
232232
}
233233
col.col.Append(v)
234234
case *time.Time:
235235
switch {
236236
case v != nil:
237-
if err := dateOverflow(minDateTime64, maxDateTime64, *v, "2006-01-02 15:04:05"); err != nil {
237+
if err := dateTimeOverflow(minDateTime64, maxDateTime64, *v, "2006-01-02 15:04:05"); err != nil {
238238
return err
239239
}
240240
col.col.Append(*v)

tests/std/datetime64_test.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package std
2020
import (
2121
"database/sql"
2222
"fmt"
23-
"github.com/ClickHouse/clickhouse-go/v2"
24-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
25-
"github.com/stretchr/testify/require"
2623
"strconv"
2724
"testing"
2825
"time"
2926

27+
"github.com/ClickHouse/clickhouse-go/v2"
28+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
3029
"github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/require"
3131
)
3232

3333
func TestStdDateTime64(t *testing.T) {
@@ -53,6 +53,7 @@ func TestStdDateTime64(t *testing.T) {
5353
, Col7 DateTime64(0, 'Europe/London')
5454
, Col8 Nullable(DateTime64(3, 'Europe/Moscow'))
5555
, Col9 DateTime64(9)
56+
, Col10 DateTime64(9)
5657
) Engine MergeTree() ORDER BY tuple()
5758
`
5859
defer func() {
@@ -81,21 +82,23 @@ func TestStdDateTime64(t *testing.T) {
8182
sql.NullTime{Time: datetime3, Valid: true},
8283
sql.NullTime{Time: time.Time{}, Valid: false},
8384
expectedMinDateTime,
85+
time.Time{},
8486
)
8587
require.NoError(t, err)
8688
require.NoError(t, scope.Commit())
8789
var (
88-
col1 time.Time
89-
col2 time.Time
90-
col3 time.Time
91-
col4 *time.Time
92-
col5 []time.Time
93-
col6 []*time.Time
94-
col7 sql.NullTime
95-
col8 sql.NullTime
96-
col9 time.Time
90+
col1 time.Time
91+
col2 time.Time
92+
col3 time.Time
93+
col4 *time.Time
94+
col5 []time.Time
95+
col6 []*time.Time
96+
col7 sql.NullTime
97+
col8 sql.NullTime
98+
col9 time.Time
99+
col10 time.Time
97100
)
98-
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime64").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8, &col9))
101+
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime64").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8, &col9, &col10))
99102
assert.Equal(t, datetime1.In(time.UTC), col1)
100103
assert.Equal(t, datetime2.UnixNano(), col2.UnixNano())
101104
assert.Equal(t, datetime3.UnixNano(), col3.UnixNano())
@@ -113,6 +116,13 @@ func TestStdDateTime64(t *testing.T) {
113116
require.Equal(t, sql.NullTime{Time: datetime3.In(col7.Time.Location()), Valid: true}, col7)
114117
require.Equal(t, sql.NullTime{Time: time.Time{}, Valid: false}, col8)
115118
require.Equal(t, time.Date(1900, 01, 01, 0, 0, 0, 0, time.UTC), col9)
119+
120+
// TODO replace after ch-go is update
121+
// minDateTime64, err := time.Parse("2006-01-02 15:04:05", "1900-01-01 00:00:00")
122+
// require.NoError(t, err)
123+
// require.Equal(t, minDateTime64}, col10)
124+
require.Equal(t, time.Time{}, col10)
125+
116126
})
117127
}
118128
}

tests/std/datetime_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package std
2020
import (
2121
"database/sql"
2222
"fmt"
23-
"github.com/ClickHouse/clickhouse-go/v2"
24-
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
25-
"github.com/stretchr/testify/require"
2623
"strconv"
2724
"testing"
2825
"time"
2926

27+
"github.com/ClickHouse/clickhouse-go/v2"
28+
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
3029
"github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/require"
3131
)
3232

3333
func TestStdDateTime(t *testing.T) {
@@ -48,6 +48,7 @@ func TestStdDateTime(t *testing.T) {
4848
, Col6 Array(Nullable(DateTime('Europe/Moscow')))
4949
, Col7 DateTime
5050
, Col8 Nullable(DateTime)
51+
,Col9 DateTime
5152
) Engine MergeTree() ORDER BY tuple()`
5253
defer func() {
5354
conn.Exec("DROP TABLE test_datetime")
@@ -74,6 +75,7 @@ func TestStdDateTime(t *testing.T) {
7475
Time: time.Time{},
7576
Valid: false,
7677
},
78+
time.Time{},
7779
)
7880
require.NoError(t, err)
7981
require.NoError(t, scope.Commit())
@@ -86,8 +88,9 @@ func TestStdDateTime(t *testing.T) {
8688
col6 []*time.Time
8789
col7 sql.NullTime
8890
col8 sql.NullTime
91+
col9 time.Time
8992
)
90-
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8))
93+
require.NoError(t, conn.QueryRow("SELECT * FROM test_datetime").Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8, &col9))
9194
assert.Equal(t, datetime.In(time.UTC), col1)
9295
assert.Equal(t, datetime.Unix(), col2.Unix())
9396
assert.Equal(t, datetime.Unix(), col3.Unix())
@@ -121,6 +124,12 @@ func TestStdDateTime(t *testing.T) {
121124
Valid: false,
122125
}, col8)
123126

127+
// Replace after ch-go is update
128+
// minDateTime, err = time.Parse("2006-01-02 15:04:05", "1970-01-01 00:00:00")
129+
// require.NoError(t, err)
130+
// require.Equal(t, minDateTime}, col9)
131+
require.Equal(t, time.Time{}, col9)
132+
124133
})
125134
}
126135
}

0 commit comments

Comments
 (0)