0

Am working on testing a django model called Authenticator, the Authenticator model has a field with ManyToManyField and OneToOneField. When i test the model i get RelatedObjectDoesNotExist and ValueError: Cannot assign "'matt'": "Authenticator.authenticator" must be a "User" instance.

Here is my model

class Authenticator(models.Model):
    orders                   =      models.ManyToManyField('client.Order', related_name = 'order_set')
    authenticator            =      models.OneToOneField(User, on_delete = models.CASCADE)
    weight                   =      models.IntegerField(default = 0)
    percentage_accuracy      =      models.FloatField("Nearness to aggregate score", default=0)
    date_assigned            =      models.DateTimeField(default = timezone.now)
    job_count                =      models.IntegerField(default = 0)
    percentage_consistency   =      models.FloatField(default = 0)


    def __unicode__(self):
        return '%s' %(self.authenticator.username)

My test code is

from django.test import TestCase
from .models import User, Authenticator

class EntryModelTest(TestCase):
    def test_string_representation_for_authenticator(self):
        auth = Authenticator(authenticator = "matt")
        print "Auth: ", auth
        consistency_weight = Authenticator(weight = "0")
        print "Consistency Weight: ", consistency_weight
        self.assertEqual(str(auth), auth.authenticator)
        self.assertEqual(int(consistency_weight), consistency_weight.weight)
uche
  • 85
  • 2
  • 2
  • 14
  • If you get an error that says RelatedObjectDoesNotExist, then I image the related object does not exist. – Daniel Roseman Jul 18 '16 at 15:13
  • If you want help decoding 2 error messages, you need to include the error messages and exactly the code that caused them. Generally, one test case with no error handling code should not raise two errors, so it seems like you've omitted the situation that caused at least one of the errors. – marr75 Jul 18 '16 at 15:26

1 Answers1

1

In the line:

auth = Authenticator(authenticator = "matt")

You attempt to initialize an Authenticator with an "authenticator" property value "matt". Since you declared that this attribute should be a user model, you should assign a User instance here. Something like:

auth = Authenticator(authenticator=User(name="matt"))

might be closer to what you are trying to do (I don't know the structure of your user model, obviously).

Beyond that, some pieces of unsolicited coding advice:

  • Use PEP8
  • The name authenticator as an attribute of the Authenticator is confusing; user might make a lot more sense here since it is the the user attribute of this authenticator
  • Avoid print statements and testing multiple assertions in your individual tests
  • Accessing another model in your __unicode__ method introduces two issues - it will cause a database lookup which is likely to produce N+1 scenarios and it is more likely to throw an error, which will make the string representation of this model in such a scenario unusable and complicate debugging/logging
marr75
  • 5,666
  • 1
  • 27
  • 41