diff --git a/temporal/t.connect/tests/conftest.py b/temporal/t.connect/tests/conftest.py new file mode 100644 index 00000000000..5070fa1b03e --- /dev/null +++ b/temporal/t.connect/tests/conftest.py @@ -0,0 +1,41 @@ +""" +Fixtures for t.connect 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 t_connect_session(tmp_path_factory): + """Start an isolated session for t.connect tests. + + Returns a SimpleNamespace containing the session. + """ + tmp_path = tmp_path_factory.mktemp("t_connect_test") + project = tmp_path / "test_project" + gs.create_project(project) + + with gs.setup.init(project, env=os.environ.copy()) as session: + tools = Tools(session=session) + + db_path = "$GISDBASE/$LOCATION_NAME/$MAPSET/tgis/sqlite.db" + tools.t_connect(driver="sqlite", database=db_path) + + yield SimpleNamespace(session=session) + + +@pytest.fixture(scope="module") +def empty_session(tmp_path_factory): + """Start an isolated session with no connection.""" + tmp_path = tmp_path_factory.mktemp("t_connect_empty") + project = tmp_path / "test_project_empty" + gs.create_project(project) + + with gs.setup.init(project, env=os.environ.copy()) as session: + yield SimpleNamespace(session=session) diff --git a/temporal/t.connect/tests/test_t_connect.py b/temporal/t.connect/tests/test_t_connect.py new file mode 100644 index 00000000000..c968fea5c30 --- /dev/null +++ b/temporal/t.connect/tests/test_t_connect.py @@ -0,0 +1,35 @@ +"""Test current baseline functionality of t.connect""" + +from grass.tools import Tools + + +def test_no_connection(empty_session): + """Check that printing an uninitialized connection returns empty fields.""" + tools = Tools(session=empty_session.session) + result = tools.t_connect(flags="pg") + + lines = result.text_split("\n") + + assert lines == ["driver=", "database="] + + +def test_print_plain(t_connect_session): + """Check that the -p flag prints the plain text layout.""" + tools = Tools(session=t_connect_session.session) + + result = tools.t_connect(flags="p") + lines = result.text_split("\n") + + assert lines[0] == "driver:sqlite" + assert lines[1] == "database:$GISDBASE/$LOCATION_NAME/$MAPSET/tgis/sqlite.db" + + +def test_print_shell(t_connect_session): + """Check that the shell format prints correctly.""" + tools = Tools(session=t_connect_session.session) + + result = tools.t_connect(flags="pg") + lines = result.text_split("\n") + + assert "driver=sqlite" in lines + assert "database=$GISDBASE/$LOCATION_NAME/$MAPSET/tgis/sqlite.db" in lines