def test_unpacking(self): """ Test if message is correctly unpacked from a binary string back into an object """ x = 1 y = 7.77 msg = self.get_vector_msg(x=1, y=7.77) packed = struct.pack("!B", 3) + struct.pack("!f", 1) + struct.pack("!f", 7.77) msg = unpack_message(packed, self.factory) self.assertEqual(msg["x"], x) self.assertAlmostEqual(msg["y"], y, places=4)
def test_unpacking(self): """ Test if message is correctly unpacked from a binary string back into an object """ x = 1 y = 7.77 msg = self.get_vector_msg(x=1, y=7.77) packed = struct.pack("!B", 3) + \ struct.pack("!f", 1) + \ struct.pack("!f", 7.77) msg = unpack_message(packed, self.factory) self.assertEqual(msg['x'], x) self.assertAlmostEqual(msg['y'], y, places=4)
def test_unpacking_with_string(self): """ Test if message containing an unicode string is is correctly unpacked from a binary string back into an object """ msg = self.get_bar_msg(name=u'Mr ☃') string_length = len(bytes(u'Mr ☃', 'utf-8')) packed = struct.pack("!B", 1) + \ struct.pack("!I", string_length) + \ struct.pack( "!{}s".format(string_length), bytes(u'Mr ☃', 'utf-8') ) + \ struct.pack("!H", 42) msg = unpack_message(packed, self.factory) self.assertEqual(msg['name'], u'Mr ☃') self.assertEqual(msg['score'], 42)
def test_unpacking_with_string(self): """ Test if message containing an unicode string is is correctly unpacked from a binary string back into an object """ msg = self.get_bar_msg(name=u"Mr ☃") string_length = len(bytes(u"Mr ☃", "utf-8")) packed = ( struct.pack("!B", 1) + struct.pack("!I", string_length) + struct.pack("!{}s".format(string_length), bytes(u"Mr ☃", "utf-8")) + struct.pack("!H", 42) ) msg = unpack_message(packed, self.factory) self.assertEqual(msg["name"], u"Mr ☃") self.assertEqual(msg["score"], 42)
def test_eating_own_dog_food(self): """ Test if message remaines the same after packing/unpacking cycle """ msg = self.get_foo_msg() packed = pack_message(msg) unpacked = unpack_message(packed, self.factory) self.assertEqual(msg, unpacked)