Browse Source

Mise en place système de tests unitaires

omassot 7 years ago
parent
commit
9ba2894b1e
10 changed files with 166 additions and 9 deletions
  1. 2 0
      core/constants.py
  2. 4 2
      core/logging_.py
  3. 0 3
      main.py
  4. 2 1
      schemas/mn1_rec.py
  5. 0 0
      test/__init__.py
  6. 27 0
      test/_base.py
  7. 14 0
      test/_stream.py
  8. 87 0
      test/test_mn1_rec.py
  9. 29 2
      ui/dlg_contact.py
  10. 1 1
      ui/dlg_main.py

+ 2 - 0
core/constants.py

@@ -8,12 +8,14 @@ from path import Path
 
 VERSION = "0.4.0"
 
+DEBUG = True
 
 MAIN = Path(__file__).parent.parent.abspath()
 
 CONTACT = "sig@manchenumerique.fr"
 
 RSCDIR = MAIN / "ui" / "rsc"
+TESTDIR = MAIN / "test"
 
 LOGCONF = MAIN / "core" / "logging.yaml"
 

+ 4 - 2
core/logging_.py

@@ -19,8 +19,7 @@ _to_qgis_level = {logging.DEBUG: 0,
                   logging.ERROR: 2,
                   logging.CRITICAL: 2, 
                   logging.NOTSET: 4}
-
-
+    
 class QgsLogHandler(logging.Handler):
     
     _qgs_iface = None
@@ -44,6 +43,9 @@ class QgsLogHandler(logging.Handler):
         except:
             self.handleError(record)
 
+    def write(self, msg):
+        QgsMessageLog.logMessage(msg, "MnCheck", 0)
+
 
 SYS_EXCEPT_HOOK = sys.excepthook
 

+ 0 - 3
main.py

@@ -26,7 +26,6 @@ __VERSION__ = VERSION
 logger = logging.getLogger("mncheck")
 logging_.start("mncheck", logging.DEBUG)
 
-
 logger.info("* loading plugin")
 logger.info(f"version: {VERSION}")
 
@@ -97,5 +96,3 @@ class MnCheck:
         self.main_window = dlg
         dlg.show()
         dlg.exec_()
-        
-

+ 2 - 1
schemas/mn1_rec.py

@@ -556,7 +556,6 @@ class Mn1Checker(BaseChecker):
                 self.log_error("Le nom du PBO ne coincide avec le nom d'aucune des ZAPBO qui le contiennent", item=equipement)
                 break
             
-
     # a venir (webservice?)
     def __test_pbo_dimension(self):
         """ Dimensionnement des PBO """
@@ -582,5 +581,7 @@ class Mn1Checker(BaseChecker):
         
 
 checkers = [Mn1Checker]
+    
+
 
 

+ 0 - 0
test/__init__.py


+ 27 - 0
test/_base.py

@@ -0,0 +1,27 @@
+'''
+
+@author: olivier.massot, 2019
+'''
+from core import mncheck
+from qgis.core import QgsProject
+import unittest
+
+
+class SchemaTest(unittest.TestCase):
+    SCHEMA_NAME = ""
+    PROJECT_FILE = ""
+    
+    def _open_qgs_project(self, filename):
+        QgsProject.instance().clear()
+        QgsProject.instance().read(filename)
+
+    def _close_qgs_project(self):
+        QgsProject.instance().clear()
+        
+    def setUp(self):
+        
+        if self.PROJECT_FILE:
+            self._open_qgs_project(self.PROJECT_FILE)
+            
+        if self.SCHEMA_NAME:
+            self.schema = mncheck.get_schema(self.SCHEMA_NAME)

+ 14 - 0
test/_stream.py

@@ -0,0 +1,14 @@
+'''
+
+@author: olivier.massot, 2019
+'''
+
+
+
+class TestStream():
+    def write(self, s):
+        print(str(s).strip())
+        
+    def flush(self):
+        pass
+    

+ 87 - 0
test/test_mn1_rec.py

@@ -0,0 +1,87 @@
+'''
+
+@author: olivier.massot, janv. 2019
+'''
+import logging
+
+from path import Path
+from test._base import SchemaTest
+
+logger = logging.getLogger("mncheck")
+
+class Test(SchemaTest):
+    SCHEMA_NAME = "mn1_rec"
+    PROJECT_FILE = Path(__file__).parent / 'projects' / 'mn1_rec' / '01_valid' / '01_valid.qgs'
+
+    def test_load_layers(self):
+        pass
+        
+    def test_scr(self):
+        pass
+    
+    def test_structure_arteres(self):
+        pass
+    
+    def test_structure_cables(self):
+        pass
+    
+    def test_structure_equipements(self):
+        pass
+        
+    def test_structure_noeuds(self):
+        pass
+
+    def test_structure_tranchees(self):
+        pass
+
+    def test_structure_zapbos(self):
+        pass
+
+    def test_geometry_validity(self):
+        pass
+
+    def test_geometry_type(self):
+        pass
+
+    def test_bounding_box(self):
+        pass
+
+    def test_duplicates(self):
+        pass
+        
+    def test_constraints_arteres_noeuds(self):
+        pass
+
+    def test_constraints_cables_equipements(self):
+        pass
+
+    def test_constraints_cables_equipements_b(self):
+        pass
+
+    def test_constraints_equipements_noeuds(self):
+        pass
+
+    def test_graphic_duplicates(self):
+        pass
+        
+    def test_positions_noeuds(self):
+        pass
+
+    def test_positions_equipements(self):
+        pass
+    
+    def test_tranchee_artere(self):
+        pass
+        
+    def test_cable_artere(self):
+        pass
+        
+    def test_artere_cable(self):
+        pass
+    
+    def test_dimensions_fourreaux(self):
+        pass
+         
+    def test_pbos(self):
+        pass
+        

+ 29 - 2
ui/dlg_contact.py

@@ -3,15 +3,19 @@
 import logging
 import os
 import platform
+from qgis.core import QgsProject
 import subprocess
+import unittest
 
 from PyQt5 import QtWidgets
 from PyQt5 import uic
-from PyQt5.QtCore import Qt
+from PyQt5.QtCore import Qt, QFileInfo
 from PyQt5.QtGui import QIcon, QPixmap
+from PyQt5.QtWidgets import QTextBrowser
 
-from core.constants import MAIN, LOGDIR, RSCDIR, CONTACT
+from core.constants import MAIN, LOGDIR, RSCDIR, CONTACT, TESTDIR, DEBUG
 from path import Path
+from test._stream import TestStream
 
 
 logger = logging.getLogger("mncheck")
@@ -40,6 +44,9 @@ class DlgContact(QtWidgets.QDialog):
         self.ui.lbl_mail.setOpenExternalLinks(True)
         
         self.ui.btn_open_log_dir.clicked.connect(self.open_log_dir)
+            
+        self.ui.btn_test.setVisible(DEBUG)
+        self.ui.btn_test.clicked.connect(self.run_tests)
     
     def open_log_dir(self):
         path = Path(LOGDIR).abspath()
@@ -48,3 +55,23 @@ class DlgContact(QtWidgets.QDialog):
         else:
             subprocess.Popen(["open", path])
 
+    def run_tests(self):
+        _initial_project = QgsProject.instance().absoluteFilePath()
+        if _initial_project:
+            QgsProject.instance().write(QFileInfo(_initial_project))
+        
+        loader = unittest.TestLoader()
+        tests = loader.discover(Path(TESTDIR).abspath())
+        
+        with open(LOGDIR / "checkup.txt", "w+") as f:
+            runner = unittest.TextTestRunner(stream=f)
+            runner.run(tests)
+
+        if _initial_project:
+            QgsProject.instance().clear()
+            QgsProject.read(QFileInfo(_initial_project))
+
+        with open(LOGDIR / "checkup.txt", "r") as f:
+            dlg = QTextBrowser()
+            dlg.setText(f.read())
+            dlg.show()

+ 1 - 1
ui/dlg_main.py

@@ -3,7 +3,6 @@
 from core import mncheck, checking
 import logging
 from qgis.core import QgsProject
-import sys
 
 from PyQt5 import QtWidgets
 from PyQt5 import uic
@@ -279,3 +278,4 @@ class DlgMain(QtWidgets.QDialog):
         
         super().closeEvent(event)
 
+