-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EBR-79: add bucket widget and tests for the functionalities, add JUWE…
…LS site for remote run
- Loading branch information
1 parent
eff4545
commit fec5ba2
Showing
8 changed files
with
232 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# "TheVirtualBrain - Widgets" package | ||
# | ||
# (c) 2022-2024, TVB Widgets Team | ||
# | ||
import os | ||
|
||
import pytest | ||
from ebrains_drive.exceptions import Unauthorized | ||
from tvbwidgets.ui.bucket_widget import BucketWidget | ||
|
||
DUMMY_CONTENT = b'test content' | ||
|
||
|
||
class MockBucketDTO: | ||
def __init__(self, name, role='', is_public=True): | ||
self.name = name | ||
self.role = role | ||
self.is_public = is_public | ||
|
||
|
||
class MockFile: | ||
def __init__(self, name): | ||
# type: (str) -> None | ||
self.name = name | ||
|
||
def get_content(self): | ||
return DUMMY_CONTENT | ||
|
||
def get_download_link(self): | ||
return '' | ||
|
||
|
||
class MockBucket: | ||
def __init__(self, files_count=2, name='test_bucket', target='buckets', dataproxy_entity_name='test_bucket'): | ||
self.name = name | ||
self.files = [MockFile(f'file{number}') for number in range(files_count)] | ||
self.target = target | ||
self.dataproxy_entity_name = dataproxy_entity_name | ||
|
||
def ls(self, prefix=''): | ||
return [f for f in self.files if f.name.startswith(prefix)] | ||
|
||
|
||
class MockBuckets: | ||
def __init__(self): | ||
self.buckets = { | ||
'test_bucket': MockBucket() | ||
} | ||
|
||
def get_bucket(self, name): | ||
try: | ||
return self.buckets[name] | ||
except KeyError: | ||
raise Unauthorized('Unauthorized in tests') | ||
|
||
def list_buckets(self): | ||
return [MockBucketDTO(b) for b in self.buckets.keys()] | ||
|
||
|
||
class MockBucketApiClient: | ||
def __init__(self, token=''): | ||
self.token = token | ||
self.buckets = MockBuckets() | ||
|
||
|
||
@pytest.fixture | ||
def mock_client(mocker): | ||
def mock_get_client(_): | ||
return MockBucketApiClient() | ||
|
||
mocker.patch('tvb_ext_bucket.ebrains_drive_wrapper.BucketWrapper.get_client', mock_get_client) | ||
|
||
|
||
@pytest.fixture | ||
def mock_requests_get(mocker): | ||
mock_response = mocker.Mock() | ||
mock_response.content = DUMMY_CONTENT | ||
return mocker.patch('requests.get', return_value=mock_response) | ||
|
||
|
||
def test_get_files_in_bucket(mock_client, mock_requests_get): | ||
""" | ||
tests that client returns list of files from bucket | ||
""" | ||
widget = BucketWidget() | ||
|
||
# test observe event on buckets dropdown | ||
assert widget.buckets_dropdown.value is None | ||
assert widget.files_list.value is None | ||
assert len(widget.files_list.options) == 0 | ||
widget.buckets_dropdown.value = widget.buckets_dropdown.options[0] | ||
assert len(widget.files_list.options) == 2 | ||
widget.files_list.value = widget.files_list.options[0] | ||
|
||
# test BucketWidget functions | ||
assert widget.get_selected_file_path() == widget.buckets_dropdown.value + '/' + widget.files_list.value | ||
assert widget.get_selected_file_content() == DUMMY_CONTENT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# "TheVirtualBrain - Widgets" package | ||
# | ||
# (c) 2022-2024, TVB Widgets Team | ||
# | ||
|
||
import ipywidgets | ||
import requests | ||
from tvb_ext_bucket.ebrains_drive_wrapper import BucketWrapper | ||
from tvbwidgets.ui.base_widget import TVBWidget | ||
|
||
|
||
class BucketWidget(ipywidgets.VBox, TVBWidget): | ||
|
||
def __init__(self, **kwargs): | ||
TVBWidget.__init__(self, **kwargs) | ||
self.client = BucketWrapper() | ||
|
||
try: | ||
all_buckets = self.client.list_buckets() | ||
except Exception: | ||
self.logger.error("Could not retrieve the list of available Buckets!") | ||
all_buckets = [] | ||
layout = ipywidgets.Layout(width='400px') | ||
self.buckets_dropdown = ipywidgets.Dropdown(description='Bucket', value=None, | ||
options=all_buckets, layout=layout) | ||
self.files_list = ipywidgets.Select(description='Files', value=None, disabled=False, layout=layout) | ||
|
||
self.buckets_dropdown.observe(self.select_bucket, names='value') | ||
|
||
self._parent_dir = None | ||
self._map_names_to_files = dict() | ||
ipywidgets.VBox.__init__(self, [self.buckets_dropdown, self.files_list], **kwargs) | ||
|
||
def get_chosen_bucket(self): | ||
return self.buckets_dropdown.value | ||
|
||
def get_selected_file_path(self): | ||
return self.buckets_dropdown.value + "/" + self.files_list.value | ||
|
||
def get_selected_file_content(self): | ||
path = self.files_list.value | ||
downloadable_url = self.client.get_download_url(path, self.buckets_dropdown.value) | ||
response = requests.get(downloadable_url) | ||
return response.content | ||
|
||
def select_bucket(self, _): | ||
selected_bucket = self.buckets_dropdown.value | ||
self.files_list.options = self.client.get_files_in_bucket(selected_bucket) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters