Commit 762a6f06 by Dudás Ádám

school: some tests

parent 45b1ffcb
......@@ -6,27 +6,45 @@ from django.core.exceptions import ValidationError
from datetime import datetime
from django.conf import settings
import one.models
import logging
LANGUAGE_CODE = settings.LANGUAGE_CODE
LANGUAGE_CHOICES = (('hu', _('Hungarian')), ('en', _('English')))
logger = logging.getLogger(__name__)
def create_user_profile(sender, instance, created, **kwargs):
"""
User creation hook.
Ensure that the specified user has an associated profile.
@param sender: The model class.
@type instance: User
@param instance: The instance being saved.
@type created: Boolean
@param created: True if a new record was created.
"""
if created:
try:
p = Person.objects.get(code=instance.username)
except Exception:
except Person.DoesNotExist:
p = Person.objects.create(code=instance.username)
except:
except Exception as e:
logger.warning("Couldn't create profile for user: %(username)s"
"\nReason: %(exception)s",
{"username": instance.username,
"exception": e})
return
p.code = instance.username
p.clean()
p.save()
post_save.connect(create_user_profile, sender=User)
class Person(models.Model):
user = models.ForeignKey(User, null=True, blank=True, unique=True)
language = models.CharField(verbose_name=_('language'), blank=False, max_length=10,
choices=LANGUAGE_CHOICES, default=LANGUAGE_CODE)
language = models.CharField(verbose_name=_('language'), blank=False,
max_length=10, choices=LANGUAGE_CHOICES, default=LANGUAGE_CODE)
code = models.CharField(_('code'), max_length=30, unique=True)
def get_owned_shares(self):
......@@ -51,7 +69,7 @@ class Person(models.Model):
if u.last_name and u.first_name:
# TRANSLATORS: full name format used in enumerations
return _("%(first)s %(last)s") % {'first': u.first_name,
'last': u.last_name}
'last': u.last_name}
else:
return u.username
......
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
from django.test import TestCase
from models import create_user_profile, Person
Replace this with more appropriate tests for your application.
"""
class MockUser:
username = "testuser"
class CreateUserProfileTestCase(TestCase):
def setUp(self):
for p in Person.objects.all():
p.delete()
def test_new_profile(self):
"""Test profile creation functionality for new user."""
user = MockUser()
create_user_profile(user.__class__, user, True)
self.assertEqual(Person.objects.filter(code=user.username).count(), 1)
def test_existing_profile(self):
"""Test profile creation functionality when it already exists."""
user = MockUser()
Person.objects.create(code=user.username)
create_user_profile(user.__class__, user, True)
self.assertEqual(Person.objects.filter(code=user.username).count(), 1)
from django.test import TestCase
class PersonTestCase(TestCase):
def setUp(self):
Person.objects.create()
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
def test_language_code_in_choices(self):
"""Test whether the default value for language is a valid choice."""
language_field = Person.objects.all()[0]._meta.get_field('language')
choice_codes = [code for (code, _) in language_field.choices]
self.assertIn(language_field.default, choice_codes)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment