Skip to content

Commit

Permalink
Merge pull request #10 from jdebacker/testing
Browse files Browse the repository at this point in the history
Add additional unit tests and GH Action to run tests
  • Loading branch information
jdebacker authored Jan 20, 2023
2 parents f37eb00 + 6fc0557 commit eed2d57
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
44 changes: 44 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build and test [Python 3.7, 3.8, 3.9]

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]

steps:
- name: Checkout
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Setup Miniconda using Python ${{ matrix.python-version }}
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: ogmys-dev
environment-file: environment.yml
python-version: ${{ matrix.python-version }}
auto-activate-base: false

- name: Build
shell: bash -l {0}
run: |
pip install -e .
pip install pytest-cov
pip install pytest-pycodestyle
- name: Test
shell: bash -l {0}
working-directory: ./
run: |
pytest -m 'not local and not regression' --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
with:
files: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
verbose: true
14 changes: 14 additions & 0 deletions .github/workflows/check_black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Check Black formatting

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: psf/black@stable
with:
options: "-l 79 --check"
src: "."
9 changes: 8 additions & 1 deletion ogmys/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ def __init__(

# demographics
self.demographic_params = demographics.get_pop_objs(
p.E, p.S, p.T, 1, 100, p.start_year - 1, p.start_year, GraphDiag=True
p.E,
p.S,
p.T,
1,
100,
p.start_year - 1,
p.start_year,
GraphDiag=True,
)
# demographics for 80 period lives (needed for getting e below)
demog80 = demographics.get_pop_objs(
Expand Down
47 changes: 47 additions & 0 deletions ogmys/tests/test_calibrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Tests of calibrate.py module
"""

import pytest
import numpy as np
import os
import ogcore
from ogmys.calibrate import Calibration

CUR_PATH = os.path.abspath(os.path.dirname(__file__))


def test_calibrate():
p = ogcore.Specifications()
_ = Calibration(p)


def test_read_tax_func_estimate_error():
with pytest.raises(RuntimeError):
p = ogcore.Specifications()
tax_func_path = os.path.join(
CUR_PATH, "test_io_data", "TxFuncEst_policy.pkl"
)
c = Calibration(p)
_, _ = c.read_tax_func_estimate(p, tax_func_path)


def test_read_tax_func_estimate():
p = ogcore.Specifications()
p.BW = 11
tax_func_path = os.path.join(
CUR_PATH, "test_io_data", "TxFuncEst_policy.pkl"
)
c = Calibration(p)
dict_params, _ = c.read_tax_func_estimate(p, tax_func_path)
print("Dict keys = ", dict_params.keys())

assert isinstance(dict_params["tfunc_etr_params_S"], np.ndarray)


def test_get_dict():
p = ogcore.Specifications()
c = Calibration(p)
c_dict = c.get_dict()

assert isinstance(c_dict, dict)
Binary file added ogmys/tests/test_io_data/TxFuncEst_policy.pkl
Binary file not shown.
43 changes: 43 additions & 0 deletions ogmys/tests/test_run_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This model tests whether using the `OG-MYS/examples/run_og_mys.py`
work by making sure that it does not break (is still running) after
5 minutes (300 seconds).
"""
import multiprocessing
import time
import os
import sys
import importlib.util
from pathlib import Path
import pytest


def call_run_og_mys():
cur_path = os.path.split(os.path.abspath(__file__))[0]
path = Path(cur_path)
roe_fldr = os.path.join(path.parent.parent, "examples")
roe_file_path = os.path.join(roe_fldr, "run_og_mys.py")
spec = importlib.util.spec_from_file_location(
"run_og_mys.py", roe_file_path
)
roe_module = importlib.util.module_from_spec(spec)
sys.modules["run_og_mys.py"] = roe_module
spec.loader.exec_module(roe_module)
roe_module.main()


@pytest.mark.local
def test_run_og_mys(f=call_run_og_mys):
p = multiprocessing.Process(target=f, name="run_og_mys", args=())
p.start()
time.sleep(300)
if p.is_alive():
p.terminate()
p.join()
timetest = True
else:
print("run_og_mys.py did not run for minimum time")
timetest = False
print("timetest ==", timetest)

assert timetest

0 comments on commit eed2d57

Please sign in to comment.