More than a problem is a doubt of not knowing what is happening, the logic is not very complex and the code at least by shell works well but when I want to write test to prove it gives me error and I would like to know why. .. the logic is as follows: I have 3 profile models (Administrator, Evaluated and Evaluator) and each one has a document (Document) that has a unique pair of type and value (let's say type="DNI", value="777 ", only as an example) if there is an Administrator with a document that has a type and value equal to what I am sending, it must give an error because there can not be 2 Administrator accounts with the same Document, but if it is an Evaluated or Evaluator, it must not create no Document but only add it to the Document of the Administrator that already exists, this is the Document model:
class Document(TimeStampedModel):
type = models.CharField(max_length=10, verbose_name="Tipo")
value = models.CharField(max_length=10, verbose_name="Valor")
administrator = models.ForeignKey(
"Administrator", related_name="documents", blank=True, null=True,
verbose_name="Administrador")
evaluated = models.ForeignKey(
"Evaluated", related_name="documents", blank=True, null=True,
verbose_name="Evaluado")
evaluator = models.ForeignKey(
"Evaluator", related_name="documents", blank=True, null=True,
verbose_name="Evaluador")
class Meta:
verbose_name = "Documento"
unique_together = ("type", "value")
def __str__(self):
for person in (self.administrator, self.evaluated, self.evaluator):
if person:
return person.user.get_full_name()
def save(self):
if not any([self.administrator, self.evaluated, self.evaluator]):
raise ValidationError(
"No se puede crear un documento sin una persona asociada")
documents_qs = Document.objects.filter(
type=self.type, value=self.value)
if documents_qs.exists():
if self.id:
return super().save()
else:
document = documents_qs.first()
if self.administrator:
if document.administrator:
raise ValidationError(
"Ya existe un administrador con este documento")
else:
document.administrator = self.administrator
document.save()
if self.evaluated:
if document.evaluated:
raise ValidationError(
"Ya existe un evaluado con este documento")
else:
document.evaluated = self.evaluated
document.save()
if self.evaluator:
if document.evaluator:
raise ValidationError(
"Ya existe un evaluador con este documento")
else:
document.evaluator = self.evaluator
document.save()
else:
return super().save()
and this is the test that fails me, when I put print to the new document I get id = None when I should leave id = the id of the document that is updating ... in any case if the code works in shell and the test fails what is wrong the test but I would like to know why or how I should write the tests, thanks in advance
class DocumentTestCase(TestCase):
def setUp(self):
self.evaluator = mommy.make(Evaluator)
self.document_evaluator = mommy.make(
Document, evaluator=self.evaluator)
def test_new_administrator_with_other_existing_document(self):
document_type = self.document_evaluator.type
document_value = self.document_evaluator.value
administrator = mommy.make(Administrator)
document_new = Document(
type=document_type, value=document_value,
administrator=administrator)
document_new.save()
self.document_evaluator.save()
print(self.document_evaluator.__dict__)
print(document_new.__dict__)
print("----------")
print(self.document_evaluator.pk)
print(document_new.pk)
print("----------")
print(self.document_evaluator._state.adding)
print(document_new._state.adding)
self.assertEqual(
self.document_evaluator.id, document_new.id)
def tearDown(self):
self.document_evaluator.delete()
self.evaluator.delete()
PS: I put the prints to see what is happening, also in the object _state the adding I get one True and the other False for more than I am after the save () and do not put the other tests to not do even more extensive this publication (and also because those do not give error: D), thanks in advance