Skip to content

Commit

Permalink
Added Scenario Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apardods committed Nov 22, 2024
1 parent 1b04b70 commit 52b07e5
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 14 deletions.
4 changes: 2 additions & 2 deletions addons/es/verifactu/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ var extensions = []*cbc.KeyDefinition{
},
Desc: i18n.String{
i18n.EN: here.Doc(`
Tax classification code used to identify the type of tax being applied to the invoice. In Verifactu these
lists are separate but here they are combined.
Tax classification code used to identify the type of tax being applied to the line. It includes both exemption reasons and tax scenarios.
These lists are separate in Verifactu but combined here for convenience.
`),
},
Values: []*cbc.ValueDefinition{
Expand Down
19 changes: 18 additions & 1 deletion addons/es/verifactu/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var invoiceCorrectionDefinitions = tax.CorrectionSet{

func validateInvoice(inv *bill.Invoice) error {
return validation.ValidateStruct(inv,
validation.Field(&inv.Series, validation.Required),
validation.Field(&inv.Customer,
validation.By(validateInvoiceCustomer),
validation.Skip,
Expand All @@ -33,6 +32,11 @@ func validateInvoice(inv *bill.Invoice) error {
validation.By(validateInvoicePreceding),
validation.Skip,
),
validation.Field(&inv.Tax,
validation.Required,
validation.By(validateInvoiceTax),
validation.Skip,
),
validation.Field(&inv.Lines,
validation.Each(
validation.By(validateInvoiceLine),
Expand All @@ -47,6 +51,19 @@ func validateInvoice(inv *bill.Invoice) error {
)
}

func validateInvoiceTax(val any) error {
obj, ok := val.(*bill.Tax)
if obj == nil || !ok {
return nil
}
return validation.ValidateStruct(obj,
validation.Field(&obj.Ext,
tax.ExtensionsRequires(ExtKeyDocType),
validation.Skip,
),
)
}

func validateInvoiceCustomer(val any) error {
obj, _ := val.(*org.Party)
if obj == nil {
Expand Down
48 changes: 37 additions & 11 deletions addons/es/verifactu/invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -23,9 +22,7 @@ func TestInvoiceValidation(t *testing.T) {
t.Run("missing customer tax ID", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Customer.TaxID = nil
require.NoError(t, inv.Calculate())
err := inv.Validate()
assert.ErrorContains(t, err, "customer: (tax_id: cannot be blank.)")
assertValidationError(t, inv, "customer: (tax_id: cannot be blank.)")
})

t.Run("with exemption reason", func(t *testing.T) {
Expand All @@ -34,17 +31,42 @@ func TestInvoiceValidation(t *testing.T) {
assertValidationError(t, inv, "es-verifactu-tax-classification: required")
})

t.Run("without series", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Series = ""
assertValidationError(t, inv, "series: cannot be blank")
})

t.Run("without notes", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Notes = nil
assertValidationError(t, inv, "notes: with key 'general' missing")
})

t.Run("missing doc type", func(t *testing.T) {
inv := testInvoiceStandard(t)
require.NoError(t, inv.Calculate())
inv.Tax.Ext = nil
err := inv.Validate()
require.ErrorContains(t, err, "es-verifactu-doc-type: required")
})

t.Run("correction invoice requires preceding", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Type = bill.InvoiceTypeCreditNote
assertValidationError(t, inv, "preceding: cannot be blank")
})

t.Run("correction invoice with preceding", func(t *testing.T) {
inv := testInvoiceStandard(t)
inv.Type = bill.InvoiceTypeCreditNote
inv.Tax.Ext[verifactu.ExtKeyDocType] = "R1"
inv.Preceding = []*org.DocumentRef{
{
Series: "ABC",
Code: "122",
Ext: tax.Extensions{
verifactu.ExtKeyDocType: "F1",
},
},
}
require.NoError(t, inv.Calculate())
require.NoError(t, inv.Validate())
})
}

func assertValidationError(t *testing.T, inv *bill.Invoice, expected string) {
Expand All @@ -57,7 +79,6 @@ func testInvoiceStandard(t *testing.T) *bill.Invoice {
t.Helper()
return &bill.Invoice{
Addons: tax.WithAddons(verifactu.V1),
Series: "ABC",
Code: "123",
Supplier: &org.Party{
Name: "Test Supplier",
Expand Down Expand Up @@ -98,5 +119,10 @@ func testInvoiceStandard(t *testing.T) *bill.Invoice {
Text: "This is a test invoice",
},
},
Tax: &bill.Tax{
Ext: tax.Extensions{
verifactu.ExtKeyDocType: "F1",
},
},
}
}
55 changes: 55 additions & 0 deletions addons/es/verifactu/scenarios_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package verifactu

import (
"testing"
)

func TestScenarios(t *testing.T) {
tests := []struct {
name string
scenario string
tags []string
want bool
}{
{
name: "standard invoice",
scenario: "standard",
tags: []string{},
want: true,
},
{
name: "simplified invoice",
scenario: "simplified",
tags: []string{"simplified"},
want: true,
},
{
name: "corrective invoice",
scenario: "corrective",
tags: []string{"corrective"},
want: true,
},
{
name: "invalid scenario",
scenario: "invalid",
tags: []string{},
want: false,
},
{
name: "simplified with wrong tags",
scenario: "simplified",
tags: []string{"corrective"},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateScenario(tt.scenario, tt.tags)

Check failure on line 48 in addons/es/verifactu/scenarios_test.go

View workflow job for this annotation

GitHub Actions / Test

undefined: ValidateScenario

Check failure on line 48 in addons/es/verifactu/scenarios_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: ValidateScenario (typecheck)

Check failure on line 48 in addons/es/verifactu/scenarios_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: ValidateScenario) (typecheck)
if got != tt.want {
t.Errorf("ValidateScenario(%v, %v) = %v, want %v",
tt.scenario, tt.tags, got, tt.want)
}
})
}
}
Loading

0 comments on commit 52b07e5

Please sign in to comment.