Skip to content

Commit

Permalink
Fix test function for empty ICGEM gdf file (#345)
Browse files Browse the repository at this point in the history
The test for checking if proper errors and warnings were raised when reading an empty ICGEM gdf file was failing in my local system due to a change in the Numpy warning for the empty file. Took the opportunity to refactor the test using pytest.raises and pytest.warns to capture the errors and warnings.
  • Loading branch information
santisoler authored Aug 26, 2022
1 parent dc5cf30 commit 5204a17
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions harmonica/tests/test_icgem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

import numpy as np
import numpy.testing as npt
from pytest import raises
import pytest
from pytest import raises, warns

from .. import load_icgem_gdf

Expand Down Expand Up @@ -280,14 +281,20 @@ def test_corrupt_area(tmpdir):
load_icgem_gdf(corrupt)


def test_empty_file(tmpdir, recwarn):
"Empty ICGEM file"
@pytest.fixture(name="empty_fname")
def fixture_empty_fname(tmpdir):
"""
Return the path to a temporary empty file
"""
empty_fname = str(tmpdir.join("empty.gdf"))
with open(empty_fname, "w") as gdf_file:
gdf_file.write("")
with raises(IOError):
return empty_fname


def test_empty_file(empty_fname):
"Empty ICGEM file"
error = raises(IOError, match=r"Couldn't read \w+ field from gdf file header")
warn = warns(UserWarning, match=r"loadtxt: input contained no data")
with error, warn:
load_icgem_gdf(empty_fname)
assert len(recwarn) == 1
warning = recwarn.pop()
assert warning.category == UserWarning
assert "Empty input file" in str(warning.message)

0 comments on commit 5204a17

Please sign in to comment.