Merge branch 'master' of github.com:MongoEngine/mongoengine into fix_complex_datetime_field_invalid_string_set

This commit is contained in:
Bastien Gérard
2020-04-25 22:12:35 +02:00
62 changed files with 588 additions and 741 deletions

View File

@@ -3,13 +3,12 @@ import uuid
from bson import Binary
import pytest
import six
from mongoengine import *
from tests.utils import MongoDBTestCase
BIN_VALUE = six.b(
"\xa9\xf3\x8d(\xd7\x03\x84\xb4k[\x0f\xe3\xa2\x19\x85p[J\xa3\xd2>\xde\xe6\x87\xb1\x7f\xc6\xe6\xd9r\x18\xf5"
BIN_VALUE = "\xa9\xf3\x8d(\xd7\x03\x84\xb4k[\x0f\xe3\xa2\x19\x85p[J\xa3\xd2>\xde\xe6\x87\xb1\x7f\xc6\xe6\xd9r\x18\xf5".encode(
"latin-1"
)
@@ -22,7 +21,7 @@ class TestBinaryField(MongoDBTestCase):
content_type = StringField()
blob = BinaryField()
BLOB = six.b("\xe6\x00\xc4\xff\x07")
BLOB = "\xe6\x00\xc4\xff\x07".encode("latin-1")
MIME_TYPE = "application/octet-stream"
Attachment.drop_collection()
@@ -32,7 +31,7 @@ class TestBinaryField(MongoDBTestCase):
attachment_1 = Attachment.objects().first()
assert MIME_TYPE == attachment_1.content_type
assert BLOB == six.binary_type(attachment_1.blob)
assert BLOB == bytes(attachment_1.blob)
def test_validation_succeeds(self):
"""Ensure that valid values can be assigned to binary fields.
@@ -47,11 +46,11 @@ class TestBinaryField(MongoDBTestCase):
attachment_required = AttachmentRequired()
with pytest.raises(ValidationError):
attachment_required.validate()
attachment_required.blob = Binary(six.b("\xe6\x00\xc4\xff\x07"))
attachment_required.blob = Binary("\xe6\x00\xc4\xff\x07".encode("latin-1"))
attachment_required.validate()
_5_BYTES = six.b("\xe6\x00\xc4\xff\x07")
_4_BYTES = six.b("\xe6\x00\xc4\xff")
_5_BYTES = "\xe6\x00\xc4\xff\x07".encode("latin-1")
_4_BYTES = "\xe6\x00\xc4\xff".encode("latin-1")
with pytest.raises(ValidationError):
AttachmentSizeLimit(blob=_5_BYTES).validate()
AttachmentSizeLimit(blob=_4_BYTES).validate()
@@ -123,10 +122,7 @@ class TestBinaryField(MongoDBTestCase):
upsert=True, new=True, set__bin_field=BIN_VALUE
)
assert doc.some_field == "test"
if six.PY3:
assert doc.bin_field == BIN_VALUE
else:
assert doc.bin_field == Binary(BIN_VALUE)
assert doc.bin_field == BIN_VALUE
def test_update_one(self):
"""Ensures no regression of bug #1127"""
@@ -136,7 +132,7 @@ class TestBinaryField(MongoDBTestCase):
MyDocument.drop_collection()
bin_data = six.b("\xe6\x00\xc4\xff\x07")
bin_data = "\xe6\x00\xc4\xff\x07".encode("latin-1")
doc = MyDocument(bin_field=bin_data).save()
n_updated = MyDocument.objects(bin_field=bin_data).update_one(
@@ -144,7 +140,4 @@ class TestBinaryField(MongoDBTestCase):
)
assert n_updated == 1
fetched = MyDocument.objects.with_id(doc.id)
if six.PY3:
assert fetched.bin_field == BIN_VALUE
else:
assert fetched.bin_field == Binary(BIN_VALUE)
assert fetched.bin_field == BIN_VALUE

View File

@@ -2,7 +2,6 @@
import datetime
import pytest
import six
try:
import dateutil
@@ -89,17 +88,6 @@ class TestDateField(MongoDBTestCase):
assert log.date == d1.date()
assert log.date == d2.date()
if not six.PY3:
# Pre UTC dates microseconds below 1000 are dropped
# This does not seem to be true in PY3
d1 = datetime.datetime(1969, 12, 31, 23, 59, 59, 999)
d2 = datetime.datetime(1969, 12, 31, 23, 59, 59)
log.date = d1
log.save()
log.reload()
assert log.date == d1.date()
assert log.date == d2.date()
def test_regular_usage(self):
"""Tests for regular datetime fields"""

View File

@@ -2,7 +2,6 @@
import datetime as dt
import pytest
import six
try:
import dateutil
@@ -98,17 +97,6 @@ class TestDateTimeField(MongoDBTestCase):
assert log.date != d1
assert log.date == d2
if not six.PY3:
# Pre UTC dates microseconds below 1000 are dropped
# This does not seem to be true in PY3
d1 = dt.datetime(1969, 12, 31, 23, 59, 59, 999)
d2 = dt.datetime(1969, 12, 31, 23, 59, 59)
log.date = d1
log.save()
log.reload()
assert log.date != d1
assert log.date == d2
def test_regular_usage(self):
"""Tests for regular datetime fields"""
@@ -213,7 +201,7 @@ class TestDateTimeField(MongoDBTestCase):
# make sure that passing a parsable datetime works
dtd = DTDoc()
dtd.date = date_str
assert isinstance(dtd.date, six.string_types)
assert isinstance(dtd.date, str)
dtd.save()
dtd.reload()

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import pytest
from bson import InvalidDocument
import pytest
from mongoengine import *
from mongoengine.base import BaseDict

View File

@@ -75,7 +75,7 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
Person.objects(settings__notexist="bar").first()
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
with pytest.raises(LookUpError):
Person.objects.only("settings.notexist")
@@ -111,7 +111,7 @@ class TestEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
assert Person.objects(settings__notexist="bar").first().id == p.id
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
# Test existing attribute
assert Person.objects(settings__base_foo="basefoo").first().id == p.id
@@ -319,7 +319,7 @@ class TestGenericEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
Person.objects(settings__notexist="bar").first()
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
with pytest.raises(LookUpError):
Person.objects.only("settings.notexist")
@@ -347,7 +347,7 @@ class TestGenericEmbeddedDocumentField(MongoDBTestCase):
# Test non exiting attribute
with pytest.raises(InvalidQueryError) as exc_info:
assert Person.objects(settings__notexist="bar").first().id == p.id
assert unicode(exc_info.value) == u'Cannot resolve field "notexist"'
assert str(exc_info.value) == u'Cannot resolve field "notexist"'
# Test existing attribute
assert Person.objects(settings__base_foo="basefoo").first().id == p.id

View File

@@ -3,14 +3,13 @@ import copy
import os
import tempfile
import unittest
from io import BytesIO
import gridfs
import pytest
import six
from mongoengine import *
from mongoengine.connection import get_db
from mongoengine.python_support import StringIO
try:
from PIL import Image
@@ -30,7 +29,7 @@ TEST_IMAGE2_PATH = os.path.join(os.path.dirname(__file__), "mongodb_leaf.png")
def get_file(path):
"""Use a BytesIO instead of a file to allow
to have a one-liner and avoid that the file remains opened"""
bytes_io = StringIO()
bytes_io = BytesIO()
with open(path, "rb") as f:
bytes_io.write(f.read())
bytes_io.seek(0)
@@ -58,7 +57,7 @@ class TestFileField(MongoDBTestCase):
PutFile.drop_collection()
text = six.b("Hello, World!")
text = "Hello, World!".encode("latin-1")
content_type = "text/plain"
putfile = PutFile()
@@ -80,7 +79,7 @@ class TestFileField(MongoDBTestCase):
PutFile.drop_collection()
putfile = PutFile()
putstring = StringIO()
putstring = BytesIO()
putstring.write(text)
putstring.seek(0)
putfile.the_file.put(putstring, content_type=content_type)
@@ -101,8 +100,8 @@ class TestFileField(MongoDBTestCase):
StreamFile.drop_collection()
text = six.b("Hello, World!")
more_text = six.b("Foo Bar")
text = "Hello, World!".encode("latin-1")
more_text = "Foo Bar".encode("latin-1")
content_type = "text/plain"
streamfile = StreamFile()
@@ -137,8 +136,8 @@ class TestFileField(MongoDBTestCase):
StreamFile.drop_collection()
text = six.b("Hello, World!")
more_text = six.b("Foo Bar")
text = "Hello, World!".encode("latin-1")
more_text = "Foo Bar".encode("latin-1")
streamfile = StreamFile()
streamfile.save()
@@ -167,8 +166,8 @@ class TestFileField(MongoDBTestCase):
class SetFile(Document):
the_file = FileField()
text = six.b("Hello, World!")
more_text = six.b("Foo Bar")
text = "Hello, World!".encode("latin-1")
more_text = "Foo Bar".encode("latin-1")
SetFile.drop_collection()
@@ -196,7 +195,7 @@ class TestFileField(MongoDBTestCase):
GridDocument.drop_collection()
with tempfile.TemporaryFile() as f:
f.write(six.b("Hello World!"))
f.write("Hello World!".encode("latin-1"))
f.flush()
# Test without default
@@ -213,7 +212,7 @@ class TestFileField(MongoDBTestCase):
assert doc_b.the_file.grid_id == doc_c.the_file.grid_id
# Test with default
doc_d = GridDocument(the_file=six.b(""))
doc_d = GridDocument(the_file="".encode("latin-1"))
doc_d.save()
doc_e = GridDocument.objects.with_id(doc_d.id)
@@ -240,7 +239,7 @@ class TestFileField(MongoDBTestCase):
# First instance
test_file = TestFile()
test_file.name = "Hello, World!"
test_file.the_file.put(six.b("Hello, World!"))
test_file.the_file.put("Hello, World!".encode("latin-1"))
test_file.save()
# Second instance
@@ -297,7 +296,9 @@ class TestFileField(MongoDBTestCase):
test_file = TestFile()
assert not bool(test_file.the_file)
test_file.the_file.put(six.b("Hello, World!"), content_type="text/plain")
test_file.the_file.put(
"Hello, World!".encode("latin-1"), content_type="text/plain"
)
test_file.save()
assert bool(test_file.the_file)
@@ -319,7 +320,7 @@ class TestFileField(MongoDBTestCase):
class TestFile(Document):
the_file = FileField()
text = six.b("Hello, World!")
text = "Hello, World!".encode("latin-1")
content_type = "text/plain"
testfile = TestFile()
@@ -363,7 +364,7 @@ class TestFileField(MongoDBTestCase):
testfile.the_file.put(text, content_type=content_type, filename="hello")
testfile.save()
text = six.b("Bonjour, World!")
text = "Bonjour, World!".encode("latin-1")
testfile.the_file.replace(text, content_type=content_type, filename="hello")
testfile.save()
@@ -387,7 +388,7 @@ class TestFileField(MongoDBTestCase):
TestImage.drop_collection()
with tempfile.TemporaryFile() as f:
f.write(six.b("Hello World!"))
f.write("Hello World!".encode("latin-1"))
f.flush()
t = TestImage()
@@ -503,21 +504,21 @@ class TestFileField(MongoDBTestCase):
# First instance
test_file = TestFile()
test_file.name = "Hello, World!"
test_file.the_file.put(six.b("Hello, World!"), name="hello.txt")
test_file.the_file.put("Hello, World!".encode("latin-1"), name="hello.txt")
test_file.save()
data = get_db("test_files").macumba.files.find_one()
assert data.get("name") == "hello.txt"
test_file = TestFile.objects.first()
assert test_file.the_file.read() == six.b("Hello, World!")
assert test_file.the_file.read() == "Hello, World!".encode("latin-1")
test_file = TestFile.objects.first()
test_file.the_file = six.b("HELLO, WORLD!")
test_file.the_file = "Hello, World!".encode("latin-1")
test_file.save()
test_file = TestFile.objects.first()
assert test_file.the_file.read() == six.b("HELLO, WORLD!")
assert test_file.the_file.read() == "Hello, World!".encode("latin-1")
def test_copyable(self):
class PutFile(Document):
@@ -525,7 +526,7 @@ class TestFileField(MongoDBTestCase):
PutFile.drop_collection()
text = six.b("Hello, World!")
text = "Hello, World!".encode("latin-1")
content_type = "text/plain"
putfile = PutFile()

View File

@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import pytest
import six
from mongoengine import *
@@ -52,9 +51,8 @@ class TestFloatField(MongoDBTestCase):
big_person = BigPerson()
for value, value_type in enumerate(six.integer_types):
big_person.height = value_type(value)
big_person.validate()
big_person.height = int(0)
big_person.validate()
big_person.height = 2 ** 500
big_person.validate()

View File

@@ -152,7 +152,7 @@ class TestLazyReferenceField(MongoDBTestCase):
LazyReference(BadDoc, animal.pk),
):
with pytest.raises(ValidationError):
p = Ocurrence(person="test", animal=bad).save()
Ocurrence(person="test", animal=bad).save()
def test_lazy_reference_query_conversion(self):
"""Ensure that LazyReferenceFields can be queried using objects and values
@@ -386,7 +386,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
mineral = Mineral(name="Granite").save()
occ_animal = Ocurrence(living_thing=animal, thing=animal).save()
occ_vegetal = Ocurrence(living_thing=vegetal, thing=vegetal).save()
_ = Ocurrence(living_thing=vegetal, thing=vegetal).save()
with pytest.raises(ValidationError):
Ocurrence(living_thing=mineral).save()
@@ -458,7 +458,7 @@ class TestGenericLazyReferenceField(MongoDBTestCase):
baddoc = BadDoc().save()
for bad in (42, "foo", baddoc, LazyReference(BadDoc, animal.pk)):
with pytest.raises(ValidationError):
p = Ocurrence(person="test", animal=bad).save()
Ocurrence(person="test", animal=bad).save()
def test_generic_lazy_reference_query_conversion(self):
class Member(Document):

View File

@@ -1,11 +1,5 @@
# -*- coding: utf-8 -*-
from bson.int64 import Int64
import pytest
import six
try:
from bson.int64 import Int64
except ImportError:
Int64 = long
from mongoengine import *
from mongoengine.connection import get_db
@@ -28,7 +22,7 @@ class TestLongField(MongoDBTestCase):
assert isinstance(
db.test_long_field_considered_as_int64.find()[0]["some_long"], Int64
)
assert isinstance(doc.some_long, six.integer_types)
assert isinstance(doc.some_long, int)
def test_long_validation(self):
"""Ensure that invalid values cannot be assigned to long fields.

View File

@@ -21,7 +21,7 @@ class TestSequenceField(MongoDBTestCase):
assert c["next"] == 10
ids = [i.id for i in Person.objects]
assert ids == range(1, 11)
assert ids == list(range(1, 11))
c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
assert c["next"] == 10
@@ -76,7 +76,7 @@ class TestSequenceField(MongoDBTestCase):
assert c["next"] == 10
ids = [i.id for i in Person.objects]
assert ids == range(1, 11)
assert ids == list(range(1, 11))
c = self.db["mongoengine.counters"].find_one({"_id": "jelly.id"})
assert c["next"] == 10
@@ -101,10 +101,10 @@ class TestSequenceField(MongoDBTestCase):
assert c["next"] == 10
ids = [i.id for i in Person.objects]
assert ids == range(1, 11)
assert ids == list(range(1, 11))
counters = [i.counter for i in Person.objects]
assert counters == range(1, 11)
assert counters == list(range(1, 11))
c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
assert c["next"] == 10
@@ -166,10 +166,10 @@ class TestSequenceField(MongoDBTestCase):
assert c["next"] == 10
ids = [i.id for i in Person.objects]
assert ids == range(1, 11)
assert ids == list(range(1, 11))
id = [i.id for i in Animal.objects]
assert id == range(1, 11)
assert id == list(range(1, 11))
c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
assert c["next"] == 10
@@ -193,7 +193,7 @@ class TestSequenceField(MongoDBTestCase):
assert c["next"] == 10
ids = [i.id for i in Person.objects]
assert ids == map(str, range(1, 11))
assert ids == [str(i) for i in range(1, 11)]
c = self.db["mongoengine.counters"].find_one({"_id": "person.id"})
assert c["next"] == 10
@@ -267,12 +267,12 @@ class TestSequenceField(MongoDBTestCase):
foo = Foo(name="Foo")
foo.save()
assert not (
"base.counter" in self.db["mongoengine.counters"].find().distinct("_id")
assert "base.counter" not in self.db["mongoengine.counters"].find().distinct(
"_id"
)
assert ("foo.counter" and "bar.counter") in self.db[
"mongoengine.counters"
].find().distinct("_id")
existing_counters = self.db["mongoengine.counters"].find().distinct("_id")
assert "foo.counter" in existing_counters
assert "bar.counter" in existing_counters
assert foo.counter == bar.counter
assert foo._fields["counter"].owner_document == Foo
assert bar._fields["counter"].owner_document == Bar

View File

@@ -2,7 +2,6 @@
import pytest
from mongoengine import *
from tests.utils import MongoDBTestCase
@@ -35,7 +34,7 @@ class TestURLField(MongoDBTestCase):
with pytest.raises(ValidationError) as exc_info:
link.validate()
assert (
unicode(exc_info.value)
str(exc_info.value)
== u"ValidationError (Link:None) (Invalid URL: http://\u043f\u0440\u0438\u0432\u0435\u0442.com: ['url'])"
)