-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_test.go
More file actions
170 lines (139 loc) · 3.87 KB
/
data_test.go
File metadata and controls
170 lines (139 loc) · 3.87 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package tinybird_test
import (
"testing"
"github.com/the-hotels-network/go-tinybird"
"github.com/stretchr/testify/assert"
)
func TestFirst_ReturnsFirstRow(t *testing.T) {
d := tinybird.Data{
{"id": 1, "name": "first"},
{"id": 2, "name": "second"},
}
first := d.First()
assert.Equal(t, tinybird.Row{"id": 1, "name": "first"}, first)
}
func TestFirst_PanicsOnEmptyData(t *testing.T) {
var d tinybird.Data
assert.Equal(t, d.First(), tinybird.Row{})
empty := tinybird.Data{}
assert.Equal(t, empty.First(), tinybird.Row{})
}
func TestFetchOne_ReturnsValueFromFirstRow(t *testing.T) {
d := tinybird.Data{
{"foo": "bar", "n": 42},
{"foo": "baz"},
}
val := d.FetchOne("foo")
assert.Equal(t, "bar", val)
valNum := d.FetchOne("n")
assert.Equal(t, 42, valNum)
}
func TestFetchOne_ReturnsNilWhenKeyMissing(t *testing.T) {
d := tinybird.Data{
{"foo": "bar"},
}
val := d.FetchOne("missing")
assert.Nil(t, val)
}
func TestFetchOne_PanicsOnEmptyData(t *testing.T) {
var d tinybird.Data
assert.Nil(t, d.FetchOne("foo"))
}
func TestToString_JSONRoundTripEq(t *testing.T) {
d := tinybird.Data{
{"a": 1, "b": "x"},
{"a": 2, "c": true},
}
expected := `[{"a":1,"b":"x"},{"a":2,"c":true}]`
assert.JSONEq(t, expected, d.ToString())
}
func TestToString_OnNilAndEmpty(t *testing.T) {
var dNil tinybird.Data
dEmpty := tinybird.Data{}
assert.Equal(t, "null", dNil.ToString())
assert.Equal(t, "[]", dEmpty.ToString())
}
func TestGet(t *testing.T) {
d := tinybird.Data{
{"a": nil},
{"b": tinybird.Row{"a": 1}},
{"b": tinybird.Row{"b": 2}},
{"b": tinybird.Row{"c": 3}},
{"c": tinybird.Row{"a": 4}},
{"d": tinybird.Row{"a": 5}},
{"d": tinybird.Row{"b": tinybird.Row{"a": 6}}},
{"f": 7},
}
assert.Nil(t, d.Get(""))
assert.Nil(t, d.Get("a"))
assert.Nil(t, d.Get("e"))
assert.Equal(t, 7, d.Get("f"))
assert.Equal(t, 4, d.Get("c.a"))
assert.Equal(t, 6, d.Get("d.b.a"))
assert.Equal(t, tinybird.Row{"a": 4}, d.Get("c"))
}
func TestAutoCast(t *testing.T) {
tests := []struct {
name string
val any
expected any
}{
// Non-string values pass through unchanged
{"nil", nil, nil},
{"int", 42, 42},
{"float64", float64(3.14), float64(3.14)},
{"bool", true, true},
// Strings that are integers
{"string int", "42", int64(42)},
{"string negative int", "-999", int64(-999)},
{"string big int", "1125523841434490335", int64(1125523841434490335)},
{"string zero", "0", int64(0)},
// Strings that are floats
{"string float", "3.14", float64(3.14)},
{"string negative float", "-0.5", float64(-0.5)},
{"string float with exponent", "1.5e2", float64(150)},
// Strings that stay as strings
{"plain string", "hello", "hello"},
{"empty string", "", ""},
{"date string", "2022-03-30", "2022-03-30"},
{"datetime string", "2022-03-30 14:34:57", "2022-03-30 14:34:57"},
{"mixed string", "abc123", "abc123"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tinybird.AutoCast(tt.val)
assert.Equal(t, tt.expected, got)
})
}
}
func TestSet(t *testing.T) {
d := tinybird.Data{
{"a": nil},
{"b": tinybird.Row{"a": 1}},
{"b": tinybird.Row{"b": 2}},
{"b": tinybird.Row{"c": 3}},
{"c": tinybird.Row{"a": 4}},
{"d": tinybird.Row{"a": 5}},
{"d": tinybird.Row{"b": tinybird.Row{"a": 6}}},
{"f": 7},
}
assert.Nil(t, d.Set("a", 0))
assert.Equal(t, 0, d.Get("a"))
assert.Nil(t, d.Set("b.b", 22))
assert.Equal(t, 22, d.Get("b.b"))
assert.Nil(t, d.Set("d.b.a", 66))
assert.Equal(t, 66, d.Get("d.b.a"))
}
func TestSetWithAutoCast(t *testing.T) {
d := tinybird.Data{
{"a": nil},
}
assert.Nil(t, d.SetWithAutoCast("a", "42"))
assert.Equal(t, int64(42), d.Get("a"))
assert.Nil(t, d.SetWithAutoCast("a", "3.14"))
assert.Equal(t, float64(3.14), d.Get("a"))
assert.Nil(t, d.SetWithAutoCast("a", "hello"))
assert.Equal(t, "hello", d.Get("a"))
assert.Nil(t, d.SetWithAutoCast("a", 99))
assert.Equal(t, 99, d.Get("a"))
}