|
| 1 | +import sys |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from netCDF4 import Dataset, utils |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize("command", [utils.ncinfo, utils.nc3tonc4, utils.nc4tonc3]) |
| 11 | +@pytest.mark.parametrize("args", [[], ["--unknown"]]) |
| 12 | +def test_usage_error(command, args, monkeypatch, capsys): |
| 13 | + monkeypatch.setattr(sys, "argv", [command.__name__, *args]) |
| 14 | + with pytest.raises(SystemExit) as exc: |
| 15 | + command() |
| 16 | + assert exc.value.code == 2 |
| 17 | + assert "usage:" in capsys.readouterr().err |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize("command, option", [ |
| 21 | + (utils.ncinfo, "--group"), |
| 22 | + (utils.nc3tonc4, "--chunk"), |
| 23 | + (utils.nc4tonc3, "--chunk"), |
| 24 | +]) |
| 25 | +def test_missing_option_value(command, option, monkeypatch, capsys): |
| 26 | + monkeypatch.setattr(sys, "argv", [command.__name__, option]) |
| 27 | + with pytest.raises(SystemExit) as exc: |
| 28 | + command() |
| 29 | + assert exc.value.code == 2 |
| 30 | + assert "usage:" in capsys.readouterr().err |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("command", [utils.nc3tonc4, utils.nc4tonc3]) |
| 34 | +@pytest.mark.parametrize("args", [["source.nc"], ["source.nc", "dest.nc", "extra.nc"]]) |
| 35 | +def test_converter_file_count(command, args, monkeypatch, capsys): |
| 36 | + monkeypatch.setattr(sys, "argv", [command.__name__, *args]) |
| 37 | + with pytest.raises(SystemExit) as exc: |
| 38 | + command() |
| 39 | + assert exc.value.code == 2 |
| 40 | + assert "usage:" in capsys.readouterr().err |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("command", [utils.ncinfo, utils.nc3tonc4, utils.nc4tonc3]) |
| 44 | +@pytest.mark.parametrize("option", ["-h", "--help"]) |
| 45 | +def test_help(command, option, monkeypatch, capsys): |
| 46 | + monkeypatch.setattr(sys, "argv", [command.__name__, option]) |
| 47 | + with pytest.raises(SystemExit) as exc: |
| 48 | + command() |
| 49 | + assert exc.value.code == 0 |
| 50 | + output = capsys.readouterr() |
| 51 | + assert "usage:" in output.err |
| 52 | + assert "Error" not in output.out |
| 53 | + |
| 54 | + |
| 55 | +def test_ncinfo_group_dimension(tmp_path, monkeypatch, capsys): |
| 56 | + filename = str(tmp_path / "groups.nc") |
| 57 | + with Dataset(filename, "w") as dataset: |
| 58 | + dataset.createGroup("g1").createDimension("x", 7) |
| 59 | + monkeypatch.setattr(sys, "argv", ["ncinfo", "-g", "g1", "-d", "x", filename]) |
| 60 | + utils.ncinfo() |
| 61 | + assert "name = 'x', size = 7" in capsys.readouterr().out |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("chunk", [None, 0, 5]) |
| 65 | +def test_nc4tonc3_chunk(tmp_path, chunk, monkeypatch): |
| 66 | + source = str(tmp_path / "source.nc") |
| 67 | + destination = str(tmp_path / "destination.nc") |
| 68 | + values = np.arange(25, dtype="i4") |
| 69 | + with Dataset(source, "w", format="NETCDF4_CLASSIC") as dataset: |
| 70 | + dataset.createDimension("record", None) |
| 71 | + dataset.createVariable("values", "i4", ("record",))[:] = values |
| 72 | + args = ["nc4tonc3", "--quiet=1"] |
| 73 | + if chunk is not None: |
| 74 | + args.append("--chunk=%s" % chunk) |
| 75 | + monkeypatch.setattr(sys, "argv", [*args, source, destination]) |
| 76 | + with patch.object(utils, "_nc4tonc3", wraps=utils._nc4tonc3) as convert: |
| 77 | + utils.nc4tonc3() |
| 78 | + assert convert.call_args.kwargs["nchunk"] == (10 if chunk is None else chunk) |
| 79 | + with Dataset(destination) as dataset: |
| 80 | + assert dataset.file_format == "NETCDF3_64BIT_OFFSET" |
| 81 | + np.testing.assert_array_equal(dataset.variables["values"][:], values) |
0 commit comments