Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
fixes ydaniv#29
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Scott committed Oct 3, 2017
1 parent b00107a commit 85e5314
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
29 changes: 18 additions & 11 deletions rest_assured/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,31 @@ def get_object(self, factory):

return factory.create()

def setUp(self):
"""Generates the main object and user instance if needed.
def get_user_factory(self):
"""Generates the user instance if needed.
The user instance will be created only if the ``user_factory`` attribute is set to the factory class.
If there is an available user instance, that user will be force authenticated.
"""

# create and force authenticate user
user_factory = getattr(self, 'user_factory')
if user_factory:
self.user = user_factory.create()
self.client.force_authenticate(self.user)

# create the object
self.object = self.get_object(self.get_factory_class())
def setUp(self):
"""Generates the user instance if needed and the main object if required."""
self.get_user_factory()

# Avoid creating 2 objects if testing create.
# ```CreateAPITestCaseMixin.get_create_response()```
if self._testMethodName != 'test_create':
self.object = self.get_object(self.get_factory_class())

class ListAPITestCaseMixin(object):

class ListAPITestCaseMixin(object):
"""Adds a list view test to the test case."""

#: When using pagination set this attribute to the name of the property in the response data that holds the result set. Defaults to ``None``.
pagination_results_field = None

Expand Down Expand Up @@ -213,6 +216,8 @@ class CreateAPITestCaseMixin(object):

#: *required*: Dictionary of data to use as the POST request's body.
create_data = None
#: Django model under test
model_class = None
#: The name of the field in the response data for looking up the created object in DB.
response_lookup_field = 'id'

Expand Down Expand Up @@ -257,6 +262,9 @@ def get_lookup_from_response(self, data):
"""
return data.get(self.response_lookup_field)

def get_model(self):
return self.model_class or self.object.__class__

def test_create(self, data=None, **kwargs):
"""Send request to the create view endpoint, verify and return the response.
Expand All @@ -273,7 +281,7 @@ def test_create(self, data=None, **kwargs):

# another sanity check:
# getting the instance from database simply to see that it's found and does not raise any exception
created = self.object.__class__.objects.get(
created = self.get_model().objects.get(
**{self.lookup_field: self.get_lookup_from_response(response.data)})

return response, created
Expand All @@ -288,7 +296,6 @@ def _get_create_name(self):


class DestroyAPITestCaseMixin(object):

"""Adds a destroy view test to the test case."""

def get_destroy_url(self):
Expand Down Expand Up @@ -490,11 +497,11 @@ class ReadRESTAPITestCaseMixin(ListAPITestCaseMixin, DetailAPITestCaseMixin):
pass


class WriteRESTAPITestCaseMixin(CreateAPITestCaseMixin, UpdateAPITestCaseMixin, DestroyAPITestCaseMixin):
class WriteRESTAPITestCaseMixin(UpdateAPITestCaseMixin, DestroyAPITestCaseMixin, CreateAPITestCaseMixin):

"""Adds the write CRUD operations tests to the test case.
Includes: :class:`CreateAPITestCaseMixin`, :class:`UpdateAPITestCaseMixin`, :class:`DestroyAPITestCaseMixin`.
Includes: :class:`UpdateAPITestCaseMixin`, :class:`DestroyAPITestCaseMixin`, :class:`CreateAPITestCaseMixin`.
"""

pass
Expand Down
23 changes: 17 additions & 6 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ def get_case(self, **kwargs):
class MockCreateTestCase(CreateAPITestCaseMixin, mocks.MockTestCase):
base_name = kwargs.pop('base_name', 'stuff')
factory_class = mocks.StuffFactory
model_class = Stuff
create_data = {"name": "moar stuff"}

self.case_class = MockCreateTestCase

return MockCreateTestCase(**kwargs)

def test_get_create_url(self):
instance = self.get_case(methodName='dummy')
instance = self.get_case(methodName='test_create')
assert instance.get_create_url() == '/stuff/'

def test_get_create_data(self):
instance = self.get_case(methodName='dummy')
instance = self.get_case(methodName='test_create')
assert instance.get_create_data() is self.case_class.create_data

def test_get_create_response(self):
instance = self.get_case(methodName='dummy')
instance = self.get_case(methodName='test_create')
assert instance.get_create_response()

def test_get_lookup_from_response(self):
instance = self.get_case(methodName='dummy')
instance = self.get_case(methodName='test_create')
response = instance.get_create_response()
assert instance.get_lookup_from_response(response.data)

def test_test_create(self):
instance = self.get_case(methodName='dummy')
instance = self.get_case(methodName='test_create')
instance.setUp()
response, created = instance.test_create()
assert response
Expand All @@ -52,7 +53,7 @@ def test_test_create(self):
assert response.data['name'] == created.name

def test_test_create_with_hyperlinkedmodelserializer(self):
instance = self.get_case(methodName='dummy', base_name='stuff-linked')
instance = self.get_case(methodName='test_create', base_name='stuff-linked')
instance.setUp()
instance.response_lookup_field = 'name'
instance.lookup_field = 'name'
Expand All @@ -62,3 +63,13 @@ def test_test_create_with_hyperlinkedmodelserializer(self):
assert isinstance(created, Stuff)
assert response.data['name'] == created.name
assert response.data['url']

def test_create_only_creates_one_object(self):
instance = self.get_case(methodName='test_create')
instance.setUp()
response, created = instance.test_create({'name': 'this is unique'})
assert response
assert created
assert isinstance(created, Stuff)
assert not instance.object
assert created.id is not None

0 comments on commit 85e5314

Please sign in to comment.