-
-
Notifications
You must be signed in to change notification settings - Fork 427
grass.script: add create_mapset function #7298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
petrasovaa
wants to merge
2
commits into
OSGeo:main
Choose a base branch
from
petrasovaa:create_mapset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
python/grass/script/tests/grass_script_create_mapset_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| """Tests for grass.script.create_mapset""" | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| import grass.script as gs | ||
| from grass.exceptions import ScriptError | ||
| from grass.tools import Tools | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project_path(tmp_path): | ||
| """Create a XY project and return its path.""" | ||
| project = tmp_path / "test_project" | ||
| gs.create_project(project) | ||
| return project | ||
|
|
||
|
|
||
| def test_create_mapset_full_path(project_path): | ||
| """Check that mapset is created when full path is given.""" | ||
| mapset_path = project_path / "new_mapset" | ||
| gs.create_mapset(mapset_path, initialize_db=False) | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
|
|
||
|
|
||
| def test_create_mapset_string_path(project_path): | ||
| """Check that mapset is created when path is given as a string.""" | ||
| gs.create_mapset(str(project_path), name="new_mapset", initialize_db=False) | ||
| mapset_path = project_path / "new_mapset" | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
|
|
||
|
|
||
| def test_create_mapset_with_name(project_path): | ||
| """Check that mapset is created when project path and name are given.""" | ||
| gs.create_mapset(project_path, name="new_mapset", initialize_db=False) | ||
| mapset_path = project_path / "new_mapset" | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
|
|
||
|
|
||
| def test_create_mapset_usable(project_path): | ||
| """Check that a created mapset can be used in a session.""" | ||
| gs.create_mapset(project_path, name="new_mapset") | ||
| with gs.setup.init(project_path / "new_mapset", env=os.environ.copy()) as session: | ||
| info = gs.gisenv(env=session.env) | ||
| assert info["MAPSET"] == "new_mapset" | ||
|
|
||
|
|
||
| def test_create_mapset_no_overwrite(project_path): | ||
| """Check that existing mapset raises error without overwrite.""" | ||
| gs.create_mapset(project_path, name="new_mapset") | ||
| with pytest.raises(ScriptError, match="already exists"): | ||
| gs.create_mapset(project_path, name="new_mapset") | ||
|
|
||
|
|
||
| def test_create_mapset_overwrite(project_path): | ||
| """Check that existing mapset can be overwritten.""" | ||
| mapset_path = project_path / "new_mapset" | ||
| gs.create_mapset(project_path, name="new_mapset", initialize_db=False) | ||
| # Add a file to verify old content is removed | ||
| marker = mapset_path / "marker_file" | ||
| marker.write_text("test") | ||
| assert marker.exists() | ||
|
|
||
| gs.create_mapset( | ||
| project_path, name="new_mapset", overwrite=True, initialize_db=False | ||
| ) | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
| assert not marker.exists() | ||
|
|
||
|
|
||
| def test_create_mapset_nonexistent_project(tmp_path): | ||
| """Check error when project does not exist.""" | ||
| with pytest.raises(ScriptError, match="does not exist"): | ||
| gs.create_mapset(tmp_path / "nonexistent" / "new_mapset") | ||
|
|
||
|
|
||
| def test_create_mapset_invalid_project(tmp_path): | ||
| """Check error when project directory exists but has no PERMANENT mapset.""" | ||
| invalid_project = tmp_path / "invalid_project" | ||
| invalid_project.mkdir() | ||
| with pytest.raises(ScriptError, match="PERMANENT"): | ||
| gs.create_mapset(invalid_project, name="new_mapset") | ||
|
|
||
|
|
||
| def test_create_mapset_permanent_rejected(project_path): | ||
| """Check that creating PERMANENT mapset is rejected.""" | ||
| with pytest.raises(ScriptError, match="PERMANENT"): | ||
| gs.create_mapset(project_path, name="PERMANENT") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("name", [".hidden", "has space", "with@at"]) | ||
| def test_create_mapset_illegal_name(project_path, name): | ||
| """Check that illegal mapset names are rejected.""" | ||
| with pytest.raises(ScriptError, match="Illegal"): | ||
| gs.create_mapset(project_path, name=name, initialize_db=False) | ||
|
|
||
|
|
||
| def test_create_multiple_mapsets(project_path): | ||
| """Check that multiple mapsets can be created in the same project.""" | ||
| names = ["mapset_a", "mapset_b", "mapset_c"] | ||
| for name in names: | ||
| gs.create_mapset(project_path, name=name, initialize_db=False) | ||
| for name in names: | ||
| assert (project_path / name).exists() | ||
| assert (project_path / name / "WIND").exists() | ||
|
|
||
|
|
||
| def test_create_mapset_name_only(project_path): | ||
| """Check that mapset is created using only name within an active session.""" | ||
| with gs.setup.init(project_path, env=os.environ.copy()) as session: | ||
| gs.create_mapset(name="new_mapset", env=session.env) | ||
| mapset_path = project_path / "new_mapset" | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("mock_no_session") | ||
| def test_create_mapset_name_only_no_session(): | ||
| """Check that name-only fails without an active session.""" | ||
| with pytest.raises(ScriptError, match="No active session"): | ||
| gs.create_mapset(name="new_mapset") | ||
|
|
||
|
|
||
| def test_create_mapset_no_arguments(): | ||
| """Check that calling without path or name raises an error.""" | ||
| with pytest.raises(ScriptError, match="Either path or name"): | ||
| gs.create_mapset() | ||
|
|
||
|
|
||
| def test_create_mapset_db_initialized(project_path): | ||
| """Check that database connection is initialized by default.""" | ||
| gs.create_mapset(project_path, name="new_mapset") | ||
| mapset_path = project_path / "new_mapset" | ||
| assert (mapset_path / "VAR").exists() | ||
| with gs.setup.init(mapset_path, env=os.environ.copy()) as session: | ||
| tools = Tools(session=session) | ||
| conn = tools.db_connect(flags="p", format="json") | ||
| assert conn["driver"] == "sqlite" | ||
| assert "new_mapset" in conn["database"] | ||
|
|
||
|
|
||
| def test_create_mapset_db_not_initialized(project_path): | ||
| """Check that database initialization is skipped when disabled.""" | ||
| gs.create_mapset(project_path, name="new_mapset", initialize_db=False) | ||
| mapset_path = project_path / "new_mapset" | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
| assert not (mapset_path / "VAR").exists() | ||
|
|
||
|
|
||
| def test_create_mapset_with_existing_session_env(project_path): | ||
| """Check that creating a mapset works when env comes from an existing session. | ||
|
|
||
| The env already has a GISRC pointing to PERMANENT. The DB initialization | ||
| must create its own session for the new mapset, not reuse the old one. | ||
| """ | ||
| with gs.setup.init(project_path, env=os.environ.copy()) as session: | ||
| gs.create_mapset(project_path, name="new_mapset", env=session.env) | ||
| mapset_path = project_path / "new_mapset" | ||
| assert mapset_path.exists() | ||
| assert (mapset_path / "WIND").exists() | ||
| assert (mapset_path / "VAR").exists() | ||
| with gs.setup.init(mapset_path, env=os.environ.copy()) as session: | ||
| tools = Tools(session=session) | ||
| conn = tools.db_connect(flags="p", format="json") | ||
| assert conn["driver"] == "sqlite" | ||
| assert "new_mapset" in conn["database"] | ||
|
|
||
|
|
||
| def test_create_mapset_region_from_default(project_path): | ||
| """Check that the new mapset's region matches the project's default region.""" | ||
| with gs.setup.init(project_path, env=os.environ.copy()) as session: | ||
| tools = Tools(session=session) | ||
| tools.g_region(n=100, s=0, e=200, w=0, res=10, flags="s") | ||
| gs.create_mapset(project_path, name="new_mapset") | ||
| with gs.setup.init(project_path / "new_mapset", env=os.environ.copy()) as session: | ||
| tools = Tools(session=session) | ||
| region = tools.g_region(flags="p", format="json") | ||
| assert region["north"] == 100 | ||
| assert region["south"] == 0 | ||
| assert region["east"] == 200 | ||
| assert region["west"] == 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.