2023-04-08 09:27:14 +03:00
|
|
|
from operator import attrgetter
|
2023-10-24 09:42:39 +03:00
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2017-11-10 11:41:03 +03:00
|
|
|
from django.urls import NoReverseMatch
|
2014-09-12 20:03:42 +04:00
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockObject:
|
2023-10-24 08:47:49 +03:00
|
|
|
"""
|
|
|
|
A mock object for testing purposes.
|
|
|
|
"""
|
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._kwargs = kwargs
|
2023-10-24 08:47:49 +03:00
|
|
|
for key, value in kwargs.items():
|
|
|
|
setattr(self, key, value)
|
2014-09-12 20:03:42 +04:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-10-24 08:47:49 +03:00
|
|
|
sorted_kwargs = ', '.join(['%s=%s' % (key, value) for key, value in sorted(self._kwargs.items())])
|
|
|
|
return f'<MockObject {sorted_kwargs}>'
|
2014-09-12 20:03:42 +04:00
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockQueryset:
|
2023-10-24 08:47:49 +03:00
|
|
|
"""
|
|
|
|
A mock queryset for testing purposes.
|
|
|
|
"""
|
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def __init__(self, iterable):
|
|
|
|
self.items = iterable
|
|
|
|
|
2023-10-24 08:47:49 +03:00
|
|
|
def __getitem__(self, index):
|
|
|
|
return self.items[index]
|
2017-05-25 02:46:18 +03:00
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def get(self, **lookup):
|
|
|
|
for item in self.items:
|
2023-10-24 08:47:49 +03:00
|
|
|
if all(attrgetter(key.replace('__', '.'))(item) == value for key, value in lookup.items()):
|
2014-09-12 20:03:42 +04:00
|
|
|
return item
|
|
|
|
raise ObjectDoesNotExist()
|
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class BadType:
|
2014-09-12 20:03:42 +04:00
|
|
|
"""
|
2023-10-24 08:47:49 +03:00
|
|
|
Raise a `TypeError` when used as a lookup value with a `MockQueryset`.
|
|
|
|
This behavior mimics Django's queryset lookups with incorrect types.
|
2014-09-12 20:03:42 +04:00
|
|
|
"""
|
2023-04-08 09:27:14 +03:00
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def __eq__(self):
|
|
|
|
raise TypeError()
|
|
|
|
|
|
|
|
|
|
|
|
def mock_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
2023-10-24 08:47:49 +03:00
|
|
|
"""
|
|
|
|
Mocked implementation of Django's reverse function for testing.
|
|
|
|
"""
|
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
args = args or []
|
|
|
|
kwargs = kwargs or {}
|
2023-10-24 08:47:49 +03:00
|
|
|
first_value = (args + list(kwargs.values()) + ['-'])[0]
|
2014-09-12 20:03:42 +04:00
|
|
|
prefix = 'http://example.org' if request else ''
|
2023-10-24 08:47:49 +03:00
|
|
|
suffix = f'.{format}' if format is not None else ''
|
|
|
|
return f'{prefix}/{view_name}/{first_value}{suffix}/'
|
2014-09-12 20:03:42 +04:00
|
|
|
|
|
|
|
|
|
|
|
def fail_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
2023-10-24 08:47:49 +03:00
|
|
|
"""
|
|
|
|
Mocked implementation of Django's reverse function to raise a NoReverseMatch for testing.
|
|
|
|
"""
|
2014-09-12 20:03:42 +04:00
|
|
|
raise NoReverseMatch()
|