Skip to content

Commit

Permalink
Add TransactionSpecification.from_text() constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Apr 19, 2017
1 parent 8dac422 commit 3845aef
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
40 changes: 39 additions & 1 deletion netsgiro/records.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import re
from typing import List, Optional, Sequence, Union
from typing import Iterable, List, Optional, Sequence, Tuple, Union

import attr
from attr.validators import instance_of, optional
Expand Down Expand Up @@ -627,9 +627,47 @@ class TransactionSpecification(TransactionRecord):
]

_MAX_LINES = 42
_MAX_LINE_LENGTH = 80
_MAX_COLUMNS = 2
_MAX_RECORDS = _MAX_LINES * _MAX_COLUMNS

@classmethod
def from_text(
cls, *,
service_code, transaction_type, transaction_number, text
) -> Iterable['TransactionSpecification']:
for line, column, text in (
cls._split_text_to_lines_and_columns(text)):
yield cls(
service_code=service_code,
transaction_type=transaction_type,
transaction_number=transaction_number,

line_number=line,
column_number=column,
text=text,
)

@classmethod
def _split_text_to_lines_and_columns(
cls, text) -> Iterable[Tuple[int, int, str]]:
lines = text.splitlines()

if len(lines) > cls._MAX_LINES:
raise ValueError(
'Max {} specification lines allowed, got {}'
.format(cls._MAX_LINES, len(lines)))

for line_number, line_text in enumerate(lines, 1):
if len(line_text) > cls._MAX_LINE_LENGTH:
raise ValueError(
'Specification lines must be max {} chars long, '
'got {}: {!r}'
.format(cls._MAX_LINE_LENGTH, len(line_text), line_text))

yield (line_number, 1, '{:40}'.format(line_text[0:40]))
yield (line_number, 2, '{:40}'.format(line_text[40:80]))

@classmethod
def to_text(cls, records: Sequence['TransactionSpecification']) -> str:
if len(records) > cls._MAX_RECORDS:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_record_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,34 @@ def test_transaction_specification_for_avtalegiro_payment_request():
)


def test_transaction_specification_from_longer_text():
records = list(netsgiro.TransactionSpecification.from_text(
service_code=netsgiro.ServiceCode.AVTALEGIRO,
transaction_type=(
netsgiro.TransactionType.AVTALEGIRO_WITH_BANK_NOTIFICATION),
transaction_number=1,
text=' Gjelder Faktura: 168837 Dato: 19/03/04\nFoo bar baz quux',
))

assert len(records) == 4
assert records[0].to_ocr() == (
'NY212149000000140011 Gjelder Faktura: 16'
'8837 Dato: 19/03/0400000000000000000000'
)
assert records[1].to_ocr() == (
'NY212149000000140012 '
' 00000000000000000000'
)
assert records[2].to_ocr() == (
'NY212149000000140021Foo bar baz quux '
' 00000000000000000000'
)
assert records[3].to_ocr() == (
'NY212149000000140022 '
' 00000000000000000000'
)


def test_avtalegiro_agreements():
record = netsgiro.AvtaleGiroAgreement(
service_code=netsgiro.ServiceCode.AVTALEGIRO,
Expand Down

0 comments on commit 3845aef

Please sign in to comment.