Commit a158e679 authored by Robin Obůrka's avatar Robin Obůrka

Add infrastructure for tests and first wave of test for parsing function

parent b4fd22bd
import sys
sys.path.append("..")
from .fixtures import *
import pytest
from .fixtures import *
import pytest
import msgpack
@pytest.fixture
def good_type():
return "sentinel/tests/good_string"
@pytest.fixture
def good_dict():
return {
"key1": "val1",
"key2": 2,
"key3": True,
"key4": "ěščřžýáíé",
}
@pytest.fixture
def good_msg(good_type, good_dict):
t = bytes(good_type, encoding="UTF-8")
p = msgpack.packb(good_dict, encoding="UTF-8")
return (t, p)
@pytest.fixture
def good_long(good_type, good_dict):
t = bytes(good_type, encoding="UTF-8")
p = msgpack.packb(good_dict, encoding="UTF-8")
x = msgpack.packb(good_dict, encoding="UTF-8")
return (t, p, x)
@pytest.fixture(params=[1, 5, 7, 9, 12, 15, 18, 20])
def broken_msg(request, good_msg):
# Parameters are tested by eye, but it generates several different
# exception from msgpack. So, the test should be sufficient
pos = request.param
# Swap "random" bytes
ba = bytearray(good_msg[1])
ba[0], ba[pos] = ba[pos], ba[0]
msg = bytes(ba)
return (good_msg[0], msg)
@pytest.fixture(params=[
"sentinel/tests/goodstring",
"sentinel/tests/good/string",
"sentinel/tests/good_string",
"sentinel/tests/good_string_123",
"sentinel/tests/box1",
"sentinel/tests/box/1",
"s/t/b",
"a/b/c/s/d/g",
])
def good_type_msg(request, good_msg):
t = bytes(request.param, encoding="UTF-8")
return (t, good_msg[1])
@pytest.fixture(params=[
"/sentinel/tests/broken",
"sentinel/tests/broken/",
"/sentinel/tests/broken/",
"sentinel/tests/bro ken",
"sentinel/tests/bro-ken",
"sentinel/tests/Broken",
"sentinel/tests/břoken",
"sentinel/tests/bro?ken",
"sentinel/tests/bro@ken",
"sentinel//broken",
"s//b",
])
def broken_type_msg(request, good_msg):
t = bytes(request.param, encoding="UTF-8")
return (t, good_msg[1])
import sn
def test_encoding(good_type, good_dict, good_msg):
encoded = sn.encode_msg(good_type, good_dict)
assert encoded == good_msg
import pytest
import sn
def test_parse(good_type, good_dict, good_msg):
(t, p) = sn.parse_msg(good_msg)
assert t == good_type
assert p == good_dict
def test_longer(good_type, good_dict, good_long):
(t, p) = sn.parse_msg(good_long)
assert t == good_type
assert p == good_dict
def test_missing_type(good_msg):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(good_msg[1])
def test_missing_payload(good_msg):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(good_msg[0])
def test_broken_payload(broken_msg):
with pytest.raises(sn.InvalidMsgError):
(t, p) = sn.parse_msg(broken_msg)
def test_good_type(request, good_type_msg):
# I mean: "Not raise any exception"
(t, p) = sn.parse_msg(good_type_msg)
def test_invalid_type(broken_type_msg):
with pytest.raises(sn.InvalidMsgTypeError):
(t, p) = sn.parse_msg(broken_type_msg)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment