Add some tests about how profiles types should relate to each other.
This commit is contained in:
parent
22da04c3e2
commit
25c3106168
2 changed files with 42 additions and 11 deletions
|
@ -10,10 +10,22 @@ from __future__ import division
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db.utils import IntegrityError
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from gestion.models import Profile, User
|
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):
|
class SimpleTest(TestCase):
|
||||||
def test_delete_user(self):
|
def test_delete_user(self):
|
||||||
|
@ -31,3 +43,22 @@ class SimpleTest(TestCase):
|
||||||
u.save()
|
u.save()
|
||||||
Profile.objects.get(user__username='foo').delete()
|
Profile.objects.get(user__username='foo').delete()
|
||||||
self.assertFalse(User.objects.filter(username='foo').exists())
|
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)
|
||||||
|
|
|
@ -150,7 +150,7 @@ class Account(models.Model):
|
||||||
perms.add('kfet.perform_negative_operations')
|
perms.add('kfet.perform_negative_operations')
|
||||||
return perms, stop_ope
|
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:
|
# Args:
|
||||||
# - data : datas pour User et CofProfile
|
# - data : datas pour User et CofProfile
|
||||||
# Action:
|
# Action:
|
||||||
|
@ -166,10 +166,10 @@ class Account(models.Model):
|
||||||
user.last_name = data.get("last_name", user.last_name)
|
user.last_name = data.get("last_name", user.last_name)
|
||||||
user.email = data.get("email", user.email)
|
user.email = data.get("email", user.email)
|
||||||
user.save()
|
user.save()
|
||||||
# Updating CofProfile with data
|
# Updating Profile with data
|
||||||
cof = self.profile
|
profile = self.profile
|
||||||
cof.departement = data.get("departement", cof.departement)
|
profile.departement = data.get("departement", profile.departement)
|
||||||
cof.save()
|
profile.save()
|
||||||
elif data:
|
elif data:
|
||||||
# New account
|
# New account
|
||||||
|
|
||||||
|
@ -192,15 +192,15 @@ class Account(models.Model):
|
||||||
if "email" in data:
|
if "email" in data:
|
||||||
user.email = data['email']
|
user.email = data['email']
|
||||||
user.save()
|
user.save()
|
||||||
# Creating or updating CofProfile instance
|
# Creating or updating Profile instance
|
||||||
(cof, _) = Profile.objects.get_or_create(user=user)
|
(profile, _) = Profile.objects.get_or_create(user=user)
|
||||||
if "login_clipper" in data:
|
if "login_clipper" in data:
|
||||||
cof.login_clipper = data['login_clipper']
|
profile.login_clipper = data['login_clipper']
|
||||||
if "departement" in data:
|
if "departement" in data:
|
||||||
cof.departement = data['departement']
|
profile.departement = data['departement']
|
||||||
cof.save()
|
profile.save()
|
||||||
if data:
|
if data:
|
||||||
self.profile = cof
|
self.profile = profile
|
||||||
super(Account, self).save(*args, **kwargs)
|
super(Account, self).save(*args, **kwargs)
|
||||||
|
|
||||||
# Surcharge de delete
|
# Surcharge de delete
|
||||||
|
|
Loading…
Reference in a new issue