2025-12-17 17:45:18 +01:00
|
|
|
#test_linkpruefer.py
|
2025-12-02 20:55:51 +01:00
|
|
|
import unittest
|
2025-12-17 17:45:18 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
# QGIS-Module mocken, damit der Import funktioniert
|
|
|
|
|
with patch.dict("sys.modules", {
|
|
|
|
|
"qgis": MagicMock(),
|
|
|
|
|
"qgis.PyQt": MagicMock(),
|
|
|
|
|
"qgis.PyQt.QtCore": MagicMock(),
|
|
|
|
|
"qgis.PyQt.QtNetwork": MagicMock(),
|
|
|
|
|
"qgis.core": MagicMock(),
|
|
|
|
|
}):
|
|
|
|
|
from modules.linkpruefer import Linkpruefer
|
2025-12-02 20:55:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestLinkpruefer(unittest.TestCase):
|
|
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
@patch("modules.linkpruefer.QNetworkReply")
|
|
|
|
|
@patch("modules.linkpruefer.QNetworkRequest")
|
|
|
|
|
@patch("modules.linkpruefer.QUrl")
|
|
|
|
|
@patch("modules.linkpruefer.QEventLoop")
|
|
|
|
|
@patch("modules.linkpruefer.QgsNetworkAccessManager")
|
|
|
|
|
def test_remote_link_ok(
|
|
|
|
|
self, mock_manager, mock_loop, mock_url, mock_request, mock_reply
|
|
|
|
|
):
|
|
|
|
|
# Setup: simulate successful HEAD request
|
|
|
|
|
reply_instance = MagicMock()
|
|
|
|
|
reply_instance.error.return_value = mock_reply.NetworkError.NoError
|
|
|
|
|
reply_instance.attribute.return_value = 200
|
|
|
|
|
|
|
|
|
|
mock_manager.return_value.head.return_value = reply_instance
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
lp = Linkpruefer("http://example.com", "REST")
|
|
|
|
|
result = lp.pruefe_link()
|
2025-12-02 20:55:51 +01:00
|
|
|
|
|
|
|
|
self.assertTrue(result.erfolgreich)
|
2025-12-17 17:45:18 +01:00
|
|
|
self.assertEqual(result.daten["quelle"], "remote")
|
|
|
|
|
|
|
|
|
|
@patch("modules.linkpruefer.QNetworkReply")
|
|
|
|
|
@patch("modules.linkpruefer.QNetworkRequest")
|
|
|
|
|
@patch("modules.linkpruefer.QUrl")
|
|
|
|
|
@patch("modules.linkpruefer.QEventLoop")
|
|
|
|
|
@patch("modules.linkpruefer.QgsNetworkAccessManager")
|
|
|
|
|
def test_remote_link_error(
|
|
|
|
|
self, mock_manager, mock_loop, mock_url, mock_request, mock_reply
|
|
|
|
|
):
|
|
|
|
|
# Fake-Reply erzeugen
|
|
|
|
|
reply_instance = MagicMock()
|
|
|
|
|
reply_instance.error.return_value = mock_reply.NetworkError.ConnectionRefusedError
|
|
|
|
|
reply_instance.errorString.return_value = "Connection refused"
|
|
|
|
|
|
|
|
|
|
# WICHTIG: finished-Signal simulieren
|
|
|
|
|
reply_instance.finished = MagicMock()
|
|
|
|
|
reply_instance.finished.connect = MagicMock()
|
|
|
|
|
|
|
|
|
|
# Wenn loop.exec() aufgerufen wird, rufen wir loop.quit() sofort auf
|
|
|
|
|
mock_loop.return_value.exec.side_effect = lambda: mock_loop.return_value.quit()
|
|
|
|
|
|
|
|
|
|
# Manager gibt unser Fake-Reply zurück
|
|
|
|
|
mock_manager.return_value.head.return_value = reply_instance
|
|
|
|
|
|
|
|
|
|
lp = Linkpruefer("http://example.com", "REST")
|
|
|
|
|
result = lp.pruefe_link()
|
2025-12-02 20:55:51 +01:00
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
self.assertFalse(result.erfolgreich)
|
|
|
|
|
self.assertIn("Verbindungsfehler", result.fehler[0])
|
2025-12-02 20:55:51 +01:00
|
|
|
|
|
|
|
|
|
2025-12-17 17:45:18 +01:00
|
|
|
def test_local_link_warning(self):
|
|
|
|
|
lp = Linkpruefer("/path/to/file_without_extension", "OGR")
|
|
|
|
|
result = lp.pruefe_link()
|
2025-12-02 20:55:51 +01:00
|
|
|
|
|
|
|
|
self.assertTrue(result.erfolgreich)
|
|
|
|
|
self.assertIn("ungewöhnlich", result.warnungen[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|