diff --git a/drf_tools/test/base.py b/drf_tools/test/base.py index fb96e54..e952aea 100644 --- a/drf_tools/test/base.py +++ b/drf_tools/test/base.py @@ -1,5 +1,5 @@ from collections import defaultdict -from datetime import datetime, date +from datetime import datetime, date, time import json import random @@ -37,7 +37,7 @@ class BaseRestTest(TestCase): def setUp(self): logger = logging.getLogger() logger.setLevel(logging.DEBUG) - self.client.get("/") # hack: has to be called initial for router registration + self.client.get("/api") # hack: has to be called initial for router registration def _assertDatetimesEqual(self, datetime1, datetime2): if datetime1 and isinstance(datetime1, datetime): @@ -53,6 +53,13 @@ def _assertDatesEqual(self, date1, date2): date2 = date2.strftime('%Y-%m-%d') self.assertEqual(date1, date2) + def _assertTimesEqual(self, time1, time2): + if time1 and isinstance(time1, time): + time1 = time1.strftime("%H:%M:%S") + if time2 and isinstance(time2, time): + time2 = time2.strftime("%H:%M:%S") + self.assertEqual(time1, time2) + def _doGETDetails(self, modelObj, queryParams=None, **headers): resp = self.client.get(self._getRelativeDetailURI(modelObj=modelObj), queryParams, **headers) self.assertEqual(resp[self._CONTENT_TYPE_HEADER_NAME], HAL_JSON_MEDIA_TYPE) @@ -127,10 +134,16 @@ def _getRelativeDetailURI(self, modelObj): def _getAbsoluteListURI(self, modelClass, parentLookups=None): return self._TESTSERVER_BASE_URL + self._getRelativeListURI(modelClass, parentLookups) + def _getAbsoluteListURIByBaseViewName(self, baseViewName, parentLookups=None): + return self._TESTSERVER_BASE_URL + self._getRelativeListURIByBaseViewName(baseViewName, parentLookups) + def _getRelativeListURI(self, modelClass, parentLookups=None): - parent_lookups = drf_nested_routing.get_parent_query_lookups_by_class(modelClass) + return self._getRelativeListURIByBaseViewName(modelClass.__name__.lower(), parentLookups) + + def _getRelativeListURIByBaseViewName(self, baseViewName, parentLookups=None): + parent_lookups = drf_nested_routing.get_parent_query_lookups_by_view(baseViewName) if parent_lookups and not parentLookups: - raise ValueError("Please specify parent lookups for '{}'".format(modelClass.__name__)) + raise ValueError("Please specify parent lookups for '{}'".format(baseViewName)) composedParentLookups = dict() if parent_lookups and parentLookups: @@ -140,7 +153,7 @@ def _getRelativeListURI(self, modelClass, parentLookups=None): continue composedParentLookups[drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX + lookup] = lookupId - return reverse(modelClass.__name__.lower() + '-list', kwargs=composedParentLookups) + return reverse(baseViewName + '-list', kwargs=composedParentLookups) def _assertLinksAndModelListEqual(self, linksList, modelList): if linksList is not None: @@ -151,13 +164,7 @@ def _assertLinksAndModelListEqual(self, linksList, modelList): self.assertEqual(0, len(modelList)) def __contentToJson(self, content): - cleanedContent = dict() - for attr, value in content.items(): - cleanedValue = value - if not isinstance(value, dict) and value is not None: - cleanedValue = str(value) - cleanedContent[attr] = cleanedValue - return json.dumps(cleanedContent) + return json.dumps(content) def _buildContent(self, stateAttrs, linkAttrs=None, embeddedAttrs=None): content = dict(stateAttrs) @@ -288,7 +295,6 @@ def testPOST(self): class ReadModelViewSetTest(BaseModelViewSetTest): - def _getAllowedListMethods(self): return super(ReadModelViewSetTest, self)._getAllowedListMethods() + ["GET", "HEAD"] @@ -311,10 +317,13 @@ def testGETList(self): modelCount = len(modelList) queryParams = {self._PAGE_SIZE_FIELD_NAME: modelCount} modelsByUrl = {self._getAbsoluteDetailURI(model): model for model in modelList} + print(modelsByUrl) wildCardedParentLookups = self._getWildcardedParentLookups(self._getModelClass()) + print(wildCardedParentLookups) resp = self._doGETList(self._getModelClass(), queryParams, wildCardedParentLookups) self.assertEqual(200, resp.status_code, resp.content) stateAttrs, linkAttrs, embeddedAttrs = self._splitContent(resp.data) + print(resp.data) self.assertEqual(stateAttrs[self._COUNT_FIELD_NAME], modelCount) self.assertEqual(stateAttrs[self._PAGE_SIZE_FIELD_NAME], modelCount) self.assertEqual(linkAttrs[self._SELF_FIELD_NAME], "{}?{}={}".format( diff --git a/drf_tools/test/utils.py b/drf_tools/test/utils.py index f629ce9..6cfb987 100644 --- a/drf_tools/test/utils.py +++ b/drf_tools/test/utils.py @@ -1,7 +1,7 @@ from functools import wraps -def withDebug(func): +def with_debug(func): """Switch on DEBUG during a test (disabled by default). Useful for query logging.""" @wraps(func) diff --git a/requirements.txt b/requirements.txt index e13c891..29b763c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,10 @@ Django==1.8.2 -django-tooling==0.9.0 +django-tooling==0.9.2 djangorestframework==3.1.3 drf-nested-fields==0.9.3 drf-hal-json==0.9.0 drf-enum-field==0.9.1 -drf-nested-routing==0.9.0 +drf-nested-routing==0.9.1 django-filter==0.10.0 openpyxl==2.2.5 chardet==2.3.0 diff --git a/setup.py b/setup.py index 78c37c6..0071dc8 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='drf-tools', - version="0.9.6", + version="0.9.10", url='https://github.com/seebass/drf-tools', license='MIT', description='Multiple extensions and test utilities for Django REST Framework 3', diff --git a/tests/testproject/tests.py b/tests/testproject/tests.py index d3548a8..54d1e4d 100644 --- a/tests/testproject/tests.py +++ b/tests/testproject/tests.py @@ -143,8 +143,9 @@ def _getIncludeFields(self): class ApiRootTest(BaseRestTest): def testGetApiRoot(self): resp = self.client.get("/") + print(resp.data) self.assertEqual(200, resp.status_code) self.assertEqual(1, len(resp.data)) self.assertEqual(2, len(resp.data[drf_hal_json.LINKS_FIELD_NAME])) self.assertTrue(len(resp.data[drf_hal_json.LINKS_FIELD_NAME]['viewsets']) > 0) - self.assertTrue(len(resp.data[drf_hal_json.LINKS_FIELD_NAME]['views']) > 0) + self.assertTrue(len(resp.data[drf_hal_json.LINKS_FIELD_NAME]['views']) == 0) diff --git a/tests/testproject/urls.py b/tests/testproject/urls.py index 6ac0ce3..64c69b3 100644 --- a/tests/testproject/urls.py +++ b/tests/testproject/urls.py @@ -3,9 +3,7 @@ from django.contrib import admin from drf_tools.routers import NestedRouterWithExtendedRootView -from tests.testproject.models import BusinessModel -from .views import TestResourceViewSet, RelatedResource1ViewSet, RelatedResource2ViewSet, PermissionModelViewSet, \ - BusinessModelViewSet +from .views import TestResourceViewSet, RelatedResource1ViewSet, RelatedResource2ViewSet admin.autodiscover()