Example #1
0
 def test_bad_descriptor_input(self, resolwe_mock):
     # Raise error is only one of deswcriptor/descriptor_schema is given:
     message = "Set both or neither descriptor and descriptor_schema."
     with six.assertRaisesRegex(self, ValueError, message):
         Resolwe.run(resolwe_mock, descriptor="a")
     with six.assertRaisesRegex(self, ValueError, message):
         Resolwe.run(resolwe_mock, descriptor_schema="a")
Example #2
0
    def test_file_processing(self, resolwe_mock, data_mock):

        resolwe_mock.api = MagicMock(
            **{
                'process.get.return_value': self.process_mock,
                'data.post.return_value': {}
            })
        resolwe_mock._process_file_field = MagicMock(
            side_effect=[{
                'file': 'file_name1',
                'file_temp': 'temp_file1'
            }, {
                'file': 'file_name2',
                'file_temp': 'temp_file2'
            }, {
                'file': 'file_name3',
                'file_temp': 'temp_file3'
            }])
        data_mock.return_value = "Data object"

        Resolwe.run(resolwe_mock,
                    input={
                        "src": "/path/to/file1",
                        "src_list": ["/path/to/file2", "/path/to/file3"]
                    })
Example #3
0
 def test_bad_descriptor_input(self, resolwe_mock):
     # Raise error is only one of deswcriptor/descriptor_schema is given:
     message = "Set both or neither descriptor and descriptor_schema."
     with six.assertRaisesRegex(self, ValueError, message):
         Resolwe.run(resolwe_mock, descriptor="a")
     with six.assertRaisesRegex(self, ValueError, message):
         Resolwe.run(resolwe_mock, descriptor_schema="a")
Example #4
0
 def test_bad_inputs(self, resolwe_mock, os_mock):
     # Good file, upload fails becouse of bad input keyword
     os_mock.path.isfile.return_value = True
     resolwe_mock.api = MagicMock(**{'process.get.return_value': self.process_json})
     resolwe_mock._upload_file = MagicMock(return_value=None)
     message = r'Field .* not in process .* input schema.'
     with six.assertRaisesRegex(self, KeyError, message):
         Resolwe.run(resolwe_mock, input={"bad_key": "/good/path/to/file"})
Example #5
0
    def test_file_processing(self, resolwe_mock, data_mock):

        resolwe_mock.api = MagicMock(**{'process.get.return_value': self.process_json, 'data.post.return_value': {}})
        resolwe_mock._process_file_field = MagicMock(side_effect=[
            {'file': 'file_name1', 'file_temp': 'temp_file1'},
            {'file': 'file_name2', 'file_temp': 'temp_file2'},
            {'file': 'file_name3', 'file_temp': 'temp_file3'}])
        data_mock.return_value = "Data object"

        Resolwe.run(resolwe_mock,
                    input={"src": "/path/to/file1",
                           "src_list": ["/path/to/file2", "/path/to/file3"]})
Example #6
0
    def test_dehydrate_collections(self, resolwe_mock):
        resolwe_mock.configure_mock(**{'_get_process.return_value': {'slug': 'some:prc:slug:'},
                                       '_process_inputs.return_value': {}})
        resolwe_mock.collection = MagicMock()
        resolwe_mock.api = MagicMock(**{'process.get.return_value': self.process_json,
                                        'data.post.return_value': {}})

        collection = Collection(id=1, resolwe=MagicMock())
        collection.id = 1  # this is overriden when initialized

        Resolwe.run(resolwe_mock, collections=[collection])
        resolwe_mock.api.data.post.assert_called_once_with(
            {'process': 'some:prc:slug:', 'input': {}, 'collections': [1]})
Example #7
0
    def test_dehydrate_collections(self, resolwe_mock):
        resolwe_mock.configure_mock(
            **{'_get_process.return_value': MagicMock(spec=Process, slug='some:prc:slug:'),
               '_process_inputs.return_value': {}}
        )
        resolwe_mock.collection = MagicMock()
        resolwe_mock.api = MagicMock(**{'process.get.return_value': self.process_mock,
                                        'data.post.return_value': {}})

        collection = Collection(id=1, resolwe=MagicMock())
        collection.id = 1  # this is overriden when initialized

        Resolwe.run(resolwe_mock, collections=[collection])
        resolwe_mock.api.data.post.assert_called_once_with(
            {'process': 'some:prc:slug:', 'input': {}, 'collections': [1]})
Example #8
0
    def test_dehydrate_collection(self, resolwe_mock):
        resolwe_mock._get_process.return_value = Process(
            resolwe=MagicMock(), slug="process-slug"
        )
        resolwe_mock._process_inputs.return_value = {}
        resolwe_mock.api = MagicMock(**{"data.post.return_value": {}})

        Resolwe.run(
            self=resolwe_mock,
            slug="process-slug",
            collection=Collection(id=1, resolwe=MagicMock()),
        )
        resolwe_mock.api.data.post.assert_called_once_with(
            {
                "process": {"slug": "process-slug"},
                "input": {},
                "collection": {"id": 1},
            }
        )
Example #9
0
    def test_dehydrate_collection(self, resolwe_mock):
        resolwe_mock._get_process.return_value = Process(resolwe=MagicMock(),
                                                         slug='process-slug')
        resolwe_mock._process_inputs.return_value = {}
        resolwe_mock.api = MagicMock(**{'data.post.return_value': {}})

        Resolwe.run(
            self=resolwe_mock,
            slug='process-slug',
            collection=Collection(id=1, resolwe=MagicMock()),
        )
        resolwe_mock.api.data.post.assert_called_once_with({
            'process': {
                'slug': 'process-slug'
            },
            'input': {},
            'collection': {
                'id': 1
            },
        })
Example #10
0
    def test_file_processing(self, resolwe_mock, data_mock):

        resolwe_mock.api = MagicMock(
            **{
                "process.get.return_value": self.process_mock,
                "data.post.return_value": {},
            }
        )
        resolwe_mock._process_file_field = MagicMock(
            side_effect=[
                {"file": "file_name1", "file_temp": "temp_file1"},
                {"file": "file_name2", "file_temp": "temp_file2"},
                {"file": "file_name3", "file_temp": "temp_file3"},
            ]
        )
        data_mock.return_value = "Data object"

        Resolwe.run(
            resolwe_mock,
            input={
                "src": "/path/to/file1",
                "src_list": ["/path/to/file2", "/path/to/file3"],
            },
        )
Example #11
0
    def test_call_with_all_args(self, resolwe_mock, os_mock, data_mock):
        resolwe_mock.api = MagicMock(**{
            'process.get.return_value': self.process_mock,
            'data.post.return_value': {'data': 'some_data'}})
        data_mock.return_value = "Data object"

        data = Resolwe.run(resolwe_mock,
                           data_name="some_name",
                           descriptor="descriptor",
                           descriptor_schema="descriptor_schema",
                           collections=[1, 2, 3])
        # Confirm that no files to upload in input:
        self.assertEqual(resolwe_mock._upload_file.call_count, 0)
        data_mock.assert_called_with(data='some_data', resolwe=resolwe_mock)
        self.assertEqual(data, "Data object")
Example #12
0
    def test_call_with_all_args(self, resolwe_mock, os_mock, data_mock):
        resolwe_mock.api = MagicMock(
            **{
                'process.get.return_value': self.process_mock,
                'data.post.return_value': {
                    'data': 'some_data'
                }
            })
        data_mock.return_value = "Data object"

        data = Resolwe.run(resolwe_mock,
                           data_name="some_name",
                           descriptor="descriptor",
                           descriptor_schema="descriptor_schema",
                           collections=[1, 2, 3])
        # Confirm that no files to upload in input:
        self.assertEqual(resolwe_mock._upload_file.call_count, 0)
        data_mock.assert_called_with(data='some_data', resolwe=resolwe_mock)
        self.assertEqual(data, "Data object")
Example #13
0
    def test_call_with_all_args(self, resolwe_mock, os_mock, data_mock):
        resolwe_mock.api = MagicMock(**{
            'process.get.return_value': self.process_json,
            'data.post.return_value': "model_data"})
        data_mock.return_value = "Data object"

        data = Resolwe.run(resolwe_mock,
                           data_name="some_name",
                           descriptor="descriptor",
                           descriptor_schema="descriptor_schema",
                           collections=[1, 2, 3],
                           src="123",
                           tools="456")
        # Confirm that process was registred, tool uploaded but no files to upload in input:
        self.assertEqual(resolwe_mock._register.call_count, 1)
        self.assertEqual(resolwe_mock._upload_tools.call_count, 1)
        self.assertEqual(resolwe_mock._upload_file.call_count, 0)
        data_mock.assert_called_with(model_data='model_data', resolwe=resolwe_mock)
        self.assertEqual(data, "Data object")
Example #14
0
    def test_call_with_all_args(self, resolwe_mock, os_mock, data_mock):
        resolwe_mock.api = MagicMock(**{
            'process.get.return_value': self.process_json,
            'data.post.return_value': "model_data"})
        data_mock.return_value = "Data object"

        data = Resolwe.run(resolwe_mock,
                           data_name="some_name",
                           descriptor="descriptor",
                           descriptor_schema="descriptor_schema",
                           collections=[1, 2, 3],
                           src="123",
                           tools="456")
        # Confirm that process was registred, tool uploaded but no files to upload in input:
        self.assertEqual(resolwe_mock._register.call_count, 1)
        self.assertEqual(resolwe_mock._upload_tools.call_count, 1)
        self.assertEqual(resolwe_mock._upload_file.call_count, 0)
        data_mock.assert_called_with(model_data='model_data', resolwe=resolwe_mock)
        self.assertEqual(data, "Data object")
Example #15
0
    def test_run_process(self, resolwe_mock, data_mock):
        resolwe_mock.api = MagicMock(
            **{'process.get.return_value': self.process_mock})

        Resolwe.run(resolwe_mock)
        self.assertEqual(resolwe_mock.api.data.post.call_count, 1)
Example #16
0
 def test_process_length_gt1(self, resolwe_mock):
     process_out = ['process1', 'process2']
     resolwe_mock.api = MagicMock(**{'process.get.return_value': process_out})
     message = "Unexpected behaviour at get process with slug .*"
     with six.assertRaisesRegex(self, ValueError, message):
         Resolwe.run(resolwe_mock)
Example #17
0
    def test_run_process(self, resolwe_mock, data_mock):
        resolwe_mock.api = MagicMock(**{'process.get.return_value': self.process_mock})

        Resolwe.run(resolwe_mock)
        self.assertEqual(resolwe_mock.api.data.post.call_count, 1)
Example #18
0
class ResolweHelper:
    def __init__(self):
        self.url = environ.get('RESOLWE_HOST_URL', DEFAULT_URL)
        self.username = environ.get('RESOLWE_API_USERNAME', DEFAULT_USERNAME)
        self.password = environ.get('RESOLWE_API_PASSWORD', DEFAULT_PASSWORD)

        try:
            self.res = Resolwe(self.username, self.password, self.url)
        except Exception:
            # TODO: raise proper exceptions and handle in GUI
            raise

    @staticmethod
    async def check_object_status(data_object):
        while True:
            data_object.update()
            if data_object.status == 'OK' or data_object.status == 'ER':
                return True

            await asyncio.sleep(0.5)

    def run_process(self, slug, **kwargs):

        process = self.res.get_or_run(slug, input={**kwargs})
        if process.status == 'OK':
            return process

        # wait till task is finished
        loop = asyncio.new_event_loop()
        try:
            loop.run_until_complete(
                asyncio.wait_for(self.check_object_status(process),
                                 timeout=60))
        finally:
            loop.close()

        return process

    def get_json(self, data_object, output_field, json_field=None):
        storage_data = self.res.api.storage(
            data_object.output[output_field]).get()
        if json_field:
            return storage_data['json'][json_field]
        else:
            return storage_data['json']

    def get_object(self, *args, **kwargs):
        return self.res.data.get(*args, **kwargs)

    def list_data_objects(self, data_type):
        return self.res.data.filter(type='data:table:{}'.format(data_type))

    def get_descriptor_schema(self, slug):
        return self.res.descriptor_schema.get(slug)

    def upload_data_table(self, data_table):

        if not data_table and isinstance(data_table, Table):
            # raise proper warning
            return

        # create temp dir and pickle data.Table object
        with tempfile.TemporaryDirectory() as temp_dir:
            # TODO: What to do if Table is not named?
            file_name = data_table.name + '.pickle'
            file_path = os.path.join(temp_dir, file_name)
            # save Table as pickled object
            data_table.save(file_path)
            # run resolwe upload process
            self.res.run('data-table-upload', input={'src': file_path})

    @staticmethod
    def download_data_table(data_table_object):
        with tempfile.TemporaryDirectory() as temp_dir:
            data_table_object.download(download_dir=temp_dir)
            return Table(os.path.join(temp_dir, data_table_object.name))
Example #19
0
 def test_process_length_0(self, resolwe_mock):
     resolwe_mock.api = MagicMock(**{'process.get.return_value': []})
     message = "Could not get process for given slug."
     with six.assertRaisesRegex(self, ValueError, message):
         Resolwe.run(resolwe_mock)