mongoengine
mongoengine copied to clipboard
Error occured when disable auto dereference and use StringField regex check
import unittest
from mongoengine import DynamicDocument, connect
from mongoengine.fields import StringField
class User(DynamicDocument):
username = StringField(required=True)
pa_id = StringField(required=True, regex=r'(^PA\d\d\d\d$)')
class Test(unittest.TestCase):
def setUp(self) -> None:
connection = connect(db='mongotest', host='mongomock://localhost')
def test(self):
a = User(username='stonemoe', pa_id='PA0001').save()
self.assertEqual(1, User.objects.count())
# RAISE HERE: TypeError: cannot deepcopy this pattern object
for user in User.objects.no_dereference():
print(user)
if __name__ == "__main__":
suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
unittest.TextTestRunner().run(suite)
Root cause may be: https://github.com/MongoEngine/mongoengine/blob/92f6fce77d12f930333bd8980fa611627d3a8d1b/mongoengine/base/document.py#L782-L783