Skip to content

Commit

Permalink
Merge pull request #9 from harmsm/main
Browse files Browse the repository at this point in the history
added unit test
  • Loading branch information
harmsm authored Jul 16, 2024
2 parents 02f5090 + e9d648a commit 854eb6b
Show file tree
Hide file tree
Showing 10 changed files with 1,006 additions and 7 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This workflow will install Python dependencies, run tests and flake8 across
# platforms and python versions.

name: ElliptiCBn

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 3
matrix:
os: [ubuntu-latest,macos-latest,windows-latest]
python-version: ['3.7', '3.8', '3.9', '3.10' , '3.11', '3.12']

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: install ElliptiCBn
run: |
# platform specific packages
python -m pip install . -vv -r requirements.txt
- name: run flake8
run: |
pip install flake8
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: run pytest coverage
run: |
pip install pytest
pytest tests/
12 changes: 8 additions & 4 deletions ElliptiCBn/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def get_macrocycles(filename,

print(f'Analyzing {filename}.',flush=True)

if min_num_carbons >= max_num_carbons:
err = f"min_num_carbons ({min_num_carbons}) must be smaller than max_num_carbons ({max_num_carbons})\n"
raise ValueError(err)

allowed_atoms = ["C","N","O","H"]
chno_lines = []
non_chno_dict = {"atom_type":[],
Expand Down Expand Up @@ -604,10 +608,10 @@ def plot_results(atom_df,
yaxis_visible=False,
zaxis_visible=False)

# Save html if requested
if html_file is not None:
print(f"Saving plot to {html_file}",flush=True)
fig.write_html(html_file)
# Save html if requested
if html_file is not None:
print(f"Saving plot to {html_file}",flush=True)
fig.write_html(html_file)

return fig

Expand Down
29 changes: 29 additions & 0 deletions run_all_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

echo "Running flake8"
flake_test=`flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics`
if [[ "${flake_test}" != 0 ]]; then
echo "flake failed"
exit
fi

rm -rf reports
mkdir reports
mkdir reports/junit
mkdir reports/coverage
mkdir reports/badges

echo "Running flake8, aggressive"
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics > reports/flake.txt

echo "Running coverage.py"
coverage erase
coverage run --branch -m pytest --junit-xml=reports/junit/junit.xml

echo "Generating reports"
coverage html
mv htmlcov reports

coverage xml
mv coverage.xml reports/coverage/coverage.xml

6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#Package meta-data
DESCRIPTION= \
""" Python package for analyzing ellipticity in CBn crystal structures. """
URL = "https://github.com/harmslab/ElliptiCBn" # temp URL
EMAIL = "mshavlik@uoregon.edu"
AUTHOR = "Michael Shavlik"
URL = "https://github.com/harmslab/ElliptiCBn"
EMAIL = "harms@uoregon.edu; mshavlik@uoregon.edu"
AUTHOR = "Michael J. Harms; Michael Shavlik"
REQUIRES_PYTHON = ">=3.7.0"

about = {}
Expand Down
83 changes: 83 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest
import os
import glob

def get_files(base_dir):
"""
Traverse base_dir and return a dictionary that keys all files and some
rudimentary *.ext expressions to absolute paths to those files. They keys
will be things like "some_dir/test0/rocket.txt" mapping to
"c:/some_dir/life/base_dir/some_dir/test0/rocket.txt". The idea is to have
easy-to-read cross-platform keys within unit tests.
Classes of keys:
+ some_dir/test0/rocket.txt maps to a file (str)
+ some_dir/test0/ maps to the test0 directory itself (str)
+ some_dir/test0/*.txt maps to all .txt (list)
+ some_dir/test0/* maps to all files or directories in the directory
(list)
Note that base_dir is *not* included in the keys. All are relative to that
directory by :code:`os.path.basename(__file__)/{base_dir}`.
Parameters
----------
base_dir : str
base directory for search. should be relative to test file location.
Returns
-------
output : dict
dictionary keying string paths to absolute paths
"""

containing_dir = os.path.dirname(os.path.realpath(__file__))
starting_dir = os.path.abspath(os.path.join(containing_dir,base_dir))

base_length = len(starting_dir.split(os.sep))

# Traverse starting_dir
output = {}
for root, dirs, files in os.walk(starting_dir):

# path relative to base_dir as a list
this_path = root.split(os.sep)[base_length:]

# Build paths to specific files
local_files = []
for file in files:
local_files.append(os.path.join(root,file))
new_key = this_path[:]
new_key.append(file)
output["/".join(new_key)] = local_files[-1]

# Build paths to patterns of file types
patterns = {}
ext = list(set([f.split(".")[-1] for f in local_files]))
for e in ext:
new_key = this_path[:]
new_key.append(f"*.{e}")
output["/".join(new_key)] = glob.glob(os.path.join(root,f"*.{e}"))

# Build path to all files in this directory
new_key = this_path[:]
new_key.append("*")
output["/".join(new_key)] = glob.glob(os.path.join(root,f"*"))

# Build paths to directories in this directory
for this_dir in dirs:
new_key = this_path[:]
new_key.append(this_dir)
# dir without terminating /
output["/".join(new_key)] = os.path.join(root,this_dir)

# dir with terminating /
new_key.append("")
output["/".join(new_key)] = os.path.join(root,this_dir)

return output

@pytest.fixture(scope="module")
def example_xyz():
return get_files(os.path.join("data","example-xyz"))
Loading

0 comments on commit 854eb6b

Please sign in to comment.