Skip to content

Commit

Permalink
#99 use contextmanager decorator from contextlib instead of verbose c…
Browse files Browse the repository at this point in the history
…lass + some annotations
  • Loading branch information
ephes committed Aug 14, 2023
1 parent 03dde68 commit 42d9e0c
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions tests/management_command_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Generator
from contextlib import contextmanager
from io import BytesIO

import pytest
Expand All @@ -21,32 +23,25 @@ def test_media_backup_with_wrong_django_version(mocker):


class StubStorage:
def __init__(self):
self._files = []
def __init__(self) -> None:
self._files: dict[str, str] = {}

def exists(self, path):
def exists(self, path: str) -> bool:
return path in self._files

def save(self, name, _content):
self._files.append(name)
def save(self, name: str, content: str) -> None:
self._files[name] = content

@staticmethod
def open(name, _mode):
class StubFile:
def __init__(self, file_name):
self.name = file_name

def __enter__(self):
return self

def __exit__(self, *args):
pass

return StubFile(name)

def listdir(self, _path):
def listdir(self, _path: str) -> tuple[list, dict[str, str]]:
return [], self._files

@contextmanager
def open(self, name: str, _mode: str) -> Generator[str, None, None]:
try:
yield self._files[name]
finally:
pass


@pytest.fixture
def stub_storages(settings):
Expand Down

0 comments on commit 42d9e0c

Please sign in to comment.