From 5f5390c1f84686d98fc392771a422131a02a7011 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Thu, 27 Apr 2023 14:01:00 +1000 Subject: [PATCH 1/3] TST: move tests into orsopy hierarchy --- .github/workflows/pytest.yml | 4 +- .../fileio/tests}/__init__.py | 0 {tests => orsopy/fileio/tests}/not_orso.ort | 0 .../fileio/tests}/test_base.py | 24 ++++----- .../fileio/tests}/test_data_source.py | 49 +++++++++++-------- .../fileio/tests}/test_example.ort | 0 .../fileio/tests}/test_example2.ort | 0 .../fileio/tests}/test_model_language.py | 1 - .../fileio/tests}/test_orso.py | 7 ++- .../fileio/tests}/test_reduction.py | 0 .../fileio/tests}/test_schema.py | 11 +++-- .../slddb/tests}/__init__.py | 0 .../slddb/tests}/test_converters.py | 0 .../slddb/tests}/test_data/1011267.cif | 0 .../slddb/tests}/test_data/2300202.cif | 0 .../slddb/tests}/test_data/7lzm.cif | 0 .../slddb/tests}/test_data/AMS_DATA.cif | 0 .../slddb/tests}/test_dbcreation.py | 0 .../slddb/tests}/test_dbmaterial.py | 0 .../slddb/tests}/test_elements.py | 0 .../slddb/tests}/test_formula.py | 0 .../slddb/tests}/test_material.py | 0 .../slddb/tests}/test_webapi.py | 0 tests/__init__.py | 1 - 24 files changed, 55 insertions(+), 42 deletions(-) rename {tests/test_fileio => orsopy/fileio/tests}/__init__.py (100%) rename {tests => orsopy/fileio/tests}/not_orso.ort (100%) rename {tests/test_fileio => orsopy/fileio/tests}/test_base.py (96%) rename {tests/test_fileio => orsopy/fileio/tests}/test_data_source.py (89%) rename {tests => orsopy/fileio/tests}/test_example.ort (100%) rename {tests => orsopy/fileio/tests}/test_example2.ort (100%) rename {tests/test_fileio => orsopy/fileio/tests}/test_model_language.py (99%) rename {tests/test_fileio => orsopy/fileio/tests}/test_orso.py (98%) rename {tests/test_fileio => orsopy/fileio/tests}/test_reduction.py (100%) rename {tests/test_fileio => orsopy/fileio/tests}/test_schema.py (72%) rename {tests/test_slddb => orsopy/slddb/tests}/__init__.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_converters.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_data/1011267.cif (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_data/2300202.cif (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_data/7lzm.cif (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_data/AMS_DATA.cif (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_dbcreation.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_dbmaterial.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_elements.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_formula.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_material.py (100%) rename {tests/test_slddb => orsopy/slddb/tests}/test_webapi.py (100%) delete mode 100644 tests/__init__.py diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 4f832096..e0ec1634 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -31,7 +31,7 @@ jobs: - name: Test with pytest run: | - pytest + pytest --pyargs pytest - name: Test with coverage run: | @@ -68,4 +68,4 @@ jobs: - name: Test with pytest run: | - pytest + pytest --pyargs orsopy diff --git a/tests/test_fileio/__init__.py b/orsopy/fileio/tests/__init__.py similarity index 100% rename from tests/test_fileio/__init__.py rename to orsopy/fileio/tests/__init__.py diff --git a/tests/not_orso.ort b/orsopy/fileio/tests/not_orso.ort similarity index 100% rename from tests/not_orso.ort rename to orsopy/fileio/tests/not_orso.ort diff --git a/tests/test_fileio/test_base.py b/orsopy/fileio/tests/test_base.py similarity index 96% rename from tests/test_fileio/test_base.py rename to orsopy/fileio/tests/test_base.py index 890eb5bf..8ab3cd51 100644 --- a/tests/test_fileio/test_base.py +++ b/orsopy/fileio/tests/test_base.py @@ -3,12 +3,11 @@ """ # pylint: disable=R0201 -import pathlib +from pathlib import Path import unittest from datetime import datetime from math import log, sqrt -from os.path import join as pjoin import pint import pytest @@ -18,6 +17,9 @@ from orsopy.fileio import base, orso +pth = Path(__file__).absolute().parent + + class TestErrorValue(unittest.TestCase): """ Testing the Value class. @@ -501,9 +503,9 @@ def test_creation_for_existing_file(self): """ Creation for a file that does exist with a given modified date. """ - fname = pathlib.Path("README.rst") + fname = Path(pth / "not_orso.ort") value = base.File(str(fname.absolute()), datetime.fromtimestamp(fname.stat().st_mtime)) - assert value.file == str(pathlib.Path().resolve().joinpath("README.rst")) + assert value.file == str(fname) assert value.timestamp == datetime.fromtimestamp(fname.stat().st_mtime) def test_to_yaml_for_existing_file(self): @@ -511,11 +513,11 @@ def test_to_yaml_for_existing_file(self): Transformation to yaml a file that does exist with a given modified date. """ - fname = pathlib.Path("README.rst") + fname = Path(pth / "not_orso.ort") value = base.File(str(fname.absolute()), datetime.fromtimestamp(fname.stat().st_mtime)) assert ( value.to_yaml() - == f'file: {str(pathlib.Path().resolve().joinpath("README.rst"))}\n' + == f'file: {str(fname)}\n' + "timestamp: " + f"{datetime.fromtimestamp(fname.stat().st_mtime).isoformat()}\n" ) @@ -525,9 +527,9 @@ def test_creation_for_existing_file_no_mod_time(self): Transformation to yaml a file that does exist without a given modified date. """ - fname = pathlib.Path("AUTHORS.rst") + fname = Path(pth / "not_orso.ort") value = base.File(str(fname.absolute()), None) - assert value.file == str(pathlib.Path().resolve().joinpath("AUTHORS.rst")) + assert value.file == str(fname) assert value.timestamp == datetime.fromtimestamp(fname.stat().st_mtime) def test_to_yaml_for_existing_file_no_mod_time(self): @@ -535,12 +537,12 @@ def test_to_yaml_for_existing_file_no_mod_time(self): Transformation to yaml a file that does exist without a given modified date. """ - fname = pathlib.Path("AUTHORS.rst") + fname = Path(pth / "not_orso.ort") value = base.File(str(fname.absolute()), None) assert ( value.to_yaml() == "file: " - + f'{str(pathlib.Path().resolve().joinpath("AUTHORS.rst"))}\n' + + f'{str(fname)}\n' + "timestamp: " + f"{datetime.fromtimestamp(fname.stat().st_mtime).isoformat()}\n" ) @@ -548,4 +550,4 @@ def test_to_yaml_for_existing_file_no_mod_time(self): def test_not_orso(): with pytest.raises(base.NotOrsoCompatibleFileError, match="First line does not appea"): - orso.load_orso(pjoin("tests", "not_orso.ort")) + orso.load_orso(pth / "not_orso.ort") diff --git a/tests/test_fileio/test_data_source.py b/orsopy/fileio/tests/test_data_source.py similarity index 89% rename from tests/test_fileio/test_data_source.py rename to orsopy/fileio/tests/test_data_source.py index d69dc413..1aa9e4d5 100644 --- a/tests/test_fileio/test_data_source.py +++ b/orsopy/fileio/tests/test_data_source.py @@ -2,7 +2,7 @@ Tests for fileio.data_source module """ -import pathlib +from pathlib import Path import unittest from datetime import datetime @@ -10,6 +10,9 @@ from orsopy.fileio import base, data_source +pth = Path(__file__).absolute().parent + + class TestExperiment(unittest.TestCase): """ Testing the Experiment class. @@ -260,27 +263,29 @@ def test_creation(self): """ Creation with minimal set. """ + fname = pth / "not_orso.ort" value = data_source.Measurement( data_source.InstrumentSettings(base.Value(4.0, "deg"), base.ValueRange(2.0, 12.0, "angstrom"),), - [base.File(str(pathlib.Path().resolve().joinpath("README.rst")), None)], + [base.File(str(fname), None)], ) assert value.instrument_settings.incident_angle.magnitude == 4.0 assert value.instrument_settings.incident_angle.unit == "deg" assert value.instrument_settings.wavelength.min == 2.0 assert value.instrument_settings.wavelength.max == 12.0 assert value.instrument_settings.wavelength.unit == "angstrom" - assert value.data_files[0].file == str(pathlib.Path().resolve().joinpath("README.rst")) - assert value.data_files[0].timestamp == datetime.fromtimestamp(pathlib.Path("README.rst").stat().st_mtime) + assert value.data_files[0].file == str(pth / "not_orso.ort") + assert value.data_files[0].timestamp == datetime.fromtimestamp(fname.stat().st_mtime) def test_to_yaml(self): """ Transform to yaml with minimal set. """ + fname = pth / "not_orso.ort" + value = data_source.Measurement( data_source.InstrumentSettings(base.Value(4.0, "deg"), base.ValueRange(2.0, 12.0, "angstrom"),), - [base.File(str(pathlib.Path().resolve().joinpath("README.rst")), None)], + [base.File(str(fname), None)], ) - fname = pathlib.Path("README.rst") assert ( value.to_yaml() == "instrument_settings:\n incident_angle:" @@ -294,44 +299,48 @@ def test_creation_optionals(self): """ Creation with optionals. """ + fname0 = pth / "not_orso.ort" + fname1 = pth / "test_base.py" + value = data_source.Measurement( data_source.InstrumentSettings(base.Value(4.0, "deg"), base.ValueRange(2.0, 12.0, "angstrom"),), - [base.File(str(pathlib.Path().resolve().joinpath("README.rst")), None)], - [base.File(str(pathlib.Path().resolve().joinpath("AUTHORS.rst")), None)], + [base.File(str(fname0), None)], + [base.File(str(fname1), None)], ) assert value.instrument_settings.incident_angle.magnitude == 4.0 assert value.instrument_settings.incident_angle.unit == "deg" assert value.instrument_settings.wavelength.min == 2.0 assert value.instrument_settings.wavelength.max == 12.0 assert value.instrument_settings.wavelength.unit == "angstrom" - assert value.data_files[0].file == str(pathlib.Path().resolve().joinpath("README.rst")) - assert value.data_files[0].timestamp == datetime.fromtimestamp(pathlib.Path("README.rst").stat().st_mtime) - assert value.additional_files[0].file == str(pathlib.Path().resolve().joinpath("AUTHORS.rst")) + assert value.data_files[0].file == str(fname0) + assert value.data_files[0].timestamp == datetime.fromtimestamp(fname0.stat().st_mtime) + assert value.additional_files[0].file == str(fname1) assert value.additional_files[0].timestamp == datetime.fromtimestamp( - pathlib.Path("AUTHORS.rst").stat().st_mtime + fname1.stat().st_mtime ) def test_to_yaml_optionals(self): """ Transform to yaml with optionals. """ + fname0 = pth / "not_orso.ort" + fname1 = pth / "test_base.py" + value = data_source.Measurement( data_source.InstrumentSettings(base.Value(4.0, "deg"), base.ValueRange(2.0, 12.0, "angstrom"),), - [base.File(str(pathlib.Path().resolve().joinpath("README.rst")), None)], - [base.File(str(pathlib.Path().resolve().joinpath("AUTHORS.rst")), None)], + [base.File(str(fname0), None)], + [base.File(str(fname1), None)], "energy-dispersive", ) - fname = pathlib.Path("README.rst") - gname = pathlib.Path("AUTHORS.rst") assert ( value.to_yaml() == "instrument_settings:\n incident_angle:" + " {magnitude: 4.0, unit: deg}\n wavelength: {min: " + "2.0, max: 12.0, unit: angstrom}\ndata_files:\n- file: " - + f"{str(fname.absolute())}\n timestamp: " - + f"{datetime.fromtimestamp(fname.stat().st_mtime).isoformat()}\n" + + f"{str(fname0.absolute())}\n timestamp: " + + f"{datetime.fromtimestamp(fname0.stat().st_mtime).isoformat()}\n" + "additional_files:\n- file: " - + f"{str(gname.absolute())}\n timestamp: " - + f"{datetime.fromtimestamp(gname.stat().st_mtime).isoformat()}\n" + + f"{str(fname1.absolute())}\n timestamp: " + + f"{datetime.fromtimestamp(fname1.stat().st_mtime).isoformat()}\n" + "scheme: energy-dispersive\n" ) diff --git a/tests/test_example.ort b/orsopy/fileio/tests/test_example.ort similarity index 100% rename from tests/test_example.ort rename to orsopy/fileio/tests/test_example.ort diff --git a/tests/test_example2.ort b/orsopy/fileio/tests/test_example2.ort similarity index 100% rename from tests/test_example2.ort rename to orsopy/fileio/tests/test_example2.ort diff --git a/tests/test_fileio/test_model_language.py b/orsopy/fileio/tests/test_model_language.py similarity index 99% rename from tests/test_fileio/test_model_language.py rename to orsopy/fileio/tests/test_model_language.py index 57d657f4..cb71959e 100644 --- a/tests/test_fileio/test_model_language.py +++ b/orsopy/fileio/tests/test_model_language.py @@ -3,7 +3,6 @@ """ # pylint: disable=R0201 -import pathlib import unittest from datetime import datetime diff --git a/tests/test_fileio/test_orso.py b/orsopy/fileio/tests/test_orso.py similarity index 98% rename from tests/test_fileio/test_orso.py rename to orsopy/fileio/tests/test_orso.py index 417e98d8..f2e66403 100644 --- a/tests/test_fileio/test_orso.py +++ b/orsopy/fileio/tests/test_orso.py @@ -2,7 +2,7 @@ Tests for fileio module """ -import os.path +from pathlib import Path import unittest from datetime import datetime @@ -18,6 +18,9 @@ from orsopy.fileio.reduction import Reduction, Software +pth = Path(__file__).absolute().parent + + class TestOrso(unittest.TestCase): """ Testing the Orso class. @@ -226,7 +229,7 @@ def test_extra_elements(self): # if there are extra elements present in the ORT file they should still # be loadable. They won't be there as dataclass fields, but they'll be # visible as attributes. - datasets = fileio.load_orso(os.path.join("tests", "test_example.ort")) + datasets = fileio.load_orso(pth / "test_example.ort") info = datasets[0].info assert hasattr(info.data_source.measurement.instrument_settings.incident_angle, "resolution") diff --git a/tests/test_fileio/test_reduction.py b/orsopy/fileio/tests/test_reduction.py similarity index 100% rename from tests/test_fileio/test_reduction.py rename to orsopy/fileio/tests/test_reduction.py diff --git a/tests/test_fileio/test_schema.py b/orsopy/fileio/tests/test_schema.py similarity index 72% rename from tests/test_fileio/test_schema.py rename to orsopy/fileio/tests/test_schema.py index e485b851..10cfb2fd 100644 --- a/tests/test_fileio/test_schema.py +++ b/orsopy/fileio/tests/test_schema.py @@ -1,5 +1,5 @@ import json -import os.path +from pathlib import Path import jsonschema import numpy as np @@ -8,15 +8,16 @@ from orsopy import fileio from orsopy.fileio.base import _read_header_data, _validate_header_data +pth = Path(__file__).absolute().parent + class TestSchema: def test_example_ort(self): - pth = os.path.dirname(fileio.__file__) - schema_pth = os.path.join(pth, "schema", "refl_header.schema.json") + schema_pth = pth / ".." / "schema" / "refl_header.schema.json" with open(schema_pth, "r") as f: schema = json.load(f) - dct_list, data, version = _read_header_data(os.path.join("tests", "test_example.ort"), validate=True) + dct_list, data, version = _read_header_data(pth / "test_example.ort", validate=True) assert data[0].shape == (2, 4) assert version == "0.1" @@ -29,7 +30,7 @@ def test_example_ort(self): _validate_header_data(dct_list) # try a 2 dataset file - dct_list, data, version = _read_header_data(os.path.join("tests", "test_example2.ort"), validate=True) + dct_list, data, version = _read_header_data(pth / "test_example2.ort", validate=True) assert len(dct_list) == 2 assert dct_list[1]["data_set"] == "spin_down" assert data[1].shape == (4, 4) diff --git a/tests/test_slddb/__init__.py b/orsopy/slddb/tests/__init__.py similarity index 100% rename from tests/test_slddb/__init__.py rename to orsopy/slddb/tests/__init__.py diff --git a/tests/test_slddb/test_converters.py b/orsopy/slddb/tests/test_converters.py similarity index 100% rename from tests/test_slddb/test_converters.py rename to orsopy/slddb/tests/test_converters.py diff --git a/tests/test_slddb/test_data/1011267.cif b/orsopy/slddb/tests/test_data/1011267.cif similarity index 100% rename from tests/test_slddb/test_data/1011267.cif rename to orsopy/slddb/tests/test_data/1011267.cif diff --git a/tests/test_slddb/test_data/2300202.cif b/orsopy/slddb/tests/test_data/2300202.cif similarity index 100% rename from tests/test_slddb/test_data/2300202.cif rename to orsopy/slddb/tests/test_data/2300202.cif diff --git a/tests/test_slddb/test_data/7lzm.cif b/orsopy/slddb/tests/test_data/7lzm.cif similarity index 100% rename from tests/test_slddb/test_data/7lzm.cif rename to orsopy/slddb/tests/test_data/7lzm.cif diff --git a/tests/test_slddb/test_data/AMS_DATA.cif b/orsopy/slddb/tests/test_data/AMS_DATA.cif similarity index 100% rename from tests/test_slddb/test_data/AMS_DATA.cif rename to orsopy/slddb/tests/test_data/AMS_DATA.cif diff --git a/tests/test_slddb/test_dbcreation.py b/orsopy/slddb/tests/test_dbcreation.py similarity index 100% rename from tests/test_slddb/test_dbcreation.py rename to orsopy/slddb/tests/test_dbcreation.py diff --git a/tests/test_slddb/test_dbmaterial.py b/orsopy/slddb/tests/test_dbmaterial.py similarity index 100% rename from tests/test_slddb/test_dbmaterial.py rename to orsopy/slddb/tests/test_dbmaterial.py diff --git a/tests/test_slddb/test_elements.py b/orsopy/slddb/tests/test_elements.py similarity index 100% rename from tests/test_slddb/test_elements.py rename to orsopy/slddb/tests/test_elements.py diff --git a/tests/test_slddb/test_formula.py b/orsopy/slddb/tests/test_formula.py similarity index 100% rename from tests/test_slddb/test_formula.py rename to orsopy/slddb/tests/test_formula.py diff --git a/tests/test_slddb/test_material.py b/orsopy/slddb/tests/test_material.py similarity index 100% rename from tests/test_slddb/test_material.py rename to orsopy/slddb/tests/test_material.py diff --git a/tests/test_slddb/test_webapi.py b/orsopy/slddb/tests/test_webapi.py similarity index 100% rename from tests/test_slddb/test_webapi.py rename to orsopy/slddb/tests/test_webapi.py diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index 1e7041f1..00000000 --- a/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Unit test package for orsopy.""" From f66ef43ef62509ae0f6961791ce453baaa117237 Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Thu, 27 Apr 2023 14:31:08 +1000 Subject: [PATCH 2/3] TST: fix bugs --- .github/workflows/pylint.yml | 1 - .github/workflows/pytest.yml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index dea2abef..ae84b2a5 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -28,4 +28,3 @@ jobs: - name: Lint run: | flake8 --max-line-length=120 --ignore=F401,W503,E203 --count --show-source --statistics orsopy - flake8 --max-line-length=120 --ignore=F401,W503,E203 --count --show-source --statistics tests diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index e0ec1634..81fa2f8e 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -31,7 +31,7 @@ jobs: - name: Test with pytest run: | - pytest --pyargs pytest + pytest --pyargs orsopy - name: Test with coverage run: | From f8d74c0e940573fa5bbf8f485952b81b009a72da Mon Sep 17 00:00:00 2001 From: Andrew Nelson Date: Thu, 27 Apr 2023 14:42:18 +1000 Subject: [PATCH 3/3] TST: fix bugs --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 01e313c5..a0bfbdf4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -31,3 +31,4 @@ include_package_data = True [options.package_data] orsopy.slddb.element_table = nabs_geant4/*.npz orsopy.fileio = schema/*.* +orsopy.fileio.tests = *.ort \ No newline at end of file