-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathudf_test.go
274 lines (236 loc) · 6.25 KB
/
udf_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package neatly_test
import (
"github.com/stretchr/testify/assert"
"github.com/viant/neatly"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"github.com/viant/toolbox/storage"
"github.com/viant/toolbox/url"
"strings"
"testing"
"time"
)
func Test_Md5(t *testing.T) {
var md5, err = neatly.Md5("554257_popularmechanics.com", nil)
assert.Nil(t, err)
assert.EqualValues(t, "ed045d398e8e1924486afa44acbb6b82", md5)
aMap := data.NewMap()
aMap.Put("md5", neatly.Md5)
var text = "11$md5(554257_popularmechanics.com)22"
expanded := aMap.ExpandAsText(text)
assert.EqualValues(t, "11ed045d398e8e1924486afa44acbb6b8222", expanded)
}
func Test_WorkingDirectory(t *testing.T) {
var path, err = neatly.WorkingDirectory("../../abc.txt", nil)
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(toolbox.AsString(path), "/abc.txt"))
assert.True(t, !strings.Contains(toolbox.AsString(path), ".."))
}
func Test_HasResource(t *testing.T) {
var state = data.NewMap()
{
has, err := neatly.HasResource("abc", state)
assert.Nil(t, err)
assert.False(t, toolbox.AsBoolean(has))
}
{
has, err := neatly.HasResource("udf.go", state)
assert.Nil(t, err)
assert.True(t, toolbox.AsBoolean(has))
}
state.Put(neatly.OwnerURL, url.NewResource("test/use_case1.csv").URL)
{
has, err := neatly.HasResource("abc", state)
assert.Nil(t, err)
assert.False(t, toolbox.AsBoolean(has))
}
state.Put(neatly.OwnerURL, url.NewResource("test/usecase7/001/customer.json").URL)
{
has, err := neatly.HasResource("use_case.txt", state)
assert.Nil(t, err)
assert.True(t, toolbox.AsBoolean(has))
}
}
func Test_Zip_Unzip(t *testing.T) {
{
compressed, err := neatly.Zip("abc", nil)
assert.Nil(t, err)
{
origin, err := neatly.Unzip(compressed, nil)
assert.Nil(t, err)
assert.Equal(t, "abc", toolbox.AsString(origin))
}
{
origin, err := neatly.UnzipText(compressed, nil)
assert.Nil(t, err)
assert.Equal(t, "abc", origin)
}
}
{
compressed, err := neatly.Zip([]byte("abc"), nil)
assert.Nil(t, err)
origin, err := neatly.Unzip(compressed, nil)
assert.Nil(t, err)
assert.Equal(t, "abc", toolbox.AsString(origin))
}
{ //Error case
_, err := neatly.Zip(1, nil)
assert.NotNil(t, err)
_, err = neatly.Unzip(1, nil)
assert.NotNil(t, err)
_, err = neatly.Unzip([]byte{}, nil)
assert.NotNil(t, err)
}
}
func Test_Markdown(t *testing.T) {
{
html, err := neatly.Markdown("*Hello*", nil)
assert.Nil(t, err)
assert.EqualValues(t, "<p><em>Hello</em></p>\n", html)
}
}
func Test_Cat(t *testing.T) {
{
content, err := neatly.Cat("udf.go", nil)
assert.Nil(t, err)
assert.True(t, content != "")
}
{
_, err := neatly.Cat("uaaadf.go", nil)
assert.NotNil(t, err)
}
{
content, err := neatly.Cat("test/../udf.go", nil)
assert.Nil(t, err)
assert.True(t, content != "")
}
}
func Test_IsSON(t *testing.T) {
//Table driven tests
useCases := []struct {
name string
input string
expected bool
}{
{"Case to check if invalid JSON is validated", "test/invalid_file.json", false},
{"Case to check if valid JSON is validated", "test/valid_json_file.json", true},
}
for _, useCase := range useCases {
t.Run(useCase.name, func(t *testing.T) {
isJson, _ := neatly.IsJSON(useCase.input, nil)
assert.Equal(t, useCase.expected, toolbox.AsBoolean(isJson))
})
}
}
func Test_AssetsToMap(t *testing.T) {
storageService := storage.NewMemoryService()
_ = storageService.Upload("mem:///folder1/asset1.txt", strings.NewReader("k1: v1"))
_ = storageService.Upload("mem:///folder1/asset2.txt", strings.NewReader("k2: v2"))
useCases := []struct {
description string
input interface{}
hasError bool
}{
{
description: "URL param",
input: "mem:///folder1/",
},
{
description: "url.Resource param",
input: url.NewResource("mem:///folder1/"),
},
{
description: "URL, credentials params",
input: []interface{}{"mem:///folder1/", ""},
},
{
description: "invalid param type",
input: 12,
hasError: true,
},
}
for _, useCase := range useCases {
result, err := neatly.AssetsToMap(useCase.input, nil)
if useCase.hasError {
assert.NotNil(t, err, useCase.description)
continue
}
assert.Nil(t, err)
assert.Equal(t, map[string]string{
"asset1.txt": "k1: v1",
"asset2.txt": "k2: v2",
}, result, useCase.description)
result, _ = neatly.BinaryAssetsToMap(useCase.input, nil)
assert.Nil(t, err)
assert.Equal(t, map[string][]byte{
"asset1.txt": []byte("k1: v1"),
"asset2.txt": []byte("k2: v2"),
}, result, useCase.description)
}
}
func Test_TimeNowUDFs(t *testing.T) {
currentHour, _ := neatly.CurrentHour(nil, nil)
assert.Equal(t, time.Now().Hour(), toolbox.AsInt(currentHour))
}
func Test_MatchToAny(t *testing.T) {
var path, err = neatly.WorkingDirectory("./test/matchAnyRows/testrows.txt", nil)
if err != nil {
assert.Fail(t, err.Error())
}
useCases := []struct {
description string
input1 interface{}
input2 interface{}
expected bool
}{
{
description: "Value equals 10 and row should match",
input1: "10",
input2: path,
expected: true,
},
{
description: "Value equals 39 and row should match",
input1: "39",
input2: path,
expected: true,
},
{
description: "Value equals a string of text and row should match",
input1: "this is a test",
input2: path,
expected: true,
},
{
description: "Value equals 12345 and row should not match",
input1: "12345",
input2: path,
expected: false,
},
{
description: "Path is not valid should be false",
input1: "12345",
input2: "NOT A VALID PATH",
expected: false,
},
}
for _, useCase := range useCases {
argumentsMap := map[string]interface{}{
"value": useCase.input1.(interface{}),
"path": useCase.input2.(interface{}),
}
matched, _ := neatly.MatchAnyRow(argumentsMap, nil)
assert.Equal(t, useCase.expected, matched)
}
//Testing missing map fields
argumentsMap := map[string]interface{}{
"value": "test",
}
matched, _ := neatly.MatchAnyRow(argumentsMap, nil)
assert.Equal(t, false, matched)
argumentsMap = map[string]interface{}{
"path": "test",
}
matched, _ = neatly.MatchAnyRow(argumentsMap, nil)
assert.Equal(t, false, matched)
}