def test_from_bytes_date_already_set(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(d).set_type('some_type').set_version('1.0')) b = h.to_bytes() with pytest.raises(AssertionError): Header().set_date(d).from_bytes(b)
def test_from_bytes_nothing_set_yet(self): h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(datetime.strptime( '1990-04-27', '%Y-%m-%d')).set_type('some_type').set_version('1.0')) b = h.to_bytes() # Load into a header with nothing yet set new_h = Header().from_bytes(b) assert h.to_dict() == new_h.to_dict()
def test_encode_passed_Header(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(d).set_type('some_type').set_version('1.0')) o = Header.encode(h) assert o[Header.name()] assert o['_id'] == h.get_id() assert o['_username'] == h.get_username() assert o['_session'] == h.get_session() assert o['_date'] == h.get_date() assert o['_type'] == h.get_type() assert o['_version'] == h.get_version()
def test_decode_passed_encoded_Header(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(d).set_type('some_type').set_version('1.0')) encoded = {} encoded[Header.name()] = True encoded['_id'] = h.get_id() encoded['_username'] = h.get_username() encoded['_session'] = h.get_session() encoded['_date'] = h.get_date() encoded['_type'] = h.get_type() encoded['_version'] = h.get_version() new_h = Header.decode(encoded) assert new_h.to_dict() == h.to_dict()
def test_decode_passed_encoded_datetime(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') encoded = { '__datetime__': True, 's': d.strftime('%Y%m%dT%H:%M:%S.%f'), } new_d = Header.decode(encoded) assert new_d == d
def test_to_bytes_version_not_set_yet(self): h = (Header().set_id('12345').set_username( 'senkwich').set_session('mysession').set_date( datetime.strptime('1990-04-27', '%Y-%m-%d')).set_type('some_type')) with pytest.raises(AssertionError): h.to_bytes()
def test_to_bytes_everything_already_set(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(d).set_type('some_type').set_version('1.0')) b = h.to_bytes() expected = {} expected[Header.name()] = True expected['_id'] = h.get_id() expected['_username'] = h.get_username() expected['_session'] = h.get_session() expected['_date'] = { '__datetime__': True, 's': d.strftime('%Y%m%dT%H:%M:%S.%f'), } expected['_type'] = h.get_type() expected['_version'] = h.get_version() expected = msgpack.packb(expected, use_bin_type=True) assert b == expected
def test_to_dict(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(d).set_type('some_type').set_version('1.0')) assert h.to_dict() == { 'id': '12345', 'username': '******', 'session': 'mysession', 'date': d, 'type': 'some_type', 'version': '1.0', }
def test_process(self): def my_action(p): old_value = p.get_content().get_data() p.get_content().set_data(1000) return old_value ar = ActionRegistry() ar.register(TEST_TYPE, my_action) p = (Packet().set_header(Header().set_type(TEST_TYPE)).set_content( Content().set_data(999))) assert ar.process(p) == 999 assert p.get_content().get_data() == 1000
def test_encode_passed_Header(self, header): assert Packet.encode(header) == Header.encode(header)
def test_set_version_success(self): value = '1.0' assert Header().set_version(value).get_version() == value
def test_set_id_success(self): value = '12345' assert Header().set_id(value).get_id() == value
def test_decode_not_passed_encoded_Header_or_datetime(self): d = {'key': 'value'} o = Header.decode(d) assert o == d
def test_set_session_success(self): value = 'mysession' assert Header().set_session(value).get_session() == value
def test_set_username_success(self): value = 'senkwich' assert Header().set_username(value).get_username() == value
def test_set_date_success(self): value = datetime.strptime('1990-04-27', '%Y-%m-%d') assert Header().set_date(value).get_date() == value
def parent_header(): d = datetime.strptime('1990-04-27', '%Y-%m-%d') return (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_date(d).set_type('some_type').set_version('1.0'))
def test_to_bytes_date_not_set_yet(self): h = (Header().set_id('12345').set_username('senkwich').set_session( 'mysession').set_type('some_type').set_version('1.0')) with pytest.raises(AssertionError): h.to_bytes()
def test_encode_passed_datetime(self): d = datetime.strptime('1990-04-27', '%Y-%m-%d') o = Header.encode(d) assert o['__datetime__'] assert o['s'] == d.strftime('%Y%m%dT%H:%M:%S.%f')
def test_set_type_success(self): value = 'some_type' assert Header().set_type(value).get_type() == value