Skip to content

Commit

Permalink
update unit test for formatting and bounding box values
Browse files Browse the repository at this point in the history
  • Loading branch information
gantian127 committed Mar 15, 2024
1 parent 3268112 commit abed15e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 66 deletions.
35 changes: 17 additions & 18 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
from click.testing import CliRunner


def test_command_line_interface():
runner = CliRunner()
result = runner.invoke(main, ["--help"])
@pytest.fixture
def cli_runner():
return CliRunner()


def test_command_line_interface(cli_runner):
result = cli_runner.invoke(main, ["--help"])
assert result.exit_code == 0
assert "Usage:" in result.output

result = runner.invoke(main, ["--version"])
result = cli_runner.invoke(main, ["--version"])
assert result.exit_code == 0
assert "version" in result.output


def test_output(tmpdir):
runner = CliRunner()

result = runner.invoke(
def test_output(cli_runner, tmpdir):
result = cli_runner.invoke(
main,
[
"--var_name=carbonate",
Expand All @@ -32,9 +34,8 @@ def test_output(tmpdir):
assert result.exit_code != 0


def test_var_name():
runner = CliRunner()
result = runner.invoke(
def test_var_name(cli_runner):
result = cli_runner.invoke(
main,
[
"--var_name=error",
Expand All @@ -45,9 +46,8 @@ def test_var_name():
assert result.exit_code != 0


def test_bbox():
runner = CliRunner()
result = runner.invoke(
def test_bbox(cli_runner):
result = cli_runner.invoke(
main,
[
"--var_name=carbonate",
Expand All @@ -59,14 +59,13 @@ def test_bbox():


@pytest.mark.filterwarnings("ignore:numpy.ufunc size")
def test_data_download(tmpdir):
runner = CliRunner()
def test_data_download(cli_runner, tmpdir):
with tmpdir.as_cwd():
result = runner.invoke(
result = cli_runner.invoke(
main,
[
"--var_name=carbonate",
"--bbox=-66.8,18,-66.2,18.4",
"--bbox=-98,18,-80,31",
"test.tif",
],
)
Expand Down
115 changes: 67 additions & 48 deletions tests/dbseabed_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,42 @@
from bmi_dbseabed import DbSeabed


# test user input for get_data()
def test_var_name():
with pytest.raises(ValueError):
DbSeabed().get_data(
"error",
west=-66.8,
south=18.0,
east=-66.2,
north=18.4,
output="test.tif",
)
@pytest.fixture
def dbseabed_instance():
return DbSeabed()


def test_bounding_box():
with pytest.raises(ValueError): # west > east
DbSeabed().get_data(
"carbonate",
west=-66.2,
def test_invalid_var_name(dbseabed_instance):
with pytest.raises(ValueError, match="Please provide a valid var_name value."):
dbseabed_instance.get_data(
var_name="error",
west=-98,
south=18.0,
east=-66.8,
north=18.4,
east=--80,
north=31,
output="test.tif",
)

with pytest.raises(ValueError): # south > north
DbSeabed().get_data(

@pytest.mark.parametrize(
"west, south, east, north",
[(-80, 18.0, -98, 31), (-98, 31, -80, 18.0)], # west > east # south > north
)
def test_invalid_bounding_box(west, south, east, north, dbseabed_instance):
with pytest.raises(ValueError):
dbseabed_instance.get_data(
"carbonate",
west=-66.8,
south=18.4,
east=-66.2,
north=18.0,
west=west,
south=south,
east=east,
north=north,
output="test.tif",
)


def test_output():
def test_invalid_output(dbseabed_instance):
with pytest.raises(ValueError):
DbSeabed().get_data(
dbseabed_instance.get_data(
"carbonate",
west=-66.8,
south=18.0,
Expand All @@ -54,42 +52,63 @@ def test_output():
)


# test data download for get_data()
@pytest.mark.filterwarnings("ignore:numpy.ufunc size")
def test_data_download(tmpdir):
data = DbSeabed().get_data(
# test data download for all variables
@pytest.mark.parametrize(
"var_name",
[
"carbonate",
west=-66.8,
south=18.0,
east=-66.2,
north=18.4,
output=os.path.join(tmpdir, "test.tif"),
)
"carbonate_totlsu",
"grainsize",
"grainsize_totlsu",
"gravel",
"gravel_totlsu",
"mud",
"mud_totlsu",
"organic_carbon",
"organic_carbon_totlsu",
"rock",
"rock_totlsu",
"sand",
"sand_totlsu",
],
)
@pytest.mark.filterwarnings("ignore:numpy.ufunc size")
def test_data_download(var_name, tmpdir):
var_name_list = []
for var_name in var_name_list:
data = DbSeabed().get_data(
var_name=var_name,
west=-98,
south=18.0,
east=-80,
north=31,
output=os.path.join(tmpdir, "test.tif"),
)

assert isinstance(data, xarray.core.dataarray.DataArray)
assert len(os.listdir(tmpdir)) == 1
assert isinstance(data, xarray.core.dataarray.DataArray)
assert len(os.listdir(tmpdir)) == 1


# test loading local file for get_coverage_data()
# test loading local file
@pytest.mark.filterwarnings("ignore:numpy.ufunc size")
def test_load_localfile(tmpdir):
DbSeabed().get_data(
def test_load_local_file(tmpdir, dbseabed_instance):
dbseabed_instance.get_data(
"carbonate",
west=-66.8,
west=-98,
south=18.0,
east=-66.2,
north=18.4,
east=-80,
north=31,
output=os.path.join(tmpdir, "test.tif"),
)
file1_info = os.path.getmtime(os.path.join(tmpdir, "test.tif"))
assert len(os.listdir(tmpdir)) == 1

DbSeabed().get_data(
dbseabed_instance.get_data(
"carbonate",
west=-66.8,
west=-98,
south=18.0,
east=-66.2,
north=18.4,
east=-80,
north=31,
output=os.path.join(tmpdir, "test.tif"),
local_file=True,
)
Expand Down

0 comments on commit abed15e

Please sign in to comment.