Skip to content

Commit

Permalink
CHANGED: url lookup by base view name
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Bredehöft committed Jul 8, 2015
1 parent fd02a01 commit ff33c44
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
35 changes: 22 additions & 13 deletions drf_tools/test/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from datetime import datetime, date
from datetime import datetime, date, time
import json
import random

Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -288,7 +295,6 @@ def testPOST(self):


class ReadModelViewSetTest(BaseModelViewSetTest):

def _getAllowedListMethods(self):
return super(ReadModelViewSetTest, self)._getAllowedListMethods() + ["GET", "HEAD"]

Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion drf_tools/test/utils.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion tests/testproject/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 1 addition & 3 deletions tests/testproject/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit ff33c44

Please sign in to comment.