Skip to content

Commit

Permalink
Fixing formatting with black (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
manasaV3 committed Sep 28, 2023
1 parent a3c4aa2 commit 192d5f4
Show file tree
Hide file tree
Showing 32 changed files with 527 additions and 390 deletions.
41 changes: 30 additions & 11 deletions backend/api/_tests/test_fixtures.py
@@ -1,20 +1,39 @@
from dateutil.relativedelta import relativedelta
from datetime import datetime, date, timezone

BASE = datetime.combine(date.today(), datetime.min.time()).replace(day=1, tzinfo=timezone.utc)
BASE = datetime.combine(date.today(), datetime.min.time()).replace(
day=1, tzinfo=timezone.utc
)


def _to_timestamp(i):
return int((BASE + relativedelta(months=i)).timestamp()) * 1000


def _generate_timeline(start_range, value_key, to_value, timestamp_key='timestamp'):
return [{timestamp_key: _to_timestamp(i), value_key: to_value(abs(i))} for i in range(start_range, 0)]


def generate_installs_timeline(start_range, ts_key='timestamp', to_value=lambda i: 2 if i % 2 == 0 else 0):
return _generate_timeline(start_range=start_range, value_key='installs', to_value=to_value, timestamp_key=ts_key)


def generate_commits_timeline(start_range, ts_key='timestamp', to_value=lambda i: i + 5):
return _generate_timeline(start_range=start_range, value_key='commits', to_value=to_value, timestamp_key=ts_key)
def _generate_timeline(start_range, value_key, to_value, timestamp_key="timestamp"):
return [
{timestamp_key: _to_timestamp(i), value_key: to_value(abs(i))}
for i in range(start_range, 0)
]


def generate_installs_timeline(
start_range, ts_key="timestamp", to_value=lambda i: 2 if i % 2 == 0 else 0
):
return _generate_timeline(
start_range=start_range,
value_key="installs",
to_value=to_value,
timestamp_key=ts_key,
)


def generate_commits_timeline(
start_range, ts_key="timestamp", to_value=lambda i: i + 5
):
return _generate_timeline(
start_range=start_range,
value_key="commits",
to_value=to_value,
timestamp_key=ts_key,
)
39 changes: 17 additions & 22 deletions backend/api/_tests/test_home.py
Expand Up @@ -17,7 +17,6 @@ def get_name(plugin: Dict):


class TestHomepage:

@pytest.fixture
def mock_get_index(self, monkeypatch, index_data):
get_index = Mock()
Expand Down Expand Up @@ -73,48 +72,44 @@ def index_data(self):
"summary": "A plugin named plugin 4",
"total_installs": 200,
"plugin_types": ["reader", "widget"],
}
},
]

def test_invalid_section_names(self, mock_get_index):
assert {} == home.get_plugin_sections({"foo"})
mock_get_index.assert_not_called()

@pytest.mark.parametrize("section, sort_key", [
("newest", "first_released"),
("recently_updated", "release_date"),
("top_installed", "total_installs"),
])
def test_valid_section_names(
self, section, sort_key, index_data, mock_get_index
):
@pytest.mark.parametrize(
"section, sort_key",
[
("newest", "first_released"),
("recently_updated", "release_date"),
("top_installed", "total_installs"),
],
)
def test_valid_section_names(self, section, sort_key, index_data, mock_get_index):
actual = home.get_plugin_sections({section})

plugins = sorted(
index_data, key=lambda item: item.get(sort_key), reverse=True
)[0:3]
plugins = sorted(index_data, key=lambda item: item.get(sort_key), reverse=True)[
0:3
]
expected = {section: {"plugins": filter_plugins(plugins)}}
assert expected == actual
mock_get_index.assert_called_once_with(
include_total_installs=True, visibility_filter={PluginVisibility.PUBLIC}
)

@pytest.mark.parametrize(
"plugin_type, minute, expected", [
"plugin_type, minute, expected",
[
("reader", 1, ["plugin-1", "plugin-4"]),
("sample_data", 27, ["plugin-2", "plugin-3"]),
("widget", 13, ["plugin-3", "plugin-4"]),
("writer", 58, ["plugin-1", "plugin-2"]),
]
],
)
def test_valid_plugin_types_section(
self,
plugin_type,
minute,
expected,
index_data,
mock_get_index,
mock_date_time
self, plugin_type, minute, expected, index_data, mock_get_index, mock_date_time
):
mock_date_time.now.return_value = datetime(2023, 7, 25, 17, minute, 55)

Expand Down
37 changes: 31 additions & 6 deletions backend/api/_tests/test_metrics.py
Expand Up @@ -83,15 +83,40 @@ def usage_mocks(self, monkeypatch) -> None:
@pytest.mark.parametrize(
"limit_str, limit, plugin, repo",
[
("3", 3, MOCK_EMPTY_PLUGIN, None,),
("0", 0, MOCK_PLUGIN_OBJ, REPO,),
("foo", 0, MOCK_PLUGIN_OBJ, REPO,),
("-5", 0, MOCK_PLUGIN_OBJ, REPO,),
("3", 3, MOCK_PLUGIN_OBJ, REPO,),
(
"3",
3,
MOCK_EMPTY_PLUGIN,
None,
),
(
"0",
0,
MOCK_PLUGIN_OBJ,
REPO,
),
(
"foo",
0,
MOCK_PLUGIN_OBJ,
REPO,
),
(
"-5",
0,
MOCK_PLUGIN_OBJ,
REPO,
),
(
"3",
3,
MOCK_PLUGIN_OBJ,
REPO,
),
],
)
def test_get_metrics_for_plugin(
self, limit_str: str, limit: int, plugin: Dict, repo: str
self, limit_str: str, limit: int, plugin: Dict, repo: str
):
self._plugin = plugin

Expand Down
71 changes: 32 additions & 39 deletions backend/api/_tests/test_model.py
Expand Up @@ -9,7 +9,6 @@


class TestModel:

@pytest.fixture
def plugin_get_index_result(self) -> List[Dict[str, Any]]:
return [
Expand All @@ -20,9 +19,9 @@ def plugin_get_index_result(self) -> List[Dict[str, Any]]:

@pytest.fixture
def mock_get_index(
self,
plugin_get_index_result: List[Dict[str, Any]],
monkeypatch: pytest.MonkeyPatch,
self,
plugin_get_index_result: List[Dict[str, Any]],
monkeypatch: pytest.MonkeyPatch,
) -> Mock:
mock = Mock(spec=plugin.get_index, return_value=plugin_get_index_result)
monkeypatch.setattr(model.plugin_model, "get_index", mock)
Expand All @@ -34,13 +33,13 @@ def get_total_installs_result(self) -> Dict[str, int]:

@pytest.fixture
def mock_total_installs(
self,
get_total_installs_result: Dict[str, int],
monkeypatch: pytest.MonkeyPatch,
self,
get_total_installs_result: Dict[str, int],
monkeypatch: pytest.MonkeyPatch,
) -> Mock:
mock = Mock(
spec=install_activity.get_total_installs_by_plugins,
return_value=get_total_installs_result
return_value=get_total_installs_result,
)
monkeypatch.setattr(
model.install_activity, "get_total_installs_by_plugins", mock
Expand All @@ -49,49 +48,43 @@ def mock_total_installs(

@pytest.fixture
def plugin_index_with_total_installs(
self,
plugin_get_index_result: List[Dict[str, Any]],
get_total_installs_result: Dict[str, int],

self,
plugin_get_index_result: List[Dict[str, Any]],
get_total_installs_result: Dict[str, int],
) -> List[Dict[str, Any]]:
result = []
for item in plugin_get_index_result:
installs = get_total_installs_result.get(item["name"].lower(), 0)
result.append({**item, **{"total_installs": installs}})
return result

@pytest.mark.parametrize("visibility, include_total_installs, expected", [
({PluginVisibility.PUBLIC}, True, "plugin_index_with_total_installs"),
(
@pytest.mark.parametrize(
"visibility, include_total_installs, expected",
[
({PluginVisibility.PUBLIC}, True, "plugin_index_with_total_installs"),
(
{PluginVisibility.PUBLIC, PluginVisibility.HIDDEN},
True,
"plugin_index_with_total_installs"
),
(
None,
True,
"plugin_index_with_total_installs"
),
({PluginVisibility.PUBLIC}, False, "plugin_get_index_result"),
(
"plugin_index_with_total_installs",
),
(None, True, "plugin_index_with_total_installs"),
({PluginVisibility.PUBLIC}, False, "plugin_get_index_result"),
(
{PluginVisibility.PUBLIC, PluginVisibility.HIDDEN},
False,
"plugin_get_index_result"
),
(
None,
False,
"plugin_get_index_result"
),
])
"plugin_get_index_result",
),
(None, False, "plugin_get_index_result"),
],
)
def test_get_index_with_include_installs(
self,
visibility: Optional[Set[PluginVisibility]],
include_total_installs: bool,
expected: str,
mock_get_index: Mock,
mock_total_installs: Mock,
request: pytest.FixtureRequest,
self,
visibility: Optional[Set[PluginVisibility]],
include_total_installs: bool,
expected: str,
mock_get_index: Mock,
mock_total_installs: Mock,
request: pytest.FixtureRequest,
):
actual = model.get_index(visibility, include_total_installs)

Expand Down
1 change: 0 additions & 1 deletion backend/api/_tests/test_shield.py
Expand Up @@ -9,7 +9,6 @@ def validate(result, message):


class TestShield:

def test_get_shield_valid_plugin(self, monkeypatch):
monkeypatch.setattr(model, "get_plugin", lambda _: {"version": "0.0.1"})
result = shield.get_shield("package1")
Expand Down
27 changes: 18 additions & 9 deletions backend/api/app.py
Expand Up @@ -9,7 +9,7 @@
from api.custom_wsgi import script_path_middleware
from api.model import get_index, get_manifest, get_plugin
from api.metrics import get_metrics_for_plugin
from nhcommons.models import (category as categories)
from nhcommons.models import category as categories
from api.shield import get_shield
from nhcommons.models.plugin_utils import PluginVisibility
from utils.utils import send_alert
Expand Down Expand Up @@ -106,7 +106,7 @@ def shield(plugin: str) -> Response:

@app.route(
"/categories",
defaults={"version": os.getenv("category_version", "EDAM-BIOIMAGING:alpha06")}
defaults={"version": os.getenv("category_version", "EDAM-BIOIMAGING:alpha06")},
)
def get_categories(version: str) -> Response:
return jsonify(categories.get_all_categories(version))
Expand Down Expand Up @@ -140,14 +140,21 @@ def get_plugin_metrics(plugin: str) -> Response:

@app.errorhandler(404)
def handle_exception(e) -> Response:
links = [rule.rule for rule in app.url_map.iter_rules()
if 'GET' in rule.methods and
any((rule.rule.startswith("/plugins"), rule.rule.startswith("/shields")))
]
links = [
rule.rule
for rule in app.url_map.iter_rules()
if "GET" in rule.methods
and any((rule.rule.startswith("/plugins"), rule.rule.startswith("/shields")))
]
links.sort()
links = "\n".join(links)
return app.make_response((f"Invalid Endpoint, valid endpoints are:\n{links}", 404,
{'Content-Type': 'text/plain; charset=utf-8'}))
return app.make_response(
(
f"Invalid Endpoint, valid endpoints are:\n{links}",
404,
{"Content-Type": "text/plain; charset=utf-8"},
)
)


@app.errorhandler(exceptions.Unauthorized)
Expand All @@ -165,7 +172,9 @@ def handle_exception(e) -> Response:

@app.before_request
def authenticate_request():
if request.method == "POST" and request.headers.get("X-API-Key") != os.getenv("API_KEY"):
if request.method == "POST" and request.headers.get("X-API-Key") != os.getenv(
"API_KEY"
):
raise exceptions.Unauthorized("Invalid API key")


Expand Down
8 changes: 4 additions & 4 deletions backend/api/custom_wsgi.py
@@ -1,12 +1,12 @@
def script_path_middleware(script_path):
def wrapper(wsgi_app):

# Allows route mapping to ignore the specific prefix if present
def new_app(environ, start_response):
if environ['PATH_INFO'].startswith(script_path):
environ['PATH_INFO'] = environ['PATH_INFO'][len(script_path):]
environ['SCRIPT_NAME'] += script_path
if environ["PATH_INFO"].startswith(script_path):
environ["PATH_INFO"] = environ["PATH_INFO"][len(script_path) :]
environ["SCRIPT_NAME"] += script_path
return wsgi_app(environ, start_response)

return new_app

return wrapper

0 comments on commit 192d5f4

Please sign in to comment.