Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code for improved readability and PEP 8 compliance #9147

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 27 additions & 21 deletions tests/utils.py
@@ -1,59 +1,65 @@
from operator import attrgetter

from django.core.exceptions import ObjectDoesNotExist
from django.urls import NoReverseMatch


class MockObject:
"""
A mock object for testing purposes.
"""

def __init__(self, **kwargs):
self._kwargs = kwargs
for key, val in kwargs.items():
setattr(self, key, val)
for key, value in kwargs.items():
setattr(self, key, value)

def __str__(self):
kwargs_str = ', '.join([
'%s=%s' % (key, value)
for key, value in sorted(self._kwargs.items())
])
return '<MockObject %s>' % kwargs_str
sorted_kwargs = ', '.join(['%s=%s' % (key, value) for key, value in sorted(self._kwargs.items())])
return f'<MockObject {sorted_kwargs}>'


class MockQueryset:
"""
A mock queryset for testing purposes.
"""

def __init__(self, iterable):
self.items = iterable

def __getitem__(self, val):
return self.items[val]
def __getitem__(self, index):
return self.items[index]

def get(self, **lookup):
for item in self.items:
if all([
attrgetter(key.replace('__', '.'))(item) == value
for key, value in lookup.items()
]):
if all(attrgetter(key.replace('__', '.'))(item) == value for key, value in lookup.items()):
return item
raise ObjectDoesNotExist()


class BadType:
"""
When used as a lookup with a `MockQueryset`, these objects
will raise a `TypeError`, as occurs in Django when making
queryset lookups with an incorrect type for the lookup value.
Raise a `TypeError` when used as a lookup value with a `MockQueryset`.
This behavior mimics Django's queryset lookups with incorrect types.
"""

def __eq__(self):
raise TypeError()


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 []
kwargs = kwargs or {}
value = (args + list(kwargs.values()) + ['-'])[0]
first_value = (args + list(kwargs.values()) + ['-'])[0]
prefix = 'http://example.org' if request else ''
suffix = ('.' + format) if (format is not None) else ''
return '%s/%s/%s%s/' % (prefix, view_name, value, suffix)
suffix = f'.{format}' if format is not None else ''
return f'{prefix}/{view_name}/{first_value}{suffix}/'


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()