Add some tests about how profiles types should relate to each other.

This commit is contained in:
Michele Orrù 2017-02-10 23:47:45 +01:00
parent 22da04c3e2
commit 25c3106168
2 changed files with 42 additions and 11 deletions

View file

@ -10,10 +10,22 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from django.db.utils import IntegrityError
from django.test import TestCase
from gestion.models import Profile, User
def create_profile(username):
"""
In order to create a profile, I need to create a User first.
This is annoying in most situations, as I just want a profile.
"""
u = User.objects.create(username=username,
first_name=username.title(),
last_name=username.title())
p, = Profile.objects.filter(user__username=username)
return p
class SimpleTest(TestCase):
def test_delete_user(self):
@ -31,3 +43,22 @@ class SimpleTest(TestCase):
u.save()
Profile.objects.get(user__username='foo').delete()
self.assertFalse(User.objects.filter(username='foo').exists())
def test_create_profiles(self):
from kfet.models import Account
from cof.models import CofProfile
# test that two accounts can be linked to the same profile.
p = create_profile('foo')
pkfet = Account.objects.create(profile=p, trigramme='PIG')
pcof = CofProfile.objects.create(profile=p)
self.assertEqual(pkfet.profile, pcof.profile)
# each profile is linked to at most one account of the same type.
p1 = create_profile('bar')
p2 = create_profile('baz')
pkfet1 = Account.objects.create(profile=p1, trigramme='BAR')
pcof2 = CofProfile.objects.create(profile=p2)
pkfet2 = Account.objects.create(profile=p2, trigramme='BAZ')
self.assertRaises(IntegrityError,
CofProfile.objects.create, profile=p2)

View file

@ -150,7 +150,7 @@ class Account(models.Model):
perms.add('kfet.perform_negative_operations')
return perms, stop_ope
# Surcharge Méthode save() avec gestions de User et CofProfile
# Surcharge Méthode save() avec gestions de User et Profile
# Args:
# - data : datas pour User et CofProfile
# Action:
@ -166,10 +166,10 @@ class Account(models.Model):
user.last_name = data.get("last_name", user.last_name)
user.email = data.get("email", user.email)
user.save()
# Updating CofProfile with data
cof = self.profile
cof.departement = data.get("departement", cof.departement)
cof.save()
# Updating Profile with data
profile = self.profile
profile.departement = data.get("departement", profile.departement)
profile.save()
elif data:
# New account
@ -192,15 +192,15 @@ class Account(models.Model):
if "email" in data:
user.email = data['email']
user.save()
# Creating or updating CofProfile instance
(cof, _) = Profile.objects.get_or_create(user=user)
# Creating or updating Profile instance
(profile, _) = Profile.objects.get_or_create(user=user)
if "login_clipper" in data:
cof.login_clipper = data['login_clipper']
profile.login_clipper = data['login_clipper']
if "departement" in data:
cof.departement = data['departement']
cof.save()
profile.departement = data['departement']
profile.save()
if data:
self.profile = cof
self.profile = profile
super(Account, self).save(*args, **kwargs)
# Surcharge de delete