Skip to content

Commit

Permalink
Merge pull request #105 from andyfaff/test
Browse files Browse the repository at this point in the history
TST: move tests into orsopy hierarchy
  • Loading branch information
andyfaff authored May 8, 2023
2 parents 4623a87 + f8d74c0 commit a4b4de0
Show file tree
Hide file tree
Showing 26 changed files with 56 additions and 43 deletions.
1 change: 0 additions & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Test with pytest
run: |
pytest
pytest --pyargs orsopy
- name: Test with coverage
run: |
Expand Down Expand Up @@ -68,4 +68,4 @@ jobs:
- name: Test with pytest
run: |
pytest
pytest --pyargs orsopy
File renamed without changes.
File renamed without changes.
24 changes: 13 additions & 11 deletions tests/test_fileio/test_base.py → orsopy/fileio/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +17,9 @@
from orsopy.fileio import base, orso


pth = Path(__file__).absolute().parent


class TestErrorValue(unittest.TestCase):
"""
Testing the Value class.
Expand Down Expand Up @@ -501,21 +503,21 @@ 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):
"""
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"
)
Expand All @@ -525,27 +527,27 @@ 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):
"""
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"
)


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")
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
Tests for fileio.data_source module
"""

import pathlib
from pathlib import Path
import unittest

from datetime import datetime

from orsopy.fileio import base, data_source


pth = Path(__file__).absolute().parent


class TestExperiment(unittest.TestCase):
"""
Testing the Experiment class.
Expand Down Expand Up @@ -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:"
Expand All @@ -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"
)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
# pylint: disable=R0201

import pathlib
import unittest

from datetime import datetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tests for fileio module
"""

import os.path
from pathlib import Path
import unittest

from datetime import datetime
Expand All @@ -18,6 +18,9 @@
from orsopy.fileio.reduction import Reduction, Software


pth = Path(__file__).absolute().parent


class TestOrso(unittest.TestCase):
"""
Testing the Orso class.
Expand Down Expand Up @@ -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")

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import os.path
from pathlib import Path

import jsonschema
import numpy as np
Expand All @@ -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"

Expand All @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion tests/__init__.py

This file was deleted.

0 comments on commit a4b4de0

Please sign in to comment.