forked from reanahub/reana-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatautils_test.go
More file actions
213 lines (202 loc) · 4.66 KB
/
Copy pathdatautils_test.go
File metadata and controls
213 lines (202 loc) · 4.66 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
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
/*
This file is part of REANA.
Copyright (C) 2022, 2025, 2026 CERN.
REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/
package datautils
import (
"testing"
"time"
"golang.org/x/exp/slices"
)
func TestHasAnyPrefix(t *testing.T) {
tests := map[string]struct {
prefixes []string
str string
want bool
}{
"one prefix": {
prefixes: []string{"foo"},
str: "foobar",
want: true,
},
"wrong prefix": {
prefixes: []string{"foo"},
str: "bar",
want: false,
},
"exact word": {
prefixes: []string{"foo", "bar"},
str: "foo",
want: true,
},
"two prefix options": {
prefixes: []string{"foo", "bar"},
str: "foobar",
want: true,
},
"wrong prefix two options": {
prefixes: []string{"foo", "bar"},
str: "baz",
want: false,
},
"no options": {
prefixes: []string{},
str: "foobar",
want: false,
},
"empty string": {
prefixes: []string{"foo", "bar"},
str: "",
want: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := HasAnyPrefix(test.str, test.prefixes)
if got != test.want {
t.Errorf("Expected %t, got %t", test.want, got)
}
})
}
}
func TestFromIsoToTimestamp(t *testing.T) {
now := time.Now().UTC()
tests := map[string]struct {
dateIso string
wantError bool
want time.Time
}{
"regular date": {
dateIso: "2020-01-01T03:16:45",
want: time.Date(2020, 1, 1, 3, 16, 45, 0, time.UTC),
},
"current timestamp": {
dateIso: now.Format("2006-01-02T15:04:05"),
want: now.Truncate(time.Second),
},
"wrong format": {
dateIso: "2020-01-01T00:00:00Z",
wantError: true,
},
"only time": {
dateIso: "09:30:00Z",
wantError: true,
},
"empty string": {
dateIso: "",
wantError: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got, err := FromIsoToTimestamp(test.dateIso)
if test.wantError {
if err == nil {
t.Errorf("Expected error, got nil")
}
} else {
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if got != test.want {
t.Errorf("Expected %v, got %v", test.want, got)
}
}
})
}
}
func TestSplitLinesNoEmpty(t *testing.T) {
tests := map[string]struct {
arg string
want []string
}{
"empty string": {arg: "", want: []string{}},
"only one line": {arg: "a", want: []string{"a"}},
"two lines": {arg: "a\nb", want: []string{"a", "b"}},
"three lines": {arg: "a\nb\nc", want: []string{"a", "b", "c"}},
"ending with newline": {
arg: "a\nb\nc\n",
want: []string{"a", "b", "c"},
},
"with empty lines": {
arg: "a\n\nb\n\nc",
want: []string{"a", "b", "c"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := SplitLinesNoEmpty(test.arg)
if !slices.Equal(got, test.want) {
t.Errorf("Expected %v, got %v", test.want, got)
}
})
}
}
func TestSplitKeyValue(t *testing.T) {
tests := map[string]struct {
str string
key string
value string
wantError bool
}{
"regular filter": {str: "key=value", key: "key", value: "value"},
"missing value": {str: "key=", key: "key", value: ""},
"missing key": {str: "=value", key: "", value: "value"},
"value including '='": {
str: "key=value=value",
key: "key",
value: "value=value",
},
"invalid input": {str: "invalid", wantError: true},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
key, value, err := SplitKeyValue(test.str)
if test.wantError {
if err == nil {
t.Errorf(
"Expected error for SplitKeyValue(%s), got nil",
test.str,
)
}
} else if err != nil {
t.Errorf("Unexpected error for SplitKeyValue(%s): '%s'", test.str, err.Error())
} else if key != test.key || value != test.value {
t.Errorf("Expected result to be %s,%s, got %s,%s", test.key, test.value, key, value)
}
})
}
}
func TestRemoveFromSlice(t *testing.T) {
tests := map[string]struct {
slice []string
elem string
want []string
}{
"contains elem": {
slice: []string{"elem1", "elem2", "elem3"},
elem: "elem2",
want: []string{"elem1", "elem3"},
},
"doesn't contain elem": {
slice: []string{"elem1", "elem3"},
elem: "elem2",
want: []string{"elem1", "elem3"},
},
"empty slice": {
slice: []string{},
elem: "elem",
want: []string{},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := RemoveFromSlice(test.slice, test.elem)
if !slices.Equal(got, test.want) {
t.Errorf("Expected %v, got %v", test.want, got)
}
})
}
}