from decimal import Decimal import pytest from django.urls import reverse from apps.jobs.models import GeocodeLocationLookup from apps.profiles.forms import SearchProfileForm def _profile_post_data(**overrides): data = { "name": "Testprofiel", "is_active": "on", "home_postal_code": "3500", "max_distance_km": "45", "desired_titles": ["infrastructure engineer", "cloud engineer"], "excluded_titles": ["sales"], "desired_skills": ["azure", "terraform"], "excluded_skills": ["cold calling"], "allowed_employment_types": ["full_time", "permanent"], "preferred_workplace": ["hybrid"], "preferred_regions": ["Limburg"], "excluded_regions": ["West-Vlaanderen"], "recommendation_threshold": "65", "top_match_threshold": "90", "digest_time": "07:30", } data.update(overrides) return data @pytest.mark.django_db def test_profile_form_uses_guided_choices_and_preserves_existing_custom_values(profile): form = SearchProfileForm(instance=profile) assert form.fields["home_postal_code"].widget.attrs["maxlength"] == 4 assert "home_municipality" not in form.fields assert "home_latitude" not in form.fields assert "home_longitude" not in form.fields assert form.fields["desired_titles"].widget.allow_multiple_selected is True assert ("systeembeheerder", "systeembeheerder") in form.fields["desired_titles"].choices assert ("VMware", "VMware") in form.fields["desired_skills"].choices @pytest.mark.django_db def test_profile_form_rejects_non_belgian_postcode_and_conflicting_choices(profile): invalid_postcode = SearchProfileForm( data=_profile_post_data(home_postal_code="3500 Hasselt"), instance=profile ) assert invalid_postcode.is_valid() is False assert "home_postal_code" in invalid_postcode.errors conflicting = SearchProfileForm( data=_profile_post_data(preferred_regions=["Limburg"], excluded_regions=["Limburg"]), instance=profile, ) assert conflicting.is_valid() is False assert "excluded_regions" in conflicting.errors @pytest.mark.django_db def test_profile_update_derives_home_location_from_postcode(client, user, profile): GeocodeLocationLookup.objects.create( source_name="test-geodata", source_version="2026-01", query_kind=GeocodeLocationLookup.QueryKind.POSTAL, query_value="3500", postal_code="3500", municipality="Hasselt", region="Limburg", latitude=Decimal("50.930700"), longitude=Decimal("5.332500"), confidence=Decimal("1.00"), ) client.force_login(user) response = client.post( reverse("profiles:edit", kwargs={"pk": profile.pk}), _profile_post_data() ) assert response.status_code == 302 profile.refresh_from_db() assert profile.home_postal_code == "3500" assert profile.home_municipality == "Hasselt" assert profile.home_latitude == Decimal("50.930700") assert profile.desired_titles == ["infrastructure engineer", "cloud engineer"] assert profile.preferred_workplace == ["hybrid"] @pytest.mark.django_db def test_profile_update_clears_stale_coordinates_when_postcode_is_unknown(client, user, profile): client.force_login(user) response = client.post( reverse("profiles:edit", kwargs={"pk": profile.pk}), _profile_post_data(home_postal_code="9999"), follow=True, ) assert response.status_code == 200 assert "De postcode is bewaard" in response.content.decode("utf-8") profile.refresh_from_db() assert profile.home_postal_code == "9999" assert profile.home_municipality == "" assert profile.home_latitude is None assert profile.home_longitude is None