-
Notifications
You must be signed in to change notification settings - Fork 0
fix(cli): handle missing database gracefully during classify #63
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
Merged
Merged
Changes from all commits
Commits
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
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,105 @@ | ||
| """Tests for classify command behavior.""" | ||
|
|
||
| from importlib import import_module | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| import typer | ||
| from sqlmodel import create_engine | ||
|
|
||
| cli_app = import_module("review_classification.cli.app") | ||
|
|
||
|
|
||
| def test_classify_missing_database_shows_clean_error( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path: Path, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """classify in a dir with no DB exits 1 with a clean message, no traceback.""" | ||
| monkeypatch.chdir(tmp_path) | ||
|
|
||
| with pytest.raises(typer.Exit) as exc_info: | ||
| cli_app.classify( | ||
| repo=["my-org/repo"], | ||
| org=None, | ||
| config=None, | ||
| threshold=2.0, | ||
| min_samples=30, | ||
| output_format="table", | ||
| verbose=False, | ||
| start=None, | ||
| end=None, | ||
| exclude_primary_merged=False, | ||
| ) | ||
|
|
||
| assert exc_info.value.exit_code == 1 | ||
|
|
||
| captured = capsys.readouterr() | ||
| assert "No database found" in captured.err | ||
| assert "fetch" in captured.err | ||
| # The raw SQLAlchemy error must never reach the user. | ||
| assert "OperationalError" not in captured.err | ||
| assert "no such table" not in captured.err | ||
| # No stray empty database file should be created. | ||
| assert not (tmp_path / "review_classification.db").exists() | ||
|
|
||
|
|
||
| def test_classify_missing_database_for_org_shows_clean_error( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path: Path, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """The org path (which reads the DB to resolve repos) also fails gracefully.""" | ||
| monkeypatch.chdir(tmp_path) | ||
|
|
||
| with pytest.raises(typer.Exit) as exc_info: | ||
| cli_app.classify( | ||
| repo=None, | ||
| org=["my-org"], | ||
| config=None, | ||
| threshold=2.0, | ||
| min_samples=30, | ||
| output_format="table", | ||
| verbose=False, | ||
| start=None, | ||
| end=None, | ||
| exclude_primary_merged=False, | ||
| ) | ||
|
|
||
| assert exc_info.value.exit_code == 1 | ||
|
|
||
| captured = capsys.readouterr() | ||
| assert "No database found" in captured.err | ||
| assert "OperationalError" not in captured.err | ||
|
|
||
|
|
||
| def test_classify_empty_database_for_org_shows_clean_error( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path: Path, | ||
| capsys: pytest.CaptureFixture[str], | ||
| ) -> None: | ||
| """A leftover empty DB file (no tables) must not yield a silent empty report.""" | ||
| import review_classification.sqlite.database as db_module | ||
|
|
||
| monkeypatch.chdir(tmp_path) | ||
| db_path = tmp_path / db_module.sqlite_file_name | ||
| db_path.touch() # file exists but has no tables, e.g. from a failed run | ||
| monkeypatch.setattr(db_module, "engine", create_engine(f"sqlite:///{db_path}")) | ||
|
|
||
| with pytest.raises(typer.Exit) as exc_info: | ||
| cli_app.classify( | ||
| repo=None, | ||
| org=["my-org"], | ||
| config=None, | ||
| threshold=2.0, | ||
| min_samples=30, | ||
| output_format="table", | ||
| verbose=False, | ||
| start=None, | ||
| end=None, | ||
| exclude_primary_merged=False, | ||
| ) | ||
|
|
||
| assert exc_info.value.exit_code == 1 | ||
| captured = capsys.readouterr() | ||
| assert "No database found" in captured.err |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a
review_classification.dbfile exists but has no tables (for example, left behind by an earlier failed run), the newdatabase_exists()guard passes andclassify --org ...reaches this handler. Returning[]for the missingpullrequesttable makes_resolve_targetsproduce no repos, so_print_detect_resultssees no failures and the command exits successfully with an empty report instead of telling the user to runfetchfirst.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍