Skip to content

Commit

Permalink
Merge pull request #32 from cfe-lab/model-type
Browse files Browse the repository at this point in the history
Raise specific DoesNotExist exception for the model.
  • Loading branch information
stphivos authored Feb 6, 2017
2 parents 4762263 + 13d10dc commit c153802
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
8 changes: 8 additions & 0 deletions django_mock_queries/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
COMPARISON_LT = 'lt'
COMPARISON_LTE = 'lte'
COMPARISON_IN = 'in'
COMPARISON_STARTSWITH = 'startswith'
COMPARISON_ISTARTSWITH = 'istartswith'
COMPARISON_ENDSWITH = 'endswith'
COMPARISON_IENDSWITH = 'iendswith'
COMPARISON_ISNULL = 'isnull'
COMPARISONS = (
COMPARISON_EXACT,
Expand All @@ -20,6 +24,10 @@
COMPARISON_LT,
COMPARISON_LTE,
COMPARISON_IN,
COMPARISON_STARTSWITH,
COMPARISON_ISTARTSWITH,
COMPARISON_ENDSWITH,
COMPARISON_IENDSWITH,
COMPARISON_ISNULL,
)

Expand Down
10 changes: 7 additions & 3 deletions django_mock_queries/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,22 @@ def distinct():

mock_set.distinct = MagicMock(side_effect=distinct)

def raise_does_not_exist():
does_not_exist = getattr(mock_set.cls, 'DoesNotExist', ObjectDoesNotExist)
raise does_not_exist()

def latest(field):
results = sorted(items, key=attrgetter(field), reverse=True)
if len(results) == 0:
raise ObjectDoesNotExist()
raise_does_not_exist()
return results[0]

mock_set.latest = MagicMock(side_effect=latest)

def earliest(field):
results = sorted(items, key=attrgetter(field))
if len(results) == 0:
raise ObjectDoesNotExist()
raise_does_not_exist()
return results[0]

mock_set.earliest = MagicMock(side_effect=earliest)
Expand Down Expand Up @@ -181,7 +185,7 @@ def create(**attrs):
def get(**attrs):
results = filter(**attrs)
if not results.exists():
raise ObjectDoesNotExist()
raise_does_not_exist()
elif results.count() > 1:
raise MultipleObjectsReturned()
else:
Expand Down
4 changes: 4 additions & 0 deletions django_mock_queries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def is_match(first, second, comparison=None):
COMPARISON_LT: lambda: first < second,
COMPARISON_LTE: lambda: first <= second,
COMPARISON_IN: lambda: first in second,
COMPARISON_STARTSWITH: lambda: first.startswith(second),
COMPARISON_ISTARTSWITH: lambda: first.lower().startswith(second.lower()),
COMPARISON_ENDSWITH: lambda: first.endswith(second),
COMPARISON_IENDSWITH: lambda: first.lower().endswith(second.lower()),
COMPARISON_ISNULL: lambda: (first is None) == bool(second),
}[comparison]()

Expand Down
13 changes: 13 additions & 0 deletions tests/test_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def test_delegation(self):
""" We can still access fields from the original relation manager. """
self.assertTrue(Manufacturer.car_set.related_manager_cls.do_not_call_in_templates)

@patch.object(Manufacturer, 'car_set', MockOneToManyMap(Manufacturer.car_set))
def test_raises(self):
""" Raises an error specific to the child class. """
m = Manufacturer()

with self.assertRaises(Car.DoesNotExist):
m.car_set.get(speed=0)


# noinspection PyUnusedLocal
def zero_sum(items):
Expand Down Expand Up @@ -325,3 +333,8 @@ def test_mock_twice(self):

with mocked_relations(Car):
self.assertEqual(1, Car.objects.count())

@mocked_relations(Manufacturer)
def test_raises_specific_error(self):
with self.assertRaises(Manufacturer.DoesNotExist):
Manufacturer.objects.get(name='sam')
39 changes: 33 additions & 6 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django_mock_queries.constants import *
from django_mock_queries.exceptions import ModelNotSpecified, ArgumentNotSupported
from django_mock_queries.query import MockSet, MockModel
from tests.mock_models import Car, Sedan
from tests.mock_models import Car, Sedan, Manufacturer


class TestQuery(TestCase):
Expand Down Expand Up @@ -88,6 +88,16 @@ def test_query_filters_model_objects(self):

assert results == [item_3]

def test_query_filters_related_model_objects(self):
item_1 = Car(make=Manufacturer(name='apple'))
item_2 = Car(make=Manufacturer(name='banana'))
item_3 = Car(make=Manufacturer(name='cherry'))

self.mock_set.add(item_1, item_2, item_3)
results = list(self.mock_set.filter(make__name='cherry'))

assert results == [item_3]

def test_query_filters_model_objects_by_subclass(self):
item_1 = Car(speed=1)
item_2 = Sedan(speed=2)
Expand Down Expand Up @@ -377,7 +387,7 @@ def test_query_last(self):

def test_query_latest_raises_error_exist_when_empty_set(self):
self.mock_set.clear()
self.assertRaises(Exception, self.mock_set.latest, 'foo')
self.assertRaises(ObjectDoesNotExist, self.mock_set.latest, 'foo')

def test_query_earliest_returns_the_first_element_from_ordered_set(self):
item_1 = MockModel(foo=1)
Expand All @@ -391,7 +401,7 @@ def test_query_earliest_returns_the_first_element_from_ordered_set(self):

def test_query_earliest_raises_error_exist_when_empty_set(self):
self.mock_set.clear()
self.assertRaises(Exception, self.mock_set.earliest, 'foo')
self.assertRaises(ObjectDoesNotExist, self.mock_set.earliest, 'foo')

def test_query_order_by(self):
item_1 = MockModel(foo=1, bar='a', mock_name='item_1')
Expand Down Expand Up @@ -460,15 +470,32 @@ def test_query_get_raises_does_not_exist_when_no_match(self):
item_3 = MockModel(foo=3)

self.mock_set.add(item_1, item_2, item_3)
self.assertRaises(Exception, self.mock_set.get, foo=4)
self.assertRaises(ObjectDoesNotExist, self.mock_set.get, foo=4)

def test_query_get_raises_specific_exception(self):
item_1 = Car(model='battle')
item_2 = Car(model='pious')
item_3 = Car(model='hummus')

self.mock_set = MockSet(item_1, item_2, item_3, cls=Car)
self.assertRaises(Car.DoesNotExist, self.mock_set.get, model='clowncar')

def test_filter_keeps_class(self):
item_1 = Car(model='battle')
item_2 = Car(model='pious')
item_3 = Car(model='hummus')

self.mock_set = MockSet(item_1, item_2, item_3, cls=Car)
filtered = self.mock_set.filter(model__endswith='s')
self.assertRaises(Car.DoesNotExist, filtered.get, model='clowncar')

def test_query_get_raises_does_multiple_objects_returned_when_more_than_one_match(self):
item_1 = MockModel(foo=1)
item_2 = MockModel(foo=1)
item_3 = MockModel(foo=2)

self.mock_set.add(item_1, item_2, item_3)
self.assertRaises(Exception, self.mock_set.get, foo=1)
self.assertRaises(MultipleObjectsReturned, self.mock_set.get, foo=1)

def test_query_get_or_create_gets_existing_unique_match(self):
item_1 = MockModel(foo=1)
Expand All @@ -487,7 +514,7 @@ def test_query_get_or_create_raises_does_multiple_objects_returned_when_more_tha
item_3 = MockModel(foo=2)

self.mock_set.add(item_1, item_2, item_3)
self.assertRaises(Exception, self.mock_set.get_or_create, foo=1)
self.assertRaises(MultipleObjectsReturned, self.mock_set.get_or_create, foo=1)

def test_query_get_or_create_creates_new_model_when_no_match(self):
item_1 = MockModel(foo=1)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,34 @@ def test_is_match_case_insensitive_contains_check(self):
result = utils.is_match('abc', 'a', constants.COMPARISON_ICONTAINS)
assert result is True

def test_is_match_startswith_check(self):
result = utils.is_match('abc', 'a', constants.COMPARISON_STARTSWITH)
assert result is True

result = utils.is_match('abc', 'A', constants.COMPARISON_STARTSWITH)
assert result is False

def test_is_match_istartswith_check(self):
result = utils.is_match('abc', 'a', constants.COMPARISON_ISTARTSWITH)
assert result is True

result = utils.is_match('abc', 'A', constants.COMPARISON_ISTARTSWITH)
assert result is True

def test_is_match_endswith_check(self):
result = utils.is_match('abc', 'c', constants.COMPARISON_ENDSWITH)
assert result is True

result = utils.is_match('abc', 'C', constants.COMPARISON_ENDSWITH)
assert result is False

def test_is_match_iendswith_check(self):
result = utils.is_match('abc', 'c', constants.COMPARISON_IENDSWITH)
assert result is True

result = utils.is_match('abc', 'C', constants.COMPARISON_IENDSWITH)
assert result is True

def test_is_match_greater_than_value_check(self):
result = utils.is_match(5, 3, constants.COMPARISON_GT)
assert result is True
Expand Down

0 comments on commit c153802

Please sign in to comment.