Improve Queryset.get to avoid confusing message in case multiple match are found
This commit is contained in:
parent
f49baf5d90
commit
2b0157aecd
@ -7,6 +7,7 @@ Development
|
|||||||
===========
|
===========
|
||||||
- (Fill this out as you fix issues and develop your features).
|
- (Fill this out as you fix issues and develop your features).
|
||||||
- Add Mongo 4.0 to Travis
|
- Add Mongo 4.0 to Travis
|
||||||
|
- Improve Queryset.get to avoid confusing MultipleObjectsReturned message in case multiple match are found #630
|
||||||
- Fixed a bug causing inaccurate query results, while combining ``__raw__`` and regular filters for the same field #2264
|
- Fixed a bug causing inaccurate query results, while combining ``__raw__`` and regular filters for the same field #2264
|
||||||
- Add support for the `elemMatch` projection operator in .fields() (e.g BlogPost.objects.fields(elemMatch__comments="test")) #2267
|
- Add support for the `elemMatch` projection operator in .fields() (e.g BlogPost.objects.fields(elemMatch__comments="test")) #2267
|
||||||
- DictField validate failed without default connection (bug introduced in 0.19.0) #2239
|
- DictField validate failed without default connection (bug introduced in 0.19.0) #2239
|
||||||
|
@ -259,16 +259,18 @@ class BaseQuerySet(object):
|
|||||||
except StopIteration:
|
except StopIteration:
|
||||||
msg = "%s matching query does not exist." % queryset._document._class_name
|
msg = "%s matching query does not exist." % queryset._document._class_name
|
||||||
raise queryset._document.DoesNotExist(msg)
|
raise queryset._document.DoesNotExist(msg)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# Check if there is another match
|
||||||
six.next(queryset)
|
six.next(queryset)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# If we were able to retrieve the 2nd doc, rewind the cursor and
|
# If we were able to retrieve the 2nd doc, rewind the cursor and
|
||||||
# raise the MultipleObjectsReturned exception.
|
# raise the MultipleObjectsReturned exception.
|
||||||
queryset.rewind()
|
raise queryset._document.MultipleObjectsReturned(
|
||||||
message = u"%d items returned, instead of 1" % queryset.count()
|
u"2 or more items returned, instead of 1"
|
||||||
raise queryset._document.MultipleObjectsReturned(message)
|
)
|
||||||
|
|
||||||
def create(self, **kwargs):
|
def create(self, **kwargs):
|
||||||
"""Create new object. Returns the saved object instance.
|
"""Create new object. Returns the saved object instance.
|
||||||
|
@ -274,32 +274,47 @@ class TestQueryset(unittest.TestCase):
|
|||||||
with pytest.raises(InvalidQueryError):
|
with pytest.raises(InvalidQueryError):
|
||||||
self.Person.objects(name="User A").with_id(person1.id)
|
self.Person.objects(name="User A").with_id(person1.id)
|
||||||
|
|
||||||
def test_find_only_one(self):
|
def test_get_no_document_exists_raises_doesnotexist(self):
|
||||||
"""Ensure that a query using ``get`` returns at most one result.
|
assert self.Person.objects.count() == 0
|
||||||
"""
|
|
||||||
# Try retrieving when no objects exists
|
# Try retrieving when no objects exists
|
||||||
with pytest.raises(DoesNotExist):
|
with pytest.raises(DoesNotExist):
|
||||||
self.Person.objects.get()
|
self.Person.objects.get()
|
||||||
with pytest.raises(self.Person.DoesNotExist):
|
with pytest.raises(self.Person.DoesNotExist):
|
||||||
self.Person.objects.get()
|
self.Person.objects.get()
|
||||||
|
|
||||||
|
def test_get_multiple_match_raises_multipleobjectsreturned(self):
|
||||||
|
"""Ensure that a query using ``get`` returns at most one result.
|
||||||
|
"""
|
||||||
|
assert self.Person.objects().count() == 0
|
||||||
|
|
||||||
person1 = self.Person(name="User A", age=20)
|
person1 = self.Person(name="User A", age=20)
|
||||||
person1.save()
|
person1.save()
|
||||||
person2 = self.Person(name="User B", age=30)
|
|
||||||
|
p = self.Person.objects.get()
|
||||||
|
assert p == person1
|
||||||
|
|
||||||
|
person2 = self.Person(name="User B", age=20)
|
||||||
person2.save()
|
person2.save()
|
||||||
|
|
||||||
# Retrieve the first person from the database
|
person3 = self.Person(name="User C", age=30)
|
||||||
|
person3.save()
|
||||||
|
|
||||||
|
# .get called without argument
|
||||||
with pytest.raises(MultipleObjectsReturned):
|
with pytest.raises(MultipleObjectsReturned):
|
||||||
self.Person.objects.get()
|
self.Person.objects.get()
|
||||||
with pytest.raises(self.Person.MultipleObjectsReturned):
|
with pytest.raises(self.Person.MultipleObjectsReturned):
|
||||||
self.Person.objects.get()
|
self.Person.objects.get()
|
||||||
|
|
||||||
|
# check filtering
|
||||||
|
with pytest.raises(MultipleObjectsReturned):
|
||||||
|
self.Person.objects.get(age__lt=30)
|
||||||
|
with pytest.raises(MultipleObjectsReturned) as exc_info:
|
||||||
|
self.Person.objects(age__lt=30).get()
|
||||||
|
assert "2 or more items returned, instead of 1" == str(exc_info.value)
|
||||||
|
|
||||||
# Use a query to filter the people found to just person2
|
# Use a query to filter the people found to just person2
|
||||||
person = self.Person.objects.get(age=30)
|
person = self.Person.objects.get(age=30)
|
||||||
assert person.name == "User B"
|
assert person == person3
|
||||||
|
|
||||||
person = self.Person.objects.get(age__lt=30)
|
|
||||||
assert person.name == "User A"
|
|
||||||
|
|
||||||
def test_find_array_position(self):
|
def test_find_array_position(self):
|
||||||
"""Ensure that query by array position works.
|
"""Ensure that query by array position works.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user