Skip to content

Commit 062e6ae

Browse files
authored
Merge pull request #29 from kif/test_modules
Non regression test for all modules import
2 parents a848c8e + d66bc03 commit 062e6ae

File tree

6 files changed

+54
-23
lines changed

6 files changed

+54
-23
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
ignore = E203, E266, E501, W503, F403, F401, F405
3+
max-line-length = 120
4+
max-complexity = 18
5+
select = C,E,F,W,B,B950

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ repos:
2222
rev: 6.0.0
2323
hooks:
2424
- id: flake8
25+
args: [--config=.flake8]

src/modacor/io/hdf/hdf_loader.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
__status__ = "Alpha"
2828

2929

30-
from ..io_source import IoSource
31-
from ..io_sources import IoSources
32-
from modacor.dataclasses.messagehandler import *
33-
34-
from os.path import abspath
3530
from logging import WARNING
36-
import numpy as np
31+
from os.path import abspath
32+
3733
import h5py
34+
import numpy as np
35+
36+
from modacor.dataclasses.messagehandler import *
37+
38+
from ..io_source import IoSource
39+
from ..io_sources import IoSources
3840

3941

4042
class HDFLoader(IoSource):
@@ -46,22 +48,20 @@ def __init__(self, source_reference: str, logging_level = WARNING):
4648
self._file_datasets = []
4749
self._file_datasets_shapes = {}
4850

49-
50-
def _open_file(self, file_path = None):
51+
def _open_file(self, file_path=None):
5152
if file_path is None:
52-
error = 'No filepath given'
53+
error = "No filepath given"
5354
self.hdf_logger.log.error(error)
5455
raise OSError(error)
55-
56+
5657
try:
57-
self._file_reference = h5py.File(file_path, 'r')
58+
self._file_reference = h5py.File(file_path, "r")
5859
self._file_path = abspath(file_path)
5960
self._file_reference.visititems(self._find_datasets)
6061
except OSError as error:
6162
self.hdf_logger.logger.error(error)
6263
raise OSError(error)
6364

64-
6565
def _close_file(self):
6666
try:
6767
self._file_reference.close()
@@ -73,9 +73,8 @@ def _close_file(self):
7373
self.hdf_logger.log.error(error)
7474
raise OSError(error)
7575

76-
7776
def _find_datasets(self, path_name, path_object):
7877
"""An internal function to be used to walk the tree of an HDF5 file and return a list of the datasets within"""
79-
if(isinstance(self._file_reference[path_name], h5py._hl.dataset.Dataset)):
78+
if isinstance(self._file_reference[path_name], h5py._hl.dataset.Dataset):
8079
self._file_datasets.append(path_name)
81-
self._file_datasets_shapes[path_name] = self._file_reference[path_name].shape
80+
self._file_datasets_shapes[path_name] = self._file_reference[path_name].shape

src/modacor/tests/io/hdf/test_hdf_loader.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626
__copyright__ = "Copyright 2025 MoDaCor Authors"
2727
__status__ = "Alpha"
2828

29-
from modacor.io.hdf.hdf_loader import *
30-
31-
from os.path import abspath
29+
import unittest
3230
from logging import WARNING
3331
from os.path import abspath
3432
from os import unlink
3533
import numpy as np
3634
import tempfile
3735
import unittest
3836
import h5py
37+
import numpy as np
38+
39+
from ....io.hdf.hdf_loader import *
3940

4041

4142
class TestHDFLoader(unittest.TestCase):
@@ -52,23 +53,19 @@ def setUp(self):
5253
self.temp_hdf_file[self.temp_dataset_name] = np.zeros(self.temp_dataset_shape)
5354
self.temp_file_handle.close()
5455

55-
5656
def tearDown(self):
5757
self.test_h5_loader = None
5858
self.test_file_path = None
5959
self.test_dataset_name = None
6060
self.test_dataset_shape = None
6161
unlink(self.temp_file_path)
6262

63-
6463
def test_open_file(self):
6564
self.test_hdf_loader._open_file(self.temp_file_path)
66-
6765
self.assertEqual(self.temp_file_path, self.test_hdf_loader._file_path)
6866
self.assertEqual(self.temp_dataset_name, self.test_hdf_loader._file_datasets[0])
6967
self.assertEqual(self.temp_dataset_shape, self.test_hdf_loader._file_datasets_shapes[self.temp_dataset_name])
7068

71-
7269
def test_close_file(self):
7370
self.test_open_file()
7471
self.test_hdf_loader._close_file()

src/modacor/tests/test_import.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""try to import all sub-modules from the project"""
2+
3+
import importlib
4+
import os
5+
6+
dirname = os.path.dirname
7+
8+
9+
def test_import_all():
10+
project_dir = dirname(dirname(os.path.abspath(__file__)))
11+
start = len(project_dir) - len("modacor")
12+
modules = []
13+
for path, dirs, files in os.walk(project_dir):
14+
for f in files:
15+
if f.endswith(".py") and not f.startswith("__"):
16+
modules.append(os.path.join(path[start:], f[:-3]))
17+
cnt = 0
18+
for i in modules:
19+
j = i.replace(os.sep, ".")
20+
try:
21+
_ = importlib.import_module(j)
22+
except Exception as err:
23+
print(f"{type(err)} in {j}: {err}.")
24+
cnt += 1
25+
assert cnt == 0
26+
27+
28+
if __name__ == "__main__":
29+
test_import_all()

src/modacor/tests/test_variance_calculations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111
from uncertainties.unumpy import nominal_values, std_devs, uarray
1212

13-
import modacor.math.variance_calculations as varc
13+
from ..math import variance_calculations as varc
1414

1515
samples = 1000
1616

0 commit comments

Comments
 (0)