|
| 1 | +"""Tests for grass.jupyter.utils.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import grass.script as gs |
| 5 | +from grass.jupyter.utils import ( |
| 6 | + get_region, |
| 7 | + get_rendering_size, |
| 8 | + get_map_name_from_d_command, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def test_get_region(session_with_data): |
| 13 | + """get_region() should match GRASS region output.""" |
| 14 | + env = session_with_data.env |
| 15 | + |
| 16 | + expected = gs.region(env=env) |
| 17 | + result = get_region(env=env) |
| 18 | + |
| 19 | + assert result["north"] == pytest.approx(expected["n"]) |
| 20 | + assert result["south"] == pytest.approx(expected["s"]) |
| 21 | + assert result["east"] == pytest.approx(expected["e"]) |
| 22 | + assert result["west"] == pytest.approx(expected["w"]) |
| 23 | + |
| 24 | + |
| 25 | +def test_get_rendering_size(): |
| 26 | + """Test rendering size calculations.""" |
| 27 | + region = {"n": 4, "s": 0, "e": 8, "w": 0} |
| 28 | + |
| 29 | + # both provided |
| 30 | + assert get_rendering_size({}, 800, 600) == (800, 600) |
| 31 | + |
| 32 | + # width only |
| 33 | + width, height = get_rendering_size(region, 800, None) |
| 34 | + assert width == 800 |
| 35 | + assert height == 400 |
| 36 | + |
| 37 | + # height only |
| 38 | + width, height = get_rendering_size(region, None, 400) |
| 39 | + assert width == 800 |
| 40 | + assert height == 400 |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + ("cmd", "kwargs", "expected"), |
| 45 | + [ |
| 46 | + ("d.rast", {"map": "elevation"}, "elevation"), |
| 47 | + ("d.rgb", {"red": "red_band"}, "red_band"), |
| 48 | + ("d.legend", {"raster": "elevation"}, "elevation"), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_get_map_name_from_d_command(cmd, kwargs, expected): |
| 52 | + """Test map name extraction from display commands.""" |
| 53 | + assert get_map_name_from_d_command(cmd, **kwargs) == expected |
0 commit comments