Skip to content

Commit

Permalink
Add tests to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
duckduckgrayduck committed Dec 11, 2024
1 parent 63aaaff commit 5678a33
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 1 deletion.
25 changes: 24 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
jobs:
lint-and-format:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3
Expand All @@ -33,3 +32,27 @@ jobs:
- name: Run Black on squarelet.py
run: |
black src/squarelet/squarelet.py
run-tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
env:
SQ_USER: ${{ secrets.SQ_USER }}
SQ_PASSWORD: ${{ secrets.SQ_PASSWORD }}
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install python-squarelet pytest
- name: Run tests
run: |
pytest src/squarelet/tests.py
Binary file added src/squarelet/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified src/squarelet/__pycache__/squarelet.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
51 changes: 51 additions & 0 deletions src/squarelet/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
""" Tests for python-squarelet """

import os
import pytest
from squarelet import (CredentialsFailedError, DoesNotExistError,
SquareletClient)

#pylint:disable=redefined-outer-name
@pytest.fixture
def squarelet_client():
"""Fixture to mock a SquareletClient instance."""
sq_user = os.environ.get("SQ_USER")
sq_password = os.environ.get("SQ_PASSWORD")
return SquareletClient(
base_uri="https://api.www.documentcloud.org/api/",
username=sq_user,
password=sq_password,
)


def test_get_tokens(squarelet_client):
"""Test token retrieval via username and password."""
assert squarelet_client.access_token is not None
assert squarelet_client.refresh_token is not None


def test_get_tokens_invalid_credentials(squarelet_client):
""" Try to authenticate with fake credentials """
# pylint:disable = protected-access
with pytest.raises(CredentialsFailedError):
squarelet_client._get_tokens("invalid_user", "invalid_pass")


def test_raises_for_status(squarelet_client):
""" Assert that other errors are raised """
with pytest.raises(DoesNotExistError) as excinfo:
# This should raise the DoesNotExistError since the status code will be 404
squarelet_client.request("get", "blank")
assert excinfo.value.response.status_code == 404


def test_access_documentcloud(squarelet_client):
""" Test that we can access the DocumentCloud endpoint """
sq_user = os.environ.get("SQ_USER")
my_user = squarelet_client.request("get", "users/me/")
user_data = my_user.json()
# We assert here that the username returned by DocumentCloud is our current username
assert user_data["username"] == sq_user

## TO DO def test_access_muckrock():
## TO DO def test_rate_limit(squarelet_client):
12 changes: 12 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tox]
envlist = py37,py38,py39,py310,py311,py312

[testenv]
deps =
pytest
requests-mock
ratelimit
commands =
pytest src/squarelet/tests.py --maxfail=5

passenv = SQ_USER, SQ_PASSWORD

0 comments on commit 5678a33

Please sign in to comment.