-
Notifications
You must be signed in to change notification settings - Fork 0
/
mabe_test.go
83 lines (68 loc) · 2.25 KB
/
mabe_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
package addendas_test
import (
"testing"
"github.com/invopop/gobl.cfdi/addendas"
"github.com/invopop/gobl.cfdi/test"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/org"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddendaMabeValidation(t *testing.T) {
env, err := test.LoadTestEnvelope("invoice-b2b-bare.json")
require.NoError(t, err)
inv := env.Extract().(*bill.Invoice)
// Prepare the invoice to be raise all Mabe validation errors.
inv.Type = bill.InvoiceTypeProforma
inv.Supplier.Identities = []*org.Identity{
{
Key: addendas.MabeKeyIdentityProviderCode,
Code: "12345",
},
}
// Check every validation and then fix it.
assertValidationError(t, inv, "type: must be a valid value")
inv.Type = bill.InvoiceTypeStandard
assertValidationError(t, inv, "delivery: cannot be blank")
inv.Delivery = &bill.Delivery{}
assertValidationError(t, inv, "delivery: (receiver: cannot be blank")
inv.Delivery.Receiver = &org.Party{
Name: "Test Receiver",
}
assertValidationError(t, inv, "delivery: (receiver: (identities: missing key mx-mabe-delivery-plant")
inv.Delivery.Receiver.Identities = []*org.Identity{
{
Key: addendas.MabeKeyIdentityDeliveryPlant,
Code: "S001",
},
}
assertValidationError(t, inv, "lines: (0: (item: (identities: missing key mx-mabe-article-code")
inv.Lines[0].Item.Identities = []*org.Identity{
{
Key: addendas.MabeKeyIdentityArticleCode,
Code: "12345",
},
}
assertValidationError(t, inv, "ordering: cannot be blank")
inv.Ordering = &bill.Ordering{}
assertValidationError(t, inv, "ordering: (identities: missing key mx-mabe-purchase-order.)")
inv.Ordering.Identities = []*org.Identity{
{
Key: addendas.MabeKeyIdentityPurchaseOrder,
Code: "12345",
},
}
assertValidationError(t, inv, "ordering: (identities: missing key mx-mabe-reference1")
inv.Ordering.Identities = append(inv.Ordering.Identities, &org.Identity{
Key: addendas.MabeKeyIdentityRef1,
Code: "12345",
})
// All validation errors must be fixed by now.
_, err = addendas.For(inv)
require.NoError(t, err)
}
func assertValidationError(t *testing.T, inv *bill.Invoice, expected string) {
_, err := addendas.For(inv)
require.Error(t, err)
assert.Contains(t, err.Error(), expected)
}