Skip to content

Commit

Permalink
Merge branch 'main' into n42-parser3
Browse files Browse the repository at this point in the history
  • Loading branch information
jvavrek authored Sep 20, 2023
2 parents 9d100c3 + 20330a0 commit b226f1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions becquerel/tools/materials_nist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import requests
import pandas as pd
from io import StringIO
from collections.abc import Iterable
from .element import element_symbol
from .materials_error import MaterialsError
Expand Down Expand Up @@ -77,7 +78,7 @@ def fetch_element_data():
# remove open <TR> at the end of the table
text = text.replace("</TD></TR><TR>", "</TD></TR>")
# read HTML table into pandas DataFrame
tables = pd.read_html(text, header=0, skiprows=[1, 2])
tables = pd.read_html(StringIO(text), header=0, skiprows=[1, 2])
if len(tables) != 1:
raise MaterialsError(f"1 HTML table expected, but found {len(tables)}")
df = tables[0]
Expand Down Expand Up @@ -162,7 +163,7 @@ def fetch_compound_data():
# replace <BR> symbols in composition lists with semicolons
text = text.replace("<BR>", ";")
# read HTML table into pandas DataFrame
tables = pd.read_html(text, header=0, skiprows=[1, 2])
tables = pd.read_html(StringIO(text), header=0, skiprows=[1, 2])
if len(tables) != 1:
raise MaterialsError(f"1 HTML table expected, but found {len(tables)}")
df = tables[0]
Expand Down
29 changes: 24 additions & 5 deletions tests/materials_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from utils import xcom_is_up


def _get_warning_messages(record):
return [str(rec.message) for rec in record]


@pytest.mark.webtest
@pytest.mark.skipif(not xcom_is_up(), reason="XCOM is down.")
class TestConvertComposition:
Expand Down Expand Up @@ -68,9 +72,15 @@ def test_materials_force():
with pytest.warns(MaterialsWarning) as record:
fetch_materials(force=True)
if not os.path.exists(materials_compendium.FNAME):
assert len(record) == 2, "Expected two MaterialsWarnings to be raised"
assert len(record) == 2, (
"Expected two MaterialsWarnings to be raised; "
f"got {_get_warning_messages(record)}"
)
else:
assert len(record) == 1, "Expected one MaterialsWarning to be raised"
assert len(record) == 1, (
"Expected one MaterialsWarning to be raised; "
f"got {_get_warning_messages(record)}"
)
assert os.path.exists(materials.FILENAME)


Expand Down Expand Up @@ -130,7 +140,10 @@ def test_materials_dummy_compendium_pre2022():
json.dump(data, f, indent=4)
with pytest.warns(None) as record:
materials._load_and_compile_materials()
assert len(record) == 0, "Expected no MaterialsWarnings to be raised"
assert len(record) == 0, (
"Expected no MaterialsWarnings to be raised; "
f"got {_get_warning_messages(record)}"
)
# remove the dummy file and point back to original
os.remove(materials_compendium.FNAME)
materials_compendium.FNAME = fname_orig
Expand Down Expand Up @@ -179,7 +192,10 @@ def test_materials_dummy_compendium_2022():
json.dump(data, f, indent=4)
with pytest.warns(None) as record:
materials._load_and_compile_materials()
assert len(record) == 0, "Expected no MaterialsWarnings to be raised"
assert len(record) == 0, (
"Expected no MaterialsWarnings to be raised; "
f"got {_get_warning_messages(record)}"
)
# remove siteVersion and make sure there is an error raised
del data["siteVersion"]
with open(materials_compendium.FNAME, "w") as f:
Expand Down Expand Up @@ -222,7 +238,10 @@ def test_materials_no_compendium():
os.remove(materials_compendium.FNAME)
with pytest.warns(MaterialsWarning) as record:
materials_compendium.fetch_compendium_data()
assert len(record) == 1, "Expected MaterialsWarning to be raised"
assert len(record) == 1, (
"Expected one MaterialsWarning to be raised; "
f"got {_get_warning_messages(record)}"
)
# point back to original file
materials_compendium.FNAME = fname_orig

Expand Down

0 comments on commit b226f1b

Please sign in to comment.