Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShishckovA committed Nov 1, 2023
1 parent 71fa770 commit 9e4daf7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
24 changes: 19 additions & 5 deletions back-end/src/common/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
# pylint: disable=unused-argument,redefined-outer-name,import-outside-toplevel,invalid-name,too-many-arguments,redefined-builtin
import pytest

from auth.models import User
from common.models.milspecialties import Milspecialty
from common.models.subjects import Subject

SUPERUSER_EMAIL = "superuserfortests@mail.com"
SUPERUSER_PASSWORD = "superuserpasswordfortests"

@pytest.fixture
def create_milspeciality():
def call_me(title: str, code: str, available_for=None):
if available_for is None:
available_for = ["MO"]
milspeciality, _ = Milspecialty.objects.get_or_create(
title=title,
code=code,
available_for=available_for,
)
return milspeciality

return call_me


@pytest.fixture
def create_subject():

def call_me(
user: User,
milspecialty: Milspecialty,
title: str = "Тактическая подготовка 2",
annotation: str = None,
):
if annotation is None:
annotation = f"Пример анноттации для {title}"
subject, _ = Subject.objects.get_or_create(
milspecialty=milspecialty,
title=title,
annotation=annotation,
user=user,
Expand All @@ -41,15 +54,16 @@ def create_test_user(email: str = "test@email.ru", password: str = "1234"):

@pytest.fixture
def get_new_subject_data():

def call_me(
milspecialty: int,
title: str = "Тактическая подготовка 2",
annotation: str = None,
):
if annotation is None:
annotation = f"Пример анноттации для {title}"
user = create_test_user()
data = {
"milspecialty": milspecialty,
"title": title,
"annotation": annotation,
"user": user,
Expand Down
41 changes: 31 additions & 10 deletions back-end/src/common/tests/test_subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ def unpack_subject(subject: Subject):
return {
"id": subject.id,
"title": subject.title,
"annotation": subject.annotation
"annotation": subject.annotation,
"milspecialty": subject.milspecialty.id,
}


Expand All @@ -20,25 +21,39 @@ def test_trailing_slash_redirect(su_client):


@pytest.mark.django_db
def test_get_subject_by_id(su_client, get_new_subject_data, create_subject):
subj_data = get_new_subject_data(title="Строевая подготовка 2")
def test_get_subject_by_id(
su_client, create_milspeciality, get_new_subject_data, create_subject
):
milspeciality = create_milspeciality(
title="Математическое обеспечения комплексов ПРО", code="453100"
)
subj_data = get_new_subject_data(
title="Строевая подготовка", milspecialty=milspeciality
)
subject = create_subject(**subj_data)
subj_data = unpack_subject(subject)
subject_get_response = su_client.get(f"/api/lms/subjects/{subject.id}/")
assert subject_get_response.status_code == 200

subject_get_response = subject_get_response.json()
print(subject_get_response, subj_data)
assert subject_get_response == subj_data


@pytest.mark.django_db
def test_get_subjects_by_title(su_client, get_new_subject_data, create_subject):
subj_data = get_new_subject_data(title="Тактическая подготовка 2")
def test_get_subjects_by_title(
su_client, create_milspeciality, get_new_subject_data, create_subject
):
milspeciality = create_milspeciality(
title="Математическое обеспечения комплексов ПРО", code="453100"
)
subj_data = get_new_subject_data(
title="Тактическая подготовка", milspecialty=milspeciality
)
subject = create_subject(**subj_data)
subj_data = unpack_subject(subject)

subject_response_get = su_client.get(
f"/api/lms/subjects/?title={subject.title}")
subject_response_get = su_client.get(f"/api/lms/subjects/?title={subject.title}")
assert subject_response_get.status_code == 200

subject_response_get = subject_response_get.json()
Expand All @@ -49,9 +64,15 @@ def test_get_subjects_by_title(su_client, get_new_subject_data, create_subject):


@pytest.mark.django_db
def test_get_subjects_by_search(su_client, get_new_subject_data,
create_subject):
subj_data = get_new_subject_data(title="Тактико-специальная подготовка 2")
def test_get_subjects_by_search(
su_client, create_milspeciality, get_new_subject_data, create_subject
):
milspeciality = create_milspeciality(
title="Математическое обеспечения комплексов ПРО", code="453100"
)
subj_data = get_new_subject_data(
title="Тактико-специальная подготовка 2", milspecialty=milspeciality
)
subject = create_subject(**subj_data)
word = subject.title.split()[1]

Expand Down

0 comments on commit 9e4daf7

Please sign in to comment.