-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Added comprehensive unit testing and github action to run tests on new pull requests #8
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
dklawren
wants to merge
14
commits into
main
Choose a base branch
from
unit-tests
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1791608
feat: Added comprehensive unit testing and github action to run tests…
dklawren d6cb74c
Copilot suggested fixes
dklawren 5836a84
Black formatted
dklawren 76f54f3
Used isort to fix sorting order
dklawren 9c288cc
Mypy test fixes
dklawren b95c05f
Copilot fixes
dklawren 8b7eb48
Fixed review comments
dklawren 4bb878e
Added conftest.py and moved tests to test/ directory
dklawren e3647c4
- Fixed integration test gitub action to use docker compose properly.
dklawren c4dd862
Separate TESTING.md not necessary. Added testing section to README.md
dklawren 48c1c46
Copoilot suggested fixes
dklawren b5c2ccf
Review fixes
dklawren 7887050
Fixed Python version mismatch
dklawren 4a8c8d7
Copilot suggested fixes
dklawren 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # Dockerfile for mock GitHub API service | ||
| FROM python:3.11-slim | ||
| FROM python:3.14.2-slim | ||
|
|
||
| WORKDIR /app | ||
|
|
||
|
|
||
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
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,20 @@ | ||
| from unittest.mock import MagicMock, Mock | ||
|
|
||
| import pytest | ||
| import requests | ||
| from google.cloud import bigquery | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_session() -> Mock: | ||
| session = Mock(spec=requests.Session) | ||
| session.headers = {} | ||
| return session | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_bigquery_client() -> Mock: | ||
| client = Mock(spec=bigquery.Client) | ||
| client.project = "test-project" | ||
| client.insert_rows_json = MagicMock(return_value=[]) # Empty list = no errors | ||
| return client |
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,38 @@ | ||
| #!/usr/bin/env python3 | ||
| from unittest.mock import Mock, patch | ||
|
Member
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. We install We should get rid of |
||
|
|
||
| import pytest | ||
|
|
||
| import main | ||
|
|
||
|
|
||
| @patch("main.sleep_for_rate_limit") | ||
| def test_rate_limit_handling_comments(mock_sleep, mock_session): | ||
| """Test rate limit handling when fetching comments.""" | ||
| rate_limit_response = Mock() | ||
| rate_limit_response.status_code = 403 | ||
| rate_limit_response.headers = {"X-RateLimit-Remaining": "0"} | ||
|
|
||
| success_response = Mock() | ||
| success_response.status_code = 200 | ||
| success_response.json.return_value = [] | ||
|
|
||
| mock_session.get.side_effect = [rate_limit_response, success_response] | ||
|
|
||
| main.extract_comments(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| mock_sleep.assert_called_once() | ||
|
|
||
|
|
||
| def test_api_error_comments(mock_session): | ||
| """Test API error handling when fetching comments.""" | ||
| error_response = Mock() | ||
| error_response.status_code = 404 | ||
| error_response.text = "Not Found" | ||
|
|
||
| mock_session.get.return_value = error_response | ||
|
|
||
| with pytest.raises(SystemExit) as exc_info: | ||
| main.extract_comments(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| assert "GitHub API error 404" in str(exc_info.value) | ||
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,123 @@ | ||
| #!/usr/bin/env python3 | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| import main | ||
|
|
||
|
|
||
| def test_extract_commits_with_files(mock_session): | ||
| """Test that file details are fetched per commit and merged into commit data.""" | ||
| commits_response = Mock() | ||
| commits_response.status_code = 200 | ||
| commits_response.json.return_value = [ | ||
| {"sha": "abc123"}, | ||
| {"sha": "def456"}, | ||
| ] | ||
|
|
||
| commit_detail_1 = Mock() | ||
| commit_detail_1.status_code = 200 | ||
| commit_detail_1.json.return_value = { | ||
| "sha": "abc123", | ||
| "files": [{"filename": "file1.py", "additions": 10}], | ||
| } | ||
|
|
||
| commit_detail_2 = Mock() | ||
| commit_detail_2.status_code = 200 | ||
| commit_detail_2.json.return_value = { | ||
| "sha": "def456", | ||
| "files": [{"filename": "file2.py", "deletions": 5}], | ||
| } | ||
|
|
||
| mock_session.get.side_effect = [ | ||
| commits_response, | ||
| commit_detail_1, | ||
| commit_detail_2, | ||
| ] | ||
|
|
||
| result = main.extract_commits(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| # Verify the individual commit detail endpoints were fetched | ||
| assert mock_session.get.call_count == 3 | ||
| calls = mock_session.get.call_args_list | ||
| assert "commits/abc123" in calls[1][0][0] | ||
| assert "commits/def456" in calls[2][0][0] | ||
|
|
||
| # Verify file data from detail responses is merged into each commit | ||
| assert result[0]["files"][0]["filename"] == "file1.py" | ||
| assert result[1]["files"][0]["filename"] == "file2.py" | ||
|
|
||
|
|
||
| def test_multiple_files_per_commit(mock_session): | ||
| """Test that all files from a commit detail response are merged into the commit.""" | ||
| commits_response = Mock() | ||
| commits_response.status_code = 200 | ||
| commits_response.json.return_value = [{"sha": "abc123"}] | ||
|
|
||
| commit_detail = Mock() | ||
| commit_detail.status_code = 200 | ||
| commit_detail.json.return_value = { | ||
| "sha": "abc123", | ||
| "files": [ | ||
| {"filename": "file1.py", "additions": 10}, | ||
| {"filename": "file2.py", "additions": 20}, | ||
| {"filename": "file3.py", "deletions": 5}, | ||
| ], | ||
| } | ||
|
|
||
| mock_session.get.side_effect = [commits_response, commit_detail] | ||
|
|
||
| result = main.extract_commits(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| assert len(result[0]["files"]) == 3 | ||
|
|
||
|
|
||
| @patch("main.sleep_for_rate_limit") | ||
| def test_rate_limit_on_commits_list(mock_sleep, mock_session): | ||
| """Test rate limit handling when fetching commits list.""" | ||
| rate_limit_response = Mock() | ||
| rate_limit_response.status_code = 403 | ||
| rate_limit_response.headers = {"X-RateLimit-Remaining": "0"} | ||
|
|
||
| success_response = Mock() | ||
| success_response.status_code = 200 | ||
| success_response.json.return_value = [] | ||
|
|
||
| mock_session.get.side_effect = [rate_limit_response, success_response] | ||
|
|
||
| result = main.extract_commits(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| mock_sleep.assert_called_once() | ||
| assert result == [] | ||
|
|
||
|
|
||
| def test_api_error_on_commits_list(mock_session): | ||
| """Test API error handling when fetching commits list.""" | ||
| error_response = Mock() | ||
| error_response.status_code = 500 | ||
| error_response.text = "Internal Server Error" | ||
|
|
||
| mock_session.get.return_value = error_response | ||
|
|
||
| with pytest.raises(SystemExit) as exc_info: | ||
| main.extract_commits(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| assert "GitHub API error 500" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_api_error_on_individual_commit(mock_session): | ||
| """Test API error when fetching individual commit details.""" | ||
| commits_response = Mock() | ||
| commits_response.status_code = 200 | ||
| commits_response.json.return_value = [{"sha": "abc123"}] | ||
|
|
||
| commit_error = Mock() | ||
| commit_error.status_code = 404 | ||
| commit_error.text = "Commit not found" | ||
|
|
||
| mock_session.get.side_effect = [commits_response, commit_error] | ||
|
|
||
| with pytest.raises(SystemExit) as exc_info: | ||
| main.extract_commits(mock_session, "mozilla/firefox", 123) | ||
|
|
||
| assert "GitHub API error 404" in str(exc_info.value) |
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.