refactor(itim): Serializer Unit Test Suite re-written to Pytest for model Port
ref: #932 #930
This commit is contained in:
@ -1,111 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView, User
|
||||
|
||||
from itim.serializers.port import Port, PortModelSerializer
|
||||
|
||||
|
||||
|
||||
class PortValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = Port
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
# 2. Create an item
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
self.organization = organization
|
||||
|
||||
# self.item = self.model.objects.create(
|
||||
# organization=organization,
|
||||
# number = 'os name',
|
||||
# )
|
||||
|
||||
self.mock_view = MockView( user = self.user )
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_can_create(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that a valid item has no validation errors
|
||||
"""
|
||||
|
||||
serializer = PortModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"number": 80,
|
||||
"protocol": Port.Protocol.TCP
|
||||
})
|
||||
|
||||
assert serializer.is_valid(raise_exception = True)
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_number(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no number is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = PortModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
# "number": 80,
|
||||
"protocol": Port.Protocol.TCP
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['number'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_protocol(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no protocol is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = PortModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"number": 80,
|
||||
# "protocol": Port.Protocol.TCP
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['protocol'][0] == 'required'
|
@ -17,3 +17,9 @@ def model_kwargs(request, kwargs_port):
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_port):
|
||||
|
||||
yield serializer_port
|
||||
|
98
app/itim/tests/unit/port/test_unit_port_serializer.py
Normal file
98
app/itim/tests/unit/port/test_unit_port_serializer.py
Normal file
@ -0,0 +1,98 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from api.tests.unit.test_unit_serializer import (
|
||||
SerializerTestCases
|
||||
)
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_port
|
||||
class PortSerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
|
||||
|
||||
def test_serializer_validation_no_number(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no number is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['number']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['number'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_protocol(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no protocol is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['protocol']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['protocol'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
class PortSerializerInheritedCases(
|
||||
PortSerializerTestCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_itim
|
||||
class PortSerializerPyTest(
|
||||
PortSerializerTestCases
|
||||
):
|
||||
pass
|
1
app/tests/fixtures/__init__.py
vendored
1
app/tests/fixtures/__init__.py
vendored
@ -230,6 +230,7 @@ from .model_person import (
|
||||
from .model_port import (
|
||||
kwargs_port,
|
||||
model_port,
|
||||
serializer_port,
|
||||
)
|
||||
|
||||
from .model_project import (
|
||||
|
18
app/tests/fixtures/model_port.py
vendored
18
app/tests/fixtures/model_port.py
vendored
@ -1,8 +1,12 @@
|
||||
import datetime
|
||||
import pytest
|
||||
import random
|
||||
|
||||
from itim.models.services import Port
|
||||
from itim.serializers.port import (
|
||||
PortBaseSerializer,
|
||||
PortModelSerializer,
|
||||
PortViewSerializer
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -19,8 +23,20 @@ def kwargs_port(kwargs_centurionmodel):
|
||||
|
||||
kwargs = {
|
||||
**kwargs_centurionmodel.copy(),
|
||||
'description': 'a descriptive str',
|
||||
'number': random_port,
|
||||
'protocol': Port.Protocol.TCP
|
||||
}
|
||||
|
||||
yield kwargs.copy()
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def serializer_port():
|
||||
|
||||
yield {
|
||||
'base': PortBaseSerializer,
|
||||
'model': PortModelSerializer,
|
||||
'view': PortViewSerializer
|
||||
}
|
||||
|
Reference in New Issue
Block a user