Skip to content

Commit

Permalink
Merge pull request #333 from lbl-anp/patch-flaky-tests
Browse files Browse the repository at this point in the history
Skip NNDC/XCOM tests if databases are down
  • Loading branch information
jvavrek committed May 16, 2022
2 parents 1758ebb + 7b4154a commit 574a460
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ bump2version
coverage
flake8>=4.0.1
pre-commit
pytest<7.0.0
pytest
pytest-black
pytest-cov
pytest-rerunfailures
pytest-xdist
requests
1 change: 1 addition & 0 deletions tests/df_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_bad_path(self):
"""Test ExampleCache.check_path() exception for a bad path."""
d = ExampleCache()
d.path = "/bad/path"
d.filename = "/bad/path/filename.csv"
with pytest.raises(CacheError):
d.check_path()
with pytest.raises(CacheError):
Expand Down
8 changes: 8 additions & 0 deletions tests/materials_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import becquerel.tools.materials as materials
import becquerel.tools.materials_compendium as materials_compendium
import pytest
from utils import xcom_is_up


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
class TestConvertComposition:
"""Test convert_composition."""

Expand Down Expand Up @@ -51,13 +53,15 @@ def test_z_out_of_range(self):


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
def test_materials():
"""Test fetch_materials."""
fetch_materials()
assert os.path.exists(materials.FILENAME)


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
def test_materials_force():
"""Test fetch_materials with force=True."""
assert os.path.exists(materials.FILENAME)
Expand Down Expand Up @@ -87,6 +91,7 @@ def test_materials_dummy_csv():


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
def test_materials_dummy_compendium_pre2022():
"""Test fetch_materials with a dummy Compendium JSON file.
Expand Down Expand Up @@ -132,6 +137,7 @@ def test_materials_dummy_compendium_pre2022():


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
def test_materials_dummy_compendium_2022():
"""Test fetch_materials with a dummy Compendium JSON file.
Expand Down Expand Up @@ -186,6 +192,7 @@ def test_materials_dummy_compendium_2022():


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
def test_materials_dummy_compendium_error():
"""Test fetch_materials with a dummy Compendium JSON file.
Expand All @@ -205,6 +212,7 @@ def test_materials_dummy_compendium_error():


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
def test_materials_no_compendium():
"""Test fetch_materials with no Compendium JSON file."""
# point to a dummy JSON file that does not exist
Expand Down
10 changes: 7 additions & 3 deletions tests/nndc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pandas as pd
from becquerel.tools import nndc
from utils import nndc_is_up
import pytest


Expand Down Expand Up @@ -137,7 +138,8 @@ def test_20(self):
nndc._parse_float_uncertainty("7", "@")


@pytest.mark.flaky(reruns=3)
@pytest.mark.skipif(not nndc_is_up(), reason="NNDC is down.")
@pytest.mark.flaky(reruns=10)
class NNDCQueryTests:
"""Tests common to NNDCQuery-derived classes."""

Expand Down Expand Up @@ -559,7 +561,8 @@ def test_query_zrange_7_20_elevelrange_01_inf(self):


@pytest.mark.webtest
@pytest.mark.flaky(reruns=3)
@pytest.mark.skipif(not nndc_is_up(), reason="NNDC is down.")
@pytest.mark.flaky(reruns=10)
class TestNuclearWalletCard(NNDCQueryTests):
"""Test NNDC nuclear_wallet_card query."""

Expand Down Expand Up @@ -703,7 +706,8 @@ def test_wallet_exception_too_many(self):


@pytest.mark.webtest
@pytest.mark.flaky(reruns=3)
@pytest.mark.skipif(not nndc_is_up(), reason="NNDC is down.")
@pytest.mark.flaky(reruns=10)
class TestDecayRadiationQuery(NNDCQueryTests):
"""Test NNDC decay_radiation."""

Expand Down
25 changes: 25 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
"""Helpers for testing."""
from pathlib import Path
import requests

TESTS_PATH = Path(__file__).parent.absolute()
REPO_PATH = TESTS_PATH.parent


def database_is_up(url):
"""Check whether an online database can be reached.
Parameters
----------
url : str
URL to query.
"""
try:
return requests.post(url).status_code == requests.codes.ok
except requests.exceptions.ConnectionError:
return False


def nndc_is_up():
"""Check whether the NNDC databases can be reached."""
return database_is_up("https://www.nndc.bnl.gov/nudat3/indx_sigma.jsp")


def xcom_is_up():
"""Check whether the NIST XCOM databases can be reached."""
return database_is_up("https://physics.nist.gov/PhysRefData/Xcom/html/xcom1.html")
2 changes: 2 additions & 0 deletions tests/wallet_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uncertainties
import becquerel.tools.wallet_cache as wallet_cache
import pytest
from utils import nndc_is_up


@pytest.mark.parametrize(
Expand Down Expand Up @@ -49,6 +50,7 @@ def test_format_ufloat(arg, result):

@pytest.mark.webtest
@pytest.mark.flaky(reruns=3)
@pytest.mark.skipif(not nndc_is_up(), reason="NNDC is down.")
class TestWalletCardCache:
"""Test functionality of wallet_cache."""

Expand Down
2 changes: 2 additions & 0 deletions tests/xcom_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
from becquerel.tools import xcom
from utils import xcom_is_up
import pytest

XCOM_URL_ORIG = xcom._URL
Expand Down Expand Up @@ -122,6 +123,7 @@


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
class TestFetchXCOMData:
"""Test fetch_xcom_data function."""

Expand Down

0 comments on commit 574a460

Please sign in to comment.