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 tests for unique_with constraint #241

Open
wants to merge 2 commits 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
108 changes: 108 additions & 0 deletions tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ class ReferencingDocWithUniqueField(Document):
ref = fields.ReferenceField(ReferencedDocWithUniqueField, unique=True)


class ReferencingDocWithUniqueWithField(Document):
ref1 = fields.ReferenceField(ReferencedDoc)
ref2 = fields.ReferenceField(OtherReferencedDoc)

meta = {
'indexes': [
{
'fields': ['ref1', 'ref2'],
'unique': True
}
]
}


class GenericReferencingDoc(Document):
ref = fields.GenericReferenceField()

Expand Down Expand Up @@ -611,6 +625,100 @@ class Meta:
assert serializer.data == expected


class TestUniqueWithReferenceIntegration(TestCase):
def setUp(self):
self.target1 = ReferencedDoc.objects.create(
name='Foo'
)
self.target2 = OtherReferencedDoc.objects.create(
name='Bar'
)
self.target2_ = OtherReferencedDoc.objects.create(
name='Bar2'
)

def doCleanups(self):
ReferencedDocWithUniqueField.drop_collection()
ReferencingDocWithUniqueWithField.drop_collection()

def test_retrieval(self):
instance = ReferencingDocWithUniqueWithField.objects.create(
ref1=self.target1,
ref2=self.target2
)

class TestSerializer(DocumentSerializer):
class Meta:
model = ReferencingDocWithUniqueWithField
fields = '__all__'
depth = 0

serializer = TestSerializer(instance)
expected = {
'id': str(instance.id),
'ref1': str(self.target1.id),
'ref2': str(self.target2.id),
}
assert serializer.data == expected

def test_create(self):
instance = ReferencingDocWithUniqueWithField.objects.create(
ref1=self.target1,
ref2=self.target2
)

class TestSerializer(DocumentSerializer):
class Meta:
model = ReferencingDocWithUniqueWithField
fields = '__all__'
depth = 0

data = {'ref1': str(self.target1.id), 'ref2': str(self.target2.id)}
serializer = TestSerializer(data=data)
assert not serializer.is_valid(), not serializer.errors

data = {'ref1': str(self.target1.id), 'ref2': str(self.target2_.id)}
serializer = TestSerializer(data=data)

assert serializer.is_valid(), serializer.errors

expected = {
'ref1': str(self.target1.id),
'ref2': str(self.target2_.id),
}
assert serializer.data == expected


def test_update(self):
instance1 = ReferencingDocWithUniqueWithField.objects.create(
ref1=self.target1,
ref2=self.target2
)
instance2 = ReferencingDocWithUniqueWithField.objects.create(
ref1=self.target1,
ref2=self.target2_
)

class TestSerializer(DocumentSerializer):
class Meta:
model = ReferencingDocWithUniqueWithField
fields = '__all__'
depth = 0

data = {'ref1': str(instance1.ref1.id)}
serializer = TestSerializer(instance1, data=data, partial=True,)
assert serializer.is_valid(), serializer.errors

obj = serializer.save()
assert obj.id == instance1.id
assert obj.ref1 == instance1.ref1
assert obj.ref2 == instance1.ref2

data = {'ref2': str(instance2.ref2.id)}
serializer = TestSerializer(instance1, data=data, partial=True,)
assert not serializer.is_valid(), not serializer.errors


class TestGenericReferenceIntegration(TestCase):
def setUp(self):
self.target = ReferencedDoc.objects.create(name='Foo')
Expand Down