Skip to content

Commit

Permalink
Save example output and compare
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Jul 24, 2023
1 parent fc2c01a commit 6617882
Show file tree
Hide file tree
Showing 14 changed files with 1,497 additions and 7 deletions.
45 changes: 38 additions & 7 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gobl_test

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
Expand All @@ -10,6 +11,7 @@ import (
"testing"

"github.com/invopop/gobl"
"github.com/invopop/gobl/uuid"
"github.com/invopop/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -18,9 +20,12 @@ import (
var skipExamplePaths = []string{
"build/",
".out.",
"/out/",
".github",
}

var updateExamples = flag.Bool("update", false, "Update the examples in the repository")

// TestConvertExamplesToJSON finds all of the `.json` and `.yaml` files in the
// package and attempts to convert the to JSON Envelopes.
func TestConvertExamplesToJSON(t *testing.T) {
Expand Down Expand Up @@ -77,20 +82,46 @@ func processFile(t *testing.T, path string) error {
}
}

if err := env.Sign(testKey); err != nil {
return fmt.Errorf("failed to sign the doc: %w", err)
// override the UUID
env.Head.UUID = uuid.MustParse("8a51fd30-2a27-11ee-be56-0242ac120002")

if err := env.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}

// Output to the filesystem (.out.json is defined in .gitignore)
np := strings.TrimSuffix(path, filepath.Ext(path)) + ".out.json"
// Output to the filesystem in the /out/ directory
out, err := json.MarshalIndent(env, "", " ")
if err != nil {
return fmt.Errorf("marshalling output: %w", err)
}
if err := ioutil.WriteFile(np, out, 0644); err != nil {
return fmt.Errorf("saving file data: %w", err)

dir := filepath.Join(filepath.Dir(path), "out")
of := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) + ".json"
np := filepath.Join(dir, of)
if _, err := os.Stat(np); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("checking file: %s: %w", np, err)
}
if !*updateExamples {
return fmt.Errorf("output file missing, run tests with `--update` flag to create")
}
}

if *updateExamples {
if err := ioutil.WriteFile(np, out, 0644); err != nil {
return fmt.Errorf("saving file data: %w", err)
}
t.Logf("wrote file: %v", np)
} else {
// Compare to existing file
existing, err := ioutil.ReadFile(np)
if err != nil {
return fmt.Errorf("reading existing file: %w", err)
}
t.Run(np, func(t *testing.T) {
assert.JSONEq(t, string(existing), string(out), "output file does not match, run tests with `--update` flag to update")
})
}

t.Logf("wrote file: %v", np)
return nil
}
16 changes: 16 additions & 0 deletions note/examples/out/message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "45ac3115c8569a1789e58af8d0dc91ef3baa1fb71daaf38f5aef94f82b4d0033"
},
"draft": true
},
"doc": {
"$schema": "https://gobl.org/draft-0/note/message",
"title": "Test Message",
"content": "We hope you like this test message!"
}
}
108 changes: 108 additions & 0 deletions regimes/co/examples/out/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "5fff065413fac95076f9ee278ee219b351da74f6251244aa7accc6f6a57ae471"
},
"draft": true
},
"doc": {
"$schema": "https://gobl.org/draft-0/bill/invoice",
"series": "SETT",
"code": "1234",
"type": "standard",
"currency": "COP",
"issue_date": "2021-01-01",
"supplier": {
"name": "EXAMPLE SUPPLIER S.A.S.",
"tax_id": {
"country": "CO",
"zone": "11001",
"type": "tin",
"code": "9014514812"
}
},
"customer": {
"name": "EXAMPLE CUSTOMER S.A.S.",
"tax_id": {
"country": "CO",
"zone": "11001",
"type": "tin",
"code": "9014514805"
},
"addresses": [
{
"street": "CRA 8 113 31 OF 703",
"locality": "Bogotá, D.C.",
"region": "Bogotá",
"country": "CO"
}
],
"emails": [
{
"addr": "[email protected]"
}
],
"telephones": [
{
"num": "3114131811"
}
]
},
"lines": [
{
"i": 1,
"quantity": "1",
"item": {
"name": "Servicios Mes de Julio 2022",
"price": "200000.00"
},
"sum": "200000.00",
"taxes": [
{
"cat": "VAT",
"percent": "19%"
}
],
"total": "200000.00"
}
],
"payment": {
"terms": {
"key": "",
"due_dates": [
{
"date": "2021-01-01",
"amount": "238000.00",
"percent": "100%"
}
]
}
},
"totals": {
"sum": "200000.00",
"total": "200000.00",
"taxes": {
"categories": [
{
"code": "VAT",
"rates": [
{
"base": "200000.00",
"percent": "19%",
"amount": "38000.00"
}
],
"amount": "38000.00"
}
],
"sum": "38000.00"
},
"tax": "38000.00",
"total_with_tax": "238000.00",
"payable": "238000.00"
}
}
}
137 changes: 137 additions & 0 deletions regimes/es/examples/out/invoice-es-es-freelance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"$schema": "https://gobl.org/draft-0/envelope",
"head": {
"uuid": "8a51fd30-2a27-11ee-be56-0242ac120002",
"dig": {
"alg": "sha256",
"val": "f1d54d434ba07ddd90f33d711f6a6725a80228f80cd831f11fcaa21357796571"
},
"draft": true
},
"doc": {
"$schema": "https://gobl.org/draft-0/bill/invoice",
"code": "SAMPLE-001",
"type": "standard",
"currency": "EUR",
"issue_date": "2022-02-01",
"supplier": {
"name": "MªF. Services",
"tax_id": {
"country": "ES",
"code": "58384285G"
},
"people": [
{
"name": {
"given": "MARIA FRANCISCA",
"surname": "MONTERO",
"surname2": "ESTEBAN"
}
}
],
"addresses": [
{
"num": "9",
"street": "CAMÍ MADRID",
"locality": "CANENA",
"region": "JAÉN",
"code": "23480",
"country": "ES"
}
],
"emails": [
{
"addr": "[email protected]"
}
]
},
"customer": {
"name": "Sample Consumer",
"tax_id": {
"country": "ES",
"code": "54387763P"
}
},
"lines": [
{
"i": 1,
"quantity": "20",
"item": {
"name": "Development services",
"price": "90.00",
"unit": "h"
},
"sum": "1800.00",
"discounts": [
{
"percent": "10%",
"amount": "180.00",
"reason": "Special discount"
}
],
"taxes": [
{
"cat": "VAT",
"rate": "standard",
"percent": "21.0%"
},
{
"cat": "IRPF",
"percent": "15.0%"
}
],
"total": "1620.00"
}
],
"payment": {
"terms": {
"key": "instant"
},
"instructions": {
"key": "credit-transfer",
"credit_transfer": [
{
"iban": "ES06 0128 0011 3901 0008 1391",
"name": "Bankinter"
}
]
}
},
"totals": {
"sum": "1620.00",
"total": "1620.00",
"taxes": {
"categories": [
{
"code": "VAT",
"rates": [
{
"key": "standard",
"base": "1620.00",
"percent": "21.0%",
"amount": "340.20"
}
],
"amount": "340.20"
},
{
"code": "IRPF",
"retained": true,
"rates": [
{
"base": "1620.00",
"percent": "15.0%",
"amount": "243.00"
}
],
"amount": "243.00"
}
],
"sum": "97.20"
},
"tax": "97.20",
"total_with_tax": "1717.20",
"payable": "1717.20"
}
}
}
Loading

0 comments on commit 6617882

Please sign in to comment.