Skip to content
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
57 changes: 57 additions & 0 deletions temporal/t.list/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Configuration and fixtures for t.list pytest suite.
"""

import os
from types import SimpleNamespace

import pytest

import grass.script as gs
from grass.tools import Tools


@pytest.fixture(scope="module")
def space_time_dataset(tmp_path_factory):
"""Start an isolated session and create a raster time series.

Returns an object with attributes about the dataset.
"""
tmp_path = tmp_path_factory.mktemp("raster_time_series")
project = tmp_path / "test_project"
gs.create_project(project)

with gs.setup.init(project, env=os.environ.copy()) as session:
tools = Tools(session=session)

tools.g_region(s=0, n=80, w=0, e=120, res=10)

names = [f"temp_{i}" for i in range(1, 4)]
for i, name in enumerate(names, start=1):
tools.r_mapcalc(expression=f"{name} = {i}", overwrite=True)

dataset_name = "temperature_dataset"

tools.t_create(
type="strds",
temporaltype="absolute",
output=dataset_name,
title="Temperature",
description="Random series generated for tests",
)

dataset_file = tmp_path / "names.txt"
dataset_file.write_text("\n".join(names))
tools.t_register(
type="raster",
input=dataset_name,
file=dataset_file,
start="2026-01-01",
increment="1 month",
)

yield SimpleNamespace(
session=session,
name=dataset_name,
map_names=names,
)
46 changes: 46 additions & 0 deletions temporal/t.list/tests/test_t_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Test t.list functionality"""

from grass.tools import Tools


def test_t_list_defaults(space_time_dataset):
"""Check that the module correctly lists datasets (strds) and maps (raster)."""
tools = Tools(session=space_time_dataset.session)

strds_result = tools.t_list(type="strds", columns="name")
assert strds_result.returncode == 0

strds_lines = [line.strip() for line in strds_result.stdout.strip().splitlines()]
assert space_time_dataset.name in strds_lines

# Test Map Listing (raster)
raster_result = tools.t_list(type="raster", columns="name")

raster_lines = [line.strip() for line in raster_result.stdout.strip().splitlines()]
for map_name in space_time_dataset.map_names:
assert map_name in raster_lines


def test_t_list_where_filter(space_time_dataset):
"""Check that where clause filter the output exactly."""
tools = Tools(session=space_time_dataset.session)

match = tools.t_list(type="strds", columns="name", where="name LIKE 'temp_%'")
match_lines = [line.strip() for line in match.stdout.strip().splitlines()]
assert len(match_lines) == 1
assert match_lines[0] == space_time_dataset.name

empty = tools.t_list(type="strds", columns="name", where="name LIKE 'land_%'")
assert empty is None


def test_t_list_order(space_time_dataset):
"""Check that ordering the output actually sorts the data correctly."""
tools = Tools(session=space_time_dataset.session)

result_asc = tools.t_list(type="raster", columns="name", order="start_time")
assert result_asc is not None

lines_asc = [line.strip() for line in result_asc.stdout.strip().splitlines()]

assert lines_asc == space_time_dataset.map_names
Loading