def test_image_base64_png(self): """Test the image base64 serializers - png""" s = Serializer() example_png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5/hPwAIAgL/4d1j8wAAAABJRU5ErkJggg==" test_bytes = s.serialize('image/png', example_png) assert type(test_bytes) == bytes test_str = s.deserialize('image/png', test_bytes) assert type(test_str) == str
def test_image_jsonify_not_legacy(self): s = Serializer() example_png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5/hPwAIAgL/4d1j8wAAAABJRU5ErkJggg==" test_bytes = s.serialize('image/png', example_png) assert type(test_bytes) == bytes test_str = s.deserialize('image/png', test_bytes) test_str_2 = s.jsonify('image/png', test_str) assert test_str_2 == f"data:image/jpeg;base64,{test_str}"
def test_text_plain(self): """Test the text/plain serializer""" s = Serializer() start_text = "This is a \n\n string with some \r stuff in it23894590*AS^&90R32UXZ02.66" test_bytes = s.serialize('text/plain', start_text) assert type(test_bytes) == bytes test_str = s.deserialize('text/plain', test_bytes) assert start_text == test_str assert type(test_str) == str test_str_2 = s.jsonify('text/plain', start_text) assert start_text == test_str_2 assert type(test_str_2) == str
def from_bytes(byte_array: bytes, decompress: bool = True) -> 'ActivityDetailRecord': """Method to create ActivityDetailRecord from byte array (typically stored in the detail db) Returns: ActivityDetailRecord """ serializer_obj = Serializer() obj_dict = json.loads(byte_array.decode('utf-8')) # Base64 decode detail data for mime_type in obj_dict['d']: obj_dict['d'][mime_type] = base64.b64decode( obj_dict['d'][mime_type]) # Optionally decompress if decompress: obj_dict['d'][mime_type] = blosc.decompress( obj_dict['d'][mime_type]) # Deserialize obj_dict['d'][mime_type] = serializer_obj.deserialize( mime_type, obj_dict['d'][mime_type]) # Return new instance new_instance = ActivityDetailRecord(detail_type=ActivityDetailType( obj_dict['t']), show=bool(obj_dict["s"]), importance=obj_dict["i"]) # add tags if present (missing in "old" labbooks) if "a" in obj_dict: new_instance.tags = obj_dict['a'] # add action if present (missing in "old" labbooks) if "n" in obj_dict: new_instance.action = ActivityAction(int(obj_dict['n'])) new_instance.data = obj_dict['d'] new_instance.is_loaded = True return new_instance