Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
Переработан генерацию данных в тестах
Browse files Browse the repository at this point in the history
Используется mixer
Обновлена логика фикстур
Добавлен seed для рандома в pytest
  • Loading branch information
KozyrevAndrey committed Sep 23, 2023
1 parent 1d89792 commit 68b540f
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 35 deletions.
19 changes: 19 additions & 0 deletions data/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
}
],
"profile": {
"name": "typicode"
}
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ services:
retries: 5
start_period: 30s

json-server:
image: "vimagick/json-server"
command: -H 0.0.0.0 -p 5200 --watch db.json
container_name: "json_server"
ports:
- "5200:5200"
volumes:
- ./data:/data
restart: always
networks:
- webnet


# This task is an example of how to extend existing ones:
# some_worker:
# <<: *web
Expand Down
152 changes: 149 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ dennis = "^1.1"
dump-env = "^1.3"
ipython = "^8.15"
import-linter = "^1.11"
mixer = "^7.2.2"
json-server = "^0.1.3"

[tool.poetry.group.docs]
optional = true
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ filterwarnings =
ignore::DeprecationWarning:password_reset.*:
ignore::DeprecationWarning:pytest_freezegun:

markers =
slow


[coverage:run]
# Coverage configuration:
Expand Down
21 changes: 21 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@
2. https://docs.pytest.org/en/latest/doctest.html
"""

import pytest
import random


pytest_plugins = [
# Should be the first custom one:
'plugins.django_settings',
'plugins.identity.user',
'plugins.pictures.pictures'
# TODO: add your own plugins here!
]


def pytest_configure(config: pytest.Config) -> None:
seed_value = config.getoption("randomly_seed")
# рандомный seed:
default_seed = random.Random().getrandbits(32)

if seed_value == 'last':
seed = config.cache.get('randomly_seed', default_seed)
elif seed_value == 'default':
seed = default_seed
else:
seed = seed_value

if hasattr(config, 'cache'):
config.cache.set('randomly_seed', seed)
config.option.randomly_seed = seed
1 change: 1 addition & 0 deletions tests/plugins/django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ def cache(settings: LazySettings) -> BaseCache:
# Clearing cache:
caches[test_cache].clear()
return caches[test_cache]

2 changes: 1 addition & 1 deletion tests/plugins/identity/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import datetime as dt
from typing import Protocol, Unpack, final, TypedDict
from typing import final, TypedDict

from server.apps.identity.models import User, _UserManager

Expand Down
10 changes: 5 additions & 5 deletions tests/plugins/pictures/pictures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import TypedDict
import pytest
from mixer.backend.django import mixer

from server.apps.identity.models import User
from server.apps.pictures.models import FavouritePicture
Expand All @@ -22,13 +23,12 @@ def favourite_picture_data(create_user) -> FavouritePictureData:


@pytest.fixture
def picture_factory():
def picture_factory(create_user):
def factory(
favourite_picture: FavouritePictureData
cycle_number: int = 1
) -> FavouritePicture:
return FavouritePicture.objects.create(
**favourite_picture
)
user = create_user
return mixer.cycle(cycle_number).blend(FavouritePicture, user=user)
return factory


Expand Down
Loading

0 comments on commit 68b540f

Please sign in to comment.