Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for deepcopy and regex #2865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/document/test_instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import os
import pickle
import unittest
Expand Down Expand Up @@ -3854,6 +3855,53 @@ class User(Document):
with pytest.raises(DuplicateKeyError):
User.objects().select_related()

def test_deepcopy(self):
regex_field = StringField(regex=r"(^ABC\d\d\d\d$)")
no_regex_field = StringField()
# Copy copied field object
copy.deepcopy(copy.deepcopy(regex_field))
copy.deepcopy(copy.deepcopy(no_regex_field))
# Copy same field object multiple times to make sure we restore __deepcopy__ correctly
copy.deepcopy(regex_field)
copy.deepcopy(regex_field)
copy.deepcopy(no_regex_field)
copy.deepcopy(no_regex_field)

def test_deepcopy_with_reference_itself(self):
class User(Document):
name = StringField(regex=r"(.*)")
other_user = ReferenceField("self")

user1 = User(name="John").save()
User(name="Bob", other_user=user1).save()

user1.other_user = user1
user1.save()
for u in User.objects:
copied_u = copy.deepcopy(u)
assert copied_u is not u
assert copied_u._fields["name"] is u._fields["name"]
assert (
copied_u._fields["name"].regex is u._fields["name"].regex
) # Compiled regex objects are atomic

def test_from_son_with_auto_dereference_disabled(self):
class User(Document):
name = StringField(regex=r"(^ABC\d\d\d\d$)")

data = {"name": "ABC0000"}
user_obj = User._from_son(son=data, _auto_dereference=False)

assert user_obj._fields["name"] is not User.name
assert (
user_obj._fields["name"].regex is User.name.regex
) # Compiled regex are atomic
copied_user = copy.deepcopy(user_obj)
assert user_obj._fields["name"] is not copied_user._fields["name"]
assert (
user_obj._fields["name"].regex is copied_user._fields["name"].regex
) # Compiled regex are atomic

def test_embedded_document_failed_while_loading_instance_when_it_is_not_a_dict(
self,
):
Expand Down