forked from AG_QGIS/Plugin_SN_Basis
Wrappe modular aufgebaut, Tests erfolgreich, Menüleiste und Werzeugleiste werden eingetragen (QT6 und QT5)- (Es fehlen noch Fachplugins, um zu prüfen, ob es auch wirklich in QGIS geht)
This commit is contained in:
@@ -1,107 +1,78 @@
|
||||
# sn_basis/test/test_linkpruefer.py
|
||||
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from sn_basis.modules.linkpruefer import Linkpruefer
|
||||
from sn_basis.modules.pruef_ergebnis import pruef_ergebnis
|
||||
from sn_basis.functions.qgiscore_wrapper import NetworkReply
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Mock-Ergebnisse für network_head()
|
||||
# ---------------------------------------------------------
|
||||
|
||||
class MockResponseOK:
|
||||
ok = True
|
||||
status = 200
|
||||
error = None
|
||||
|
||||
|
||||
class MockResponseNotFound:
|
||||
ok = False
|
||||
status = 404
|
||||
error = "Not Found"
|
||||
|
||||
|
||||
class MockResponseConnectionError:
|
||||
ok = False
|
||||
status = None
|
||||
error = "Connection refused"
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Testklasse
|
||||
# ---------------------------------------------------------
|
||||
|
||||
class TestLinkpruefer(unittest.TestCase):
|
||||
|
||||
# -----------------------------------------------------
|
||||
# 1. Remote-Link erreichbar
|
||||
# -----------------------------------------------------
|
||||
@patch("sn_basis.functions.qgisqt_wrapper.network_head")
|
||||
@patch("sn_basis.modules.linkpruefer.network_head")
|
||||
def test_remote_link_ok(self, mock_head):
|
||||
mock_head.return_value = MockResponseOK()
|
||||
mock_head.return_value = NetworkReply(error=0)
|
||||
|
||||
lp = Linkpruefer("http://example.com", "REST")
|
||||
result = lp.pruefe()
|
||||
lp = Linkpruefer()
|
||||
result = lp.pruefe("http://example.com")
|
||||
|
||||
self.assertTrue(result.ok)
|
||||
self.assertEqual(result.aktion, "ok")
|
||||
self.assertEqual(result.kontext, "http://example.com")
|
||||
|
||||
# -----------------------------------------------------
|
||||
# 2. Remote-Link nicht erreichbar
|
||||
# -----------------------------------------------------
|
||||
@patch("sn_basis.functions.qgisqt_wrapper.network_head")
|
||||
@patch("sn_basis.modules.linkpruefer.network_head")
|
||||
def test_remote_link_error(self, mock_head):
|
||||
mock_head.return_value = MockResponseConnectionError()
|
||||
mock_head.return_value = NetworkReply(error=1)
|
||||
|
||||
lp = Linkpruefer("http://example.com", "REST")
|
||||
result = lp.pruefe()
|
||||
lp = Linkpruefer()
|
||||
result = lp.pruefe("http://example.com")
|
||||
|
||||
self.assertFalse(result.ok)
|
||||
self.assertEqual(result.aktion, "url_nicht_erreichbar")
|
||||
self.assertIn("Connection refused", result.meldung)
|
||||
self.assertEqual(result.kontext, "http://example.com")
|
||||
|
||||
# -----------------------------------------------------
|
||||
# 3. Remote-Link 404
|
||||
# 3. Netzwerkfehler (None)
|
||||
# -----------------------------------------------------
|
||||
@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()
|
||||
@patch("sn_basis.modules.linkpruefer.network_head", return_value=None)
|
||||
def test_remote_link_network_error(self, mock_head):
|
||||
lp = Linkpruefer()
|
||||
result = lp.pruefe("http://example.com")
|
||||
|
||||
self.assertFalse(result.ok)
|
||||
self.assertEqual(result.aktion, "url_nicht_erreichbar")
|
||||
self.assertIn("404", result.meldung)
|
||||
self.assertEqual(result.aktion, "netzwerkfehler")
|
||||
self.assertEqual(result.kontext, "http://example.com")
|
||||
|
||||
# -----------------------------------------------------
|
||||
# 4. Lokaler Pfad existiert nicht
|
||||
# -----------------------------------------------------
|
||||
@patch("sn_basis.functions.syswrapper.file_exists")
|
||||
@patch("sn_basis.modules.linkpruefer.file_exists", return_value=False)
|
||||
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()
|
||||
lp = Linkpruefer()
|
||||
result = lp.pruefe("/path/to/missing/file.shp")
|
||||
|
||||
self.assertFalse(result.ok)
|
||||
self.assertEqual(result.aktion, "pfad_nicht_gefunden")
|
||||
self.assertEqual(result.kontext, Path("/path/to/missing/file.shp"))
|
||||
|
||||
# -----------------------------------------------------
|
||||
# 5. Lokaler Pfad existiert, aber ungewöhnlich
|
||||
# 5. Lokaler Pfad existiert
|
||||
# -----------------------------------------------------
|
||||
@patch("sn_basis.functions.syswrapper.file_exists")
|
||||
def test_local_link_warning(self, mock_exists):
|
||||
mock_exists.return_value = True
|
||||
|
||||
lp = Linkpruefer("/path/to/file_without_extension", "OGR")
|
||||
result = lp.pruefe()
|
||||
@patch("sn_basis.modules.linkpruefer.file_exists", return_value=True)
|
||||
def test_local_link_ok(self, mock_exists):
|
||||
lp = Linkpruefer()
|
||||
result = lp.pruefe("/path/to/file.shp")
|
||||
|
||||
self.assertTrue(result.ok)
|
||||
self.assertEqual(result.aktion, "ok")
|
||||
self.assertIn("ungewöhnlich", result.meldung)
|
||||
self.assertEqual(result.kontext, Path("/path/to/file.shp"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user