From dc2d4ac762b6024d7639d612030a52274908de45 Mon Sep 17 00:00:00 2001 From: apardods Date: Mon, 16 Dec 2024 23:43:56 +0000 Subject: [PATCH] Add tests for ParseDocument --- verifactu_test.go | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 verifactu_test.go diff --git a/verifactu_test.go b/verifactu_test.go new file mode 100644 index 0000000..fdfb8f2 --- /dev/null +++ b/verifactu_test.go @@ -0,0 +1,110 @@ +package verifactu_test + +import ( + "reflect" + "testing" + "time" + + vf "github.com/invopop/gobl.verifactu" + "github.com/invopop/gobl.verifactu/doc" + "github.com/invopop/gobl.verifactu/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParseDocument(t *testing.T) { + tests := []struct { + name string + data []byte + want *doc.Envelope + wantErr bool + }{ + { + name: "valid document", + data: []byte(` + + + + + + Test Company + B12345678 + + + + +`), + want: &doc.Envelope{ + XMLNs: doc.EnvNamespace, + SUM: doc.SUM, + SUM1: doc.SUM1, + Body: &doc.Body{ + VeriFactu: &doc.RegFactuSistemaFacturacion{ + Cabecera: &doc.Cabecera{ + Obligado: doc.Obligado{ + NombreRazon: "Test Company", + NIF: "B12345678", + }, + }, + }, + }, + }, + wantErr: false, + }, + { + name: "invalid XML", + data: []byte(`invalid xml`), + want: nil, + wantErr: true, + }, + { + name: "empty document", + data: []byte{}, + want: nil, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := vf.ParseDocument(tt.data) + if (err != nil) != tt.wantErr { + t.Errorf("ParseDocument() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !tt.wantErr && !reflect.DeepEqual(got, tt.want) { + t.Errorf("ParseDocument() = %v, want %v", got, tt.want) + } + }) + } + + t.Run("should preserve RegistroAlta when parsing complete document", func(t *testing.T) { + // Load and generate the reference XML + inv := test.LoadInvoice("inv-base.json") + ts := time.Date(2024, 11, 26, 4, 0, 0, 0, time.UTC) + sw := &doc.Software{ + CompanyName: "My Software", + TaxID: "12345678A", + SoftwareName: "My Software", + SoftwareID: "A1", + Version: "1.0", + InstallationNumber: "12345678A", + } + want, err := doc.NewVerifactu(inv, ts, doc.IssuerRoleSupplier, sw, false) + require.NoError(t, err) + + // Get the XML bytes from the reference document + xmlData, err := want.Bytes() + require.NoError(t, err) + + // Parse the XML back into a document + got, err := vf.ParseDocument(xmlData) + require.NoError(t, err) + + // Check that RegistroAlta is present and correctly structured + require.NotNil(t, got.Body.VeriFactu.RegistroFactura) + require.NotNil(t, got.Body.VeriFactu.RegistroFactura.RegistroAlta) + assert.Equal(t, want.Body.VeriFactu.RegistroFactura.RegistroAlta.IDVersion, + got.Body.VeriFactu.RegistroFactura.RegistroAlta.IDVersion) + }) +}