def test_faulty_interchange_messages():
    i = Interchange.from_str("UNB+UNOC:1+1234+3333+200102:2212+42'"
                             "UNH+42z42+PAORES:93:1:IA'"
                             "UNH+42z42+PAORES:93:1:IA'"
                             "UNZ+2+42'")

    with pytest.raises(SyntaxError):
        list(i.get_messages())

    i = Interchange.from_str("UNB+UNOC:1+1234+3333+200102:2212+42'"
                             "UNT+42z42+0'"
                             "UNZ+2+42'")

    with pytest.raises(SyntaxError):
        list(i.get_messages())
def test_faulty_interchange__UNT_without_UNH():
    """creates a message with an cloding UNT, without UNH"""
    i = Interchange.from_str(
        "UNB+UNOC:1+1234+3333+200102:2212+42'" "UNT+2+42z42'" "UNZ+2+42'"
    )

    with pytest.raises(EDISyntaxError):
        list(i.get_messages())
def test_faulty_interchange__UNH_not_closed():
    """creates a message with an opening UNH message, without closing UNT"""
    i = Interchange.from_str(
        "UNB+UNOC:1+1234+3333+200102:2212+42'" "UNH+42z42+PAORES:93:1:IA'" "UNZ+2+42'"
    )

    with pytest.raises(EDISyntaxError):
        list(i.get_messages())
def test_interchange_messages_from_str():
    i = Interchange.from_str("UNB+UNOC:1+1234+3333+200102:2212+42'"
                             "UNH+42z42+PAORES:93:1:IA'"
                             "UNT+42z42+0'"
                             "UNZ+2+42'")
    assert str(i) == ("UNB+UNOC:1+1234+3333+200102:2212+42'"
                      "UNH+42z42+PAORES:93:1:IA'"
                      "UNT+42z42+0'"
                      "UNZ+2+42'")
def test_counting_of_messages(interchange, message):
    edi_str = (
        "UNB+UNOC:1+1234+3333+200102:2212+42'"
        "UNH+42z42+PAORES:93:1:IA'"
        "UNT+2+42z42'"
        "UNH+42z43+PAORES:93:1:IA'"
        "UNT+2+42z43'"
        "UNZ+2+42'"
    )
    i = Interchange.from_str(edi_str)
    assert i.serialize() == edi_str
def test_faulty_interchange__nested_UNH_not_closed():
    """creates a message with 2 nested UNH, one of them not closed"""
    i = Interchange.from_str(
        "UNB+UNOC:1+1234+3333+200102:2212+42'"
        "UNH+42z42+PAORES:93:1:IA'"
        "UNH+42z42+PAORES:93:1:IA'"
        "UNZ+2+42'"
    )

    with pytest.raises(EDISyntaxError):
        list(i.get_messages())
def test_interchange_from_str_multi_messages():
    i = Interchange.from_str(
        "UNB+UNOC:1+1234+3333+200102:2212+42'"
        "UNH+42z42+PAORES:93:1:IA'"
        "UNT+2+42z42'"
        "UNH+43z43+PAORES:93:1:IA'"
        "UNT+2+43z43'"
        "UNZ+2+42'"
    )

    assert len(list(i.get_messages()))
def test_empty_interchange_from_str():
    i = Interchange.from_str("UNB+UNOC:1+1234+3333+200102:2212+42'" "UNZ+0+42'")
    assert str(i) == ("UNB+UNOC:1+1234+3333+200102:2212+42'" "UNZ+0+42'")