forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
149 lines (136 loc) · 3.58 KB
/
util_test.go
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
142
143
144
145
146
147
148
149
// Copyright (c) 2017-2019 Snowflake Computing Inc. All right reserved.
package gosnowflake
import (
"context"
"database/sql/driver"
"testing"
"time"
)
type tcIntMinMax struct {
v1 int
v2 int
out int
}
func TestGetRequestIDFromContext(t *testing.T) {
expectedRequestID := "snowflake-request-id"
ctx := WithRequestID(context.Background(), expectedRequestID)
requestID := getOrGenerateRequestIDFromContext(ctx)
if requestID != expectedRequestID {
t.Errorf("unexpected request id")
}
}
func TestGenerateRequestID(t *testing.T) {
firstRequestID := getOrGenerateRequestIDFromContext(context.Background())
otherRequestID := getOrGenerateRequestIDFromContext(context.Background())
if firstRequestID == otherRequestID {
t.Errorf("request id should not be the same")
}
}
func TestIntMin(t *testing.T) {
testcases := []tcIntMinMax{
{1, 3, 1},
{5, 100, 5},
{321, 3, 3},
{123, 123, 123},
}
for _, test := range testcases {
a := intMin(test.v1, test.v2)
if test.out != a {
t.Errorf("failed int min. v1: %v, v2: %v, expected: %v, got: %v", test.v1, test.v2, test.out, a)
}
}
}
func TestIntMax(t *testing.T) {
testcases := []tcIntMinMax{
{1, 3, 3},
{5, 100, 100},
{321, 3, 321},
{123, 123, 123},
}
for _, test := range testcases {
a := intMax(test.v1, test.v2)
if test.out != a {
t.Errorf("failed int max. v1: %v, v2: %v, expected: %v, got: %v", test.v1, test.v2, test.out, a)
}
}
}
type tcDurationMinMax struct {
v1 time.Duration
v2 time.Duration
out time.Duration
}
func TestDurationMin(t *testing.T) {
testcases := []tcDurationMinMax{
{1 * time.Second, 3 * time.Second, 1 * time.Second},
{5 * time.Second, 100 * time.Second, 5 * time.Second},
{321 * time.Second, 3 * time.Second, 3 * time.Second},
{123 * time.Second, 123 * time.Second, 123 * time.Second},
}
for _, test := range testcases {
a := durationMin(test.v1, test.v2)
if test.out != a {
t.Errorf("failed duratoin max. v1: %v, v2: %v, expected: %v, got: %v", test.v1, test.v2, test.out, a)
}
}
}
func TestDurationMax(t *testing.T) {
testcases := []tcDurationMinMax{
{1 * time.Second, 3 * time.Second, 3 * time.Second},
{5 * time.Second, 100 * time.Second, 100 * time.Second},
{321 * time.Second, 3 * time.Second, 321 * time.Second},
{123 * time.Second, 123 * time.Second, 123 * time.Second},
}
for _, test := range testcases {
a := durationMax(test.v1, test.v2)
if test.out != a {
t.Errorf("failed duratoin max. v1: %v, v2: %v, expected: %v, got: %v", test.v1, test.v2, test.out, a)
}
}
}
type tcNamedValues struct {
values []driver.Value
out []driver.NamedValue
}
func compareNamedValues(v1 []driver.NamedValue, v2 []driver.NamedValue) bool {
if v1 == nil && v2 == nil {
return true
}
if v1 == nil || v2 == nil {
return false
}
if len(v1) != len(v2) {
return false
}
for i := range v1 {
if v1[i] != v2[i] {
return false
}
}
return true
}
func TestToNamedValues(t *testing.T) {
testcases := []tcNamedValues{
{
values: []driver.Value{},
out: []driver.NamedValue{},
},
{
values: []driver.Value{1},
out: []driver.NamedValue{{Name: "", Ordinal: 1, Value: 1}},
},
{
values: []driver.Value{1, "test1", 9.876, nil},
out: []driver.NamedValue{
{Name: "", Ordinal: 1, Value: 1},
{Name: "", Ordinal: 2, Value: "test1"},
{Name: "", Ordinal: 3, Value: 9.876},
{Name: "", Ordinal: 4, Value: nil}},
},
}
for _, test := range testcases {
a := toNamedValues(test.values)
if !compareNamedValues(test.out, a) {
t.Errorf("failed int max. v1: %v, v2: %v, expected: %v, got: %v", test.values, test.out, test.out, a)
}
}
}