Skip to content
Merged
78 changes: 78 additions & 0 deletions raster/r.support/tests/r_support_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,81 @@ def test_remove_semantic_label(session):
tools.r_support(map="test", flags="b")
info = tools.r_info(map="test", format="json").json
assert not info.get("semantic_label")


def test_set_metadata_fields(session):
"""title, units, vdatum, source1/source2 and description are all stored."""
tools = Tools(session=session)
tools.r_support(
map="test",
title="My Title",
units="meters",
vdatum="WGS84",
source1="Source A",
source2="Source B",
description="A description",
)

info = tools.r_info(map="test", format="json")
assert info["title"] == "My Title"
assert info["units"] == "meters"
assert info["vdatum"] == "WGS84"
assert info["source1"] == "Source A"
assert info["source2"] == "Source B"
assert info["description"] == "A description"


def test_append_history(session):
"""Each history= call appends a line rather than replacing prior ones."""
tools = Tools(session=session)
tools.r_support(map="test", history="First history line")
tools.r_support(map="test", history="Second history line")

info = tools.r_info(map="test", format="json")
assert info["comments"].endswith("First history line\nSecond history line")


def test_savehistory_and_loadhistory(session, tmp_path):
"""savehistory writes the history to a file that loadhistory can restore
onto another map."""
tools = Tools(session=session)
tools.r_support(map="test", history="Line one")
tools.r_support(map="test", history="Line two")
expected = tools.r_info(map="test", format="json")["comments"]

history_file = tmp_path / "history.txt"
tools.r_support(map="test", savehistory=str(history_file))
assert history_file.read_text() == expected + "\n"

tools.r_mapcalc(expression="test2 = 2")
tools.r_support(map="test2", loadhistory=str(history_file))
info = tools.r_info(map="test2", format="json")
assert info["comments"] == expected


def test_reset_null_file_clears_null_cells(session):
"""The -n flag (re)creates the null file, here clearing the null status
of a cell that matches the CELL type's null sentinel on disk."""
tools = Tools(session=session)
tools.g_region(n=1, s=0, e=3, w=0, res=1)
tools.r_mapcalc(expression="with_null = if(col() == 2, null(), col())")
before = tools.r_univar(map="with_null", format="json")
assert before["null_cells"] == 1

tools.r_support(map="with_null", flags="n")
after = tools.r_univar(map="with_null", format="json")
assert after["null_cells"] == 0


def test_delete_null_file_restores_null_cells(session):
"""The -d flag removes the null file, falling back to the implicit
null sentinel stored in the raw cell values."""
tools = Tools(session=session)
tools.g_region(n=1, s=0, e=3, w=0, res=1)
tools.r_mapcalc(expression="with_null = if(col() == 2, null(), col())")
tools.r_support(map="with_null", flags="n")
assert tools.r_univar(map="with_null", format="json")["null_cells"] == 0

tools.r_support(map="with_null", flags="d")
after = tools.r_univar(map="with_null", format="json")
assert after["null_cells"] == 1
Loading