Skip to content

Commit

Permalink
chore: Move tests to tests/ dir (#60)
Browse files Browse the repository at this point in the history
* Move tests to tests/ directory

* Rename test files to test_ prefix

* Install before running tests in ci

* Here too

* Rework coverage workflow
  • Loading branch information
nealrichardson authored Mar 4, 2024
1 parent 08bb033 commit ec19c93
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 95 deletions.
24 changes: 10 additions & 14 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- run: make deps
- run: make install
- run: make test
# Run coverage on one of the builds, doesn't matter which
- if: ${{ matrix.python-version == '3.12' }}
run: make cov-xml
- if: ${{ matrix.python-version == '3.12' }}
uses: orgoro/coverage@v3.1
with:
coverageFile: coverage.xml
thresholdAll: 0.8
token: ${{ secrets.GITHUB_TOKEN }}
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: make deps
- run: make lint
cov:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: make deps
- run: make test
- run: make cov-xml
- if: always()
uses: orgoro/coverage@v3.1
with:
coverageFile: coverage.xml
thresholdAll: 0.8
token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ Source = "https://github.com/posit-dev/posit-sdk-py"
Issues = "https://github.com/posit-dev/posit-sdk-py/issues"

[tool.pytest.ini_options]
python_files = [
"*_test.py",
"*_test_*.py",
addopts = [
"--import-mode=importlib",
]

[tool.setuptools_scm]
Expand Down
30 changes: 0 additions & 30 deletions src/posit/connect/hooks_test.py

This file was deleted.

40 changes: 0 additions & 40 deletions src/posit/connect/hooks_test_deps.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import MagicMock, Mock, patch

from .auth import Auth
from posit.connect.auth import Auth


class TestAuth:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from unittest.mock import MagicMock, patch

from .client import Client
from posit.connect import Client


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from unittest.mock import patch

from .config import Config, _get_api_key, _get_url
from posit.connect.config import Config, _get_api_key, _get_url


@patch.dict("os.environ", {"CONNECT_API_KEY": "foobar"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import responses

from .client import Client
from posit.connect.client import Client


class TestContents:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from .errors import ClientError
from posit.connect.errors import ClientError


class TestClientError:
Expand Down
65 changes: 65 additions & 0 deletions tests/posit/connect/test_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import io

import pytest

from requests import HTTPError, Response
from unittest.mock import Mock, patch

from posit.connect.errors import ClientError
from posit.connect.hooks import handle_errors


def test_success():
response = Mock()
response.status_code = 200
assert handle_errors(response) == response


def test_client_error():
response = Mock()
response.status_code = 400
response.json = Mock(return_value={"code": 0, "error": "foobar"})
with pytest.raises(ClientError):
handle_errors(response)


@patch("posit.connect.hooks.JSONDecodeError")
def test_client_error_without_payload(JSONDecodeError):
response = Mock()
response.status_code = 404
response.json = Mock(side_effect=JSONDecodeError())
response.raise_for_status = Mock(side_effect=Exception())
with pytest.raises(Exception):
handle_errors(response)


def test_200():
response = Response()
response.status_code = 200
assert handle_errors(response) == response


def test_response_client_error_with_plaintext_payload():
response = Response()
response.status_code = 404
response.raw = io.BytesIO(b"Plain text 404 Not Found")
with pytest.raises(HTTPError):
handle_errors(response)


def test_response_client_error_with_json_payload():
response = Response()
response.status_code = 400
response.raw = io.BytesIO(b'{"code":0,"error":"foobar"}')
with pytest.raises(
ClientError, match=r"foobar \(Error Code: 0, HTTP Status: 400 Bad Request\)"
):
handle_errors(response)


def test_response_client_error_without_payload():
response = Response()
response.status_code = 404
response.raw = io.BytesIO(b"Plain text 404 Not Found")
with pytest.raises(HTTPError):
handle_errors(response)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import responses

from .client import Client
from posit.connect import Client


class TestOAuthIntegrations:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from .urls import append_path, server_to_api_url, validate
from posit.connect.urls import append_path, server_to_api_url, validate


def test_append_path():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas as pd
import responses

from .client import Client
from posit.connect.client import Client


class TestUsers:
Expand Down

0 comments on commit ec19c93

Please sign in to comment.