-
Notifications
You must be signed in to change notification settings - Fork 16
/
data_units_test.go
85 lines (78 loc) · 1.86 KB
/
data_units_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
package helpers
import "testing"
func TestDcp_ResolveConnectionBufferSize(t *testing.T) {
tests := []struct {
input any
name string
want int
}{
{
name: "When_Client_Gives_Int_Value",
input: 20971520,
want: 20971520,
},
{
name: "When_Client_Gives_UInt_Value",
input: uint(10971520),
want: 10971520,
},
{
name: "When_Client_Gives_StringInt_Value",
input: "15971520",
want: 15971520,
},
{
name: "When_Client_Gives_KB_Value",
input: "500kb",
want: 500 * 1024,
},
{
name: "When_Client_Gives_MB_Value",
input: "10mb",
want: 10 * 1024 * 1024,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ResolveUnionIntOrStringValue(tt.input); got != tt.want {
t.Errorf("ResolveConnectionBufferSize() = %v, want %v", got, tt.want)
}
})
}
}
func TestConvertToBytes(t *testing.T) {
testCases := []struct {
input string
expected int
err bool
}{
{"1kb", 1024, false},
{"5mb", 5 * 1024 * 1024, false},
{"5,5mb", 5.5 * 1024 * 1024, false},
{"8.5mb", 8.5 * 1024 * 1024, false},
{"10,25 mb", 10.25 * 1024 * 1024, false},
{"10gb", 10 * 1024 * 1024 * 1024, false},
{"1KB", 1024, false},
{"5MB", 5 * 1024 * 1024, false},
{"12 MB", 12 * 1024 * 1024, false},
{"10GB", 10 * 1024 * 1024 * 1024, false},
{"123", 0, true},
{"15TB", 0, true},
{"invalid", 0, true},
{"", 0, true},
{"123 KB", 123 * 1024, false},
{"1 MB", 1 * 1024 * 1024, false},
}
for _, tc := range testCases {
result, err := convertSizeUnitToByte(tc.input)
if tc.err && err == nil {
t.Errorf("Expected an error for input %s, but got none", tc.input)
}
if !tc.err && err != nil {
t.Errorf("Unexpected error for input %s: %v", tc.input, err)
}
if result != tc.expected {
t.Errorf("For input %s, expected %d bytes, but got %d", tc.input, tc.expected, result)
}
}
}