refactor(access): Serializer Unit Test Suite re-written to Pytest for model Tenant
ref: #942 #899
This commit is contained in:
@ -1,94 +0,0 @@
|
|||||||
import django
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
from rest_framework.exceptions import ValidationError
|
|
||||||
|
|
||||||
from access.serializers.organization import (
|
|
||||||
Tenant,
|
|
||||||
TenantModelSerializer
|
|
||||||
)
|
|
||||||
|
|
||||||
User = django.contrib.auth.get_user_model()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class OrganizationValidationAPI(
|
|
||||||
TestCase,
|
|
||||||
):
|
|
||||||
|
|
||||||
model = Tenant
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpTestData(self):
|
|
||||||
"""Setup Test
|
|
||||||
|
|
||||||
1. Create an org
|
|
||||||
2. Create an item
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.user = User.objects.create(username = 'org_user', password='random password')
|
|
||||||
|
|
||||||
self.valid_data = {
|
|
||||||
'name': 'valid_org_data',
|
|
||||||
'manager': self.user.id
|
|
||||||
}
|
|
||||||
|
|
||||||
self.item = self.model.objects.create(
|
|
||||||
name = 'random title',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_serializer_valid_data(self):
|
|
||||||
"""Serializer Validation Check
|
|
||||||
|
|
||||||
Ensure that if creating and no name is provided a validation error occurs
|
|
||||||
"""
|
|
||||||
|
|
||||||
serializer = TenantModelSerializer(
|
|
||||||
data = self.valid_data
|
|
||||||
)
|
|
||||||
|
|
||||||
assert serializer.is_valid(raise_exception = True)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_serializer_validation_no_name(self):
|
|
||||||
"""Serializer Validation Check
|
|
||||||
|
|
||||||
Ensure that if creating and no name is provided a validation error occurs
|
|
||||||
"""
|
|
||||||
|
|
||||||
data = self.valid_data.copy()
|
|
||||||
|
|
||||||
del data['name']
|
|
||||||
|
|
||||||
with pytest.raises(ValidationError) as err:
|
|
||||||
|
|
||||||
serializer = TenantModelSerializer(
|
|
||||||
data = data
|
|
||||||
)
|
|
||||||
|
|
||||||
serializer.is_valid(raise_exception = True)
|
|
||||||
|
|
||||||
assert err.value.get_codes()['name'][0] == 'required'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_serializer_validation_manager_optional(self):
|
|
||||||
"""Serializer Validation Check
|
|
||||||
|
|
||||||
Ensure that if creating and no name is provided a validation error occurs
|
|
||||||
"""
|
|
||||||
|
|
||||||
data = self.valid_data.copy()
|
|
||||||
|
|
||||||
del data['manager']
|
|
||||||
|
|
||||||
serializer = TenantModelSerializer(
|
|
||||||
data = data
|
|
||||||
)
|
|
||||||
|
|
||||||
assert serializer.is_valid(raise_exception = True)
|
|
@ -1,18 +1,94 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from rest_framework.exceptions import ValidationError
|
||||||
|
|
||||||
from api.tests.unit.test_unit_serializer import (
|
from api.tests.unit.test_unit_serializer import (
|
||||||
SerializerTestCases
|
SerializerTestCases
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from centurion.tests.abstract.mock_view import MockView
|
||||||
class TenantSerializerTestCases(
|
|
||||||
SerializerTestCases
|
|
||||||
):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.model_tenant
|
@pytest.mark.model_tenant
|
||||||
|
class TenantSerializerTestCases(
|
||||||
|
SerializerTestCases
|
||||||
|
):
|
||||||
|
|
||||||
|
@pytest.fixture( scope = 'function' )
|
||||||
|
def created_model(self, django_db_blocker, model, model_kwargs):
|
||||||
|
|
||||||
|
with django_db_blocker.unblock():
|
||||||
|
|
||||||
|
item = model.objects.create( **model_kwargs )
|
||||||
|
|
||||||
|
yield item
|
||||||
|
|
||||||
|
item.delete()
|
||||||
|
|
||||||
|
|
||||||
|
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'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_serializer_validation_manager_optional(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['manager']
|
||||||
|
|
||||||
|
serializer = model_serializer['model'](
|
||||||
|
context = {
|
||||||
|
'request': mock_view.request,
|
||||||
|
'view': mock_view,
|
||||||
|
},
|
||||||
|
data = kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
assert serializer.is_valid(raise_exception = True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.module_access
|
@pytest.mark.module_access
|
||||||
class TenantSerializerPyTest(
|
class TenantSerializerPyTest(
|
||||||
TenantSerializerTestCases
|
TenantSerializerTestCases
|
||||||
|
Reference in New Issue
Block a user