Skip to content

Commit

Permalink
More unified paths in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dkratzert committed Oct 17, 2023
1 parent bdd4f49 commit 1f25842
Show file tree
Hide file tree
Showing 26 changed files with 172 additions and 127 deletions.
11 changes: 7 additions & 4 deletions finalcif/equip_property/equipment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from bisect import bisect
from contextlib import suppress
from pathlib import Path
from typing import List, Dict

Expand All @@ -16,7 +15,9 @@
from finalcif.tools.misc import include_equipment_imports
from finalcif.tools.settings import FinalCifSettings

with suppress(ImportError):
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from finalcif.appwindow import AppWindow


Expand Down Expand Up @@ -182,8 +183,8 @@ def save_equipment_template(self) -> None:
if key not in cif_all_dict.keys():
if not key.startswith('_'):
show_general_warning(self.app, '"{}" is not a valid keyword! '
'\nChange the name in order to save.\n'
'Keys must start with an underscore.'.format(key))
'\nChange the name in order to save.\n'
'Keys must start with an underscore.'.format(key))
return
show_general_warning(self.app, '"{}" is not an official CIF keyword!'.format(key))
self.settings.save_settings_list('equipment', self.selected_template_name(), table_data)
Expand All @@ -194,6 +195,8 @@ def import_equipment_from_file(self, filename='') -> None:
"""
Import an equipment entry from a cif file.
"""
if isinstance(filename, Path):
filename = str(filename)
if not filename:
filename = cif_file_open_dialog(filter="CIF file (*.cif *.cif_od *.cfx)")
if not filename:
Expand Down
6 changes: 4 additions & 2 deletions tests/test_SADABS.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

class TestSADABSWU19(unittest.TestCase):
def setUp(self) -> None:
self.s = Sadabs(r'IK_WU19.abs', searchpath=Path('test-data')) # this is a sadabs file
self.test_data = Path('test-data')
self.s = Sadabs(r'IK_WU19.abs', searchpath=self.test_data) # this is a sadabs file

def test_twincomp(self):
self.assertEqual(1, self.s.twin_components)
Expand All @@ -39,7 +40,8 @@ def test_written_reflections(self):

class TestTWINABS(unittest.TestCase):
def setUp(self) -> None:
self.s = Sadabs(r'twin-4-5.abs', searchpath=Path('test-data')) # this is a twinabs file
self.test_data = Path('test-data')
self.s = Sadabs(r'twin-4-5.abs', searchpath=self.test_data) # this is a twinabs file

def test_transmission(self):
self.assertEqual([0.794433, 0.86207], [self.s.dataset(0).transmission.tmin, self.s.dataset(0).transmission.tmax])
Expand Down
12 changes: 7 additions & 5 deletions tests/test_author_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

from PyQt5 import Qt, QtCore
from PyQt5.QtTest import QTest

from finalcif.appwindow import AppWindow

data = Path('tests')


class MyTestCase(unittest.TestCase):

def setUp(self) -> None:
os.environ["RUNNING_TEST"] = 'True'
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.testcif = data / 'examples/1979688.cif'
self.authorexport_file = data / 'examples/testexport_author.cif'
self.testimport_author = data / 'other_templates/AATest_Author.cif'
self.app = AppWindow(self.testcif)
self.author = {'address': 'address', 'footnote': 'footnote', 'email': 'email',
'name' : 'name', 'orcid': 'orcid', 'phone': 'phone', 'contact': True}
Expand Down Expand Up @@ -51,7 +52,8 @@ def _select_row_of_author(self, name):
if item is not None and item.text().startswith(name):
item_widget = listWidget.itemWidget(item)
if item_widget is not None:
QTest.mouseClick(item_widget, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, item_widget.rect().center())
QTest.mouseClick(item_widget, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier,
item_widget.rect().center())
break

def test_export_selected_author(self):
Expand Down
27 changes: 16 additions & 11 deletions tests/test_bruker_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,52 @@ class TestBrukerFrame(unittest.TestCase):
Then, the newest found file is used to gather the machine information.
"""

def setUp(self) -> None:
self.test_data = Path('./test-data')
self.example_data = Path('tests/examples/')

def test_str(self):
sfrm = BrukerFrameHeader(basename='apex_frame', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='apex_frame',
searchpath=self.test_data)
self.assertEqual("{ 'ANGLES'", str(sfrm)[:10])

def test_radiation_type(self):
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=self.test_data)
self.assertEqual('Mo', sfrm.radiation_type)

def test_radiation_type_in_work_folder(self):
# check if a basename without cu_ prefix leads to a found .sfrm file:
sfrm = BrukerFrameHeader(basename='BruecknerJK_153F40', searchpath=Path('tests/examples/work'))
sfrm = BrukerFrameHeader(basename='BruecknerJK_153F40', searchpath=self.example_data / 'work')
self.assertEqual('Cu', sfrm.radiation_type)
sfrm = BrukerFrameHeader(basename='cu_BruecknerJK_153F40', searchpath=Path('tests/examples/work'))
sfrm = BrukerFrameHeader(basename='cu_BruecknerJK_153F40', searchpath=self.example_data / 'work')
self.assertEqual('Cu', sfrm.radiation_type)

def test_radiation_type_in_work_folder_and_wrong_prefix(self):
with self.assertRaises(FileNotFoundError):
BrukerFrameHeader(basename='mo_BruecknerJK_153F40', searchpath=Path('tests/examples/work'))
BrukerFrameHeader(basename='mo_BruecknerJK_153F40', searchpath=self.example_data / 'work')

def test_measure_date(self):
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=self.test_data)
self.assertEqual('15-Apr-2019 13:32:45', sfrm.measure_date)

def test_program(self):
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=self.test_data)
self.assertEqual('BIS V6.2.10/2018-10-02', sfrm.program)

def test_kilovolts_milliamps(self):
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=self.test_data)
self.assertEqual(50.0, sfrm.kilovolts)
self.assertEqual(1.4, sfrm.milliamps)

def test_kilovolts_milliamps_work_folder(self):
sfrm = BrukerFrameHeader(basename='BruecknerJK_153F40', searchpath=Path('tests/examples/work'))
sfrm = BrukerFrameHeader(basename='BruecknerJK_153F40', searchpath=self.example_data / 'work')
self.assertEqual(50.0, sfrm.kilovolts)
self.assertEqual(1.1, sfrm.milliamps)

def test_radiation_type_temperature(self):
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=self.test_data)
self.assertEqual(100.0, sfrm.temperature)

def test_detector_type(self):
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=Path('./test-data'))
sfrm = BrukerFrameHeader(basename='mo_DK_Zucker2', searchpath=self.test_data)
self.assertEqual('CMOS-PHOTONII', sfrm.detector_type)
6 changes: 4 additions & 2 deletions tests/test_ccdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
from finalcif.cif.cif_file_io import CifContainer
from finalcif.datafiles.ccdc_mail import CCDCMail

data = Path('.')


class TestCCDC(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif'))
self.cif = CifContainer(file=data / 'tests/examples/work/cu_BruecknerJK_153F40_0m.cif')

def test_ccdc_num(self):
ccdc = CCDCMail(self.cif)
Expand All @@ -24,7 +26,7 @@ def test_ccdc_num(self):

class TestCCDCnoMail(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('test-data/DK_Zucker2_0m.cif'))
self.cif = CifContainer(file=data / 'test-data/DK_Zucker2_0m.cif')

def test_ccdc_num(self):
ccdc = CCDCMail(self.cif)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_changes_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
from finalcif import VERSION
from finalcif.appwindow import AppWindow

data = Path('tests')

# noinspection PyMissingTypeHints
class TestChangesTrackingActive(unittest.TestCase):
"""A CIF fle in a complete work folder"""

def setUp(self) -> None:
os.environ["RUNNING_TEST"] = 'True'
self.testcif = Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').absolute()
self.myapp = AppWindow(self.testcif)
self.testcif = data / 'examples/work/cu_BruecknerJK_153F40_0m.cif'
self.myapp = AppWindow(file=self.testcif)
self.myapp.finalcif_changes_filename.unlink(missing_ok=True)
self.myapp.ui.trackChangesCifCheckBox.setChecked(True)
self.myapp.setWindowIcon(QIcon('./icon/multitable.png'))
Expand Down
10 changes: 6 additions & 4 deletions tests/test_checkcif.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from finalcif.cif.checkcif.checkcif import MyHTMLParser, fix_iucr_urls

data = Path('tests')

form_choices = {'filecif' : [],
'runtype' : [],
'referer' : [],
Expand Down Expand Up @@ -123,7 +125,7 @@ def test__fix_urls_with_spaces(self):

class TestMyHTMLParser(TestCase):
def setUp(self) -> None:
htmlfile = Path('tests/examples/work/checkcif-cu_BruecknerJK_153F40_0m-test2.html')
htmlfile = data / 'examples/work/checkcif-cu_BruecknerJK_153F40_0m-test2.html'
self.parser = MyHTMLParser(fix_iucr_urls(htmlfile.read_text()))

def test_pdf_link_not_there(self):
Expand All @@ -147,7 +149,7 @@ def test_response_forms(self):

class TestMyHTMLParserPDF(TestCase):
def setUp(self) -> None:
htmlfile = Path('tests/examples/work/checkcif-cu_BruecknerJK_153F40_0m-pdf-test2.html')
htmlfile = (data / 'examples/work/checkcif-cu_BruecknerJK_153F40_0m-pdf-test2.html').resolve().absolute()
self.parser = MyHTMLParser(fix_iucr_urls(htmlfile.read_text()))

def test_pdf_link_there(self):
Expand All @@ -160,7 +162,7 @@ class TestMyHTMLParserNew(unittest.TestCase):

def test_parser_with_html(self):
# Read the sample HTML content from file
html_content = Path(r'tests/checkcif_results/check_html_ab.html').read_text()
html_content = (data / r'checkcif_results/check_html_ab.html').read_text()
parser = MyHTMLParser(html_content)

self.assertEqual(parser.structure_factor_report,
Expand All @@ -171,7 +173,7 @@ def test_parser_with_html(self):

def test_parser_with_html_pdf(self):
# Read the sample HTML PDF content from file
html_pdf_content = Path(r'tests/checkcif_results/check_pdf_ab.html').read_text()
html_pdf_content = (data / r'checkcif_results/check_pdf_ab.html').read_text()

parser = MyHTMLParser(html_pdf_content)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_checkcif_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def setUp(self) -> None:
os.environ["RUNNING_TEST"] = 'True'
if os.environ.get('NO_NETWORK'):
self.skipTest('No network available.')
self.myapp = AppWindow(Path('tests/examples/work/cu_BruecknerJK_153F40_0m.cif').resolve())
self.myapp = AppWindow(file=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')

Expand Down
4 changes: 2 additions & 2 deletions tests/test_checkcif_platon.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def setUp(self) -> None:
os.environ["RUNNING_TEST"] = 'True'
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())
self.myapp = AppWindow(file=Path('tests/examples/1979688.cif').resolve())
self.myapp.hide()

def tearDown(self) -> None:
Expand Down Expand Up @@ -69,7 +69,7 @@ def setUp(self) -> None:
os.environ["RUNNING_TEST"] = 'True'
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())
self.myapp = AppWindow(file=Path('./test-data/1000007.cif').resolve())
self.myapp.hide()
self.myapp.ui.structfactCheckBox.setChecked(True)

Expand Down
24 changes: 13 additions & 11 deletions tests/test_cif_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
from finalcif.cif.cif_file_io import CifContainer
from finalcif.cif.text import quote

data = Path('.')


class CifFileCRCTestCase(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('tests/examples/1979688.cif'))
self.cif = CifContainer(data / 'tests/examples/1979688.cif')

def test_calc_crc(self):
self.assertEqual(20714, self.cif.calc_checksum(self.cif['_shelx_hkl_file']))


class CifFileCRClargerTestCase(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('test-data/DK_Zucker2_0m.cif'))
self.cif = CifContainer(data / 'test-data/DK_Zucker2_0m.cif')

def test_calc_crc(self):
self.assertEqual(26780, self.cif.calc_checksum(self.cif['_shelx_hkl_file']))


class CifFileTestCase(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('tests/examples/1979688.cif'))
self.cif = CifContainer(data / 'tests/examples/1979688.cif')

def test_calc_crc(self):
self.assertEqual(3583, self.cif.calc_checksum('hello world'))
Expand All @@ -36,7 +38,7 @@ def test_hkl_crc(self):
self.assertEqual(20714, self.cif.hkl_checksum_calcd)

def test_res_crc_without_res(self):
self.assertEqual(0, CifContainer(Path('test-data/1000006.cif')).res_checksum_calcd)
self.assertEqual(0, CifContainer(data / 'test-data/1000006.cif').res_checksum_calcd)

def test_get_unknown_value_from_key(self):
self.assertEqual('', self.cif['_chemical_melting_point'])
Expand All @@ -55,7 +57,7 @@ def test_symmops_from_spgr(self):

def test_centrosymm(self):
self.assertEqual(False, self.cif.is_centrosymm)
c = CifContainer(Path('test-data/DK_ML7-66-final.cif'))
c = CifContainer(data / 'test-data/DK_ML7-66-final.cif')
self.assertEqual(True, c.is_centrosymm)

def test_ishydrogen(self):
Expand All @@ -78,15 +80,15 @@ def test_checksum_tests(self):
self.assertEqual(True, self.cif.test_res_checksum())

def test_checksum_test_without_checksum(self):
self.assertEqual(True, CifContainer('test-data/1000006.cif').test_res_checksum())
self.assertEqual(True, CifContainer('test-data/1000006.cif').test_hkl_checksum())
self.assertEqual(True, CifContainer(data / 'test-data/1000006.cif').test_res_checksum())
self.assertEqual(True, CifContainer(data / 'test-data/1000006.cif').test_hkl_checksum())


class TestQuotationMark(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('tests/examples/1979688.cif'))
shutil.copyfile(self.cif.fileobj, 'tests/test.cif')
self.cif2 = CifContainer('tests/test.cif')
self.cif = CifContainer(data / 'tests/examples/1979688.cif')
shutil.copyfile(self.cif.fileobj, data / 'test.cif')
self.cif2 = CifContainer(data / 'test.cif')

def tearDown(self) -> None:
self.cif2.fileobj.unlink(missing_ok=True)
Expand All @@ -101,7 +103,7 @@ def test_insert_quotation_mark(self):

class TestMultiCif(unittest.TestCase):
def setUp(self) -> None:
self.cif = CifContainer(Path('tests/examples/multi.cif'))
self.cif = CifContainer(data / 'tests/examples/multi.cif')

def test_ismulti(self):
self.assertEqual(True, self.cif.is_multi_cif)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_hkl.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from pathlib import Path
from unittest import TestCase

from finalcif.cif.cif_file_io import CifContainer

data = Path('tests')


class TestHKL(TestCase):

def setUp(self) -> None:
self.cif1 = CifContainer('tests/examples/1979688.cif')
self.cif2 = CifContainer('tests/examples/work/cu_BruecknerJK_153F40_0m.cif')
self.cif1 = CifContainer(data / 'examples/1979688.cif')
self.cif2 = CifContainer(data / 'examples/work/cu_BruecknerJK_153F40_0m.cif')
self.first_result = """data_cu_BruecknerJK_153F40_0m
loop_
_refln_index_h
Expand Down
10 changes: 6 additions & 4 deletions tests/test_import_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
from finalcif.gui.import_selector import ImportSelector
from finalcif.tools.settings import FinalCifSettings

data = Path('tests')
testdata = Path('test-data')

class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
imp_cif = CifContainer('tests/statics/import_cif.cif')
shutil.copyfile('test-data/p21c.cif', 'test-data/p21c-copy.cif')
targetcif = CifContainer('test-data/p21c-copy.cif')
imp_cif = CifContainer(data / 'statics/import_cif.cif')
shutil.copyfile(testdata / 'p21c.cif', testdata / 'p21c-copy.cif')
targetcif = CifContainer(testdata / 'p21c-copy.cif')
settings = FinalCifSettings()
self.imp = ImportSelector(None, import_cif=imp_cif, target_cif=targetcif, settings=settings)
self.imp._empty_saved_selection()
self.imp.show_import_window()

def tearDown(self) -> None:
Path('test-data/p21c-copy.cif').unlink(missing_ok=True)
Path(testdata / 'p21c-copy.cif').unlink(missing_ok=True)

def test_keys_to_import(self):
self.assertEqual(3, self.imp.keys_to_import)
Expand Down
Loading

0 comments on commit 1f25842

Please sign in to comment.