From 9ae05c2ca405fc33bc6b73804cd5cc295c7b878d Mon Sep 17 00:00:00 2001 From: Daniel Kratzert Date: Wed, 20 Sep 2023 14:03:51 +0200 Subject: [PATCH] Trying to improve test behavior --- finalcif/appwindow.py | 34 ++++++++++++++++++------------- finalcif/cif/checkcif/checkcif.py | 4 ++-- tests/test_author_templates.py | 8 ++++---- tests/test_changes_cif.py | 3 +-- tests/test_checkcif_html.py | 3 +-- tests/test_checkcif_platon.py | 5 ++--- tests/test_loops.py | 6 ++---- tests/test_main_table.py | 5 ++--- tests/test_options.py | 7 +++---- tests/test_statusbar.py | 3 +-- tests/test_tables.py | 14 ++++++------- tests/test_template_edit.py | 6 ++---- tests/test_templated_report.py | 3 +-- tests/test_workfolder.py | 9 +++----- 14 files changed, 51 insertions(+), 59 deletions(-) diff --git a/finalcif/appwindow.py b/finalcif/appwindow.py index 6ead2562..17995f08 100644 --- a/finalcif/appwindow.py +++ b/finalcif/appwindow.py @@ -16,9 +16,10 @@ from typing import Union, Dict, Tuple, List, Optional import gemmi.cif +import pytest import requests from PyQt5 import QtCore, QtGui, QtWebEngineWidgets -from PyQt5.QtCore import QThread, QTimer, Qt, QEvent +from PyQt5.QtCore import QThread, Qt, QEvent from PyQt5.QtWidgets import QMainWindow, QShortcut, QCheckBox, QListWidgetItem, QApplication, \ QPlainTextEdit, QFileDialog, QMessageBox from gemmi import cif @@ -73,14 +74,13 @@ class AppWindow(QMainWindow): - def __init__(self, file: Optional[Path] = None, unit_test: bool = False): + def __init__(self, file: Optional[Path] = None): super().__init__() self.setAttribute(QtCore.Qt.WA_DeleteOnClose) # This prevents some things to happen during unit tests: # Open of target dir of shred cif, # open report doc, # get check.def from platon server - self.running_inside_unit_test = unit_test self.sources: Optional[Dict[str, Tuple[Optional[str]]]] = None self.cif: Optional[CifContainer] = None self.report_picture_path: Optional[Path] = None @@ -91,7 +91,6 @@ def __init__(self, file: Optional[Path] = None, unit_test: bool = False): self.checkdef_file: Path = Path.home().joinpath('check.def') self.missing_data: set = set() self.temperature_warning_displayed = False - self.threadpool = [] # True if line with "these are already in" reached: self.complete_data_row = -1 self.ui = Ui_FinalCifWindow() @@ -136,6 +135,14 @@ def __init__(self, file: Optional[Path] = None, unit_test: bool = False): self.format_report_button() self.set_font_sizes() + @property + def running_inside_unit_test(self): + if "PYTEST_CURRENT_TEST" in os.environ: + if DEBUG: + print(f'pytest process running: {os.environ["PYTEST_CURRENT_TEST"]}') + return True + return False + def set_initial_button_states(self) -> None: self.ui.appendCifPushButton.setDisabled(True) self.ui.PictureWidthDoubleSpinBox.setRange(0.0, 25) @@ -682,10 +689,9 @@ def check_for_update_version(self) -> None: print('Skipping version.txt download because NO_NETWORK variable is set.') return mainurl = "https://dkratzert.de/files/finalcif/version.txt" - self.upd = MyDownloader(mainurl, parent=None) - version_thread = QThread() - self.threadpool.append(version_thread) - start_worker(self.upd, version_thread, onload=self.is_update_necessary) + self.upd = MyDownloader(mainurl, parent=self) + self.version_thread = QThread(parent=self) + start_worker(self.upd, self.version_thread, onload=self.is_update_necessary) def is_update_necessary(self, content: bytes) -> None: """ @@ -752,10 +758,9 @@ def get_checkdef_for_response_forms(self) -> None: print('Skipping check.def download because NO_NETWORK variable is set.') return url = 'http://www.platonsoft.nl/xraysoft/unix/platon/check.def' - self.updc = MyDownloader(url, parent=None) - checkdef_thread = QThread(parent=self) - self.threadpool.append(checkdef_thread) - start_worker(self.updc, checkdef_thread, onload=self._save_checkdef) + self.updc = MyDownloader(url, parent=self) + self.checkdef_thread = QThread(parent=self) + start_worker(self.updc, self.checkdef_thread, onload=self._save_checkdef) def _save_checkdef(self, reply: bytes) -> None: """ @@ -904,7 +909,7 @@ def do_html_checkcif(self) -> None: self.htmlfile.unlink() except (FileNotFoundError, PermissionError): pass - self.ckf = CheckCif(cif=self.cif, outfile=self.htmlfile, + self.ckf = CheckCif(parent=self, cif=self.cif, outfile=self.htmlfile, hkl_upload=(not self.ui.structfactCheckBox.isChecked()), pdf=False, url=self.options.checkcif_url, full_iucr=self.ui.fullIucrCheckBox.isChecked()) @@ -978,7 +983,8 @@ def do_pdf_checkcif(self) -> None: pass self.ui.CheckCifLogPlainTextEdit.appendPlainText( 'Sending pdf report request to {} ...'.format(self.options.checkcif_url)) - self.ckf = CheckCif(cif=self.cif, outfile=htmlfile, hkl_upload=(not self.ui.structfactCheckBox.isChecked()), + self.ckf = CheckCif(parent=self, cif=self.cif, outfile=htmlfile, + hkl_upload=(not self.ui.structfactCheckBox.isChecked()), pdf=True, url=self.options.checkcif_url, full_iucr=self.ui.fullIucrCheckBox.isChecked()) self.ckf.failed.connect(self._checkcif_failed) diff --git a/finalcif/cif/checkcif/checkcif.py b/finalcif/cif/checkcif/checkcif.py index 59e9374b..5d32af17 100644 --- a/finalcif/cif/checkcif/checkcif.py +++ b/finalcif/cif/checkcif/checkcif.py @@ -25,10 +25,10 @@ class CheckCif(QThread): progress = pyqtSignal(str) failed = pyqtSignal(str) - def __init__(self, cif: CifContainer, outfile: Path, hkl_upload: bool = True, + def __init__(self, parent, cif: CifContainer, outfile: Path, hkl_upload: bool = True, pdf: bool = False, url: str = '', full_iucr: bool = False): # hkl == False means no hkl upload - super().__init__() + super().__init__(parent=parent) self.hkl_upload = hkl_upload self.html_out_file = outfile self.cif = cif diff --git a/tests/test_author_templates.py b/tests/test_author_templates.py index 0f065d5f..647d5c44 100644 --- a/tests/test_author_templates.py +++ b/tests/test_author_templates.py @@ -1,7 +1,7 @@ import unittest from pathlib import Path -from finalcif.appwindow import AppWindow +from finalcif.appwindow import AppWindow, app class MyTestCase(unittest.TestCase): @@ -10,9 +10,8 @@ def setUp(self) -> None: self.testcif = Path('tests/examples/1979688.cif').resolve() self.authorexport_file = Path('tests/examples/testexport_author.cif').resolve() self.testimport_author = Path('tests/other_templates/AATest_Author.cif').resolve() - self.app = AppWindow(self.testcif, unit_test=True) - self.app.running_inside_unit_test = True - self.app.hide() + self.app = AppWindow(self.testcif) + self.app.show() self.author = {'address': 'address', 'footnote': 'footnote', 'email': 'email', 'name' : 'name', 'orcid': 'orcid', 'phone': 'phone', 'contact': True} self.app.ui.authorEditTabWidget.setCurrentIndex(0) @@ -20,6 +19,7 @@ def setUp(self) -> None: def tearDown(self) -> None: self.authorexport_file.unlink(missing_ok=True) self.app.close() + app.quit() def _import_testauthor(self): # To be used in other tests diff --git a/tests/test_changes_cif.py b/tests/test_changes_cif.py index 57b86571..093b5c92 100644 --- a/tests/test_changes_cif.py +++ b/tests/test_changes_cif.py @@ -12,9 +12,8 @@ class TestChangesTrackingActive(unittest.TestCase): def setUp(self) -> None: self.testcif = Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').absolute() - self.myapp = AppWindow(self.testcif, unit_test=True) + self.myapp = AppWindow(self.testcif) self.myapp.finalcif_changes_filename.unlink(missing_ok=True) - self.myapp.running_inside_unit_test = True self.myapp.hide() self.myapp.ui.trackChangesCifCheckBox.setChecked(True) self.myapp.setWindowIcon(QIcon('./icon/multitable.png')) diff --git a/tests/test_checkcif_html.py b/tests/test_checkcif_html.py index 981e1534..b2955ca0 100644 --- a/tests/test_checkcif_html.py +++ b/tests/test_checkcif_html.py @@ -21,8 +21,7 @@ class TestCheckCifHTML(unittest.TestCase): def setUp(self) -> None: if os.environ.get('NO_NETWORK'): self.skipTest('No network available.') - self.myapp = AppWindow(Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').resolve(), unit_test=True) - self.myapp.running_inside_unit_test = True + self.myapp = AppWindow(Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').resolve()) self.myapp.hide() # For full screen view self.resobj = self.myapp.cif.finalcif_file_prefixed(prefix='checkcif-', suffix='-finalcif.html') diff --git a/tests/test_checkcif_platon.py b/tests/test_checkcif_platon.py index 330b5d21..88f2edec 100644 --- a/tests/test_checkcif_platon.py +++ b/tests/test_checkcif_platon.py @@ -34,7 +34,7 @@ class TestPlatonCheckCIF(unittest.TestCase): def setUp(self) -> None: if not get_platon_exe() or os.environ.get('NO_NETWORK'): self.skipTest('No PLATON executable found or no network. Skipping test!') - self.myapp = AppWindow(Path('tests/examples/1979688.cif').resolve(), unit_test=True) + self.myapp = AppWindow(Path('tests/examples/1979688.cif').resolve()) self.myapp.hide() self.myapp.running_inside_unit_test = True @@ -68,10 +68,9 @@ class TestPlatonCheckCIFwithCIFwithoutHKLdata(unittest.TestCase): def setUp(self) -> None: if not get_platon_exe() or os.environ.get('NO_NETWORK'): self.skipTest('No PLATON executable found or NO_NETWORK is set. Skipping test!') - self.myapp = AppWindow(Path('./test-data/1000007.cif').resolve(), unit_test=True) + self.myapp = AppWindow(Path('./test-data/1000007.cif').resolve()) self.myapp.hide() self.myapp.ui.structfactCheckBox.setChecked(True) - self.myapp.running_inside_unit_test = True def tearDown(self) -> None: for file in filenames: diff --git a/tests/test_loops.py b/tests/test_loops.py index 0c57e349..67dd5aa0 100644 --- a/tests/test_loops.py +++ b/tests/test_loops.py @@ -21,8 +21,7 @@ class TestLoops(unittest.TestCase): def setUp(self) -> None: self.testcif = Path('tests/examples/1979688.cif').resolve() Path('tests/examples/1979688-finalcif_changes.cif').unlink(missing_ok=True) - self.myapp = AppWindow(self.testcif, unit_test=True) - self.myapp.running_inside_unit_test = True + self.myapp = AppWindow(self.testcif) self.myapp.hide() # For full screen view self.myapp.ui.LoopsPushButton.click() self.myapp.ui.trackChangesCifCheckBox.setChecked(False) @@ -87,8 +86,7 @@ def setUp(self) -> None: self.testcif = Path('tests/examples/1979688.cif').resolve() # TODO: make tests where changes file is active: Path('tests/examples/1979688-finalcif_changes.cif').unlink(missing_ok=True) - self.myapp = AppWindow(self.testcif, unit_test=True) - self.myapp.running_inside_unit_test = True + self.myapp = AppWindow(self.testcif) self.myapp.hide() # For full screen view self.myapp.ui.LoopsPushButton.click() diff --git a/tests/test_main_table.py b/tests/test_main_table.py index 1f488709..8f6239c5 100644 --- a/tests/test_main_table.py +++ b/tests/test_main_table.py @@ -20,9 +20,8 @@ class TestMainTableFieldBehavior(unittest.TestCase): def setUp(self) -> None: self.testcif = Path('tests/examples/1979688.cif').absolute() Path('tests/examples/1979688-finalcif_changes.cif').unlink(missing_ok=True) - self.myapp = AppWindow(self.testcif, unit_test=True) - self.myapp.running_inside_unit_test = True - self.myapp.hide() # For full screen view + self.myapp = AppWindow(self.testcif) + self.myapp.show() self.myapp.settings.empty_deleted_list() self.myapp.ui.trackChangesCifCheckBox.setChecked(False) diff --git a/tests/test_options.py b/tests/test_options.py index 71a7a325..d784f483 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -1,18 +1,17 @@ import unittest +from pathlib import Path -from finalcif.appwindow import AppWindow +from finalcif import appwindow class TestOptions(unittest.TestCase): def setUp(self) -> None: - self.myapp = AppWindow(unit_test=True) - self.myapp.running_inside_unit_test = True + self.myapp = appwindow.AppWindow(file=Path('test-data/1000006.cif')) self.myapp.ui.HAtomsCheckBox.setChecked(False) self.myapp.ui.ReportTextCheckBox.setChecked(False) self.myapp.ui.ADPTableCheckBox.setChecked(False) self.myapp.ui.PictureWidthDoubleSpinBox.setValue(0.0) - self.myapp.hide() def tearDown(self) -> None: self.myapp.ui.HAtomsCheckBox.setChecked(False) diff --git a/tests/test_statusbar.py b/tests/test_statusbar.py index d1a3d816..37f65919 100644 --- a/tests/test_statusbar.py +++ b/tests/test_statusbar.py @@ -10,8 +10,7 @@ class TestStausBarWithGraphics(unittest.TestCase): def setUp(self) -> None: - self.myapp = AppWindow(unit_test=True) - self.myapp.running_inside_unit_test = True + self.myapp = AppWindow() self.myapp.setWindowIcon(QIcon('./icon/multitable.png')) self.myapp.setWindowTitle('FinalCif v{}'.format(VERSION)) self.status = StatusBar(self.myapp.ui) diff --git a/tests/test_tables.py b/tests/test_tables.py index 5eebc907..2a46f490 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -8,26 +8,22 @@ from docx.table import Table from finalcif import VERSION -from finalcif.appwindow import AppWindow -# noinspection PyUnresolvedReferences -from finalcif.appwindow import app +from finalcif.appwindow import AppWindow, app class TablesTestMixin(): def setUp(self) -> None: self.testcif = Path('tests/examples/1979688.cif').absolute() - self.myapp = AppWindow(self.testcif, unit_test=True) + self.myapp = AppWindow(file=self.testcif) self.myapp.ui.HAtomsCheckBox.setChecked(False) self.myapp.ui.ReportTextCheckBox.setChecked(False) self.myapp.ui.PictureWidthDoubleSpinBox.setValue(0.0) # make sure to use no template: self.myapp.ui.docxTemplatesListWidget.setCurrentRow(0) - self.myapp.running_inside_unit_test = True - self.myapp.hide() + self.myapp.show() self.reportdoc = self.myapp.cif.finalcif_file_prefixed(prefix='report_', suffix='-finalcif.docx') self.report_zip = self.myapp.cif.finalcif_file_prefixed(prefix='', suffix='-finalcif.zip') - self.myapp.hide() def tearDown(self) -> None: self.myapp.cif.finalcif_file.unlink(missing_ok=True) @@ -37,6 +33,7 @@ def tearDown(self) -> None: self.myapp.ui.HAtomsCheckBox.setChecked(False) self.myapp.ui.PictureWidthDoubleSpinBox.setValue(7.5) self.myapp.close() + app.quit() class TablesTestCase(TablesTestMixin, unittest.TestCase): @@ -145,6 +142,9 @@ class TablesNoPictureTestCase(TablesTestMixin, unittest.TestCase): def setUp(self) -> None: super().setUp() + def tearDown(self) -> None: + super().tearDown() + def test_save_report_works(self): self.myapp.ui.SaveFullReportButton.click() self.assertEqual(True, self.reportdoc.exists()) diff --git a/tests/test_template_edit.py b/tests/test_template_edit.py index d5416618..40e1ed56 100644 --- a/tests/test_template_edit.py +++ b/tests/test_template_edit.py @@ -8,8 +8,7 @@ class EquipmentTestCase(unittest.TestCase): def setUp(self) -> None: - self.app = AppWindow(unit_test=True) - self.app.running_inside_unit_test = True + self.app = AppWindow() self.app.equipment.import_equipment_from_file('test-data/Crystallographer_Details.cif') self.app.hide() @@ -39,8 +38,7 @@ def test_template_edit_click(self): class PropertiesTestCase(unittest.TestCase): def setUp(self) -> None: - self.app = AppWindow(unit_test=True) - self.app.running_inside_unit_test = True + self.app = AppWindow() self.app.hide() def property_edit_click(self, field: str): diff --git a/tests/test_templated_report.py b/tests/test_templated_report.py index 2a7bcd83..85f30ef3 100644 --- a/tests/test_templated_report.py +++ b/tests/test_templated_report.py @@ -15,10 +15,9 @@ class TemplateReportTestCase(unittest.TestCase): def setUp(self) -> None: - self.myapp = AppWindow(unit_test=True) + self.myapp = AppWindow() self.testcif = Path('tests/examples/1979688.cif').absolute() self.myapp.load_cif_file(self.testcif.resolve()) - self.myapp.running_inside_unit_test = True self.myapp.ui.HAtomsCheckBox.setChecked(False) self.myapp.ui.ReportTextCheckBox.setChecked(False) self.myapp.ui.PictureWidthDoubleSpinBox.setValue(7.43) diff --git a/tests/test_workfolder.py b/tests/test_workfolder.py index f5d898e3..76f25b43 100644 --- a/tests/test_workfolder.py +++ b/tests/test_workfolder.py @@ -19,9 +19,8 @@ class TestFileIsOpened(unittest.TestCase): def setUp(self) -> None: self.testcif = Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').absolute() - self.myapp = AppWindow(self.testcif, unit_test=True) + self.myapp = AppWindow(self.testcif) self.myapp.ui.trackChangesCifCheckBox.setChecked(True) - self.myapp.running_inside_unit_test = True self.myapp.hide() self.myapp.setWindowIcon(QIcon('./icon/multitable.png')) self.myapp.setWindowTitle('FinalCif v{}'.format(VERSION)) @@ -45,10 +44,9 @@ def setUp(self) -> None: self.testcif = Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').resolve() # TODO: Adapt this to the bahavior with the changes file: Path('tests/examples/work/cu_BruecknerJK_153F40_0m-finalcif_changes.cif').unlink(missing_ok=True) - self.myapp = AppWindow(self.testcif, unit_test=True) + self.myapp = AppWindow(self.testcif) self.myapp.ui.trackChangesCifCheckBox.setChecked(True) self.myapp.equipment.import_equipment_from_file('test-data/Crystallographer_Details.cif') - self.myapp.running_inside_unit_test = True self.myapp.hide() self.myapp.setWindowIcon(QIcon('./icon/multitable.png')) self.myapp.setWindowTitle('FinalCif v{}'.format(VERSION)) @@ -355,10 +353,9 @@ class TestWorkfolderOtherCifName(unittest.TestCase): def setUp(self) -> None: self.testcif = Path('tests/examples/work/p21c.cif').resolve() - self.myapp = AppWindow(self.testcif, unit_test=True) + self.myapp = AppWindow(self.testcif) self.myapp.ui.trackChangesCifCheckBox.setChecked(True) self.myapp.equipment.import_equipment_from_file('test-data/Crystallographer_Details.cif') - self.myapp.running_inside_unit_test = True self.myapp.hide() self.myapp.setWindowIcon(QIcon('./icon/multitable.png')) self.myapp.setWindowTitle('FinalCif v{}'.format(VERSION))