Skip to content

Commit

Permalink
Merge pull request #1 from shaiu/shaiu/fix-tests
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
shaiu authored Jul 12, 2024
2 parents c8f9892 + 4bcc33c commit 00b880c
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 82 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

63 changes: 25 additions & 38 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
---
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.1
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/PyCQA/bandit
rev: 1.6.2
hooks:
Expand All @@ -9,15 +18,6 @@ repos:
- --format=custom
- --configfile=.bandit.yaml
files: ^pyseventeentrack/.+\.py$
- repo: https://github.com/python/black
rev: 22.3.0
hooks:
- id: black
args:
- --safe
- --quiet
language_version: python3
files: ^((pyseventeentrack|tests)/.+)?[^/]+\.py$
- repo: https://github.com/codespell-project/codespell
rev: v1.16.0
hooks:
Expand All @@ -26,26 +26,6 @@ repos:
- --skip="./.*,*.json"
- --quiet-level=4
exclude_types: [json]
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
additional_dependencies:
- flake8-docstrings==1.5.0
- pydocstyle==5.0.1
files: ^pyseventeentrack/.+\.py$
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
hooks:
- id: isort
additional_dependencies:
- toml
files: ^(pyseventeentrack|tests)/.+\.py$
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
hooks:
- id: mypy
files: ^pyseventeentrack/.+\.py$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
Expand All @@ -54,13 +34,20 @@ repos:
args:
- --branch=dev
- --branch=master
- repo: https://github.com/PyCQA/pydocstyle
rev: 5.0.2
- repo: local
hooks:
- id: pydocstyle
files: ^((pyseventeentrack|tests)/.+)?[^/]+\.py$
- repo: https://github.com/gruntwork-io/pre-commit
rev: v0.1.12
hooks:
- id: shellcheck
files: ^script/.+
# Run mypy through our wrapper script in order to get the possible
# pyenv and/or virtualenv activated; it may not have been e.g. if
# committing from a GUI tool that was not launched from an activated
# shell.
- id: mypy
name: mypy
entry: script/run-in-env.sh mypy
language: script
types_or: [ python, pyi ]
require_serial: true
- id: pylint
name: pylint
entry: script/run-in-env.sh pylint -j 0
language: script
types_or: [ python, pyi ]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Aaron Bach
Copyright (c) 2018 Shai Ungar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions examples/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Run an example script to quickly test a 17track.net account."""

import asyncio
import logging

Expand Down
14 changes: 0 additions & 14 deletions pylintrc

This file was deleted.

66 changes: 64 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,70 @@ python = "^3.9.0"
pytz = ">=2021.1"

[tool.poetry.dev-dependencies]
aresponses = "^2.0.0"
aresponses = "^3.0.0"
pre-commit = "^2.0.1"
pytest = "^7.0.0"
pytest = "^8.0.0"
pytest-aiohttp = "^1.0.0"
pytest-cov = "^3.0.0"

# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pyenv",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"site-packages",
"venv",
]

# Same as Black.
line-length = 88
indent-width = 4

# Assume Python 3.8
target-version = "py38"

[tool.ruff.lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
select = ["E4", "E7", "E9", "F"]
ignore = []

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.ruff.format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"
1 change: 1 addition & 0 deletions pyseventeentrack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""Define module-level imports."""

from .client import Client # noqa
5 changes: 3 additions & 2 deletions pyseventeentrack/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Define a 17track.net client."""

from typing import Optional

from aiohttp import ClientSession, ClientTimeout
Expand All @@ -23,7 +24,7 @@ def __init__(self, *, session: Optional[ClientSession] = None) -> None:
# This is disabled until a workaround can be found:
# self.track = Track(self._request)

async def _request(
async def _request( # pylint: disable=too-many-arguments
self,
method: str,
url: str,
Expand All @@ -50,7 +51,7 @@ async def _request(
data: dict = await resp.json(content_type=None)
return data
except ClientError as err:
raise RequestError(f"Error requesting data from {url}: {err}")
raise RequestError(f"Error requesting data from {url}: {err}") from err
finally:
if not use_running_session:
await session.close()
6 changes: 0 additions & 6 deletions pyseventeentrack/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
class SeventeenTrackError(Exception):
"""Define a base error."""

pass


class InvalidTrackingNumberError(SeventeenTrackError):
"""Define an error for an invalid tracking number."""

pass


class RequestError(SeventeenTrackError):
"""Define an error for HTTP request errors."""

pass
7 changes: 3 additions & 4 deletions pyseventeentrack/package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Define a simple structure for a package."""

from datetime import datetime
from typing import Dict, Optional

Expand Down Expand Up @@ -255,10 +256,8 @@
}


@attr.s(
frozen=True
) # pylint: disable=too-few-public-methods,too-many-instance-attributes
class Package:
@attr.s(frozen=True)
class Package: # pylint: disable=too-few-public-methods,too-many-instance-attributes
"""Define a package object."""

tracking_number: str = attr.ib()
Expand Down
5 changes: 3 additions & 2 deletions pyseventeentrack/profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Define interaction with a user profile."""

import json
import logging
from typing import Callable, Coroutine, List, Optional, Union
Expand Down Expand Up @@ -141,10 +142,10 @@ async def add_package(
new_package = next(
p for p in packages if p.tracking_number == tracking_number
)
except StopIteration:
except StopIteration as err:
raise InvalidTrackingNumberError(
f"Recently added package not found by tracking number: {tracking_number}"
)
) from err

_LOGGER.debug("Found internal ID of recently added package: %s", new_package.id)

Expand Down
1 change: 1 addition & 0 deletions pyseventeentrack/track.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Define interaction with an individual package."""

from typing import Callable, Coroutine, List

from .errors import InvalidTrackingNumberError
Expand Down
42 changes: 38 additions & 4 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
aiohttp>=3.8.0
aresponses==1.1.2
aiohttp==3.9.5
aioresponses==0.7.6
aiosignal==1.3.1
aresponses==3.0.0
astroid==3.2.3
attrs==23.2.0
cfgv==3.4.0
coverage==7.5.4
dill==0.3.8
distlib==0.3.8
filelock==3.15.4
frozenlist==1.4.1
identify==2.6.0
idna==3.7
iniconfig==2.0.0
isort==5.13.2
mccabe==0.7.0
multidict==6.0.5
mypy==1.10.1
mypy-extensions==1.0.0
nodeenv==1.9.1
packaging==24.1
platformdirs==4.2.2
pluggy==1.5.0
pre-commit==3.7.1
py==1.11.0
pylint==3.2.5
pytest==8.2.2
pytest-aiohttp==0.3.0
pytest-asyncio==0.23.7
pytest-cov==2.8.1
pytest==6.2.5
pytz>=2021.1
pytz==2024.1
PyYAML==6.0.1
ruff==0.5.1
toml==0.10.2
tomlkit==0.13.0
types-pytz==2024.1.0.20240417
typing_extensions==4.12.2
virtualenv==20.26.3
yarl==1.9.4
30 changes: 30 additions & 0 deletions script/run-in-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env sh
set -eu

# Used in venv activate script.
# Would be an error if undefined.
OSTYPE="${OSTYPE-}"

# Activate pyenv and virtualenv if present, then run the specified command

# pyenv, pyenv-virtualenv
if [ -s .python-version ]; then
PYENV_VERSION=$(head -n 1 .python-version)
export PYENV_VERSION
fi

if [ -n "${VIRTUAL_ENV-}" ] && [ -f "${VIRTUAL_ENV}/bin/activate" ]; then
. "${VIRTUAL_ENV}/bin/activate"
else
# other common virtualenvs
my_path=$(git rev-parse --show-toplevel)

for venv in venv .venv .; do
if [ -f "${my_path}/${venv}/bin/activate" ]; then
. "${my_path}/${venv}/bin/activate"
break
fi
done
fi

exec "$@"
11 changes: 11 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
"""Define common test utilities."""

import os

import pytest
from aresponses import ResponsesMockServer # type: ignore

TEST_EMAIL = "[email protected]"
TEST_PASSWORD = "password"


@pytest.fixture(autouse=True)
async def aresponses(loop):
"""replace aresponses"""
async with ResponsesMockServer(loop=loop) as server:
yield server


def load_fixture(filename):
"""Load a fixture."""
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Define tests for the client object."""

import aiohttp
import pytest

Expand All @@ -16,4 +17,4 @@ async def test_bad_request(aresponses):
with pytest.raises(RequestError):
async with aiohttp.ClientSession() as session:
client = Client(session=session)
await client._request("get", "https://random.domain/no/good")
await client._request("get", "https://random.domain/no/good") # pylint: disable=protected-access
Loading

0 comments on commit 00b880c

Please sign in to comment.