Skip to content

Commit 703cd49

Browse files
committed
remove unnecessary conditions + improve test cov
1 parent 365c8b3 commit 703cd49

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

mongoengine/base/datastructures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def mark_key_as_changed_wrapper(parent_method):
3131

3232
def wrapper(self, key, *args, **kwargs):
3333
# Can't use super() in the decorator.
34-
if not args or not key or key not in self or self[key] != args[0]:
34+
if not args or key not in self or self[key] != args[0]:
3535
self._mark_as_changed(key)
3636
return parent_method(self, key, *args, **kwargs)
3737

mongoengine/base/document.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,6 @@ def __expand_dynamic_values(self, name, value):
516516

517517
def _mark_as_changed(self, key):
518518
"""Mark a key as explicitly changed by the user."""
519-
if not key:
520-
return
521-
522519
if not hasattr(self, "_changed_fields"):
523520
return
524521

tests/fields/test_datetime_field.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from mongoengine import *
1111
from mongoengine import connection
12-
from tests.utils import MongoDBTestCase
12+
from tests.utils import MongoDBTestCase, get_as_pymongo
1313

1414

1515
class TestDateTimeField(MongoDBTestCase):
@@ -55,6 +55,21 @@ class Person(Document):
5555
assert person_created_t0 == person.created # make sure it does not change
5656
assert person._data["created"] == person.created
5757

58+
def test_set_using_callable(self):
59+
# Weird feature but it's there for a while so let's make sure we don't break it
60+
class Person(Document):
61+
created = DateTimeField()
62+
63+
Person.drop_collection()
64+
65+
person = Person()
66+
frozen_dt = dt.datetime(2020, 7, 25, 9, 56, 1)
67+
person.created = lambda: frozen_dt
68+
person.save()
69+
70+
assert callable(person.created)
71+
assert get_as_pymongo(person) == {"_id": person.id, "created": frozen_dt}
72+
5873
def test_handling_microseconds(self):
5974
"""Tests showing pymongo datetime fields handling of microseconds.
6075
Microseconds are rounded to the nearest millisecond and pre UTC

tests/queryset/test_queryset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,12 @@ def test_order_by(self):
25802580
ages = [p.age for p in self.Person.objects.order_by("-name")]
25812581
assert ages == [30, 40, 20]
25822582

2583+
ages = [p.age for p in self.Person.objects.order_by()]
2584+
assert ages == [40, 20, 30]
2585+
2586+
ages = [p.age for p in self.Person.objects.order_by("")]
2587+
assert ages == [40, 20, 30]
2588+
25832589
def test_order_by_optional(self):
25842590
class BlogPost(Document):
25852591
title = StringField()

0 commit comments

Comments
 (0)