Beispiel #1
0
    def test_search_objects_limited_to_object_permission(self):
        ObjectPermissionFactory.create(object_type=OBJECT_TYPE,
                                       mode=PermissionModes.read_only,
                                       users=[self.user])
        record = ObjectRecordFactory.create(geometry=Point(
            4.905289, 52.369918),
                                            object__object_type=OBJECT_TYPE)
        ObjectRecordFactory.create(geometry=Point(4.905289, 52.369918))
        url = reverse("object-search")

        response = self.client.post(
            url,
            {
                "geometry": {
                    "within": {
                        "type": "Polygon",
                        "coordinates": [POLYGON_AMSTERDAM_CENTRUM],
                    }
                },
                "type": OBJECT_TYPE,
            },
            **GEO_WRITE_KWARGS,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[record.object.uuid])}",
        )
Beispiel #2
0
    def test_filter_comma_separated(self):
        record = ObjectRecordFactory.create(
            data={"dimensions": {"diameter": 4}, "name": "demo"},
            object__object_type=OBJECT_TYPE,
        )
        ObjectRecordFactory.create(
            data={"dimensions": {"diameter": 5}, "name": "demo"},
            object__object_type=OBJECT_TYPE,
        )
        ObjectRecordFactory.create(
            data={"dimensions": {"diameter": 4}, "name": "other"},
            object__object_type=OBJECT_TYPE,
        )

        response = self.client.get(
            self.url, {"data_attrs": "dimensions__diameter__exact__4,name__exact__demo"}
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[record.object.uuid])}",
        )
Beispiel #3
0
    def test_filter_registration_date_list(self):
        # object 1 - show
        object1 = ObjectFactory.create(object_type=OBJECT_TYPE)
        record11 = ObjectRecordFactory.create(
            object=object1, registration_at="2020-01-01"
        )
        record12 = ObjectRecordFactory.create(
            object=object1, registration_at="2021-01-01"
        )
        # object 2 - don't show
        record21 = ObjectRecordFactory.create(
            object__object_type=OBJECT_TYPE, registration_at="2021-01-01"
        )

        url = reverse_lazy("object-list")

        response = self.client.get(url, {"registrationDate": "2020-07-01"})

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[object1.uuid])}",
        )
        self.assertEqual(data[0]["record"]["index"], record11.index)
    def test_filter_objecttype(self):
        record = ObjectRecordFactory.create(geometry=Point(
            4.905289, 52.369918),
                                            object__object_type=OBJECT_TYPE)
        ObjectRecordFactory.create(geometry=Point(4.905289, 52.369918),
                                   object__object_type=OTHER_OBJECT_TYPE)

        response = self.client.post(
            self.url,
            {
                "geometry": {
                    "within": {
                        "type": "Polygon",
                        "coordinates": [POLYGON_AMSTERDAM_CENTRUM],
                    }
                },
                "type": OBJECT_TYPE,
            },
            **GEO_WRITE_KWARGS,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f'http://testserver{reverse("object-detail", args=[record.object.uuid])}',
        )
Beispiel #5
0
    def test_update_object(self, m):
        mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
        m.get(f"{OBJECT_TYPE}/versions/1",
              json=mock_objecttype_version(OBJECT_TYPE))

        # other object - to check that correction works when there is another record with the same index
        ObjectRecordFactory.create(object__object_type=OBJECT_TYPE)
        initial_record = ObjectRecordFactory.create(
            object__object_type=OBJECT_TYPE)
        object = initial_record.object

        assert initial_record.end_at is None

        url = reverse("object-detail", args=[object.uuid])
        data = {
            "type": object.object_type,
            "record": {
                "typeVersion": 1,
                "data": {
                    "plantDate": "2020-04-12",
                    "diameter": 30
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [4.910649523925713, 52.37240093589432],
                },
                "startAt": "2020-01-01",
                "correctionFor": initial_record.index,
            },
        }

        response = self.client.put(url, data, **GEO_WRITE_KWARGS)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        object.refresh_from_db()
        initial_record.refresh_from_db()

        self.assertEqual(object.object_type, OBJECT_TYPE)
        self.assertEqual(object.records.count(), 2)

        current_record = object.current_record

        self.assertEqual(current_record.version, 1)
        self.assertEqual(current_record.data, {
            "plantDate": "2020-04-12",
            "diameter": 30
        })
        self.assertEqual(current_record.geometry.coords,
                         (4.910649523925713, 52.37240093589432))
        self.assertEqual(current_record.start_at, date(2020, 1, 1))
        self.assertEqual(current_record.registration_at, date(2020, 8, 8))
        self.assertIsNone(current_record.end_at)
        self.assertEqual(current_record.correct, initial_record)
        # assert changes to initial record
        self.assertNotEqual(current_record, initial_record)
        self.assertEqual(initial_record.corrected, current_record)
        self.assertEqual(initial_record.end_at, date(2020, 1, 1))
Beispiel #6
0
    def test_filter_object_type(self):
        object = ObjectFactory.create(object_type=OBJECT_TYPE)
        ObjectRecordFactory.create(object=object)
        ObjectFactory.create(object_type=OTHER_OBJECT_TYPE)

        response = self.client.get(self.url, {"type": OBJECT_TYPE})

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[object.uuid])}",
        )
Beispiel #7
0
    def test_filter_date_detail(self):
        object = ObjectFactory.create(object_type=OBJECT_TYPE)
        record1 = ObjectRecordFactory.create(
            object=object, start_at="2020-01-01", end_at="2020-12-31"
        )
        record2 = ObjectRecordFactory.create(object=object, start_at="2021-01-01")

        url = reverse_lazy("object-detail", args=[object.uuid])

        response = self.client.get(url, {"date": "2020-07-01"})

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(data["record"]["index"], record1.index)
Beispiel #8
0
    def test_retrieve_object(self, m):
        object = ObjectFactory.create(object_type=OBJECT_TYPE)
        object_record = ObjectRecordFactory.create(
            object=object,
            start_at=date.today(),
            geometry="POINT (4.910649523925713 52.37240093589432)",
        )
        url = reverse("object-detail", args=[object.uuid])

        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(
            data,
            {
                "url":
                f'http://testserver{reverse("object-detail", args=[object.uuid])}',
                "type": object.object_type,
                "record": {
                    "index": object_record.index,
                    "typeVersion": object_record.version,
                    "data": object_record.data,
                    "geometry": json.loads(object_record.geometry.json),
                    "startAt": object_record.start_at.isoformat(),
                    "endAt": object_record.end_at,
                    "registrationAt":
                    object_record.registration_at.isoformat(),
                    "correctionFor": None,
                    "correctedBy": None,
                },
            },
        )
Beispiel #9
0
    def test_create_object_correction_invalid(self, m):
        mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
        m.get(f"{OBJECT_TYPE}/versions/1",
              json=mock_objecttype_version(OBJECT_TYPE))

        record = ObjectRecordFactory.create()
        url = reverse("object-list")
        data = {
            "type": OBJECT_TYPE,
            "record": {
                "typeVersion": 1,
                "data": {
                    "plantDate": "2020-04-12",
                    "diameter": 30
                },
                "startAt": "2020-01-01",
                "correctionFor": record.index,
            },
        }

        response = self.client.post(url, data, **GEO_WRITE_KWARGS)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(
            Object.objects.exclude(id=record.object.id).count(), 0)

        data = response.json()
        self.assertEqual(
            data["record"]["correctionFor"],
            [f"Object with index={record.index} does not exist."],
        )
Beispiel #10
0
    def test_update_object_type_invalid(self, m):
        old_object_type = "https://example.com/objecttypes/v1/types/qwe109"
        ObjectPermissionFactory(
            object_type=old_object_type,
            mode=PermissionModes.read_and_write,
            users=[self.user],
        )
        mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
        m.get(OBJECT_TYPE, json=mock_objecttype(OBJECT_TYPE))

        initial_record = ObjectRecordFactory.create(
            object__object_type=old_object_type,
            data={
                "plantDate": "2020-04-12",
                "diameter": 30
            },
            version=1,
        )
        object = initial_record.object

        url = reverse("object-detail", args=[object.uuid])
        data = {
            "type": OBJECT_TYPE,
        }

        response = self.client.patch(url, data, **GEO_WRITE_KWARGS)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

        data = response.json()
        self.assertEqual(
            data["type"],
            ["This field can't be changed"],
        )
Beispiel #11
0
    def test_history_object(self, m):
        record1 = ObjectRecordFactory.create(
            object__object_type=OBJECT_TYPE,
            start_at=date(2020, 1, 1),
            geometry="POINT (4.910649523925713 52.37240093589432)",
        )
        object = record1.object
        record2 = ObjectRecordFactory.create(object=object,
                                             start_at=date.today(),
                                             correct=record1)
        url = reverse("object-history", args=[object.uuid])

        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(
            data,
            [
                {
                    "index": 1,
                    "typeVersion": record1.version,
                    "data": record1.data,
                    "geometry": json.loads(record1.geometry.json),
                    "startAt": record1.start_at.isoformat(),
                    "endAt": record2.start_at.isoformat(),
                    "registrationAt": record1.registration_at.isoformat(),
                    "correctionFor": None,
                    "correctedBy": 2,
                },
                {
                    "index": 2,
                    "typeVersion": record2.version,
                    "data": record2.data,
                    "geometry": None,
                    "startAt": record2.start_at.isoformat(),
                    "endAt": None,
                    "registrationAt": date.today().isoformat(),
                    "correctionFor": 1,
                    "correctedBy": None,
                },
            ],
        )
Beispiel #12
0
    def test_delete_object(self, m):
        record = ObjectRecordFactory.create(object__object_type=OBJECT_TYPE)
        object = record.object
        url = reverse("object-detail", args=[object.uuid])

        response = self.client.delete(url)

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(Object.objects.count(), 0)
Beispiel #13
0
    def test_filter_registration_date_detail_no_record(self):
        object = ObjectFactory.create(object_type=OBJECT_TYPE)
        record = ObjectRecordFactory.create(object=object, registration_at="2021-01-01")

        url = reverse_lazy("object-detail", args=[object.uuid])

        response = self.client.get(url, {"registrationDate": "2020-07-01"})

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Beispiel #14
0
    def test_list_objects_limited_to_object_permission(self):
        ObjectPermissionFactory.create(object_type=OBJECT_TYPE,
                                       mode=PermissionModes.read_only,
                                       users=[self.user])
        object = ObjectFactory.create(object_type=OBJECT_TYPE)
        ObjectRecordFactory.create(object=object)
        ObjectFactory.create()
        url = reverse_lazy("object-list")

        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[object.uuid])}",
        )
Beispiel #15
0
    def test_filter_exact_number(self):
        record = ObjectRecordFactory.create(
            data={"diameter": 4}, object__object_type=OBJECT_TYPE
        )
        ObjectRecordFactory.create(
            data={"diameter": 6}, object__object_type=OBJECT_TYPE
        )
        ObjectRecordFactory.create(data={}, object__object_type=OBJECT_TYPE)

        response = self.client.get(self.url, {"data_attrs": "diameter__exact__4"})

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[record.object.uuid])}",
        )
Beispiel #16
0
    def test_filter_lte(self):
        record1 = ObjectRecordFactory.create(
            data={"diameter": 4}, object__object_type=OBJECT_TYPE
        )
        record2 = ObjectRecordFactory.create(
            data={"diameter": 5}, object__object_type=OBJECT_TYPE
        )
        ObjectRecordFactory.create(
            data={"diameter": 6}, object__object_type=OBJECT_TYPE
        )
        ObjectRecordFactory.create(data={}, object__object_type=OBJECT_TYPE)

        response = self.client.get(self.url, {"data_attrs": "diameter__lte__5"})

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()
        data = sorted(data, key=lambda x: x["record"]["data"]["diameter"])

        self.assertEqual(len(data), 2)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[record1.object.uuid])}",
        )
        self.assertEqual(
            data[1]["url"],
            f"http://testserver{reverse('object-detail', args=[record2.object.uuid])}",
        )
Beispiel #17
0
    def test_list_actual_objects(self, m):
        object_record1 = ObjectRecordFactory.create(
            object__object_type=OBJECT_TYPE,
            start_at=date.today(),
        )
        object_record2 = ObjectRecordFactory.create(
            object__object_type=OBJECT_TYPE,
            start_at=date.today() - timedelta(days=10),
            end_at=date.today() - timedelta(days=1),
        )
        url = reverse("object-list")

        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = response.json()

        self.assertEqual(len(data), 1)
        self.assertEqual(
            data[0]["url"],
            f"http://testserver{reverse('object-detail', args=[object_record1.object.uuid])}",
        )
Beispiel #18
0
    def test_patch_object_record(self, m):
        mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
        m.get(f"{OBJECT_TYPE}/versions/1",
              json=mock_objecttype_version(OBJECT_TYPE))

        initial_record = ObjectRecordFactory.create(
            version=1, object__object_type=OBJECT_TYPE, start_at=date.today())
        object = initial_record.object

        url = reverse("object-detail", args=[object.uuid])
        data = {
            "record": {
                "data": {
                    "plantDate": "2020-04-12",
                    "diameter": 30
                },
                "startAt": "2020-01-01",
                "correctionFor": initial_record.index,
            },
        }

        response = self.client.patch(url, data, **GEO_WRITE_KWARGS)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        initial_record.refresh_from_db()

        self.assertEqual(object.records.count(), 2)

        current_record = object.current_record

        self.assertEqual(current_record.version, initial_record.version)
        self.assertEqual(current_record.data, {
            "plantDate": "2020-04-12",
            "diameter": 30
        })
        self.assertEqual(current_record.start_at, date(2020, 1, 1))
        self.assertEqual(current_record.registration_at, date(2020, 8, 8))
        self.assertIsNone(current_record.end_at)
        self.assertEqual(current_record.correct, initial_record)
        # assert changes to initial record
        self.assertNotEqual(current_record, initial_record)
        self.assertEqual(initial_record.corrected, current_record)
        self.assertEqual(initial_record.end_at, date(2020, 1, 1))