|
| 1 | +import os |
| 2 | +import re |
| 3 | +import stat |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from grass.exceptions import CalledModuleError |
| 9 | +from grass.tools import Tools |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("compression_flag", ["", "c"]) |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "suffix", |
| 15 | + [".pack", ".rpack", ".r_pack", ".grass_raster", ".grr", ".arbitrary_suffix_1"], |
| 16 | +) |
| 17 | +def test_pack_different_suffixes( |
| 18 | + xy_raster_dataset_session_for_module, tmp_path, suffix, compression_flag |
| 19 | +): |
| 20 | + """Check that non-zero output is created with different suffixes |
| 21 | +
|
| 22 | + The compression should not change the allowed suffixes. |
| 23 | + """ |
| 24 | + tools = Tools(session=xy_raster_dataset_session_for_module) |
| 25 | + output = (tmp_path / "output_1").with_suffix(suffix) |
| 26 | + tools.r_pack(input="rows_raster", output=output, flags=compression_flag) |
| 27 | + assert output.exists() |
| 28 | + assert output.is_file() |
| 29 | + assert output.stat().st_size > 0 |
| 30 | + |
| 31 | + |
| 32 | +def test_input_does_not_exist(xy_empty_dataset_session, tmp_path): |
| 33 | + """Check we raise when input does not exists""" |
| 34 | + tools = Tools(session=xy_empty_dataset_session) |
| 35 | + output = tmp_path / "output_1.rpack" |
| 36 | + with pytest.raises(CalledModuleError, match="does_not_exist"): |
| 37 | + tools.r_pack(input="does_not_exist", output=output) |
| 38 | + |
| 39 | + |
| 40 | +def test_output_exists_without_overwrite( |
| 41 | + xy_raster_dataset_session_for_module, tmp_path |
| 42 | +): |
| 43 | + """Check behavior when output already exists""" |
| 44 | + tools = Tools(session=xy_raster_dataset_session_for_module) |
| 45 | + output = tmp_path / "output_1.rpack" |
| 46 | + assert not output.exists() |
| 47 | + tools.r_pack(input="rows_raster", output=output) |
| 48 | + assert output.exists() |
| 49 | + with pytest.raises( |
| 50 | + CalledModuleError, match=rf"(?s){re.escape(str(output))}.*[Oo]verwrite" |
| 51 | + ): |
| 52 | + # Same call repeated again. |
| 53 | + tools.r_pack(input="rows_raster", output=output) |
| 54 | + assert output.exists() |
| 55 | + |
| 56 | + |
| 57 | +def test_output_exists_with_overwrite(xy_raster_dataset_session_for_module, tmp_path): |
| 58 | + """Check behavior when output already exists and overwrite is set""" |
| 59 | + tools = Tools(session=xy_raster_dataset_session_for_module) |
| 60 | + output = tmp_path / "output_1.rpack" |
| 61 | + assert not output.exists() |
| 62 | + tools.r_pack(input="rows_raster", output=output) |
| 63 | + assert output.exists() |
| 64 | + # Same call repeated again but with overwrite. |
| 65 | + tools.r_pack(input="rows_raster", output=output, overwrite=True) |
| 66 | + assert output.exists() |
| 67 | + |
| 68 | + |
| 69 | +def test_output_dir_does_not_exist(xy_raster_dataset_session_for_module, tmp_path): |
| 70 | + """Check behavior when directory for output does not exist""" |
| 71 | + tools = Tools(session=xy_raster_dataset_session_for_module) |
| 72 | + output = tmp_path / "does_not_exist" / "output_1.rpack" |
| 73 | + assert not output.exists() |
| 74 | + assert not output.parent.exists() |
| 75 | + with pytest.raises( |
| 76 | + CalledModuleError, |
| 77 | + match=rf"(?s)'{re.escape(str(output.parent))}'.*does not exist", |
| 78 | + ): |
| 79 | + tools.r_pack(input="rows_raster", output=output) |
| 80 | + |
| 81 | + |
| 82 | +def test_output_dir_is_file(xy_raster_dataset_session_for_module, tmp_path): |
| 83 | + """Check behavior when what should be a directory is a file""" |
| 84 | + tools = Tools(session=xy_raster_dataset_session_for_module) |
| 85 | + parent = tmp_path / "file_as_dir" |
| 86 | + parent.touch() |
| 87 | + output = parent / "output_1.rpack" |
| 88 | + assert not output.exists() |
| 89 | + assert output.parent.exists() |
| 90 | + assert output.parent.is_file() |
| 91 | + with pytest.raises( |
| 92 | + CalledModuleError, |
| 93 | + match=rf"(?s)'{re.escape(str(output.parent))}'.*is not.*directory", |
| 94 | + ): |
| 95 | + tools.r_pack(input="rows_raster", output=output) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.skipif(sys.platform.startswith("win"), reason="Dir still writable") |
| 99 | +def test_output_dir_is_not_writable(xy_raster_dataset_session_for_module, tmp_path): |
| 100 | + """Check behavior when directory is not writable""" |
| 101 | + tools = Tools(session=xy_raster_dataset_session_for_module) |
| 102 | + parent = tmp_path / "parent_dir" |
| 103 | + parent.mkdir() |
| 104 | + # This should work for Windows according to the doc, but it does not. |
| 105 | + parent.chmod(stat.S_IREAD) |
| 106 | + output = parent / "output_1.rpack" |
| 107 | + # Calling output.exists() gives permission denied on Linux, but os.path does not. |
| 108 | + assert not os.path.exists(output) |
| 109 | + assert output.parent.exists() |
| 110 | + assert output.parent.is_dir() |
| 111 | + with pytest.raises( |
| 112 | + CalledModuleError, |
| 113 | + match=rf"(?s)'{re.escape(str(output.parent))}'.*is not.*writable", |
| 114 | + ): |
| 115 | + tools.r_pack(input="rows_raster", output=output) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize("compression_flag", ["", "c"]) |
| 119 | +def test_round_trip_with_r_unpack( |
| 120 | + xy_raster_dataset_session_mapset, tmp_path, compression_flag |
| 121 | +): |
| 122 | + """Check that we get the same values with r.unpack""" |
| 123 | + tools = Tools(session=xy_raster_dataset_session_mapset) |
| 124 | + output = tmp_path / "output_1.rpack" |
| 125 | + tools.r_pack(input="rows_raster", output=output, flags=compression_flag) |
| 126 | + imported = "imported" |
| 127 | + tools.r_unpack(input=output, output=imported) |
| 128 | + tools.r_mapcalc(expression="diff = rows_raster - imported") |
| 129 | + stats = tools.r_univar(map="diff", format="json") |
| 130 | + assert stats["n"] > 1 |
| 131 | + assert stats["sum"] == 0 |
| 132 | + assert stats["min"] == 0 |
| 133 | + assert stats["max"] == 0 |
0 commit comments