Reputation: 593
I keep getting this error after countless trials:
Traceback (most recent call last):
File "C:\...\tests.py", line 82, in test_profile_creation
w = self.create_profile()
File "C:\...\tests.py", line 78, in create_profile
self.user = Profile.objects.create(id=1)
...
django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (1, null).
I try to cover these area:
I am not sure how to proceed testing with other two methods as marked ( get_screen_name() and get_association_name()
) so guidance would be helpful.
It is my first time in unit testing and still fresh on this so appreciate your guidance, folks!
EDIT 2
tests.py
def create_profile(self):
self.asoc = Association.objects.create(id=2)
self.admin = Administrator.objects.create(id=1, association=self.asoc)
self.user = Profile.objects.create(id=2, user=self.admin)
return Profile.objects.get(user=self.user, administrator=self.admin)
Traceback (most recent call last):
File "C:\...\tests.py", line 83, in test_profile_creation
w = self.create_profile()
File "C:\...\tests.py", line 79, in create_profile
self.user = Profile.objects.create(id=2, user=self.admin)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "Profile_pkey"
DETAIL: Key (id)=(2) already exists.
EDIT 3
def create_profile(self):
self.asoc = Association.objects.create()
self.admin = Administrator.objects.create(association=self.asoc)
self.user = Profile.objects.create(user=self.admin)
return Profile.objects.get(user=self.user, administrator=self.admin)
Traceback (most recent call last):
File "C:\..\tests.py", line 83, in test_profile_creation
w = self.create_profile()
File "C:\...\tests.py", line 79, in create_profile
self.user = Profile.objects.create(user=self.admin)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "Profile_user_id_key"
DETAIL: Key (user_id)=(2) already exists.
EDIT 4
def create_profile(self):
self.asoc = Association.objects.create(id=7)
self.admin = Administrator.objects.create(id=6, association=self.asoc)
self.user = Profile.objects.create(id=5)
return Profile.objects.get(user=self.user, administrator=self.admin)
Traceback (most recent call last):
File "C:\..\tests.py", line 83, in test_profile_creation
w = self.create_profile()
File "C:\...\tests.py", line 79, in create_profile
self.user = Profile.objects.create(id=5)
...
django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (5, null).
EDIT 5
def create_profile(self):
self.asoc = Association.objects.create(id=7)
self.admin = Administrator.objects.create(id=6, association=self.asoc)
self.user = Profile.objects.create(id=self.admin)
return Profile.objects.get(user=self.user, administrator=self.admin)
Traceback (most recent call last):
File "C:\..\tests.py", line 83, in test_profile_creation
w = self.create_profile()
File "C:\...\tests.py", line 79, in create_profile
self.user = Profile.objects.create(id=self.admin)
...
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Administrator'
EDIT 6
def create_profile(self):
self.asoc = Association.objects.create(id=7)
self.admin = Administrator.objects.create(id=6, association=self.asoc)
self.user = Profile.objects.create(id=self.admin.id)
return Profile.objects.get(user=self.user, administrator=self.admin)
Traceback (most recent call last):
File "C:\..\tests.py", line 83, in test_profile_creation
w = self.create_profile()
File "C:\...\tests.py", line 79, in create_profile
self.user = Profile.objects.create(id=self.admin.id)
...
django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (6, null).
tests.py
class ProfileTest(TestCase):
def create_profile(self):
self.asoc = Association.objects.create(id=2)
self.admin = Administrator.objects.create(id=1, association=self.asoc)
self.user = Profile.objects.create(id=1)
return Profile.objects.get(user=self.user, administrator=self.admin)
def test_profile_creation(self):
w = self.create_profile()
self.assertTrue(isinstance(w, Profile))
self.assertEqual(w.__str__(), w.user.username)
models.py
class Administrator(AbstractUser):
...
association = models.ForeignKey(Association)
class Meta:
db_table = 'Administrator'
def __str__(self):
return self.username
def __unicode__(self):
return self.username
class Profile(models.Model):
user = models.OneToOneField(Administrator)
class Meta:
db_table = 'Profile'
def __str__(self):
return self.user.username
def get_screen_name(self):
try:
if self.user.get_full_name():
return self.user.get_full_name()
else:
return self.user.username
except:
return self.user.username
def get_association_name(self):
try:
if self.user.association:
return self.user.association
else:
return self.user.username
except:
return self.user.username
class Association(models.Model):
asoc_name = models.CharField(max_length=50, null=True, blank=True, unique=True)
class Meta:
db_table = 'Association'
def __str__(self):
return self.asoc_name
def __unicode__(self):
return self.asoc_name
Upvotes: 0
Views: 4241
Reputation: 27533
the error is because you mapped an OnetoOneField
. and creating multiple rows with the same id
i.e 1. try to create with id
2. it will work, but the best way is not to pass id
when you are adding it, as id
is auto incremented
and you dont need to specify it while creating, but you can specify it if you want to update an existing row. hope it helps
Upvotes: 2
Reputation: 39709
Profile is linked with Administrator via OneToOneField
so when creating Profile record you need set the user
. So in test.py -> line #78
replace with following:
self.user = Profile.objects.create(id=1, user=self.admin)
Upvotes: 0