Skip to content
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

test: improve test coverage #13

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/linters/.hadolint.yaml

This file was deleted.

45 changes: 0 additions & 45 deletions .github/workflows/linter.yml

This file was deleted.

28 changes: 28 additions & 0 deletions .github/workflows/major-version-updater.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Major Version Updater
# Whenever a new release is made, push a major version tag
on:
release:
types: [ published ]

jobs:
update-major-version-tag:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: version
id: version
run: |
tag=${GITHUB_REF/refs\/tags\//}
version=${tag#v}
major=${version%%.*}
echo "tag=${tag}" >> "$GITHUB_OUTPUT"
echo "version=${version}" >> "$GITHUB_OUTPUT"
echo "major=${major}" >> "$GITHUB_OUTPUT"

- name: force update major tag
run: |
git tag v${{ steps.version.outputs.major }}
git push origin refs/tags/v${{ steps.version.outputs.major }} -f
9 changes: 3 additions & 6 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest pytest-cov
python -m pip install flake8 pylint pytest pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
- name: Lint with flake8 and pylint
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
make lint
- name: Test with pytest
run: |
make test
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[MESSAGES CONTROL]
disable=
redefined-argument-from-local,
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ test:
.PHONY: clean
clean:
rm -rf .pytest_cache .coverage __pycache__

.PHONY: lint
lint:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
pylint --rcfile=.pylintrc --fail-under=9.0 *.py
4 changes: 1 addition & 3 deletions issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ def auth_to_github():
"""Connect to GitHub.com or GitHub Enterprise, depending on env variables."""
token = os.getenv("GH_TOKEN")
if token:
github_connection = github3.login(token=os.getenv("GH_TOKEN"))
github_connection = github3.login(token=token)
else:
raise ValueError("GH_TOKEN environment variable not set")

if not github_connection:
raise ValueError("Unable to authenticate to GitHub")
return github_connection # type: ignore


Expand Down
34 changes: 34 additions & 0 deletions test_issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ def test_auth_to_github_with_token(self, mock_login):
self.assertEqual(github_connection, mock_gh)
mock_login.assert_called_once_with(token="test_token")

def test_auth_to_github_no_token(self):
"""Test that auth_to_github raises a ValueError if GH_TOKEN is not set."""
# Unset the GH_TOKEN environment variable
if "GH_TOKEN" in os.environ:
del os.environ["GH_TOKEN"]

# Call auth_to_github and check that it raises a ValueError
with self.assertRaises(ValueError):
issue_metrics.auth_to_github()


class TestMeasureTimeToFirstResponse(unittest.TestCase):
"""Test the measure_time_to_first_response function."""
Expand Down Expand Up @@ -124,6 +134,30 @@ def test_measure_time_to_first_response(self):
self.assertEqual(issues_with_metrics[0][2], timedelta(days=1))
self.assertEqual(issues_with_metrics[1][2], timedelta(days=1))

def test_measure_time_to_first_response_no_comments(self):
"""Test that measure_time_to_first_response returns empty for an issue with no comments."""
# Set up mock issues with no comments
mock_issue1 = MagicMock(
comments=0,
created_at="2023-01-01T00:00:00Z",
)

mock_issue2 = MagicMock(
comments=0,
created_at="2023-01-01T00:00:00Z",
)

mock_issues = [mock_issue1, mock_issue2]

# Call measure_time_to_first_response and check that it returns None
time_to_first_response = issue_metrics.measure_time_to_first_response(
mock_issues
)

self.assertEqual(len(time_to_first_response), 2)
self.assertEqual(time_to_first_response[0][2], None)
self.assertEqual(time_to_first_response[1][2], None)


class TestGetAverageTimeToFirstResponse(unittest.TestCase):
"""Test the get_average_time_to_first_response function."""
Expand Down