import numpy as np import unittest from PySide2 import QtCore, QtWidgets # import logging # logging.basicConfig(level=logging.DEBUG) from hyo2.abc.lib.helper import Helper from hyo2.openbst.lib.products.formats.product_format_type import ProductFormatType from hyo2.openbst.lib.products.product_layer_type import ProductLayerType from hyo2.openbst.lib.products.product_layer import ProductLayer from hyo2.openbst.app.dialogs.array_explorer.array_explorer import ArrayExplorer @unittest.skipIf(Helper.is_linux(), "Skip Linux") class TestAppArrayExplorer(unittest.TestCase): def test_visibility(self): # noinspection PyUnresolvedReferences if not QtWidgets.qApp: QtWidgets.QApplication([]) mw = QtWidgets.QMainWindow() mw.show() layer = ProductLayer(format_type=ProductFormatType.UNKNOWN, layer_type=ProductLayerType.UNKNOWN) layer.array = np.zeros((20, 30, 40), dtype=np.float32) d = ArrayExplorer(layer=layer, parent=mw,
class TestLibModelsJackson2(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.testing = Testing( root_folder=os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir, os.pardir))) def test__init__(self): _ = Model() def test_run(self): m = Model() with self.assertRaises(RuntimeError): m.run() m.use_default_mdl_params() with self.assertRaises(RuntimeError): m.run() m.use_test_sed_params(TestSediments.COARSE_SAND) m.run() @unittest.skipIf(Helper.is_linux(), "Skip Linux") def test_plotter(self): m = Model() m.use_default_mdl_params() m.use_test_sed_params(TestSediments.COARSE_SAND) plotter = ModelPlotter(mdl=m) plotter.plot() def compare(self, use_300k: bool = False): root_folder = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)) testing = Testing(root_folder=root_folder) input_folder = testing.input_data_folder() jackson2_folder = os.path.join(input_folder, "jackson2") if use_300k: folder_freq = os.path.join(jackson2_folder, "300k") else: folder_freq = os.path.join(jackson2_folder, "20k") for sediment in TestSediments: mdl = Model() mdl.use_test_sed_params(sediment) mdl.use_default_mdl_params() if use_300k: mdl.mdl_params.f = 300000 mdl.run() mat_folder = os.path.join(folder_freq, str(sediment).split('.')[-1].lower()) mat_files = Testing.files(mat_folder, ext='.mat') mat_out = ModelOutput() for mat_file in mat_files: if "theta_g" in mat_file: mat_out.theta_g = scipy.io.loadmat(mat_file)['thetg'].flatten() self.assertTrue(np.isclose(mat_out.theta_g, mdl.out.theta_g).all()) elif "ss_tot" in mat_file: mat_out.ss_tot = scipy.io.loadmat(mat_file)['sstot'].flatten() self.assertTrue(np.isclose(mat_out.ss_tot, mdl.out.ss_tot).all()) elif "ss_rough" in mat_file: mat_out.ss_rough = scipy.io.loadmat(mat_file)['ssrough'].flatten() self.assertTrue(np.isclose(mat_out.ss_rough, mdl.out.ss_rough).all()) elif "ss_vol" in mat_file: mat_out.ss_vol = scipy.io.loadmat(mat_file)['ssvol'].flatten() self.assertTrue(np.isclose(mat_out.ss_vol, mdl.out.ss_vol).all()) elif "ref_loss" in mat_file: mat_out.ref_loss = scipy.io.loadmat(mat_file)['refloss'].flatten() self.assertTrue(np.isclose(mat_out.ref_loss, mdl.out.ref_loss).all()) @unittest.skipIf(Helper.is_linux(), "Skip Linux") def test_compare_20k(self): self.compare(use_300k=False) @unittest.skipIf(Helper.is_linux(), "Skip Linux") def test_compare_300k(self): self.compare(use_300k=True)
class TestABCLibHelper(unittest.TestCase): def setUp(self): self.h = Helper(lib_info=LibInfo()) @unittest.skipIf(Helper.is_linux(), "test not supported on Linux") def test_explore_folder(self): self.assertTrue(self.h.explore_folder(__file__)) self.assertFalse(self.h.explore_folder(__file__ + ".fake")) self.assertTrue(self.h.explore_folder(os.path.dirname(__file__))) self.assertFalse(self.h.explore_folder(os.path.dirname(__file__) + "fake")) def test_first_match(self): # fake dict a_dict = { "a": 1, "b": 99, "c": 1, } # test if it gives back the first matching key self.assertTrue(Helper.first_match(a_dict, 1) in ["a", "c"]) # test if it raises with a not-existing value with self.assertRaises(RuntimeError): Helper.first_match(a_dict, 2) def test_is_64bit_os(self): self.assertIsInstance(self.h.is_64bit_os(), bool) def test_is_64bit_python(self): self.assertIsInstance(self.h.is_64bit_python(), bool) def test_is_darwin_linux_windows(self): self.assertIsInstance(self.h.is_darwin(), bool) self.assertIsInstance(self.h.is_linux(), bool) self.assertIsInstance(self.h.is_windows(), bool) self.assertTrue(any([self.h.is_linux(), self.h.is_darwin(), self.h.is_windows()])) def test_is_pydro(self): self.assertIsInstance(self.h.is_pydro(), bool) def test_is_url(self): self.assertTrue(self.h.is_url("https://www.hydroffice.org")) self.assertTrue(self.h.is_url("http://www.hydroffice.org")) self.assertFalse(self.h.is_url("ftp://fake/url")) def test_python_path(self): self.assertTrue(os.path.exists(self.h.python_path())) def test_package_info(self): self.assertIsInstance(self.h.package_info(qt_html=True), str) self.assertIsInstance(self.h.package_info(qt_html=False), str) def test_package_folder(self): self.assertTrue(os.path.exists(self.h.package_folder())) def test_hydroffice_folder(self): self.assertTrue(os.path.exists(self.h.hydroffice_folder()))