This document describes the testing infrastructure for the gobl.ubl conversion library, including test structure, helper functions, XSD validation, and golden file management. For information about example documents used in tests, see Example Documents.
The test framework is organized around two primary conversion directions, with dedicated test data directories for each:
Sources: examples_test.go279-305
The test suite includes two primary test functions that verify bidirectional conversion:
Tests GOBL→UBL conversion by processing all JSON files in test/data/convert/:
| Function | Location | Purpose |
|---|---|---|
TestConvertToInvoice | examples_test.go38-77 | Iterates through JSON examples, converts to UBL, validates against XSD |
testInvoiceFrom | examples_test.go148-154 | Loads GOBL envelope and converts to UBL Invoice |
loadTestEnvelope | examples_test.go192-222 | Reads and validates GOBL JSON file |
The test performs the following steps:
Sources: examples_test.go38-77 examples_test.go148-154 examples_test.go192-222
Tests UBL→GOBL parsing by processing all XML files in test/data/parse/:
| Function | Location | Purpose |
|---|---|---|
TestParseInvoice | examples_test.go79-145 | Iterates through XML examples, converts to GOBL, compares output |
testParseInvoice | examples_test.go173-189 | Parses UBL XML and converts to GOBL envelope |
testLoadXML | examples_test.go157-169 | Loads raw XML data from file |
The parsing flow follows this sequence:
Sources: examples_test.go79-145 examples_test.go173-189 examples_test.go157-169
The test framework validates generated UBL XML against official XSD schemas to ensure compliance with UBL 2.1 specifications:
Located at examples_test.go264-277 this function uses libxml2 to perform XSD validation:
The validation process:
libxml2 documentSchema files are loaded once per test run using loadSchema:
| Schema File | Document Type | Location |
|---|---|---|
UBL-Invoice-2.1.xsd | Invoice | examples_test.go29 |
UBL-CreditNote-2.1.xsd | CreditNote | examples_test.go30 |
Schemas are loaded from examples_test.go283-285:
test/data/schema/maindoc/UBL-Invoice-2.1.xsd
test/data/schema/maindoc/UBL-CreditNote-2.1.xsd
Sources: examples_test.go257-277 examples_test.go283-285
The test framework uses a golden file approach where expected outputs are stored and compared against actual conversion results:
The --update flag at examples_test.go36 enables automatic regeneration of golden files:
When enabled, the flag triggers:
test/data/convert/out/ and validate against XSDtest/data/parse/out/The writeEnvelope function at examples_test.go234-245 conditionally writes files:
Sources: examples_test.go36 examples_test.go234-245
To ensure reproducible test outputs, the framework uses a static UUID defined at examples_test.go32:
staticUUID = "0195ce71-dc9c-72c8-bf2c-9890a4a9f0a2"
This UUID is applied in two places:
env.Head.UUIDinv.UUIDBefore final output, env.Calculate() is called to recalculate the digest with the static UUID, ensuring:
Sources: examples_test.go32 examples_test.go205-212
Beyond the main conversion tests, component-specific test files verify individual conversion functions:
Located in lines_parse_test.go these tests verify:
| Test Case | File | Focus |
|---|---|---|
TestParseLines/ubl-example1.xml | lines_parse_test.go19-46 | Basic line parsing, quantity, price, taxes |
TestParseLines/ubl-example2.xml | lines_parse_test.go49-102 | Line charges/discounts, item identities, metadata |
TestParseLines/partial-invoice.xml | lines_parse_test.go105-123 | OrderLineReference parsing |
TestParseLines/BaseQuantity | lines_parse_test.go126-161 | BaseQuantity price division |
Sources: lines_parse_test.go1-162
Located in charges_parse_test.go these tests verify:
| Test Case | File | Focus |
|---|---|---|
TestParseCharges/ubl-example2.xml | charges_parse_test.go16-49 | Document-level charges/discounts, tax categories |
TestParseCharges/ubl-example5.xml | charges_parse_test.go51-86 | UNTDID extension codes for charges/allowances |
TestParseCharges/Allowance-example.xml | charges_parse_test.go88-131 | BaseAmount and percentage-based calculations |
TestBaseAmountErrorHandling | charges_parse_test.go134-154 | Error handling for invalid BaseAmount values |
The error handling test demonstrates robustness by modifying valid XML and verifying proper error propagation:
Sources: charges_parse_test.go1-155
Execute the complete test suite:
Generate coverage report:
Run individual test functions:
Regenerate expected output files:
This command will:
test/data/*/out/Sources: README.md131-139
The framework provides several utility functions for test data management:
| Function | Purpose | Returns |
|---|---|---|
getDataPath | examples_test.go291-293 | Base test data directory |
getConversionTypePath | examples_test.go295-300 | Conversion-specific directory (convert/parse) |
getOutPath | examples_test.go287-289 | Output directory for golden files |
getSchemaPath | examples_test.go283-285 | XSD schema directory |
getRootFolder | examples_test.go307-314 | Repository root by finding go.mod |
| Function | Purpose | Location |
|---|---|---|
loadOutputFile | Load golden file for comparison | examples_test.go224-232 |
getDataGlob | Get all files matching pattern | examples_test.go279-281 |
outputFilepath | Determine output path for file | examples_test.go247-255 |
Sources: examples_test.go224-332
The test framework uses testify for assertions, providing clear failure messages:
| Pattern | Usage | Example |
|---|---|---|
require.NoError | Critical errors that stop test | lines_parse_test.go21 |
assert.NoError | Non-critical errors | examples_test.go73 |
assert.Equal | Value comparison | lines_parse_test.go32 |
assert.Len | Collection size | lines_parse_test.go29 |
assert.NotNil | Nil checks | lines_parse_test.go27 |
assert.JSONEq | JSON comparison ignoring whitespace | examples_test.go142 |
A common pattern for safely extracting types:
Sources: lines_parse_test.go23-24 charges_parse_test.go20-21
While not explicitly implemented as a separate test, the framework supports roundtrip verification through sequential application of conversion and parsing tests:
The equivalence is verified through:
Calculate() ensures consistent digestsassert.JSONEqSources: examples_test.go106-114
Refresh this wiki