forked from invopop/gobl.ubl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
200 lines (186 loc) · 5.12 KB
/
utils_test.go
File metadata and controls
200 lines (186 loc) · 5.12 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
package ubl
import (
"testing"
"github.com/invopop/gobl/cbc"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/tax"
"github.com/stretchr/testify/assert"
)
// Define tests for the ParseDate function
func TestParseDate(t *testing.T) {
tests := []struct {
name string
input string
expected string
expectError bool
}{
{"Valid date", "2023-05-15", "2023-05-15", false},
{"Invalid date", "2023-13-45", "", true},
{"Empty string", "", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseDate(tt.input)
if tt.expectError {
assert.Error(t, err)
assert.Empty(t, result)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result.String())
}
})
}
}
// Define tests for the TypeCodeParse function
func TestTypeCodeParse(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"Proforma invoice", "325", "proforma"},
{"Standard invoice", "380", "standard"},
{"Credit note", "381", "credit-note"},
{"Debit note", "383", "debit-note"},
{"Corrective invoice", "384", "corrective"},
{"Self-billed invoice", "389", "standard"},
{"Partial invoice", "326", "standard"},
{"Self-billed credit note", "261", "credit-note"},
{"Unknown type code", "999", "other"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := typeCodeParse(tt.input)
assert.Equal(t, tt.expected, string(result))
})
}
}
// Define tests for the TagCodeParse function
func TestTagCodeParse(t *testing.T) {
tests := []struct {
name string
input string
expected []cbc.Key
}{
{"Self-billed invoice", "389", []cbc.Key{tax.TagSelfBilled}},
{"Partial invoice", "326", []cbc.Key{tax.TagPartial}},
{"Self-billed credit note", "261", []cbc.Key{tax.TagSelfBilled}},
{"Standard invoice - no tag", "380", nil},
{"Credit note - no tag", "381", nil},
{"Unknown code - no tag", "999", nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tagCodeParse(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}
// Define tests for the UnitFromUNECE function
func TestUnitFromUNECE(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"Known UNECE code", "HUR", "h"},
{"Known UNECE code", "SEC", "s"},
{"Known UNECE code", "MTR", "m"},
{"Known UNECE code", "GRM", "g"},
{"Unknown UNECE code", "XYZ", "XYZ"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := goblUnitFromUNECE(cbc.Code(tt.input))
assert.Equal(t, tt.expected, string(result))
})
}
}
// Define tests for the FormatKey function
func TestFormatKey(t *testing.T) {
assert.Equal(t, cbc.Key("test"), formatKey("Test"))
assert.Equal(t, cbc.Key("test-key-2"), formatKey("Test Key 2"))
assert.Equal(t, cbc.Key("multiple-spaces"), formatKey("Multiple Spaces"))
assert.Equal(t, cbc.Key("numbers-123"), formatKey("Numbers 123"))
assert.Equal(t, cbc.Key("trailing-space"), formatKey("Trailing Space "))
assert.Equal(t, cbc.Key("mixed-case-with-123-numbers"), formatKey("MiXeD cAsE wItH 123 NuMbErS"))
}
func TestCalculateRequiredPrecision(t *testing.T) {
tests := []struct {
name string
price string
baseQuantity string
expected uint32
}{
{
name: "base quantity of 1",
price: "100.00",
baseQuantity: "1",
expected: 2, // 2 + 0 (log10(1) = 0)
},
{
name: "base quantity of 2",
price: "200.00",
baseQuantity: "2",
expected: 3, // 2 + ceil(log10(2)) = 2 + 1
},
{
name: "base quantity of 10",
price: "100.00",
baseQuantity: "10",
expected: 3, // 2 + ceil(log10(10)) = 2 + 1
},
{
name: "base quantity of 100",
price: "100.00",
baseQuantity: "100",
expected: 4, // 2 + ceil(log10(100)) = 2 + 2
},
{
name: "base quantity of 1000",
price: "100.00",
baseQuantity: "1000",
expected: 5, // 2 + ceil(log10(1000)) = 2 + 3
},
{
name: "price with more decimals",
price: "100.12345",
baseQuantity: "100",
expected: 7, // 5 + ceil(log10(100)) = 5 + 2
},
{
name: "price with no decimals",
price: "100",
baseQuantity: "100",
expected: 2, // 0 + ceil(log10(100)) = 0 + 2
},
{
name: "fractional base quantity less than 1",
price: "100.00",
baseQuantity: "0.5",
expected: 2, // 2 + 0 (baseQtyFloat <= 1 after Rescale(0))
},
{
name: "non-power-of-10 base quantity",
price: "100.00",
baseQuantity: "3",
expected: 3, // 2 + ceil(log10(3)) = 2 + 1
},
{
name: "large base quantity",
price: "100.00",
baseQuantity: "10000",
expected: 6, // 2 + ceil(log10(10000)) = 2 + 4
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
price, err := num.AmountFromString(tt.price)
assert.NoError(t, err)
baseQty, err := num.AmountFromString(tt.baseQuantity)
assert.NoError(t, err)
result := calculateRequiredPrecision(price, baseQty)
assert.Equal(t, tt.expected, result)
})
}
}