2025-12-18 22:00:31 +01:00
|
|
|
# sn_basis/test/test_linkpruefer.py
|
|
|
|
|
|
2025-12-02 20:55:51 +01:00
|
|
|
import unittest
|
2025-12-18 22:00:31 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
from sn_basis.modules.linkpruefer import Linkpruefer
|
|
|
|
|
from sn_basis.modules.pruef_ergebnis import pruef_ergebnis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
# Mock-Ergebnisse für network_head()
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class MockResponseOK:
|
|
|
|
|
ok = True
|
|
|
|
|
status = 200
|
|
|
|
|
error = None
|
|
|
|
|
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
class MockResponseNotFound:
|
|
|
|
|
ok = False
|
|
|
|
|
status = 404
|
|
|
|
|
error = "Not Found"
|
2025-12-02 20:55:51 +01:00
|
|
|
|
|
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
class MockResponseConnectionError:
|
|
|
|
|
ok = False
|
|
|
|
|
status = None
|
|
|
|
|
error = "Connection refused"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
# Testklasse
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
2025-12-02 20:55:51 +01:00
|
|
|
class TestLinkpruefer(unittest.TestCase):
|
|
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
# -----------------------------------------------------
|
|
|
|
|
# 1. Remote-Link erreichbar
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
@patch("sn_basis.functions.qgisqt_wrapper.network_head")
|
|
|
|
|
def test_remote_link_ok(self, mock_head):
|
|
|
|
|
mock_head.return_value = MockResponseOK()
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
lp = Linkpruefer("http://example.com", "REST")
|
2025-12-18 22:00:31 +01:00
|
|
|
result = lp.pruefe()
|
2025-12-17 17:45:18 +01:00
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
self.assertTrue(result.ok)
|
|
|
|
|
self.assertEqual(result.aktion, "ok")
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
# -----------------------------------------------------
|
|
|
|
|
# 2. Remote-Link nicht erreichbar
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
@patch("sn_basis.functions.qgisqt_wrapper.network_head")
|
|
|
|
|
def test_remote_link_error(self, mock_head):
|
|
|
|
|
mock_head.return_value = MockResponseConnectionError()
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
lp = Linkpruefer("http://example.com", "REST")
|
|
|
|
|
result = lp.pruefe()
|
|
|
|
|
|
|
|
|
|
self.assertFalse(result.ok)
|
|
|
|
|
self.assertEqual(result.aktion, "url_nicht_erreichbar")
|
|
|
|
|
self.assertIn("Connection refused", result.meldung)
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
# 3. Remote-Link 404
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
@patch("sn_basis.functions.qgisqt_wrapper.network_head")
|
|
|
|
|
def test_remote_link_404(self, mock_head):
|
|
|
|
|
mock_head.return_value = MockResponseNotFound()
|
|
|
|
|
|
|
|
|
|
lp = Linkpruefer("http://example.com/missing", "REST")
|
|
|
|
|
result = lp.pruefe()
|
|
|
|
|
|
|
|
|
|
self.assertFalse(result.ok)
|
|
|
|
|
self.assertEqual(result.aktion, "url_nicht_erreichbar")
|
|
|
|
|
self.assertIn("404", result.meldung)
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
# 4. Lokaler Pfad existiert nicht
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
@patch("sn_basis.functions.syswrapper.file_exists")
|
|
|
|
|
def test_local_link_not_found(self, mock_exists):
|
|
|
|
|
mock_exists.return_value = False
|
|
|
|
|
|
|
|
|
|
lp = Linkpruefer("/path/to/missing/file.shp", "OGR")
|
|
|
|
|
result = lp.pruefe()
|
|
|
|
|
|
|
|
|
|
self.assertFalse(result.ok)
|
|
|
|
|
self.assertEqual(result.aktion, "pfad_nicht_gefunden")
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
# 5. Lokaler Pfad existiert, aber ungewöhnlich
|
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
@patch("sn_basis.functions.syswrapper.file_exists")
|
|
|
|
|
def test_local_link_warning(self, mock_exists):
|
|
|
|
|
mock_exists.return_value = True
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
lp = Linkpruefer("/path/to/file_without_extension", "OGR")
|
2025-12-18 22:00:31 +01:00
|
|
|
result = lp.pruefe()
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-18 22:00:31 +01:00
|
|
|
self.assertTrue(result.ok)
|
|
|
|
|
self.assertEqual(result.aktion, "ok")
|
|
|
|
|
self.assertIn("ungewöhnlich", result.meldung)
|
2025-12-02 20:55:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|