from decimal import Decimal import pytest from django.core.exceptions import ValidationError from apps.profiles.models import ProfileRevision, SearchProfile from apps.profiles.services import apply_feedback_delta, save_profile_revision @pytest.mark.django_db def test_profile_validation_snapshot_activation_and_revisions(user, profile): profile.full_clean() snapshot = profile.snapshot() assert snapshot["home_municipality"] == "Hasselt" assert snapshot["home_latitude"] == pytest.approx(50.9307) assert snapshot["experience_years"] == 0 assert "seniority" not in profile.weights assert sum(profile.weights.values()) == 100 other = SearchProfile.objects.create(user=user, name="Tweede", is_active=False) other.activate() profile.refresh_from_db() other.refresh_from_db() assert other.is_active is True assert profile.is_active is False revision = save_profile_revision(other, reason="test") assert revision.version == 1 assert ProfileRevision.objects.filter(profile=other).count() == 1 second = save_profile_revision(other, reason="test-2") assert second.version == 2 other.refresh_from_db() assert other.version == 2 @pytest.mark.django_db def test_feedback_delta_is_bounded_and_can_be_disabled(profile): apply_feedback_delta(profile, "content", 100) profile.refresh_from_db() assert profile.weights["content"] == 40.0 apply_feedback_delta(profile, "content", -100) profile.refresh_from_db() assert profile.weights["content"] == 0.0 profile.learning_enabled = False profile.weights["skills"] = 20.0 profile.save() apply_feedback_delta(profile, "skills", 5) profile.refresh_from_db() assert profile.weights["skills"] == 20.0 @pytest.mark.parametrize( ("field", "value"), [ ("weights", {"content": "not-a-number"}), ("weights", {"content": -1}), ("home_latitude", Decimal("91")), ("home_longitude", Decimal("181")), ], ) @pytest.mark.django_db def test_profile_rejects_invalid_values(profile, field, value): setattr(profile, field, value) with pytest.raises(ValidationError): profile.full_clean() @pytest.mark.django_db def test_profile_rejects_inverted_thresholds(profile): profile.recommendation_threshold = 91 profile.top_match_threshold = 90 with pytest.raises(ValidationError): profile.full_clean()