Exemple #1
0
    def test_update_traffic_sign_real_3d_location(self):
        user = get_user()
        owner = get_owner()
        data = {
            "location": Point(5, 5, srid=settings.SRID),
            "z_coord": 20,
            "direction": 0,
            "created_by": user.id,
            "updated_by": user.id,
            "owner": owner.pk,
            "lifecycle": Lifecycle.ACTIVE,
            "device_type": get_traffic_control_device_type().pk,
        }
        user = get_user()
        traffic_sign_real = TrafficSignReal.objects.create(
            location=Point(10, 10, 5, srid=settings.SRID),
            direction=0,
            created_by=user,
            updated_by=user,
            owner=owner,
            lifecycle=Lifecycle.ACTIVE,
        )
        form = TrafficSignRealModelForm(data=data, instance=traffic_sign_real)
        self.assertEqual(form.fields["z_coord"].initial, 5)
        self.assertTrue(form.is_valid())

        instance = form.save()
        self.assertEqual(instance.location, Point(5, 5, 20, srid=settings.SRID))
def test__api_operational_area_permission__create(model, location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains=model.lower())
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    device_type = get_traffic_control_device_type()

    location = location.ewkt if location else None

    if model == "Plan":
        data = {
            "name": "Test plan",
            "plan_number": "2020_1",
            "location": location,
            "planner": user.pk,
            "decision_maker": user.pk,
            "plans": {
                "barrier": [],
                "mount": [],
                "road_marking": [],
                "signpost": [],
                "traffic_light": [],
                "traffic_sign": [],
                "additional_sign": [],
            },
        }
    else:
        data = {
            "location": location,
            "device_type": device_type.pk,
            "decision_date": "2020-07-01",
            "lifecycle": Lifecycle.ACTIVE.value,
            "owner": get_owner().pk,
        }

        if model in ["BarrierPlan", "BarrierReal"]:
            data["road_name"] = "testroad"
        elif model in ["RoadMarkingPlan", "RoadMarkingReal"]:
            data["source_id"] = 1
            data["source_name"] = "test source"

    api_client = get_api_client(user=user)
    response = api_client.post(reverse(f"v1:{model.lower()}-list"),
                               data=data,
                               format="json")

    ModelClass = getattr(models, model)  # noqa: N806
    if success:
        assert response.status_code == status.HTTP_201_CREATED
        assert ModelClass.objects.count() == 1
    elif not location:
        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert response.json() == {
            "location": [_("This field may not be null.")]
        }
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert ModelClass.objects.count() == 0
def test__traffic_control_device_type__target_model__update_is_valid(
        new_target_model, factory):
    device_type = get_traffic_control_device_type()
    factory(device_type=device_type)

    device_type.target_model = new_target_model
    device_type.save()

    device_type.refresh_from_db()
    assert device_type.target_model is new_target_model
 def setUp(self):
     self.user = get_user()
     self.main_sign = TrafficSignPlan.objects.create(
         location=Point(0, 0, 10, srid=settings.SRID),
         direction=0,
         device_type=get_traffic_control_device_type(code="T123"),
         created_by=self.user,
         updated_by=self.user,
         owner=get_owner(),
     )
def test__api_operational_area_permission__update(model, location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains=model.lower())
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    device_type = get_traffic_control_device_type()
    instance = model_factory_map[model](location=location)

    if model == "Plan":
        data = {
            "name": "Test plan",
            "plan_number": "2020_1",
            "location": location.ewkt,
            "planner": user.pk,
            "decision_maker": user.pk,
            "plans": {
                "barrier": [],
                "mount": [],
                "road_marking": [],
                "signpost": [],
                "traffic_light": [],
                "traffic_sign": [],
                "additional_sign": [],
            },
        }
    else:
        data = {
            "location": location.ewkt,
            "device_type": device_type.pk,
            "decision_date": "2020-07-01",
            "lifecycle": Lifecycle.ACTIVE.value,
            "owner": get_owner().pk,
        }

        if model in ["BarrierPlan", "BarrierReal"]:
            data["road_name"] = "testroad"
        elif model in ["RoadMarkingPlan", "RoadMarkingReal"]:
            data["source_id"] = 1
            data["source_name"] = "test source"

    api_client = get_api_client(user=user)
    response = api_client.put(
        reverse(f"v1:{model.lower()}-detail", kwargs={"pk": instance.pk}),
        data,
        format="json",
    )

    instance.refresh_from_db()
    if success:
        assert response.status_code == status.HTTP_200_OK
        assert instance.updated_by == user
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert instance.updated_by != user
def _get_additional_sign_content_plan(device_type):
    """
    Get AdditionalSignContentPlan instance and set its related models
    interfering device_types to universal one.
    """
    universal_device_type = get_traffic_control_device_type(code="123",
                                                            target_model=None)
    obj = get_additional_sign_content_plan(device_type=device_type)
    obj.parent.parent.device_type = universal_device_type
    obj.parent.parent.save()
    return obj
def test__traffic_control_device_type__target_model__update_is_invalid(
        new_target_model, factory):
    device_type = get_traffic_control_device_type()
    factory(device_type=device_type)

    with pytest.raises(ValidationError):
        device_type.target_model = new_target_model
        device_type.save()

    device_type.refresh_from_db()
    assert not device_type.target_model
def test__device_type__response_code_attribute():
    client = get_api_client()
    dt = get_traffic_control_device_type()

    response = client.get(
        reverse("v1:trafficcontroldevicetype-detail", kwargs={"pk": dt.pk}))
    response_data = response.json()

    assert "code" in response_data, (
        "Removing `code` attribute from the TrafficControlDeviceType API will break "
        "the admin traffic sign icon frontend functionality in "
        "AdminTrafficSignIconSelectWidget!")
def test__traffic_control_device_type__target_model__restricts_relations(
        allowed_value, factory):
    related_obj = factory()

    for choice in DeviceTypeTargetModel:
        device_type = get_traffic_control_device_type(code=get_random_string(),
                                                      target_model=choice)

        if choice == allowed_value:
            related_obj.device_type = device_type
            related_obj.save(update_fields=["device_type"])
            related_obj.refresh_from_db()
            assert related_obj.device_type == device_type
        else:
            with pytest.raises(ValidationError):
                related_obj.device_type = device_type
                related_obj.save(update_fields=["device_type"])
Exemple #10
0
 def test_create_traffic_sign_real_3d_location(self):
     user = get_user()
     data = {
         "location": Point(10, 10, srid=settings.SRID),
         "z_coord": 20,
         "direction": 0,
         "created_by": user.id,
         "updated_by": user.id,
         "owner": get_owner().pk,
         "lifecycle": Lifecycle.ACTIVE,
         "device_type": get_traffic_control_device_type().pk,
     }
     form = TrafficSignRealModelForm(data=data)
     form.is_valid()
     self.assertTrue(form.is_valid())
     instance = form.save()
     self.assertEqual(instance.location, Point(10, 10, 20, srid=settings.SRID))
def test__api_operational_area_permission__create__geojson(location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains="barrierplan")
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    device_type = get_traffic_control_device_type()

    if location:
        location = json.loads(location.geojson)
        location.update({
            "crs": {
                "type": "name",
                "properties": {
                    "name": f"EPSG:{settings.SRID}"
                }
            }
        })

    data = {
        "location": location,
        "device_type": device_type.pk,
        "decision_date": "2020-07-01",
        "lifecycle": Lifecycle.ACTIVE.value,
        "owner": get_owner().pk,
        "road_name": "testroad",
    }

    api_client = get_api_client(user=user)
    response = api_client.post(
        f"{reverse('v1:barrierplan-list')}?geo_format=geojson",
        data=data,
        format="json")

    if success:
        assert response.status_code == status.HTTP_201_CREATED
        assert BarrierPlan.objects.count() == 1
    elif not location:
        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert response.json() == {
            "location": [_("This field may not be null.")]
        }
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert BarrierPlan.objects.count() == 0
    def test__traffic_sign_type__filtering(self):
        dt_1 = get_traffic_control_device_type(code="A1")
        dt_2 = get_traffic_control_device_type(code="A2")
        get_traffic_control_device_type(code="B1")
        get_traffic_control_device_type(code="C1")

        response = self.client.get(reverse("v1:trafficcontroldevicetype-list"),
                                   data={"traffic_sign_type": "A"})

        response_data = response.json()
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response_data["count"], 2)
        self.assertEqual(response_data["results"][0]["id"], str(dt_1.pk))
        self.assertEqual(response_data["results"][1]["id"], str(dt_2.pk))
def test__traffic_control_device_type__target_model__validate_multiple_invalid_relations(
):
    device_type = get_traffic_control_device_type()
    get_barrier_plan(device_type=device_type)
    get_barrier_real(device_type=device_type)
    get_road_marking_plan(device_type=device_type)
    get_road_marking_real(device_type=device_type)
    get_signpost_plan(device_type=device_type)
    get_signpost_real(device_type=device_type)
    get_traffic_light_plan(device_type=device_type)
    get_traffic_light_real(device_type=device_type)
    get_traffic_sign_plan(device_type=device_type)
    get_traffic_sign_real(device_type=device_type)
    get_additional_sign_content_plan(device_type=device_type)
    get_additional_sign_content_real(device_type=device_type)

    for target_model in DeviceTypeTargetModel:
        with pytest.raises(ValidationError):
            device_type.target_model = target_model
            device_type.save()

        device_type.refresh_from_db()
        assert not device_type.target_model
def test__traffic_control_device_type__traffic_sign_type(code, expected_value):
    device_type = get_traffic_control_device_type(code=code)

    assert device_type.traffic_sign_type == expected_value