Repository navigation
Expand file tree
/
Copy pathvalidate_test.go
More file actions
205 lines (188 loc) · 6.21 KB
/
Copy pathvalidate_test.go
File metadata and controls
205 lines (188 loc) · 6.21 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
package einvoice
import (
"errors"
"testing"
"time"
"github.com/shopspring/decimal"
)
// TestValidate_ManualInvoiceConstruction tests the new public Validate() API
// with an invoice built programmatically rather than parsed from XML.
func TestValidate_ManualInvoiceConstruction(t *testing.T) {
// Build an invalid invoice manually
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceNumber: "", // BR-2 violation: missing invoice number
InvoiceTypeCode: 380, // OK
InvoiceDate: time.Time{}, // BR-3 violation: zero date
InvoiceCurrencyCode: "", // BR-5 violation: empty currency
Seller: Party{
Name: "", // BR-6 violation: empty seller name
PostalAddress: &PostalAddress{
CountryID: "", // BR-9 violation: empty country code
},
},
Buyer: Party{
Name: "Valid Buyer",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
LineTotal: decimal.Zero, // OK: zero is valid per EN 16931
TaxBasisTotal: decimal.Zero, // OK: zero is valid per EN 16931
GrandTotal: decimal.Zero, // OK: zero is valid per EN 16931
DuePayableAmount: decimal.Zero, // OK: zero is valid per EN 16931 (prepaid invoices)
InvoiceLines: []InvoiceLine{}, // BR-16 violation: no lines
}
// Call Validate() - should return ValidationError
err := inv.Validate()
if err == nil {
t.Fatal("Expected validation error, got nil")
}
// Check that error is ValidationError
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Fatalf("Expected ValidationError, got %T", err)
}
// Check specific violations
// Note: BR-12 through BR-15 removed - EN 16931 only requires element presence, not non-zero values.
// Zero is valid for prepaid invoices (BR-15) and other scenarios.
expectedRules := []string{"BR-02", "BR-03", "BR-05", "BR-06", "BR-09", "BR-16"}
for _, rule := range expectedRules {
if !valErr.HasRuleCode(rule) {
t.Errorf("Expected violation for rule %s, but not found", rule)
}
}
// Verify Count() method
if valErr.Count() < len(expectedRules) {
t.Errorf("Expected at least %d violations, got %d", len(expectedRules), valErr.Count())
}
// Verify Violations() accessor
violations := valErr.Violations()
if len(violations) != valErr.Count() {
t.Errorf("Violations() returned %d items, but Count() is %d", len(violations), valErr.Count())
}
// Verify error message
errMsg := valErr.Error()
if errMsg == "" {
t.Error("Error() returned empty string")
}
}
// TestValidate_Idempotency tests that calling Validate() multiple times
// produces consistent results without accumulating violations.
func TestValidate_Idempotency(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceNumber: "", // BR-2 violation
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
Seller: Party{
Name: "Test Seller",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Test Buyer",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
GrandTotal: decimal.NewFromInt(119),
DuePayableAmount: decimal.NewFromInt(119),
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Test Item",
BilledQuantity: decimal.NewFromInt(1),
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
},
},
}
// First validation
err1 := inv.Validate()
if err1 == nil {
t.Fatal("Expected validation error on first call")
}
var valErr1 *ValidationError
errors.As(err1, &valErr1)
count1 := valErr1.Count()
// Second validation - should have same count
err2 := inv.Validate()
if err2 == nil {
t.Fatal("Expected validation error on second call")
}
var valErr2 *ValidationError
errors.As(err2, &valErr2)
count2 := valErr2.Count()
if count1 != count2 {
t.Errorf("Idempotency broken: first call had %d violations, second call had %d", count1, count2)
}
// Third validation - should still have same count
err3 := inv.Validate()
var valErr3 *ValidationError
errors.As(err3, &valErr3)
count3 := valErr3.Count()
if count1 != count3 {
t.Errorf("Idempotency broken: first call had %d violations, third call had %d", count1, count3)
}
}
// TestValidate_MostlyValidInvoice tests that validation can be called on a mostly valid invoice.
// Note: Creating a fully EN 16931-compliant invoice requires many fields, so this test
// just ensures Validate() works without panicking, even if some minor violations remain.
func TestValidate_MostlyValidInvoice(t *testing.T) {
inv := &Invoice{
GuidelineSpecifiedDocumentContextParameter: SpecEN16931,
InvoiceNumber: "INV-001",
InvoiceTypeCode: 380,
InvoiceDate: time.Now(),
InvoiceCurrencyCode: "EUR",
Seller: Party{
Name: "Test Seller",
VATaxRegistration: "DE123456789",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
Buyer: Party{
Name: "Test Buyer",
PostalAddress: &PostalAddress{
CountryID: "DE",
},
},
LineTotal: decimal.NewFromInt(100),
TaxBasisTotal: decimal.NewFromInt(100),
TaxTotal: decimal.NewFromInt(19),
GrandTotal: decimal.NewFromInt(119),
DuePayableAmount: decimal.NewFromInt(119),
InvoiceLines: []InvoiceLine{
{
LineID: "1",
ItemName: "Test Item",
BilledQuantity: decimal.NewFromInt(1),
BilledQuantityUnit: "C62",
NetPrice: decimal.NewFromInt(100),
Total: decimal.NewFromInt(100),
TaxCategoryCode: "S",
TaxRateApplicablePercent: decimal.NewFromInt(19),
},
},
TradeTaxes: []TradeTax{
{
CategoryCode: "S",
BasisAmount: decimal.NewFromInt(100),
CalculatedAmount: decimal.NewFromInt(19),
Percent: decimal.NewFromInt(19),
},
},
}
// Validate should not panic - error is acceptable since creating
// a fully compliant invoice requires many additional fields
err := inv.Validate()
// We don't check if err is nil because full compliance is complex
// The test just verifies Validate() can be called successfully
_ = err
}