From 68f7927a8b0067506d69d7b303abdcf2c27816b4 Mon Sep 17 00:00:00 2001 From: Amber Malpas Date: Tue, 24 Jun 2025 10:16:31 -0400 Subject: [PATCH] Add basic pytest suite --- .github/workflows/pytest.yml | 22 ++++++++++++++ pyproject.toml | 3 ++ tests/test_lightcurves.py | 59 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 .github/workflows/pytest.yml create mode 100644 tests/test_lightcurves.py diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..87ec26a --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,22 @@ +name: Test + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.10'] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e . + pip install pytest + - name: Run tests + run: pytest diff --git a/pyproject.toml b/pyproject.toml index 09977b5..3ab974a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,6 @@ [build-system] requires = ["setuptools>=61"] build-backend = "setuptools.build_meta" +[tool.pytest.ini_options] +testpaths = ["tests"] +addopts = "-ra" diff --git a/tests/test_lightcurves.py b/tests/test_lightcurves.py new file mode 100644 index 0000000..c374b49 --- /dev/null +++ b/tests/test_lightcurves.py @@ -0,0 +1,59 @@ +import types +import sys, pathlib; sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) +sys.modules.setdefault("TripleLensing", types.ModuleType("TripleLensing")) +module = types.ModuleType("TestML"); +for name in ["get_crit_caus", "getphis_v3", "get_allimgs_with_mu", "testing"]: + setattr(module, name, lambda *args, **kwargs: None) +sys.modules["TestML"] = module +import numpy as np +import math +import VBMicrolensing +from GCMicrolensing import OneL1S, TwoLens1S + + +def test_onel1s_light_curve(): + t0 = 0.0 + tE = 1.0 + rho = 0.01 + u0 = 0.1 + model = OneL1S(t0, tE, rho, [u0]) + + vb = VBMicrolensing.VBMicrolensing() + vb.RelTol = 1e-3 + vb.Tol = 1e-3 + vb.astrometry = True + + expected = np.array([ + vb.ESPLMag2(math.sqrt(u0 ** 2 + tau ** 2), rho) + for tau in model.tau + ]) + actual = np.array([ + model.VBM.ESPLMag2(math.sqrt(u0 ** 2 + tau ** 2), rho) + for tau in model.tau + ]) + + np.testing.assert_allclose(actual, expected, rtol=1e-6) + + +def test_twolens1s_light_curve(): + t0 = 0.0 + tE = 1.0 + rho = 0.01 + u0 = 0.1 + q = 1.0 + s = 1.5 + alpha = 0.0 + + model = TwoLens1S(t0, tE, rho, [u0], q, s, alpha) + system = model.systems[0] + + vb = VBMicrolensing.VBMicrolensing() + vb.RelTol = 1e-3 + vb.Tol = 1e-3 + vb.astrometry = True + + params = [math.log(s), math.log(q), u0, math.radians(alpha), math.log(rho), math.log(tE), t0] + expected, *_ = vb.BinaryLightCurve(params, model.t) + + np.testing.assert_allclose(system['mag'], expected, rtol=1e-6) +