-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtime_byte_test.go
More file actions
177 lines (170 loc) · 3.75 KB
/
time_byte_test.go
File metadata and controls
177 lines (170 loc) · 3.75 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
package parser
import (
"testing"
)
func TestTimeVsByteDistinction(t *testing.T) {
// Test the specific case mentioned in the user's request
tests := []struct {
name string
input string
expected string
}{
{
name: "10m should be parsed as 600 seconds (time), not megabytes",
input: "Duration > 10m",
expected: "Duration > 600",
},
{
name: "10MB should be parsed as megabytes (bytes), not minutes",
input: "Size > 10MB",
expected: "Size > 10000000",
},
{
name: "1h should be parsed as 3600 seconds (time)",
input: "Timeout > 1h",
expected: "Timeout > 3600",
},
{
name: "1GB should be parsed as gigabytes (bytes)",
input: "Storage > 1GB",
expected: "Storage > 1000000000",
},
{
name: "Mixed time and byte units in same query",
input: "Duration > 30m AND Size < 500MB",
expected: "Duration > 1800 AND Size < 500000000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := normalizeHumanizedValues(tt.input)
if result != tt.expected {
t.Errorf("normalizeHumanizedValues(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestTimeDurationParsing(t *testing.T) {
tests := []struct {
name string
input string
expected int64
hasError bool
}{
{
name: "10m should parse as 600 seconds",
input: "10m",
expected: 600,
hasError: false,
},
{
name: "1h should parse as 3600 seconds",
input: "1h",
expected: 3600,
hasError: false,
},
{
name: "30s should parse as 30 seconds",
input: "30s",
expected: 30,
hasError: false,
},
{
name: "1d should parse as 86400 seconds",
input: "1d",
expected: 86400,
hasError: false,
},
{
name: "1.5h should parse as 5400 seconds",
input: "1.5h",
expected: 5400,
hasError: false,
},
{
name: "Invalid time unit should error",
input: "10x",
expected: 0,
hasError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseTimeDuration(tt.input)
if tt.hasError {
if err == nil {
t.Errorf("parseTimeDuration(%q) expected error but got none", tt.input)
}
} else {
if err != nil {
t.Errorf("parseTimeDuration(%q) unexpected error: %v", tt.input, err)
}
if result != tt.expected {
t.Errorf("parseTimeDuration(%q) = %d, want %d", tt.input, result, tt.expected)
}
}
})
}
}
func TestByteSizeParsing(t *testing.T) {
tests := []struct {
name string
input string
expected int64
hasError bool
}{
{
name: "10MB should parse as 10000000 bytes",
input: "10MB",
expected: 10000000,
hasError: false,
},
{
name: "1GB should parse as 1000000000 bytes",
input: "1GB",
expected: 1000000000,
hasError: false,
},
{
name: "1GiB should parse as 1073741824 bytes",
input: "1GiB",
expected: 1073741824,
hasError: false,
},
{
name: "1KB should parse as 1000 bytes",
input: "1KB",
expected: 1000,
hasError: false,
},
{
name: "1KiB should parse as 1024 bytes",
input: "1KiB",
expected: 1024,
hasError: false,
},
{
name: "Invalid byte unit should error",
input: "10XB",
expected: 0,
hasError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseByteSize(tt.input)
if tt.hasError {
if err == nil {
t.Errorf("parseByteSize(%q) expected error but got none", tt.input)
}
} else {
if err != nil {
t.Errorf("parseByteSize(%q) unexpected error: %v", tt.input, err)
}
if result != tt.expected {
t.Errorf("parseByteSize(%q) = %d, want %d", tt.input, result, tt.expected)
}
}
})
}
}