Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiasun0515 committed Oct 1, 2024
1 parent 98eceb8 commit 50e9c76
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/poprox_recommender/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def find_locality(past_articles: list[Article], article_id: UUID):


def normalized_category_count(counts: dict[str, int]):
if isinstance(counts, dict):
try:
total_count = sum(counts.values())
normalized_counts = {key: value / total_count for key, value in counts.items()}
else:
except Exception as _:
normalized_counts = {}
return normalized_counts

Expand Down
68 changes: 43 additions & 25 deletions tests/components/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,48 @@

import logging

from pytest import mark, skip
from pytest import skip, xfail

from poprox_concepts import ArticleSet
from poprox_concepts.api.recommendations import RecommendationRequest
from poprox_recommender.config import allow_data_test_failures
from poprox_recommender.paths import project_root
from poprox_recommender.recommenders import PipelineLoadError, select_articles
from poprox_recommender.topics import user_locality_preference, user_topic_preference

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


def test_request_with_topic_calibrator():
test_dir = project_root() / "tests"
req_f = test_dir / "request_data" / "medium_request.json"
req_f = test_dir / "request_data" / "request_body.json"
if allow_data_test_failures() and not req_f.exists():
skip("request file does not exist")

req = RecommendationRequest.model_validate_json(req_f.read_text())

base_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
)
topic_calibrated_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
pipeline_params={"pipeline": "topic-cali"},
req.interest_profile.click_topic_counts = user_topic_preference(
req.past_articles, req.interest_profile.click_history
)

try:
base_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
)
topic_calibrated_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
pipeline_params={"pipeline": "topic-cali"},
)
except PipelineLoadError as e:
if allow_data_test_failures():
xfail("data not pulled")
else:
raise e

# do we get recommendations?
tco_recs = topic_calibrated_outputs.default.articles
bo_recs = base_outputs.default.articles
Expand All @@ -51,23 +61,31 @@ def test_request_with_topic_calibrator():

def test_request_with_locality_calibrator():
test_dir = project_root() / "tests"
req_f = test_dir / "request_data" / "medium_request.json"
req_f = test_dir / "request_data" / "request_body.json"
if allow_data_test_failures() and not req_f.exists():
skip("request file does not exist")

req = RecommendationRequest.model_validate_json(req_f.read_text())

base_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
)
locality_calibrated_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
pipeline_params={"pipeline": "locality-cali"},
req.interest_profile.click_locality_counts = user_locality_preference(
req.past_articles, req.interest_profile.click_history
)
try:
base_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
)
locality_calibrated_outputs = select_articles(
ArticleSet(articles=req.todays_articles),
ArticleSet(articles=req.past_articles),
req.interest_profile,
pipeline_params={"pipeline": "locality-cali"},
)
except PipelineLoadError as e:
if allow_data_test_failures():
xfail("data not pulled")
else:
raise e

# do we get recommendations?
tco_recs = locality_calibrated_outputs.default.articles
Expand Down
55 changes: 0 additions & 55 deletions tests/components/test_topic_calibration.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/test_topic_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def load_test_articles():
return candidate, past, click_history, num_recs


@pytest.mark.skip(reason="too time intensive and not used by current prod logic")
def test_topic_classification():
candidate, _, _, _ = load_test_articles()
topic_matched_dict, todays_article_matched_topics = match_news_topics_to_general(candidate.articles)
Expand Down

0 comments on commit 50e9c76

Please sign in to comment.