def to_bytes(self, compress: bool = True) -> bytes: """Method to serialize to bytes for storage in the activity detail db Returns: bytes """ dict_data = self.to_dict(compact=True) # Serialize data items serializer_obj = Serializer() for mime_type in dict_data['d']: dict_data['d'][mime_type] = serializer_obj.serialize( mime_type, dict_data['d'][mime_type]) # Compress object data if compress: if type(dict_data['d'][mime_type]) != bytes: raise ValueError( "Data must be serialized to bytes before compression") dict_data['d'][mime_type] = blosc.compress( dict_data['d'][mime_type], typesize=8, cname='blosclz', shuffle=blosc.SHUFFLE) # Base64 encode binary data while dumping to json string return json.dumps(dict_data, cls=ActivityDetailRecordEncoder, separators=(',', ':')).encode('utf-8')
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 test_bad_mime_type(self): """Test the constructor""" s = Serializer() with pytest.raises(ValueError): s.serialize('text/asdfasdfasdfasdf', 'asdfasdfasd')