Пример #1
0
def test_get_current_memberships_on_areas(membership_data, expected):
    area = factories.CountryFactory()
    for data in membership_data(area):
        factories.GeographicalMembershipFactory(**data)
    set_current_transaction(Transaction.objects.last())

    assert len(area.get_current_memberships()) == expected
Пример #2
0
def test_set_current_transaction():
    tx = factories.TransactionFactory.create()
    assert tx is not None
    assert get_current_transaction() is None

    set_current_transaction(tx)
    assert get_current_transaction() is tx
Пример #3
0
def test_get_current_memberships_on_groups(membership_data, expected):
    group = factories.GeoGroupFactory()
    for data in membership_data(group):
        factories.GeographicalMembershipFactory(**data)
    set_current_transaction(Transaction.objects.last())

    assert len(group.get_current_memberships()) == expected
Пример #4
0
def test_measure_form_cleaned_data_geo_exclusions_erga_omnes(
    erga_omnes,
    session_with_workbasket,
):
    excluded_country1 = factories.GeographicalAreaFactory.create()
    excluded_country2 = factories.GeographicalAreaFactory.create()
    factories.GeographicalAreaFactory.create()
    measure = factories.MeasureFactory.create(geographical_area=erga_omnes)
    data = model_to_dict(measure)
    start_date = data["valid_between"].lower
    data.update(
        start_date_0=start_date.day,
        start_date_1=start_date.month,
        start_date_2=start_date.year,
    )
    exclusions_data = {
        "geo_area":
        "ERGA_OMNES",
        "erga_omnes_exclusions_formset-0-erga_omnes_exclusion":
        excluded_country1.pk,
        "erga_omnes_exclusions_formset-1-erga_omnes_exclusion":
        excluded_country2.pk,
    }
    data.update(exclusions_data)
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureForm(
        data=data,
        initial={},
        instance=Measure.objects.first(),
        request=session_with_workbasket,
    )
    assert form.is_valid()
    assert form.cleaned_data["exclusions"]
    assert not set(form.cleaned_data["exclusions"]).difference(
        {excluded_country1, excluded_country2}, )
Пример #5
0
def measure_form(measure_form_data, session_with_workbasket, erga_omnes):
    set_current_transaction(Transaction.objects.last())
    return MeasureForm(
        data=measure_form_data,
        instance=Measure.objects.first(),
        request=session_with_workbasket,
        initial={},
    )
Пример #6
0
def test_override_current_transaction():
    tx = factories.TransactionFactory.create()
    tx2 = factories.TransactionFactory.create()

    set_current_transaction(tx)

    with override_current_transaction(tx2):
        assert get_current_transaction() is tx2

    assert get_current_transaction() is tx
Пример #7
0
def test_measure_forms_geo_area_invalid_data_error_messages(
        data, error, erga_omnes):
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert not form.is_valid()
    assert error in form.errors["geo_area"]
Пример #8
0
def test_measure_forms_geo_area_valid_data_erga_omnes(erga_omnes):
    data = {
        f"{GEO_AREA_FORM_PREFIX}-geo_area": forms.GeoAreaType.ERGA_OMNES,
    }
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert form.is_valid()
Пример #9
0
def test_measure_forms_geo_area_invalid_data_geo_group(erga_omnes):
    geo_area1 = factories.GeographicalAreaFactory.create()
    data = {
        f"{GEO_AREA_FORM_PREFIX}-geo_area": forms.GeoAreaType.GROUP,
        "geographical_area_group-geographical_area_group": geo_area1.pk,
    }
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert not form.is_valid()
    assert "A country group is required." in form.errors["geo_area"][0]
Пример #10
0
def test_measure_forms_geo_area_valid_data_erga_omnes_exclusions(erga_omnes):
    geo_area1 = factories.GeographicalAreaFactory.create()
    geo_area2 = factories.GeographicalAreaFactory.create()
    data = {
        f"{GEO_AREA_FORM_PREFIX}-geo_area": forms.GeoAreaType.ERGA_OMNES,
        "erga_omnes_exclusions_formset-0-erga_omnes_exclusion": geo_area1.pk,
        "erga_omnes_exclusions_formset-1-erga_omnes_exclusion": geo_area2.pk,
    }
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert form.is_valid()
Пример #11
0
def test_with_latest_description_multiple_version():
    """Tests that, after updating a geo area description,
    with_current_description returns a queryset with one geo area annotated with
    only the latest description."""
    description = factories.GeographicalAreaDescriptionFactory.create(
        description="blarghhh", )
    current_description = description.new_version(
        description.transaction.workbasket,
        description="bleurgh",
    )
    set_current_transaction(current_description.transaction)
    qs = models.GeographicalArea.objects.current().with_latest_description()

    assert qs.count() == 1
    assert qs.first().description == "bleurgh"
Пример #12
0
def test_measure_forms_geo_area_valid_data_countries_add(erga_omnes):
    geo_area1 = factories.GeographicalAreaFactory.create()
    data = {
        f"{GEO_AREA_FORM_PREFIX}-geo_area": forms.GeoAreaType.COUNTRY,
        "country_region_formset-0-geographical_area_country_or_region":
        geo_area1.pk,
        "country_region_formset-ADD": "1",
    }
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert not form.is_valid()
Пример #13
0
def test_measure_forms_geo_area_valid_data_geo_group(erga_omnes):
    geo_group = factories.GeographicalAreaFactory.create(
        area_code=AreaCode.GROUP)
    data = {
        f"{GEO_AREA_FORM_PREFIX}-geo_area": forms.GeoAreaType.GROUP,
        f"{GEO_AREA_FORM_PREFIX}-geographical_area_group": geo_group.pk,
    }
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert form.is_valid()
    # https://uktrade.atlassian.net/browse/TP2000-437 500 error where object instead of a list of objects
    assert type(form.cleaned_data["geo_area_list"]) == list
Пример #14
0
def test_measure_forms_geo_area_valid_data_geo_group_exclusions(erga_omnes):
    geo_group = factories.GeographicalAreaFactory.create(
        area_code=AreaCode.GROUP)
    geo_area1 = factories.GeographicalAreaFactory.create()
    data = {
        f"{GEO_AREA_FORM_PREFIX}-geo_area": forms.GeoAreaType.GROUP,
        f"{GEO_AREA_FORM_PREFIX}-geographical_area_group": geo_group.pk,
        "geo_group_exclusions_formset-0-geo_group_exclusion": geo_area1.pk,
    }
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureGeographicalAreaForm(
        data,
        initial={},
        prefix=GEO_AREA_FORM_PREFIX,
    )
    assert form.is_valid()
Пример #15
0
def test_get_current_memberships_when_region_and_country_share_sid():
    country = factories.CountryFactory.create()
    region = factories.RegionFactory.create(sid=country.sid)
    country_membership = factories.GeographicalMembershipFactory.create(
        member=country)
    region_membership = factories.GeographicalMembershipFactory.create(
        member=region)
    set_current_transaction(Transaction.objects.last())
    country_memberships = country.get_current_memberships()
    region_memberships = region.get_current_memberships()

    assert country_memberships.count() == 2
    assert region_memberships.count() == 2
    assert country_membership in country_memberships
    assert region_membership in country_memberships
    assert country_membership in region_memberships
    assert region_membership in region_memberships
Пример #16
0
def test_with_latest_description_multiple_descriptions(date_ranges):
    """Tests that, where multiple current descriptions exist for an area, the
    description with the latest validity_start date is returned by
    with_latest_description."""
    earlier_description = factories.GeographicalAreaDescriptionFactory.create(
        validity_start=date_ranges.earlier.lower, )
    later_description = factories.GeographicalAreaDescriptionFactory.create(
        described_geographicalarea=earlier_description.
        described_geographicalarea,
        validity_start=date_ranges.later.lower,
    )
    set_current_transaction(later_description.transaction)
    qs = models.GeographicalArea.objects.current().with_latest_description()

    # sanity check that description objects have been created with different description values
    assert earlier_description.description != later_description.description
    assert qs.first().description == later_description.description
Пример #17
0
def test_with_current_descriptions(approved_workbasket):
    """Tests that, where multiple current descriptions exist for an area,
    with_current_descriptions returns a queryset annotated with those
    descriptions chained together and separated by a space."""
    description_1 = factories.GeographicalAreaDescriptionFactory.create(
        transaction__workbasket=approved_workbasket, )
    description_2 = factories.GeographicalAreaDescriptionFactory.create(
        transaction__workbasket=approved_workbasket,
        described_geographicalarea=description_1.described_geographicalarea,
    )
    set_current_transaction(description_2.transaction)
    qs = models.GeographicalArea.objects.current().with_current_descriptions()

    area = qs.first()
    # sanity check that description is not an empty string or None
    assert area.description
    assert (area.description ==
            f"{description_1.description} {description_2.description}")
Пример #18
0
def test_geographical_area_list_queryset():
    """Tests that geo area list queryset contains only the latest version of a
    geo_area annotated with the current description."""
    description = factories.GeographicalAreaDescriptionFactory.create(
        description="Englund",  # /PS-IGNORE
    )
    new_description = description.new_version(
        description.transaction.workbasket,
        description="England",  # /PS-IGNORE
    )
    new_area = new_description.described_geographicalarea.new_version(
        description.transaction.workbasket, )
    view = GeoAreaList()
    qs = view.get_queryset()
    set_current_transaction(new_area.transaction)

    assert qs.count() == 1
    assert qs.first().description == "England"  # /PS-IGNORE
    assert qs.first() == new_area
Пример #19
0
def test_measure_form_valid_data(erga_omnes, session_with_workbasket):
    measure = factories.MeasureFactory.create()
    data = model_to_dict(measure)
    data["geo_area"] = "COUNTRY"
    data["country_region-geographical_area_country_or_region"] = data[
        "geographical_area"]
    start_date = data["valid_between"].lower
    data.update(
        start_date_0=start_date.day,
        start_date_1=start_date.month,
        start_date_2=start_date.year,
    )
    set_current_transaction(Transaction.objects.last())
    form = forms.MeasureForm(
        data=data,
        initial={},
        instance=Measure.objects.first(),
        request=session_with_workbasket,
    )
    assert form.is_valid()
Пример #20
0
def test_current_objects_model_manager(model1_with_history):
    for model_version in model1_with_history.all_models:
        set_current_transaction(model_version.transaction)
        assert TestModel1.current_objects.count() == 1
        assert TestModel1.current_objects.get() == model_version