gestioCOF/gestion/tests.py

61 lines
2.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
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):
u = User(username='foo', first_name='foo', last_name='bar')
# to each user there's a cofprofile associated
u.save()
self.assertTrue(Profile.objects.filter(user__username='foo').exists())
# there's no point in having a cofprofile without a user associated.
u.delete()
self.assertFalse(Profile.objects.filter(user__username='foo').exists())
# there's no point in having a user without a cofprofile associated.
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)