Coverage for jbank/management/commands/test_pain001.py : 47%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import logging
2import subprocess
3from decimal import Decimal
4from os.path import join
5from django.conf import settings
6from django.core.management.base import CommandParser
7from jbank.sepa import Pain001
8from jutil.command import SafeCommand
9from jutil.format import format_xml, format_xml_bytes
10from jutil.validators import iban_bic
13logger = logging.getLogger(__name__)
16class Command(SafeCommand):
17 help = """
18 Generates pain.001.001.03 compatible SEPA payment file.
19 """
21 def add_arguments(self, parser: CommandParser):
22 parser.add_argument("--verbose", action="store_true")
23 parser.add_argument("--validate", action="store_true")
25 def do(self, *args, **options):
26 debtor_acc = "FI4947300010416310"
27 p = Pain001(
28 "201802071211XJANITEST",
29 "Kajala Group Oy",
30 debtor_acc,
31 iban_bic(debtor_acc),
32 "020840699",
33 ["Koukkukankareentie 29", "20320 Turku"],
34 "FI",
35 )
36 creditor_acc = "FI8847304720017517"
37 p.add_payment(
38 "201802071339A0001", "Jani Kajala", creditor_acc, iban_bic(creditor_acc), Decimal("49.00"), "vuokratilitys"
39 )
40 xml_str = format_xml_bytes(p.render_to_bytes()).decode()
41 print(xml_str)
43 filename = "/tmp/pain001.xml"
44 with open(filename, "wt") as fp:
45 fp.write(xml_str)
46 print(filename, "written")
48 if options["validate"]:
49 # /usr/bin/xmllint --format --pretty 1 --load-trace --debug --schema $1 $2
50 res = subprocess.run(
51 [
52 "/usr/bin/xmllint",
53 "--format",
54 "--pretty",
55 "1",
56 "--load-trace",
57 "--debug",
58 "--schema",
59 join(settings.BASE_DIR, "data/pain001/pain.001.001.03.xsd"),
60 filename,
61 ]
62 )
63 if res.returncode == 0:
64 print("OK")
65 else:
66 print("FAIL")