-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from devind-team/27-добавить-в-базовую-мутацию…
…-автоматическую-обработку-ошибки-validationerror В базовую мутацию была добавлена автоматическая обработка ошибки ValidationError
- Loading branch information
Showing
11 changed files
with
207 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Django's command-line utility for administrative tasks.""" | ||
|
||
import os | ||
import sys | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
|
||
def main() -> None: | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings') | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .schema import BaseMutationTestCase, ErrorFieldTypeTestCase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .mutations import BaseMutationTestCase | ||
from .types import ErrorFieldTypeTestCase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""Тесты вспомогательных мутаций.""" | ||
from collections import OrderedDict | ||
from typing import Any | ||
|
||
import graphene | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
from graphql import ResolveInfo | ||
|
||
from devind_helpers.schema import BaseMutation | ||
|
||
|
||
class TestMutation(BaseMutation): | ||
"""Мутация для тестирования.""" | ||
|
||
class Input: | ||
i = graphene.String(required=True) | ||
|
||
digit = graphene.Int() | ||
|
||
@staticmethod | ||
def mutate_and_get_payload(root: Any, info: ResolveInfo, i: str): | ||
if i.isdigit(): | ||
return TestMutation(digit=int(i)) | ||
raise ValidationError(message={ | ||
'i': ['Ожидалась строка с числом'] | ||
}) | ||
|
||
|
||
class TestMutations(graphene.ObjectType): | ||
"""Мутации для тестирования.""" | ||
|
||
test_mutation = TestMutation.Field(required=True) | ||
|
||
|
||
class BaseMutationTestCase(TestCase): | ||
"""Тесты класса `BaseMutation`.""" | ||
|
||
def setUp(self) -> None: | ||
"""Создание данных для тестирования.""" | ||
self.schema = graphene.Schema(mutation=TestMutations) | ||
|
||
def test_mutate(self) -> None: | ||
"""Тестирование метода `mutate` без ошибок.""" | ||
result = self.schema.execute(self._create_test_mutation_query('5')) | ||
expected_data = OrderedDict() | ||
expected_data['testMutation'] = { | ||
'digit': 5, | ||
'success': True, | ||
'errors': [] | ||
} | ||
self.assertEqual(expected_data, result.data) | ||
|
||
def test_mutate_validation_error(self) -> None: | ||
"""Тестирование метода `mutate` с ошибкой `ValidationError`.""" | ||
result = self.schema.execute(self._create_test_mutation_query('x')) | ||
expected_data = OrderedDict() | ||
expected_data['testMutation'] = { | ||
'digit': None, | ||
'success': False, | ||
'errors': [{ | ||
'field': 'i', | ||
'messages': ['Ожидалась строка с числом'] | ||
}] | ||
} | ||
self.assertEqual(expected_data, result.data) | ||
|
||
@staticmethod | ||
def _create_test_mutation_query(i: str): | ||
"""Создание запроса для тестовой мутации.""" | ||
return f""" | ||
mutation {{ | ||
testMutation(input: {{ i: "{i}" }}) {{ | ||
digit | ||
success | ||
errors {{ | ||
field | ||
messages | ||
}} | ||
}} | ||
}} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Тесты вспомогательных типов.""" | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
|
||
from devind_helpers.schema.types import ErrorFieldType | ||
from devind_helpers.validator import Validator | ||
|
||
|
||
class ErrorFieldTypeTestCase(TestCase): | ||
"""Тесты класса `ErrorFieldType`.""" | ||
|
||
class TestValidator(Validator): | ||
"""Тестовый валидатор.""" | ||
|
||
field = 'required' | ||
|
||
message = { | ||
'field': { | ||
'required': 'message' | ||
} | ||
} | ||
|
||
def test_from_validator(self) -> None: | ||
"""Тестирование метода `from_validator`.""" | ||
validator = ErrorFieldTypeTestCase.TestValidator({}) | ||
validator.validate() | ||
self.assertEqual( | ||
[ErrorFieldType(field='field', messages=['message'])], | ||
ErrorFieldType.from_validator(validator.get_message()) | ||
) | ||
|
||
def test_from_messages_dict(self) -> None: | ||
"""Тестирование метода `from_messages_dict`.""" | ||
validation_error = ValidationError(message={ | ||
'field': ['message1', 'message2'] | ||
}) | ||
self.assertEqual( | ||
[ErrorFieldType(field='field', messages=['message1', 'message2'])], | ||
ErrorFieldType.from_messages_dict(validation_error.message_dict) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Настройки Django для тестов.""" | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': 'sqlite3.db', | ||
} | ||
} | ||
|
||
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' | ||
|
||
INSTALLED_APPS = ( | ||
'tests', | ||
) | ||
|
||
MIDDLEWARE = [] | ||
|
||
USE_TZ = True | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
DOCUMENTS_DIR = '' |