mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-28 04:24:00 +03:00
Refactor code for improved readability and PEP 8 compliance
This commit is contained in:
parent
f56b85b7dd
commit
f8b64c4fbb
|
@ -1,45 +1,44 @@
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.urls import NoReverseMatch
|
from django.urls import NoReverseMatch
|
||||||
|
|
||||||
|
|
||||||
class MockObject:
|
class MockObject:
|
||||||
|
"""
|
||||||
|
A mock object for testing purposes.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self._kwargs = kwargs
|
self._kwargs = kwargs
|
||||||
for key, val in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
setattr(self, key, val)
|
setattr(self, key, value)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
kwargs_str = ', '.join([
|
sorted_kwargs = ', '.join(['%s=%s' % (key, value) for key, value in sorted(self._kwargs.items())])
|
||||||
'%s=%s' % (key, value)
|
return f'<MockObject {sorted_kwargs}>'
|
||||||
for key, value in sorted(self._kwargs.items())
|
|
||||||
])
|
|
||||||
return '<MockObject %s>' % kwargs_str
|
|
||||||
|
|
||||||
|
|
||||||
class MockQueryset:
|
class MockQueryset:
|
||||||
|
"""
|
||||||
|
A mock queryset for testing purposes.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, iterable):
|
def __init__(self, iterable):
|
||||||
self.items = iterable
|
self.items = iterable
|
||||||
|
|
||||||
def __getitem__(self, val):
|
def __getitem__(self, index):
|
||||||
return self.items[val]
|
return self.items[index]
|
||||||
|
|
||||||
def get(self, **lookup):
|
def get(self, **lookup):
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
if all([
|
if all(attrgetter(key.replace('__', '.'))(item) == value for key, value in lookup.items()):
|
||||||
attrgetter(key.replace('__', '.'))(item) == value
|
|
||||||
for key, value in lookup.items()
|
|
||||||
]):
|
|
||||||
return item
|
return item
|
||||||
raise ObjectDoesNotExist()
|
raise ObjectDoesNotExist()
|
||||||
|
|
||||||
|
|
||||||
class BadType:
|
class BadType:
|
||||||
"""
|
"""
|
||||||
When used as a lookup with a `MockQueryset`, these objects
|
Raise a `TypeError` when used as a lookup value with a `MockQueryset`.
|
||||||
will raise a `TypeError`, as occurs in Django when making
|
This behavior mimics Django's queryset lookups with incorrect types.
|
||||||
queryset lookups with an incorrect type for the lookup value.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __eq__(self):
|
def __eq__(self):
|
||||||
|
@ -47,13 +46,20 @@ class BadType:
|
||||||
|
|
||||||
|
|
||||||
def mock_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
def mock_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
||||||
|
"""
|
||||||
|
Mocked implementation of Django's reverse function for testing.
|
||||||
|
"""
|
||||||
|
|
||||||
args = args or []
|
args = args or []
|
||||||
kwargs = kwargs or {}
|
kwargs = kwargs or {}
|
||||||
value = (args + list(kwargs.values()) + ['-'])[0]
|
first_value = (args + list(kwargs.values()) + ['-'])[0]
|
||||||
prefix = 'http://example.org' if request else ''
|
prefix = 'http://example.org' if request else ''
|
||||||
suffix = ('.' + format) if (format is not None) else ''
|
suffix = f'.{format}' if format is not None else ''
|
||||||
return '%s/%s/%s%s/' % (prefix, view_name, value, suffix)
|
return f'{prefix}/{view_name}/{first_value}{suffix}/'
|
||||||
|
|
||||||
|
|
||||||
def fail_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
def fail_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
||||||
|
"""
|
||||||
|
Mocked implementation of Django's reverse function to raise a NoReverseMatch for testing.
|
||||||
|
"""
|
||||||
raise NoReverseMatch()
|
raise NoReverseMatch()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user