Skip to content

Commit

Permalink
Trying to improve test behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dkratzert committed Sep 20, 2023
1 parent 1ee1c70 commit 9ae05c2
Showing 14 changed files with 51 additions and 59 deletions.
34 changes: 20 additions & 14 deletions finalcif/appwindow.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions finalcif/cif/checkcif/checkcif.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/test_author_templates.py
Original file line number Diff line number Diff line change
@@ -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,16 +10,16 @@ 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)

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
3 changes: 1 addition & 2 deletions tests/test_changes_cif.py
Original file line number Diff line number Diff line change
@@ -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'))
3 changes: 1 addition & 2 deletions tests/test_checkcif_html.py
Original file line number Diff line number Diff line change
@@ -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')

5 changes: 2 additions & 3 deletions tests/test_checkcif_platon.py
Original file line number Diff line number Diff line change
@@ -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:
6 changes: 2 additions & 4 deletions tests/test_loops.py
Original file line number Diff line number Diff line change
@@ -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()

5 changes: 2 additions & 3 deletions tests/test_main_table.py
Original file line number Diff line number Diff line change
@@ -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)

7 changes: 3 additions & 4 deletions tests/test_options.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions tests/test_statusbar.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 7 additions & 7 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
@@ -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())
6 changes: 2 additions & 4 deletions tests/test_template_edit.py
Original file line number Diff line number Diff line change
@@ -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):
3 changes: 1 addition & 2 deletions tests/test_templated_report.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 3 additions & 6 deletions tests/test_workfolder.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 9ae05c2

Please sign in to comment.