-
Notifications
You must be signed in to change notification settings - Fork 0
/
cii_test.go
116 lines (91 loc) · 2.98 KB
/
cii_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
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
package cii_test
import (
"encoding/json"
"encoding/xml"
"flag"
"os"
"path/filepath"
"strings"
"testing"
"github.com/invopop/gobl"
cii "github.com/invopop/gobl.cii"
"github.com/invopop/gobl.cii/structs"
"github.com/invopop/gobl.cii/test"
"github.com/invopop/gobl/bill"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var update = flag.Bool("update", false, "Update out directory")
func TestNewDocument(t *testing.T) {
// schema, err := test.LoadSchema("schema.xsd")
// require.NoError(t, err)
examples, err := test.GetDataGlob("*.json")
require.NoError(t, err)
for _, example := range examples {
inName := filepath.Base(example)
outName := strings.Replace(inName, ".json", ".xml", 1)
t.Run(inName, func(t *testing.T) {
doc, err := test.NewDocumentFrom(inName)
require.NoError(t, err)
data, err := doc.Bytes()
require.NoError(t, err)
// err = test.ValidateXML(schema, data)
// require.NoError(t, err)
output, err := test.LoadOutputFile(outName)
assert.NoError(t, err)
if *update {
err = test.SaveOutputFile(outName, data)
require.NoError(t, err)
} else {
assert.Equal(t, output, data, "Output should match the expected XML. Update with --update flag.")
}
})
}
}
func TestNewDocumentGOBL(t *testing.T) {
examples, err := test.GetDataGlob("*.xml")
require.NoError(t, err)
for _, example := range examples {
inName := filepath.Base(example)
outName := strings.Replace(inName, ".xml", ".json", 1)
t.Run(inName, func(t *testing.T) {
// Load XML data into doc
xmlData, err := os.ReadFile(example)
require.NoError(t, err)
doc := new(structs.XMLDoc)
err = xml.Unmarshal(xmlData, doc)
require.NoError(t, err)
// Now doc should be populated with data from the XML file
goblEnv, err := cii.NewGOBLFromCII(doc)
require.NoError(t, err)
// Extract the invoice from the envelope
invoice, ok := goblEnv.Extract().(*bill.Invoice)
require.True(t, ok, "Document should be an invoice")
// Remove UUID from the invoice
invoice.UUID = ""
// Marshal only the invoice
data, err := json.MarshalIndent(invoice, "", " ")
require.NoError(t, err)
// Load the expected output
output, err := test.LoadOutputFile(outName)
assert.NoError(t, err)
// Parse the expected output to extract the invoice
var expectedEnv gobl.Envelope
err = json.Unmarshal(output, &expectedEnv)
require.NoError(t, err)
expectedInvoice, ok := expectedEnv.Extract().(*bill.Invoice)
require.True(t, ok, "Expected document should be an invoice")
// Remove UUID from the expected invoice
expectedInvoice.UUID = ""
// Marshal the expected invoice
expectedData, err := json.MarshalIndent(expectedInvoice, "", " ")
require.NoError(t, err)
if *update {
err = test.SaveOutputFile(outName, data)
require.NoError(t, err)
} else {
assert.JSONEq(t, string(expectedData), string(data), "Invoice should match the expected JSON. Update with --update flag.")
}
})
}
}