Beispiel #1
0
    def test_update_features_with_parent(self):
        # insert features in the database
        geojson_data = [self.data_geojson_level_0, self.data_geojson_level_1]

        with patch("builtins.open") as mock_file:
            mock_file.return_value.__enter__ = lambda filename: filename
            mock_file.return_value.__exit__ = Mock()
            mock_file.return_value.read.side_effect = lambda: geojson_data.pop(
                0)

            with CaptureSTDOUT():
                call_command("import_geojson", "admin_level_0_simplified.json",
                             "admin_level_1_simplified.json")

        self.assertEqual(AdminBoundary.objects.count(), 2)

        # update features
        geojson_data = [self.data_geojson_level_0, self.data_geojson_level_1]

        with patch("builtins.open") as mock_file:
            mock_file.return_value.__enter__ = lambda filename: filename
            mock_file.return_value.__exit__ = Mock()
            mock_file.return_value.read.side_effect = lambda: geojson_data.pop(
                0)

            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "admin_level_0_simplified.json",
                             "admin_level_1_simplified.json")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing admin_level_0_simplified.json\n ** updating Granica (R1000)\n ** removing unseen boundaries (R1000)\n=== parsing admin_level_1_simplified.json\n ** updating Međa 2 (R2000)\n ** removing unseen boundaries (R2000)\nOther unseen boundaries removed: 0\n ** updating paths for all of Granica\n",
        )

        self.assertEqual(AdminBoundary.objects.count(), 2)
Beispiel #2
0
    def test_feature_without_geometry(self):
        with patch("builtins.open",
                   mock_open(read_data=self.data_geojson_feature_no_geometry)):
            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "admin_level_0_simplified.json")

        self.assertEqual(captured_output.getvalue(),
                         "=== parsing admin_level_0_simplified.json\n")

        self.assertEqual(AdminBoundary.objects.count(), 0)
Beispiel #3
0
    def test_filename_with_no_features(self):
        with patch("builtins.open",
                   mock_open(read_data=self.data_geojson_no_features)):
            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "R188933admin0.json")

        self.assertEqual(captured_output.getvalue(),
                         "=== parsing R188933admin0.json\n")

        self.assertEqual(AdminBoundary.objects.count(), 0)
Beispiel #4
0
    def test_feature_multipolygon_geometry(self):
        with patch("builtins.open",
                   mock_open(read_data=self.data_geojson_multipolygon)):
            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "admin_level_0_simplified.json")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing admin_level_0_simplified.json\n ** adding Granica (R1000)\n ** removing unseen boundaries (R1000)\nOther unseen boundaries removed: 0\n ** updating paths for all of Granica\n",
        )

        self.assertEqual(AdminBoundary.objects.count(), 1)
Beispiel #5
0
    def test_missing_parent_in_db(self):
        with patch("builtins.open",
                   mock_open(read_data=self.data_geojson_without_parent)):
            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "admin_level_1_simplified.json")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing admin_level_1_simplified.json\nSkipping Međa (R2000) as parent R0 not found.\n",
        )

        self.assertEqual(AdminBoundary.objects.count(), 0)
Beispiel #6
0
    def test_ok_filename_admin(self):
        with patch("builtins.open",
                   mock_open(read_data=self.data_geojson_level_0)):
            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "R188933admin0.json")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing R188933admin0.json\n ** adding Granica (R1000)\n ** removing unseen boundaries (R1000)\nOther unseen boundaries removed: 0\n ** updating paths for all of Granica\n",
        )

        self.assertEqual(AdminBoundary.objects.count(), 1)
Beispiel #7
0
    def test_wrong_filename(self):
        with patch("builtins.open",
                   mock_open(read_data=self.data_geojson_level_0)):
            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "data.json")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing data.json\nSkipping 'data.json', doesn't match file pattern.\n"
        )

        self.assertEqual(AdminBoundary.objects.count(), 0)
Beispiel #8
0
    def test_remove_other_unseen_boundaries(self):
        # other unseen boundaries are boundaries which have not been updated in any way for a country

        # insert features in the database
        geojson_data = [self.data_geojson_level_0, self.data_geojson_level_1]

        with patch("builtins.open") as mock_file:
            mock_file.return_value.__enter__ = lambda filename: filename
            mock_file.return_value.__exit__ = Mock()
            mock_file.return_value.read.side_effect = lambda: geojson_data.pop(
                0)

            with CaptureSTDOUT():
                call_command("import_geojson", "admin_level_0_simplified.json",
                             "admin_level_1_simplified.json")

        self.assertEqual(AdminBoundary.objects.count(), 2)

        # update data, and add a new boundary
        geojson_data = [self.data_geojson_level_0]

        with patch("builtins.open") as mock_file:
            mock_file.return_value.__enter__ = lambda filename: filename
            mock_file.return_value.__exit__ = Mock()
            mock_file.return_value.read.side_effect = lambda: geojson_data.pop(
                0)

            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "admin_level_0_simplified.json")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing admin_level_0_simplified.json\n ** updating Granica (R1000)\n ** removing unseen boundaries (R1000)\nOther unseen boundaries removed: 1\n ** updating paths for all of Granica\n",
        )

        self.assertEqual(AdminBoundary.objects.count(), 1)
Beispiel #9
0
    def test_zipfiles_parsing(self):
        with patch("temba.locations.management.commands.import_geojson.ZipFile"
                   ) as zipfile_patched:
            zipfile_patched().namelist.return_value = [
                "admin_level_0_simplified.json"
            ]

            zipfile_patched(
            ).open.return_value.__enter__ = lambda filename: filename
            zipfile_patched().open.return_value.__exit__ = Mock()
            zipfile_patched(
            ).open.return_value.read.return_value = self.data_geojson_level_0

            with CaptureSTDOUT() as captured_output:
                call_command("import_geojson", "admin_level_0_simplified.zip")

        self.assertEqual(
            captured_output.getvalue(),
            "=== parsing admin_level_0_simplified.json\n ** adding Granica (R1000)\n ** removing unseen boundaries (R1000)\nOther unseen boundaries removed: 0\n ** updating paths for all of Granica\n",
        )

        self.assertEqual(AdminBoundary.objects.count(), 1)