Skip to content

Commit

Permalink
Refactor testing
Browse files Browse the repository at this point in the history
- Migrate to pytest testing
- Move tests out of main folder
- Add coverage reporting to github
- Add testing for pyyaml
- Refactor how ruamel vs pyyaml is implemented
  • Loading branch information
oerc0122 committed Feb 7, 2025
1 parent ee396eb commit acbf9f5
Show file tree
Hide file tree
Showing 94 changed files with 6,306 additions and 308 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/test_castep_outputs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: Python application
on:
push:
branches: [ "main" ]
pull_request:
pull_request:
types:
- opened
- synchronize
Expand All @@ -17,9 +17,9 @@ permissions:

jobs:
build:

runs-on: ubuntu-latest

permissions:
pull-requests: write
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
Expand All @@ -33,8 +33,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruamel.yaml coverage
pip install ".[test,ruamel,yaml]"
- name: Run tests
run: |
PYTHONPATH=. coverage run -m unittest discover castep_outputs/test/
coverage report
pytest --doctest-modules --cov=castep_outputs --cov-report xml:coverage.xml
- name: Get Coverage
uses: orgoro/[email protected]
with:
coverageFile: coverage.xml
token: ${{ secrets.GITHUB_TOKEN }}
22 changes: 0 additions & 22 deletions castep_outputs/test/gen_data.py

This file was deleted.

9 changes: 0 additions & 9 deletions castep_outputs/test/test_docstrings.py

This file was deleted.

191 changes: 0 additions & 191 deletions castep_outputs/test/test_dumpers.py

This file was deleted.

47 changes: 0 additions & 47 deletions castep_outputs/test/test_filters.py

This file was deleted.

31 changes: 16 additions & 15 deletions castep_outputs/utilities/dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

import json
import pprint
from contextlib import suppress
from typing import Any, Callable, TextIO

try:
from ruamel import yaml
_YAML_TYPE = None

with suppress(ImportError):
import yaml
_YAML_TYPE = "pyyaml"

with suppress(ImportError):
from ruamel import yaml as ruamel
_YAML_TYPE = "ruamel"
except ImportError:
try:
import yaml
_YAML_TYPE = "yaml"
except ImportError:
_YAML_TYPE = None


#: Dumping function protocol.
Expand Down Expand Up @@ -45,11 +46,11 @@ def ruamel_dumper(data: Any, file: TextIO):
file
File to dump to.
"""
yaml_eng = yaml.YAML(typ="unsafe")
yaml_eng = ruamel.YAML(typ="unsafe")
yaml_eng.dump(data, file)


def yaml_dumper(data: Any, file: TextIO):
def pyyaml_dumper(data: Any, file: TextIO):
"""
YAML (pyyaml) format dumper.
Expand Down Expand Up @@ -117,21 +118,21 @@ def get_dumpers(dump_fmt: str) -> Dumper:
SUPPORTED_FORMATS
Acceptable values for `dump_fmt`.
"""
if dump_fmt not in SUPPORTED_FORMATS:
raise ValueError(f"Cannot output in {dump_fmt} format. "
f"Valid keys are: {', '.join(SUPPORTED_FORMATS.keys())}")

if dump_fmt == "yaml":
if _YAML_TYPE is None:
raise ImportError("Couldn't find valid yaml dumper (ruamel.yaml / yaml)"
"please install and try again.")
dump_fmt = _YAML_TYPE

if dump_fmt not in SUPPORTED_FORMATS:
raise ValueError(f"Cannot output in {dump_fmt} format. "
f"Valid keys are: {', '.join(SUPPORTED_FORMATS.keys())}")

return SUPPORTED_FORMATS[dump_fmt]

#: Currently supported dumpers.
SUPPORTED_FORMATS: dict[str, Dumper] = {"json": json_dumper,
"ruamel": ruamel_dumper,
"yaml": yaml_dumper,
"pyyaml": pyyaml_dumper,
"pprint": pprint_dumper,
"print": print_dumper}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ruamel = ["ruamel.yaml>=0.17.22"]
yaml = ["pyYAML>=3.13"]
docs = ["jupyter-book>=0.13.1", "sphinx-book-theme>=0.3.3", "sphinx-argparse>=0.4.0", "sphinx-autodoc-typehints"]
lint = ["ruff==0.8.0"]
test = ["pytest==8.3.4", "pytest-cov==5.0.0"]

[project.scripts]
castep_outputs = "castep_outputs.cli.castep_outputs_main:main"
Expand Down
File renamed without changes.
Loading

0 comments on commit acbf9f5

Please sign in to comment.