Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing nc_tools tests #113

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SalishSeaTools/salishsea_tools/namelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def namelist2dict(file_or_file_object):

def _namelist2dict(file_object):
"""
Converts a file_object containng a namelist to a dictionary.
Converts a file_object containing a namelist to a dictionary.
"""
namelist_dict = {}
for group_name, group_values in group_generator(tokenizer(file_object)):
Expand Down
109 changes: 77 additions & 32 deletions SalishSeaTools/tests/test_nc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,40 @@ def test_show_dimensions(capsys, nc_dataset):
"""show_dimensions prints dimension string representation
"""
nc_dataset.createDimension('foo', 42)

nc_tools.show_dimensions(nc_dataset)

expected = ['"<class \'netCDF4.Dimension\'>": name = \'foo\', size = 42']
out, err = capsys.readouterr()
assert out.splitlines()[0] == (
"<class 'netCDF4._netCDF4.Dimension'>: name = 'foo', size = 42")
assert out.splitlines() == expected


def test_show_dimensions_order(capsys, nc_dataset):
"""show_dimensions prints dimension in order they were defined
"""
nc_dataset.createDimension('foo', 42)
nc_dataset.createDimension('bar', 24)

nc_tools.show_dimensions(nc_dataset)

expected = [
'"<class \'netCDF4.Dimension\'>": name = \'foo\', size = 42',
'"<class \'netCDF4.Dimension\'>": name = \'bar\', size = 24',
]
out, err = capsys.readouterr()
assert out.splitlines()[2] == (
"<class 'netCDF4._netCDF4.Dimension'>: name = 'bar', size = 24")
assert out.splitlines() == expected


def test_show_variables(capsys, nc_dataset):
"""show_variables prints list of variable names
"""
nc_dataset.createDimension('x', 42)
nc_dataset.createVariable('foo', float, ('x',))

nc_tools.show_variables(nc_dataset)

out, err = capsys.readouterr()
assert out.splitlines()[0] == "odict_keys(['foo'])"
assert out.splitlines() == ["dict_keys(['foo'])"]


def test_show_variables_order(capsys, nc_dataset):
Expand All @@ -109,9 +118,11 @@ def test_show_variables_order(capsys, nc_dataset):
nc_dataset.createDimension('x', 42)
nc_dataset.createVariable('foo', float, ('x',))
nc_dataset.createVariable('bar', float, ('x',))

nc_tools.show_variables(nc_dataset)

out, err = capsys.readouterr()
assert out.splitlines()[0] == "odict_keys(['foo', 'bar'])"
assert out.splitlines() == ["dict_keys(['foo', 'bar'])"]


def test_show_variable_attrs(capsys, nc_dataset):
Expand All @@ -120,16 +131,19 @@ def test_show_variable_attrs(capsys, nc_dataset):
nc_dataset.createDimension('x', 42)
foo = nc_dataset.createVariable('foo', float, ('x',))
foo.units = 'm'

nc_tools.show_variable_attrs(nc_dataset)

expected = [
"<class 'netCDF4.Variable'>",
"float64 foo(x)",
" units: m",
"unlimited dimensions: ",
"current shape = (42,)",
"filling on, default _FillValue of 9.969209968386869e+36 used",
]
out, err = capsys.readouterr()
assert out == (
"<class 'netCDF4._netCDF4.Variable'>\n"
"float64 foo(x)\n"
" units: m\n"
"unlimited dimensions: \n"
"current shape = (42,)\n"
"filling on, default _FillValue of 9.969209968386869e+36 used\n\n"
)
assert out.splitlines() == expected


def test_show_variable_attrs_order(capsys, nc_dataset):
Expand All @@ -138,39 +152,70 @@ def test_show_variable_attrs_order(capsys, nc_dataset):
nc_dataset.createDimension('x', 42)
nc_dataset.createVariable('foo', float, ('x',))
nc_dataset.createVariable('bar', float, ('x',))

nc_tools.show_variable_attrs(nc_dataset)

expected = [
"<class 'netCDF4.Variable'>",
"float64 foo(x)",
"unlimited dimensions: ",
"current shape = (42,)",
"filling on, default _FillValue of 9.969209968386869e+36 used",
"<class 'netCDF4.Variable'>",
"float64 bar(x)",
"unlimited dimensions: ",
"current shape = (42,)",
"filling on, default _FillValue of 9.969209968386869e+36 used",
]
out, err = capsys.readouterr()
assert out.splitlines()[7] == 'float64 bar(x)'
assert out.splitlines() == expected


def test_show_variable_attrs_spec_var(capsys, nc_dataset):
def test_show_variable_attrs_specified_var(capsys, nc_dataset):
"""show_variable_attrs prints string repr of specified variable
"""
nc_dataset.createDimension('x', 42)
foo = nc_dataset.createVariable('foo', float, ('x',))
foo.units = 'm'
nc_dataset.createVariable('bar', float, ('x',))

nc_tools.show_variable_attrs(nc_dataset, 'foo')

expected = [
"<class 'netCDF4.Variable'>",
"float64 foo(x)",
" units: m",
"unlimited dimensions: ",
"current shape = (42,)",
"filling on, default _FillValue of 9.969209968386869e+36 used",
]
out, err = capsys.readouterr()
assert out == (
"<class 'netCDF4._netCDF4.Variable'>\n"
"float64 foo(x)\n"
" units: m\n"
"unlimited dimensions: \n"
"current shape = (42,)\n"
"filling on, default _FillValue of 9.969209968386869e+36 used\n\n"
)
assert out.splitlines() == expected


def test_show_variable_attrs_spec_var_order(capsys, nc_dataset):
def test_show_variable_attrs_specified_var_order(capsys, nc_dataset):
"""show_variable_attrs prints specified vars in order they were defined
"""
nc_dataset.createDimension('x', 42)
nc_dataset.createVariable('foo', float, ('x',))
nc_dataset.createVariable('bar', float, ('x',))

nc_tools.show_variable_attrs(nc_dataset, 'foo', 'bar')

expected = [
"<class 'netCDF4.Variable'>",
"float64 foo(x)",
"unlimited dimensions: ",
"current shape = (42,)",
"filling on, default _FillValue of 9.969209968386869e+36 used",
"<class 'netCDF4.Variable'>",
"float64 bar(x)",
"unlimited dimensions: ",
"current shape = (42,)",
"filling on, default _FillValue of 9.969209968386869e+36 used",
]
out, err = capsys.readouterr()
assert out.splitlines()[7] == 'float64 bar(x)'
assert out.splitlines() == expected


def test_time_origin_value(nc_dataset):
Expand Down Expand Up @@ -372,12 +417,12 @@ def test_init_dataset_attrs_no_oversrite_quiet(

@patch(
'salishsea_tools.nc_tools.hg.default_url',
return_value='ssh://hg@github.com/SalishSeaCast/foo')
return_value='ssh://hg@bitbucket.org/SalishSeaCast/foo')
def test_notebook_hg_url(mock_dflt_url):
"""_notebook_hg_url returns expected URL
"""
url = nc_tools._notebook_hg_url('bar.ipynb')
assert url == 'https://github.com/SalishSeaCast/foo/src/tip/bar.ipynb'
assert url == 'https://bitbucket.org/SalishSeaCast/foo/src/tip/bar.ipynb'


def test_notebook_hg_url_no_notebook_name():
Expand All @@ -397,22 +442,22 @@ def test_notebook_hg_url_REQUIRED(mock_dflt_url):

@patch(
'salishsea_tools.nc_tools.hg.default_url',
return_value='ssh://hg@github.com/SalishSeaCast/foo')
return_value='ssh://hg@bitbucket.org/SalishSeaCast/foo')
def test_notebook_hg_url_adds_ipynb(mock_dflt_url):
"""_notebook_hg_url adds .ipynb extension if notebook name lacks it
"""
url = nc_tools._notebook_hg_url('bar')
assert url == 'https://github.com/SalishSeaCast/foo/src/tip/bar.ipynb'
assert url == 'https://bitbucket.org/SalishSeaCast/foo/src/tip/bar.ipynb'


@patch(
'salishsea_tools.nc_tools.hg.default_url',
return_value='ssh://hg@github.com/SalishSeaCast/foo')
return_value='ssh://hg@bitbucket.org/SalishSeaCast/foo')
def test_nc_file_hg_url(mock_dflt_url):
"""_nc_file_hg_url returns expected URL
"""
url = nc_tools._nc_file_hg_url('../bar/baz.nc')
assert url == 'https://github.com/SalishSeaCast/foo/src/tip/baz.nc'
assert url == 'https://bitbucket.org/SalishSeaCast/foo/src/tip/baz.nc'


def test_nc_file_hg_url_no_nc_filepath():
Expand Down
Loading