-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add missing tests for get_goose_sessions #165
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
Changes from all commits
7071dc6
b6c7462
c9f8449
6809aa7
fa94f17
36420c9
4a4d1da
5d067bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | ||||||||||||||||
| import pytest | ||||||||||||||||
| import sys | ||||||||||||||||
| import os | ||||||||||||||||
| from pathlib import Path | ||||||||||||||||
| from unittest.mock import patch, MagicMock | ||||||||||||||||
|
|
||||||||||||||||
| os.environ["CONFIG_PATH"] = str(Path(__file__).resolve().parent.parent / "config.yaml") | ||||||||||||||||
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | ||||||||||||||||
|
|
||||||||||||||||
| from main import get_goose_sessions | ||||||||||||||||
|
|
||||||||||||||||
| def test_get_goose_sessions_no_db(): | ||||||||||||||||
| with patch('os.path.exists', return_value=False): | ||||||||||||||||
| assert get_goose_sessions() == [] | ||||||||||||||||
|
Comment on lines
+12
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Also assert that no DB connection is attempted when the file does not exist The existing test correctly checks that an empty list is returned when the DB file is missing. To more fully verify the behavior, also assert that
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| def test_get_goose_sessions_success(): | ||||||||||||||||
| mock_sqlite3 = MagicMock() | ||||||||||||||||
| mock_conn = MagicMock() | ||||||||||||||||
| mock_cursor = MagicMock() | ||||||||||||||||
|
|
||||||||||||||||
| mock_sqlite3.connect.return_value = mock_conn | ||||||||||||||||
| mock_conn.cursor.return_value = mock_cursor | ||||||||||||||||
|
|
||||||||||||||||
| mock_cursor.fetchall.return_value = [ | ||||||||||||||||
| {"id": 1, "name": "s1"}, | ||||||||||||||||
| {"id": 2, "name": "s2"} | ||||||||||||||||
| ] | ||||||||||||||||
|
|
||||||||||||||||
| with patch('os.path.exists', return_value=True): | ||||||||||||||||
| with patch.dict(sys.modules, {'sqlite3': mock_sqlite3}): | ||||||||||||||||
| result = get_goose_sessions() | ||||||||||||||||
|
|
||||||||||||||||
| assert len(result) == 2 | ||||||||||||||||
| assert result[0] == {"id": 1, "name": "s1"} | ||||||||||||||||
| mock_sqlite3.connect.assert_called_once_with("/config/goose_sessions/sessions/sessions.db", timeout=1.0) | ||||||||||||||||
| mock_cursor.execute.assert_called_once() | ||||||||||||||||
| mock_conn.close.assert_called_once() | ||||||||||||||||
|
|
||||||||||||||||
| def test_get_goose_sessions_exception(): | ||||||||||||||||
| mock_sqlite3 = MagicMock() | ||||||||||||||||
| mock_sqlite3.connect.side_effect = Exception("DB error") | ||||||||||||||||
|
|
||||||||||||||||
| with patch('os.path.exists', return_value=True): | ||||||||||||||||
| with patch.dict(sys.modules, {'sqlite3': mock_sqlite3}): | ||||||||||||||||
| result = get_goose_sessions() | ||||||||||||||||
| assert result == [] | ||||||||||||||||
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.
suggestion (testing): Avoid module-level mutation of environment and sys.path in tests to reduce cross-test side effects
These assignments to
os.environ["CONFIG_PATH"]andsys.pathat module import time can create hidden dependencies between tests and make ordering matter. Instead, move this setup into fixtures or individual tests (e.g., usepytest’smonkeypatchfor both env vars andsys.path) so configuration is scoped per test and doesn’t leak across the suite.Suggested implementation:
If other tests in this file (not shown in the snippet) reference
get_goose_sessionsdirectly or rely on the previous module-level configuration, they should be updated to:goose_sessionsfixture as a parameter.goose_sessions()instead ofget_goose_sessions().This ensures all tests use scoped configuration via
monkeypatchand avoid cross-test side effects.