refactor(itam): Serializer Unit Test Suite re-written to Pytest for model Software
ref: #925 #924
This commit is contained in:
@ -1,63 +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 itam.serializers.software import Software, SoftwareModelSerializer
|
||||
|
||||
|
||||
|
||||
class SoftwareValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = Software
|
||||
|
||||
@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,
|
||||
name = 'os name',
|
||||
)
|
||||
|
||||
self.mock_view = MockView( user = self.user )
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = SoftwareModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
@ -17,3 +17,9 @@ def model_kwargs(request, kwargs_software):
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_software):
|
||||
|
||||
yield serializer_software
|
||||
|
@ -0,0 +1,66 @@
|
||||
import pytest
|
||||
|
||||
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_software
|
||||
class SoftwareSerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self,
|
||||
kwargs_api_create, model, model_serializer, request_user,
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['name']
|
||||
|
||||
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()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class SoftwareSerializerInheritedCases(
|
||||
SoftwareSerializerTestCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_itam
|
||||
class SoftwareSerializerPyTest(
|
||||
SoftwareSerializerTestCases
|
||||
):
|
||||
pass
|
1
app/tests/fixtures/__init__.py
vendored
1
app/tests/fixtures/__init__.py
vendored
@ -274,6 +274,7 @@ from .model_slmticket import (
|
||||
from .model_software import (
|
||||
kwargs_software,
|
||||
model_software,
|
||||
serializer_software
|
||||
)
|
||||
|
||||
from .model_softwarecategory import (
|
||||
|
34
app/tests/fixtures/model_software.py
vendored
34
app/tests/fixtures/model_software.py
vendored
@ -2,6 +2,11 @@ import datetime
|
||||
import pytest
|
||||
|
||||
from itam.models.software import Software
|
||||
from itam.serializers.software import (
|
||||
SoftwareBaseSerializer,
|
||||
SoftwareModelSerializer,
|
||||
SoftwareViewSerializer,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -12,13 +17,40 @@ def model_software(request):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def kwargs_software(kwargs_centurionmodel):
|
||||
def kwargs_software(kwargs_centurionmodel, django_db_blocker,
|
||||
model_manufacturer, kwargs_manufacturer,
|
||||
model_softwarecategory, kwargs_softwarecategory
|
||||
):
|
||||
|
||||
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
publisher = model_manufacturer.objects.create( **kwargs_manufacturer )
|
||||
|
||||
category = model_softwarecategory.objects.create( **kwargs_softwarecategory )
|
||||
|
||||
kwargs = {
|
||||
**kwargs_centurionmodel.copy(),
|
||||
'publisher': publisher,
|
||||
'name': 'software_' + random_str,
|
||||
'category': category,
|
||||
}
|
||||
|
||||
yield kwargs.copy()
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
publisher.delete()
|
||||
|
||||
category.delete()
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def serializer_software():
|
||||
|
||||
yield {
|
||||
'base': SoftwareBaseSerializer,
|
||||
'model': SoftwareModelSerializer,
|
||||
'view': SoftwareViewSerializer
|
||||
}
|
||||
|
Reference in New Issue
Block a user