Skip to content

Commit

Permalink
Fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lwasser committed Mar 10, 2024
1 parent 6a2fbfe commit 8f1b483
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions development.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ script options that you can chose to run.

`hatch run test:run-coverage`

2. To run tests without code coverage report outs use :
`hatch run test:run-coverage`
2. To run tests without code coverage report outs use:
`hatch run test:run-no-cov`

3. To run tests with an xml report generated use:
`hatch run test:run-report`
Expand Down
12 changes: 11 additions & 1 deletion src/pyosmeta/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ def get_token(self) -> str | None:
-------
str
The provided API key in the .env file.
Raises
------
KeyError
If the GITHUB_TOKEN environment variable is not found.
"""
load_dotenv()
return os.environ["GITHUB_TOKEN"]
try:
return os.environ["GITHUB_TOKEN"]
except KeyError:
raise KeyError(
"Oops! A GITHUB_TOKEN environment variable wasn't found."
)

@property
def api_endpoint(self):
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/test_github_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os

import pytest
import secrets

from pyosmeta.github_api import GitHubAPI


@pytest.fixture
def mock_github_token(monkeypatch):
"""Fixture to create a mock token - i don't believe this
is working as expected either."""
# Generate a random token
random_token = secrets.token_hex(16)

# Mocking the GitHub token in the environment variable
monkeypatch.setenv("GITHUB_TOKEN", random_token)


@pytest.fixture
def mock_missing_github_token(monkeypatch, tmpdir):
os.chdir(tmpdir)
# Remove the GitHub token from the environment variable
monkeypatch.delenv("GITHUB_TOKEN", raising=False)


def test_get_token(mock_github_token):
"""Test that get_token accesses the token correctly when it is
present."""
github_api = GitHubAPI()
token = github_api.get_token()

assert token == os.environ["GITHUB_TOKEN"]


# This test should work but it keeps finding my local envt var
# Even tho i'm removing it in the monkey patch and using a temp
# Directory
# def test_missing_token(mock_missing_github_token, tmpdir):
# """Test that a keyerror is raised when the token is missing."""
# os.chdir(tmpdir)
# github_api = GitHubAPI()

# with pytest.raises(KeyError, match="Oops! A GITHUB_TOKEN environment"):
# github_api.get_token()

0 comments on commit 8f1b483

Please sign in to comment.