69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
"""Tests for the public profile-matching code sample."""
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from profile_matching import (
|
|
INTEREST_WEIGHT,
|
|
LOCATION_WEIGHT,
|
|
PROJECT_TAG_WEIGHT,
|
|
SKILL_WEIGHT,
|
|
haversine_distance_km,
|
|
jaccard_similarity,
|
|
location_similarity,
|
|
recommendation_score,
|
|
weighted_jaccard_similarity,
|
|
)
|
|
|
|
|
|
class ProfileMatchingTests(unittest.TestCase):
|
|
def test_component_weights_add_up_to_one(self) -> None:
|
|
self.assertAlmostEqual(
|
|
SKILL_WEIGHT + INTEREST_WEIGHT + LOCATION_WEIGHT + PROJECT_TAG_WEIGHT,
|
|
1.0,
|
|
)
|
|
|
|
def test_similarity_normalises_case_whitespace_and_duplicates(self) -> None:
|
|
self.assertEqual(
|
|
jaccard_similarity([" Python ", "python", ""], ["PYTHON"]), 1.0
|
|
)
|
|
|
|
def test_empty_profiles_do_not_create_a_match(self) -> None:
|
|
self.assertEqual(jaccard_similarity([], []), 0.0)
|
|
self.assertEqual(location_similarity(None, (51.34, 12.37)), 0.0)
|
|
|
|
def test_unweighted_mode_is_backwards_compatible(self) -> None:
|
|
left, right = ["Python", "Rust"], ["Rust", "TypeScript"]
|
|
self.assertAlmostEqual(
|
|
weighted_jaccard_similarity(left, right),
|
|
jaccard_similarity(left, right),
|
|
)
|
|
|
|
def test_shared_high_affinity_interest_increases_match_score(self) -> None:
|
|
neutral = weighted_jaccard_similarity(
|
|
["Hiking", "Opera"], ["Opera", "Cooking"]
|
|
)
|
|
personalised = weighted_jaccard_similarity(
|
|
["Hiking", "Opera"], ["Opera", "Cooking"], {"opera": 3.0}
|
|
)
|
|
self.assertGreater(personalised, neutral)
|
|
|
|
def test_distance_is_symmetric_and_decays_to_zero(self) -> None:
|
|
leipzig = (51.3397, 12.3731)
|
|
berlin = (52.52, 13.405)
|
|
self.assertAlmostEqual(
|
|
haversine_distance_km(*leipzig, *berlin),
|
|
haversine_distance_km(*berlin, *leipzig),
|
|
)
|
|
self.assertEqual(location_similarity(leipzig, berlin), 0.0)
|
|
|
|
def test_final_score_is_explainable_weighted_sum(self) -> None:
|
|
self.assertAlmostEqual(
|
|
recommendation_score(0.5, 0.5, 1.0, 0.0),
|
|
0.475,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|