44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from pyinfra.parser.blob_parser import ParsingError
|
|
from pyinfra.parser.parser_composer import EitherParserComposer
|
|
from pyinfra.parser.parsers.identity import IdentityBlobParser
|
|
from pyinfra.parser.parsers.json import JsonBlobParser
|
|
from pyinfra.parser.parsers.string import StringBlobParser
|
|
from pyinfra.server.packing import bytes_to_string
|
|
|
|
|
|
def test_json_parser():
|
|
d = {"data": bytes_to_string(b"aa")}
|
|
assert JsonBlobParser()(json.dumps(d).encode()) == {"data": b"aa"}
|
|
|
|
|
|
def test_string_parser():
|
|
a = "a"
|
|
assert StringBlobParser()(a.encode()) == a
|
|
|
|
|
|
def test_identity_parser():
|
|
a = "a"
|
|
assert IdentityBlobParser()(a.encode()) == a.encode()
|
|
|
|
|
|
def test_either_parser_composer():
|
|
parser = EitherParserComposer(JsonBlobParser(), StringBlobParser(), IdentityBlobParser())
|
|
|
|
d = {"data": bytes_to_string(b"aa")}
|
|
assert parser(json.dumps(d).encode()) == {"data": b"aa"}
|
|
|
|
a = "a"
|
|
assert parser(a.encode()) == a
|
|
|
|
a = 1
|
|
assert parser(a) == a
|
|
|
|
parser = EitherParserComposer(JsonBlobParser(), StringBlobParser())
|
|
|
|
with pytest.raises(ParsingError):
|
|
parser(1)
|