Skip to content

Commit

Permalink
Add tests, and actions to run them (#22)
Browse files Browse the repository at this point in the history
* Added tests, actions to run the tests

* Pip install instead of python setup.py install
  • Loading branch information
stscirij authored Oct 11, 2023
1 parent c0c67f4 commit 2d9724c
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/python_testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: HASP Pytest

on:
push:
pull_request:
schedule:
- cron: '00 00 * * *' # every day at midnight

jobs:
build:
name: ${{ matrix.name }}
runs-on: ${{ matrix.runs-on }}
env:
lref: /grp/hst/cdbs/lref/
oref: /grp/hst/cdbs/oref/
working-directory: ./
strategy:
fail-fast: false
matrix:
include:
- name: Python 3.11
runs-on: ubuntu-latest
python-version: '3.11'
toxenv: py311

- name: Python 3.10
runs-on: ubuntu-latest
python-version: '3.10'
toxenv: py310

- name: Python 3.9
runs-on: ubuntu-latest
python-version: '3.9'
toxenv: py39

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install cython
pip install astropy
pip install scipy
pip install matplotlib
pip install stsci.tools==4.0.0
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# - name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
# run all the unit tests.
pip install .['test']
pytest tests/test_swrapper.py -v
working-directory: ${{env.working-directory}}
93 changes: 93 additions & 0 deletions tests/test_swrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Test the wrapper script
import os
import glob
import shutil
import urllib.request
import tarfile

from astropy.io.fits import FITSDiff

from hasp import wrapper

Program_10033 = {'name': '10033',
'url': 'https://stsci.box.com/shared/static/wnmma331eixblioo5jfnmjnzz2fjt01r.gz'}
Program_11839 = {'name': '11839',
'url': 'https://stsci.box.com/shared/static/a05mixac3hreg6g07f1kkogcpphdo26a.gz'}
HD104237E = {'name': 'hd-104237e',
'url': 'https://stsci.box.com/shared/static/irelgbjs7zhdiljksao4su2c6tfhyntx.gz'}
V_HK_ORI = {'name': 'v-hk-ori',
'url': 'https://stsci.box.com/shared/static/az3ytnwohj0t4wnc4m8oqbw9ziozwqx1.gz'}


class TestWrapper():

def test_10033(self):
program = Program_10033
self.setup_tree(program)
self.run_wrapper(program['name'])
report = self.compare_outputs(program['name'])
self.cleanup(program['name'])
if report is not None:
raise AssertionError(report)
return

def test_11839(self):
program = Program_11839
self.setup_tree(program)
self.run_wrapper(program['name'])
report = self.compare_outputs(program['name'])
self.cleanup(program['name'])
if report is not None:
raise AssertionError(report)
return

def setup_tree(self, program):
program_name = program['name']
url = program['url']
if os.path.isdir(program_name):
shutil.rmtree(program_name)
filename = program_name + '.tar.gz'
_ = urllib.request.urlretrieve(url, filename)
data_tarfile = tarfile.open(filename, mode='r|gz')
data_tarfile.extractall()
return

def run_wrapper(self, program):
indir = program + '/input/'
wrapper.main(indir, outdir=indir)
return

def compare_outputs(self, program):
report = None
# Outputs from current run are in ./, truth files to compare
# with are in ./truth
all_ok = True
fitsdiff_report = ''
keywords_to_ignore = ['DATE', 'FITS_SW', 'FILENAME',
'HLSP_VER', 'S_REGION']
new_hlsps = glob.glob(program + '/input/hst_')
for new_product in new_hlsps:
truth_filename = self.get_truth_filename(program, new_product)
fdiff = FITSDiff(new_product, truth_filename,
ignore_hdus=['provenance'],
ignore_keywords=keywords_to_ignore,
rtol=1.0e-7)
fitsdiff_report += fdiff.report()
if not fdiff.identical and all_ok:
all_ok = False
if not all_ok:
report = os.linesep + fitsdiff_report
return report
print(fitsdiff_report)
return None

def get_truth_filename(self, program, product):
# Get the truth filename. The data release might be different
filename = os.path.basename(product)
truth_filename = program + '/truth/' + filename
return truth_filename

def cleanup(self, program):
shutil.rmtree(program)
os.remove(program + '.tar.gz')
return

0 comments on commit 2d9724c

Please sign in to comment.