]> git.ipfire.org Git - thirdparty/python-fints.git/commitdiff
Do not ignore tests
authorHenryk Plötz <henryk@ploetzli.ch>
Mon, 6 Aug 2018 18:14:09 +0000 (20:14 +0200)
committerRaphael Michel <mail@raphaelmichel.de>
Mon, 3 Dec 2018 18:34:17 +0000 (19:34 +0100)
.gitignore
tests/test_formals.py [new file with mode: 0644]
tests/test_message_parser.py [new file with mode: 0644]
tests/test_models.py [new file with mode: 0644]

index 92e06a1b88dfa721b5f8f45a3cb6bbd36e3ede47..1617022d3e41635c116872318f7e00a158822efc 100644 (file)
@@ -5,4 +5,5 @@ dist/
 env
 .idea/
 test*.py
+!tests
 *.pyc
diff --git a/tests/test_formals.py b/tests/test_formals.py
new file mode 100644 (file)
index 0000000..19fa5e9
--- /dev/null
@@ -0,0 +1,264 @@
+import pytest
+from fints.formals import Container, ContainerField, DataElementField, DataElementGroupField, DigitsField, NumericField, Field, SegmentSequence
+
+def test_container_simple():
+    class A(Container):
+        a = DataElementField(type='an')
+        b = DigitsField()
+
+    i1 = A()
+    assert isinstance(i1, A)
+    assert hasattr(i1, 'a')
+    assert i1.a is None
+
+    i1.a = 'abc123'
+    i1.b = '007'
+    assert i1.a == 'abc123'
+    assert i1.b == '007'
+
+    i2 = A(b='0815', a='bcd234')
+
+    assert isinstance(i1, A)
+    assert i2.a == 'bcd234'
+    assert i2.b == '0815'
+
+    with pytest.raises(TypeError):
+        A(b='a')
+
+    del i2.b
+    assert i2.b is None
+
+def test_container_nested():
+    class A(Container):
+        a = NumericField()
+
+    class B(Container):
+        b = DataElementGroupField(type=A)
+
+    i1 = B()
+    assert isinstance(i1.b, A)
+
+    i1.b.a = '1'
+    assert i1.b.a == 1
+
+def test_container_count_exact():
+    class A(Container):
+        a = NumericField(count=2)
+
+    i1 = A()
+
+    assert len(i1.a) == 2
+
+    assert i1.a[0] is None
+    assert i1.a[1] is None
+
+    with pytest.raises(IndexError):
+        i1.a[2]
+
+    with pytest.raises(IndexError):
+        i1.a[2] = 1
+
+    with pytest.raises(IndexError):
+        i1.a[-1]
+
+    with pytest.raises(IndexError):
+        i1.a[-1] = 1
+
+    i1.a[0] = '3'
+    i1.a[1] = 4
+
+    with pytest.raises(TypeError):
+        i1.a[1] = '05'
+
+    assert i1.a[0] == 3
+    assert i1.a[1] == 4
+
+    assert len(i1.a) == 2
+
+    del i1.a[1]
+    assert i1.a[1] is None
+
+def test_container_count_variable_1():
+    class A(Container):
+        a = NumericField(min_count=2, max_count=5)
+
+    i1 = A()
+
+    assert len(i1.a) == 2
+
+    assert i1.a[0] is None
+    assert i1.a[1] is None
+    assert i1.a[3] is None
+
+    assert len(i1.a) == 4
+
+    with pytest.raises(IndexError):
+        i1.a[5]
+
+    with pytest.raises(IndexError):
+        i1.a[5] = 1
+
+    with pytest.raises(IndexError):
+        i1.a[-1]
+
+    i1.a[0] = '3'
+    i1.a[1] = 4
+    i1.a[3] = 17
+
+    assert i1.a[0] == 3
+    assert i1.a[1] == 4
+    assert i1.a[2] is None
+    assert i1.a[3] == 17
+
+    assert len(i1.a) == 4
+
+    assert i1.a[4] is None
+
+    assert len(i1.a) == 5
+
+def test_container_count_variable_2():
+    class A(Container):
+        a = NumericField(min_count=2)
+
+    i1 = A()
+
+    assert len(i1.a) == 2
+
+def test_container_count_variable_3():
+    class A(Container):
+        a = NumericField(max_count=4)
+
+    i1 = A()
+
+    assert len(i1.a) == 0
+
+def test_container_count_variable_iter():
+    class A(Container):
+        a = NumericField(max_count=4)
+
+    i1 = A()
+
+    assert len(i1.a) == 0
+
+    assert list(i1.a) == []
+
+    i1.a[2] = '3'
+
+    assert list(i1.a) == [None, None, 3]
+
+    assert len(i1.a) == 3
+
+def test_container_init():
+    class A(Container):
+        a = NumericField()
+        b = DigitsField()
+
+    i1 = A(a='3', b='04')
+
+    assert i1.a == 3
+    assert i1.b == '04'
+
+def test_container_nested_init():
+    class A(Container):
+        a = NumericField()
+
+    class B(Container):
+        b = DataElementGroupField(type=A)
+
+    i1 = A(a='5')
+    i2 = B(b=i1)
+
+    assert isinstance(i2.b, A)
+    assert i2.b.a == 5
+
+    class C(Container):
+        c = DataElementGroupField(type=B)
+
+    with pytest.raises(TypeError):
+        C(c=i1)
+
+    i3 = B()
+    assert i3.b.a is None
+
+def test_container_count_init():
+    class A(Container):
+        a = NumericField(count=4)
+
+    i1 = A(a=[None, '23'])
+
+    assert i1.a[0] is None
+    assert i1.a[1] == 23
+
+    assert len(i1.a) == 4
+
+def test_container_count_variable_init():
+    class A(Container):
+        a = NumericField(max_count=4)
+
+    i1 = A(a=[None, '23'])
+
+    assert i1.a[0] is None
+    assert i1.a[1] == 23
+
+    assert len(i1.a) == 2
+
+
+def test_container_naive_parse():
+    class A(Container):
+        a = NumericField()
+        b = DigitsField()
+
+    i1 = A.naive_parse(['23', '42'])
+
+    assert i1.a == 23
+    assert i1.b == '42'
+
+def test_field_flat_length():
+    class A(Container):
+        a = NumericField()
+
+    class B(Container):
+        b1 = DataElementGroupField(type=A)
+        b2 = DataElementGroupField(type=A, count=3)
+        b3 = DataElementGroupField(type=A, min_count=2)
+
+    assert A._fields['a'].flat_length == 1
+    assert B._fields['b1'].flat_length == 1
+    assert B._fields['b2'].flat_length == 1
+
+    class C(Container):
+        c1 = DataElementGroupField(type=A)
+        c2 = DataElementGroupField(type=A, count=2)
+        c3 = DataElementGroupField(type=A, count=3)
+
+    class D(Container):
+        d1 = DataElementGroupField(type=C, count=4)
+
+    class E(Container):
+        e1 = DataElementGroupField(type=D)
+        e2 = DataElementGroupField(type=B)
+
+    assert E._fields['e1'].flat_length == 4*(1+2+3)
+
+    with pytest.raises(TypeError):
+        E._fields['e2'].flat_length
+
+def test_invalid_spec():
+    with pytest.raises(ValueError):
+        class A(Container):
+            a = NumericField(length=3, max_length=5)
+
+    with pytest.raises(ValueError):
+        class A(Container):
+            a = NumericField(count=3, min_count=5)
+
+    with pytest.raises(NotImplementedError):
+        class A(Container):
+            a = Field()
+
+        A(a=123)
+
+def test_sequence_repr():
+    s = SegmentSequence()
+
+    assert repr(s) == 'SegmentSequence([])'
diff --git a/tests/test_message_parser.py b/tests/test_message_parser.py
new file mode 100644 (file)
index 0000000..a5b85d8
--- /dev/null
@@ -0,0 +1,57 @@
+from fints.message import FinTSResponse
+from fints.parser import FinTS3Parser
+#from fints.segments.message import HNHBK
+import pytest
+
+from conftest import COMPLICATED_EXAMPLE, SIMPLE_EXAMPLE
+
+def test_basic_message():
+    segments = FinTS3Parser.explode_segments(SIMPLE_EXAMPLE)
+    assert len(segments) == 4
+
+def test_explode_complicated():
+    segments = FinTS3Parser.explode_segments(COMPLICATED_EXAMPLE)
+    assert len(segments) == 4
+
+def test_legacy_response():
+    data = SIMPLE_EXAMPLE
+    m = FinTSResponse(data)
+    assert len(m.segments) == 4
+    assert len(m.payload) == 3
+
+def test_parse_simple():
+    m = FinTS3Parser().parse_message(SIMPLE_EXAMPLE)
+    assert len(m.segments) == 4
+    assert m.segments[0].__class__.__name__ == "HNHBK3"
+    m.print_nested()
+
+def test_parse_complicated():
+    m = FinTS3Parser().parse_message(COMPLICATED_EXAMPLE)
+    assert len(m.segments) == 4
+    assert m.segments[3].__class__.__name__ == "HNHBS1"
+
+def test_invalid():
+    message1 = rb"""12"""
+
+    with pytest.raises(ValueError):
+        FinTS3Parser.explode_segments(message1)
+
+    message2 = rb"""@2@12ab'"""
+
+    with pytest.raises(ValueError):
+        FinTS3Parser.explode_segments(message2)
+
+    message3 = rb"""ab@2@12'"""
+
+    with pytest.raises(ValueError):
+        FinTS3Parser.explode_segments(message3)
+
+    message4 = rb"""ab@@'"""
+
+    with pytest.raises(ValueError):
+        FinTS3Parser.explode_segments(message4)
+
+    message5 = rb"""@2@12ab"""
+
+    with pytest.raises(ValueError):
+        FinTS3Parser.explode_segments(message5)
diff --git a/tests/test_models.py b/tests/test_models.py
new file mode 100644 (file)
index 0000000..f0e5569
--- /dev/null
@@ -0,0 +1,81 @@
+import pytest
+from fints.segments import FinTS3Segment, HNHBK3, HNHBS1
+from fints.formals import SegmentHeader, DataElementField, GenericField, NumericField, AlphanumericField
+
+def test_metaclass_foo():
+    a = HNHBK3()
+
+    assert list(a._fields) == ['header', 'message_size', 'hbci_version', 'dialogue_id', 'message_number', 'reference_message']
+    assert a._fields['header']
+
+def test_descriptor_subclassing():
+    a = DataElementField(type='an')
+    assert isinstance(a, AlphanumericField)
+
+    b = AlphanumericField()
+    assert b.type == 'an'
+
+    c = DataElementField(type='fnord')
+    assert isinstance(c, GenericField)
+
+def test_descriptor_in_use():
+    assert isinstance(HNHBS1._fields['message_number'], NumericField)
+
+def test_data_element_constructor():
+    a = HNHBS1(message_number='4')
+
+    assert a.message_number == 4
+
+def test_fints3_constructor_auto_segmentheader():
+    a = HNHBS1(7)
+
+    assert a.message_number == 7
+
+def test_data_element_constructor_double_arg():
+    with pytest.raises(TypeError):
+        HNHBS1(8, message_number='9')
+
+def test_data_element_setgetdel():
+    a = HNHBS1()
+
+    a.message_number = '5'
+    assert a.message_number == 5
+
+    a.message_number = 6
+    assert a.message_number == 6
+
+    del a.message_number
+    assert not a.message_number
+
+def test_descriptors_independent():
+    a = HNHBK3()
+    b = HNHBK3()
+
+    a.hbci_version = '300'
+
+    assert a.hbci_version
+    assert not b.hbci_version
+
+def test_parse_container():
+    a = ['HNHBS', '2', '1', ]
+
+    h = SegmentHeader.naive_parse(a)
+
+    assert h.type == 'HNHBS'
+    assert h.version == 1
+    assert h.reference is None
+
+def test_segment_class_type_version():
+    assert HNHBS1.TYPE == 'HNHBS'
+    assert HNHBS1.VERSION == 1
+
+    assert HNHBK3.VERSION == 3
+
+def test_find_subclass():
+    a = [
+        ['HNHBS', '2', '1', ],
+        '3'
+    ]
+
+    clazz = FinTS3Segment.find_subclass(a)
+    assert clazz is HNHBS1