Skip to content

Commit

Permalink
Add types to tests (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jun 27, 2024
1 parent 288d154 commit e411374
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: "Run checks for python ${{ matrix.python-version }}"
run: |
poetry run flake8 .
poetry run mypy docker_image_size_limit
poetry run mypy .
# We need this specific image for tests:
poetry run docker pull python:3.6.6-alpine
poetry run pytest
Expand Down
337 changes: 170 additions & 167 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ pytest-randomly = "^3.12"
pytest-timeout = "^2.3"
pytest = "^8.1"

mypy = "^1.9"
mypy = "^1.10"
types-humanfriendly = "^10.0"
types-docker = "^7.1"

wemake-python-styleguide = ">=0.18,<0.20"
flake8-pytest-style = "^1.6"
nitpick = ">=0.33,<0.36"
wemake-python-styleguide = "^0.19"
flake8-pytest-style = "^2.0"
nitpick = "^0.35"


[build-system]
Expand Down
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@


@pytest.fixture(scope='session')
def docker_client():
def docker_client() -> docker.DockerClient:
"""Creates docker client suitable for tests."""
return docker.from_env()


@pytest.fixture(scope='session')
def image_name():
def image_name() -> str:
"""Returns image name that is used in tests."""
return 'python:3.6.6-alpine'


@pytest.fixture(scope='session', autouse=True)
def docker_pull_image(docker_client, image_name): # noqa: WPS442
def _docker_pull_image(
docker_client: docker.DockerClient, image_name: str, # noqa: WPS442
) -> None:
"""Pulls docker image from Docker Hub."""
return docker_client.images.pull(image_name)
docker_client.images.pull(image_name)
22 changes: 18 additions & 4 deletions tests/test_check_size.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import docker

from docker_image_size_limit import check_image_size


def test_check_size_binary_overflow(docker_client, image_name):
def test_check_size_binary_overflow(
docker_client: docker.DockerClient,
image_name: str,
) -> None:
"""Checks size with binary limit."""
overflow = check_image_size(docker_client, image_name, '1024')

assert overflow > 0


def test_check_size_binary_correct(docker_client, image_name):
def test_check_size_binary_correct(
docker_client: docker.DockerClient,
image_name: str,
) -> None:
"""Checks size with binary limit."""
overflow = check_image_size(docker_client, image_name, '1073741824')

assert overflow < 0


def test_check_size_human_overflow(docker_client, image_name):
def test_check_size_human_overflow(
docker_client: docker.DockerClient,
image_name: str,
) -> None:
"""Checks size with human readable limit."""
overflow = check_image_size(docker_client, image_name, '10MB')

assert overflow > 0


def test_check_size_human_correct(docker_client, image_name):
def test_check_size_human_correct(
docker_client: docker.DockerClient,
image_name: str,
) -> None:
"""Checks size with human readable limit."""
overflow = check_image_size(docker_client, image_name, '10 GiB')

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess


def test_disl_exceeds_limit(docker_client, image_name):
def test_disl_exceeds_limit(image_name: str) -> None:
"""Runs `disl` command with met limit."""
process = subprocess.Popen(
[
Expand All @@ -21,7 +21,7 @@ def test_disl_exceeds_limit(docker_client, image_name):
assert image_name in output


def test_disl_normal(docker_client, image_name):
def test_disl_normal(image_name: str) -> None:
"""Runs `disl` command with unmet limit."""
process = subprocess.Popen(
[
Expand Down
14 changes: 11 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import sys

import pytest

from docker_image_size_limit import main, sys
from docker_image_size_limit import main


def test_main_overflow(docker_client, image_name, monkeypatch):
def test_main_overflow(
image_name: str,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Checks size with binary limit."""
monkeypatch.setattr(sys, 'argv', ['', image_name, '1 MB'])
with pytest.raises(SystemExit) as exit_value:
main()
assert exit_value.value.code == 1 # noqa: WPS441


def test_main_correct(docker_client, image_name, monkeypatch):
def test_main_correct(
image_name: str,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Checks size with binary limit."""
monkeypatch.setattr(sys, 'argv', ['', image_name, '1 GB'])
with pytest.raises(SystemExit) as exit_value:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess


def test_disl_version():
def test_disl_version() -> None:
"""Runs `disl` command with `--version` option."""
process = subprocess.Popen(
[
Expand Down

0 comments on commit e411374

Please sign in to comment.