forked from AG_QGIS/Plugin_SN_Basis
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
|
|
import unittest
|
|||
|
|
import os
|
|||
|
|
import tempfile
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|||
|
|
from Dateipruefer import (
|
|||
|
|
Dateipruefer,
|
|||
|
|
LeererPfadModus,
|
|||
|
|
DateiEntscheidung,
|
|||
|
|
DateipruefErgebnis
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TestDateipruefer(unittest.TestCase):
|
|||
|
|
def setUp(self):
|
|||
|
|
self.pruefer = Dateipruefer()
|
|||
|
|
self.plugin_pfad = tempfile.gettempdir()
|
|||
|
|
self.standardname = "test_standard.gpkg"
|
|||
|
|
|
|||
|
|
def test_verbotener_leerer_pfad(self):
|
|||
|
|
result = self.pruefer.pruefe(
|
|||
|
|
pfad="",
|
|||
|
|
leer_modus=LeererPfadModus.VERBOTEN
|
|||
|
|
)
|
|||
|
|
self.assertFalse(result.erfolgreich)
|
|||
|
|
self.assertIn("Kein Pfad angegeben.", result.fehler)
|
|||
|
|
|
|||
|
|
def test_standardpfad_wird_verwendet(self):
|
|||
|
|
result = self.pruefer.pruefe(
|
|||
|
|
pfad="",
|
|||
|
|
leer_modus=LeererPfadModus.NUTZE_STANDARD,
|
|||
|
|
standardname=self.standardname,
|
|||
|
|
plugin_pfad=self.plugin_pfad
|
|||
|
|
)
|
|||
|
|
self.assertTrue(result.erfolgreich)
|
|||
|
|
expected_path = os.path.join(self.plugin_pfad, self.standardname)
|
|||
|
|
self.assertEqual(result.pfad, expected_path)
|
|||
|
|
|
|||
|
|
def test_temporärer_layer_wird_erkannt(self):
|
|||
|
|
result = self.pruefer.pruefe(
|
|||
|
|
pfad="",
|
|||
|
|
leer_modus=LeererPfadModus.TEMPORAER_ERLAUBT
|
|||
|
|
)
|
|||
|
|
self.assertTrue(result.erfolgreich)
|
|||
|
|
self.assertIsNone(result.pfad)
|
|||
|
|
self.assertFalse(result.temporär)
|
|||
|
|
|
|||
|
|
def test_existierende_datei_ohne_entscheidung(self):
|
|||
|
|
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
|||
|
|
tmp_path = tmp_file.name
|
|||
|
|
try:
|
|||
|
|
result = self.pruefer.pruefe(
|
|||
|
|
pfad=tmp_path,
|
|||
|
|
leer_modus=LeererPfadModus.VERBOTEN
|
|||
|
|
)
|
|||
|
|
self.assertTrue(result.erfolgreich) # neu: jetzt True, nicht False
|
|||
|
|
self.assertIn("Datei existiert bereits – Entscheidung ausstehend.", result.fehler)
|
|||
|
|
self.assertIsNone(result.entscheidung)
|
|||
|
|
finally:
|
|||
|
|
os.remove(tmp_path)
|
|||
|
|
|
|||
|
|
def test_existierende_datei_mit_entscheidung_ersetzen(self):
|
|||
|
|
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
|||
|
|
tmp_path = tmp_file.name
|
|||
|
|
try:
|
|||
|
|
result = self.pruefer.pruefe(
|
|||
|
|
pfad=tmp_path,
|
|||
|
|
leer_modus=LeererPfadModus.VERBOTEN,
|
|||
|
|
vorhandene_datei_entscheidung=DateiEntscheidung.ERSETZEN
|
|||
|
|
)
|
|||
|
|
self.assertTrue(result.erfolgreich)
|
|||
|
|
self.assertEqual(result.entscheidung, DateiEntscheidung.ERSETZEN)
|
|||
|
|
finally:
|
|||
|
|
os.remove(tmp_path)
|
|||
|
|
|
|||
|
|
def test_datei_nicht_existiert(self):
|
|||
|
|
fake_path = os.path.join(self.plugin_pfad, "nicht_existierend.gpkg")
|
|||
|
|
result = self.pruefer.pruefe(
|
|||
|
|
pfad=fake_path,
|
|||
|
|
leer_modus=LeererPfadModus.VERBOTEN
|
|||
|
|
)
|
|||
|
|
self.assertTrue(result.erfolgreich)
|
|||
|
|
self.assertEqual(result.pfad, fake_path)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
unittest.main()
|