-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples_test.go
125 lines (94 loc) · 3.35 KB
/
examples_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
117
118
119
120
121
122
123
124
125
package fatturapa_test
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"encoding/xml"
"github.com/invopop/gobl.fatturapa/test"
"github.com/invopop/gobl/bill"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGOBLToXMLExamples(t *testing.T) {
schema, err := test.LoadSchema()
require.NoError(t, err)
path := test.GetDataPath(test.PathGOBLFatturaPA)
var files []string
err = filepath.Walk(path, func(path string, _ os.FileInfo, _ error) error {
if filepath.Ext(path) == ".json" {
files = append(files, filepath.Base(path))
}
return nil
})
require.NoError(t, err)
for _, file := range files {
fmt.Printf("processing file: %v\n", file)
env := test.LoadTestFile(file, test.PathGOBLFatturaPA)
doc, err := test.ConvertFromGOBL(env, test.LoadOptions()...)
require.NoError(t, err)
data, err := xml.MarshalIndent(doc, "", "\t")
require.NoError(t, err)
np := strings.TrimSuffix(file, filepath.Ext(file)) + ".xml"
outPath := filepath.Join(test.GetDataPath(test.PathGOBLFatturaPA), "out", np)
if *test.UpdateOut {
errs := test.ValidateXML(schema, data)
for _, e := range errs {
require.NoError(t, e)
}
if len(errs) > 0 {
require.Fail(t, "Invalid XML:\n"+string(data))
}
err = os.WriteFile(outPath, data, 0644)
require.NoError(t, err, "writing file")
}
expected, err := os.ReadFile(outPath)
require.False(t, os.IsNotExist(err), "output file %s missing, run tests with `--update` flag to create", filepath.Base(outPath))
require.NoError(t, err)
assert.Equal(t, string(expected), string(data), "output file %s does not match, run tests with `--update` flag to update", filepath.Base(outPath))
}
}
func TestXMLToGOBLExamples(t *testing.T) {
var err error
var files []string
path := test.GetDataPath(test.PathFatturaPAGOBL)
err = filepath.Walk(path, func(path string, _ os.FileInfo, _ error) error {
if filepath.Ext(path) == ".xml" {
files = append(files, filepath.Base(path))
}
return nil
})
require.NoError(t, err)
for _, file := range files {
fmt.Printf("processing file: %v\n", file)
data, err := os.ReadFile(filepath.Join(test.GetDataPath(test.PathFatturaPAGOBL), file))
require.NoError(t, err)
env, err := test.ConvertToGOBL(data)
require.NoError(t, err)
require.NotNil(t, env)
require.NoError(t, env.Calculate())
// Extract the invoice from the envelope
doc := env.Extract().(*bill.Invoice)
// Create a copy of the invoice to avoid modifying the original
invCopy := *doc
// Set a fixed UUID for consistent test output
invCopy.UUID = "00000000-0000-0000-0000-000000000000"
inv := &invCopy
data, err = json.MarshalIndent(inv, "", "\t")
require.NoError(t, err)
np := strings.TrimSuffix(file, filepath.Ext(file)) + ".json"
outPath := filepath.Join(test.GetDataPath(test.PathFatturaPAGOBL), "out", np)
if *test.UpdateOut {
err = env.Validate()
require.NoError(t, err)
err = os.WriteFile(outPath, data, 0644)
require.NoError(t, err, "writing file")
}
expected, err := os.ReadFile(outPath)
require.False(t, os.IsNotExist(err), "output file %s missing, run tests with `--update` flag to create", filepath.Base(outPath))
require.NoError(t, err)
assert.Equal(t, string(expected), string(data), "output file %s does not match, run tests with `--update` flag to update", filepath.Base(outPath))
}
}