2023-04-08 09:27:14 +03:00
|
|
|
from operator import attrgetter
|
|
|
|
|
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:
|
2014-09-12 20:03:42 +04:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._kwargs = kwargs
|
|
|
|
for key, val in kwargs.items():
|
|
|
|
setattr(self, key, val)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
kwargs_str = ', '.join([
|
|
|
|
'%s=%s' % (key, value)
|
|
|
|
for key, value in sorted(self._kwargs.items())
|
|
|
|
])
|
|
|
|
return '<MockObject %s>' % kwargs_str
|
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockQueryset:
|
2014-09-12 20:03:42 +04:00
|
|
|
def __init__(self, iterable):
|
|
|
|
self.items = iterable
|
|
|
|
|
2017-05-25 02:46:18 +03:00
|
|
|
def __getitem__(self, val):
|
|
|
|
return self.items[val]
|
|
|
|
|
2014-09-12 20:03:42 +04:00
|
|
|
def get(self, **lookup):
|
2024-01-05 14:52:31 +03:00
|
|
|
result = self.filter(**lookup).all()
|
|
|
|
if len(result) > 0:
|
|
|
|
return result[0]
|
|
|
|
raise ObjectDoesNotExist()
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return list(self.items)
|
|
|
|
|
|
|
|
def filter(self, **lookup):
|
2024-01-05 15:19:31 +03:00
|
|
|
return MockQueryset([
|
2024-01-05 14:52:31 +03:00
|
|
|
item
|
|
|
|
for item in self.items
|
2014-09-12 20:03:42 +04:00
|
|
|
if all([
|
2024-01-05 14:52:31 +03:00
|
|
|
attrgetter(key.replace("__in", "").replace('__', '.'))(item) in value
|
|
|
|
if key.endswith("__in")
|
|
|
|
else attrgetter(key.replace('__', '.'))(item) == value
|
2014-09-12 20:03:42 +04:00
|
|
|
for key, value in lookup.items()
|
2024-01-05 14:52:31 +03:00
|
|
|
])
|
2024-01-05 15:19:31 +03:00
|
|
|
])
|
|
|
|
|
|
|
|
def annotate(self, **kwargs):
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
for item in self.items:
|
|
|
|
setattr(item, key, attrgetter(value.name.replace('__', '.'))(item))
|
|
|
|
return self
|
2014-09-12 20:03:42 +04:00
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class BadType:
|
2014-09-12 20:03:42 +04:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
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):
|
|
|
|
args = args or []
|
|
|
|
kwargs = kwargs or {}
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
def fail_reverse(view_name, args=None, kwargs=None, request=None, format=None):
|
|
|
|
raise NoReverseMatch()
|