Skip to content

Commit

Permalink
[#424] handle empty candidates for ss
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Kolomanski committed Jan 20, 2023
1 parent 87de420 commit 419b243
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
5 changes: 0 additions & 5 deletions recommender/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ def message(self):
return "Length of users and candidates is not equal"


class ServicesContextNotProvidedError(Exception):
def message(self):
return "Candidates not provided outside the context of the User Dashboard"


class UserCannotBeIdentified(Exception):
def message(self):
return "Recommendation context did not provide user_id or aai_uid"
17 changes: 3 additions & 14 deletions recommender/services/provide_service_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from typing import Dict, List, Type
from recommender.models import Service, MarketplaceDocument
from recommender.errors import ServicesContextNotProvidedError
from logger_config import get_logger

logger = get_logger(__name__)
Expand All @@ -16,18 +15,8 @@ def get_all_collection_ids(collection: Type[MarketplaceDocument]) -> List[int]:

def service_ctx(body_request: Dict) -> Dict:
"""Provide service context as "all services" if candidates are not specified
in the recommendations request's body, for the purpose of the User Dashboard"""

candidates = body_request.get("candidates")
page_id = body_request.get("page_id")

if candidates is None:
if page_id == "/dashboard": # case for the UD
body_request["candidates"] = get_all_collection_ids(Service)
else:
logger.error(
"Candidates not provided. Only for the context of page_id == '/dashboard' they are NOT required"
)
raise ServicesContextNotProvidedError()
in the recommendations request's body"""
if not body_request.get("candidates"):
body_request["candidates"] = get_all_collection_ids(Service)

return body_request
48 changes: 34 additions & 14 deletions tests/services/test_provide_service_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from recommender.services.provide_service_ctx import get_all_collection_ids, service_ctx
from recommender.models import Service, User
from tests.endpoints.conftest import recommendation_data
from recommender.errors import ServicesContextNotProvidedError


def test_get_all_collection_ids(mongo, generate_users_and_services):
Expand All @@ -31,26 +30,47 @@ def test_get_all_collection_ids(mongo, generate_users_and_services):
def test_service_ctx(mongo, generate_users_and_services, recommendation_data):
"""
Check:
1) candidates provided, page_id different from /dashboard
2) candidates NOT provided, page_id different from /dashboard
3) candidates NOT provided, page_id == /dashboard
K recommendations returned:
1) candidates provided,
2) candidates NOT provided, empty list
3) candidates NOT provided, the key does not exist
Sort by relevance:
4) candidates NOT provided,
5) candidates provided,
"""
# All services from db
args = users_services_args()
all_services = args["common_services_num"] + args["unordered_services_num"]

# 1)
returned_dict = service_ctx(recommendation_data)
assert returned_dict == recommendation_data

# 2)
del recommendation_data["candidates"]
with pytest.raises(ServicesContextNotProvidedError):
service_ctx(recommendation_data)
recommendation_data["candidates"] = []
candidates = service_ctx(recommendation_data)["candidates"]
assert len(candidates) == all_services
assert all([type(service_id) == int for service_id in candidates])

# 3)
recommendation_data.update({"page_id": "/dashboard"})
returned_dict = service_ctx(recommendation_data)
del recommendation_data["candidates"]
candidates = service_ctx(recommendation_data)["candidates"]
assert len(candidates) == all_services
assert all([type(service_id) == int for service_id in candidates])

args = users_services_args()
services_num = args["common_services_num"] + args["unordered_services_num"]
# 4)
recommendation_data["engine_version"] = "NCFRanking"

candidates = service_ctx(recommendation_data)["candidates"]
assert len(candidates) == all_services
assert all([type(service_id) == int for service_id in candidates])

# 5)
l = [1, 2, 3]
recommendation_data["candidates"] = l

assert returned_dict.get("candidates")
assert len(returned_dict["candidates"]) == services_num
assert all([type(service_id) == int for service_id in returned_dict["candidates"]])
candidates = service_ctx(recommendation_data)["candidates"]
assert len(candidates) == len(l)
assert all([type(service_id) == int for service_id in candidates])

0 comments on commit 419b243

Please sign in to comment.