Commit 762a6f06 by Dudás Ádám

school: some tests

parent 45b1ffcb
...@@ -6,27 +6,45 @@ from django.core.exceptions import ValidationError ...@@ -6,27 +6,45 @@ from django.core.exceptions import ValidationError
from datetime import datetime from datetime import datetime
from django.conf import settings from django.conf import settings
import one.models import one.models
import logging
LANGUAGE_CODE = settings.LANGUAGE_CODE LANGUAGE_CODE = settings.LANGUAGE_CODE
LANGUAGE_CHOICES = (('hu', _('Hungarian')), ('en', _('English'))) LANGUAGE_CHOICES = (('hu', _('Hungarian')), ('en', _('English')))
logger = logging.getLogger(__name__)
def create_user_profile(sender, instance, created, **kwargs): 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: if created:
try: try:
p = Person.objects.get(code=instance.username) p = Person.objects.get(code=instance.username)
except Exception: except Person.DoesNotExist:
p = Person.objects.create(code=instance.username) 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 return
p.code = instance.username p.clean()
p.save() p.save()
post_save.connect(create_user_profile, sender=User) post_save.connect(create_user_profile, sender=User)
class Person(models.Model): class Person(models.Model):
user = models.ForeignKey(User, null=True, blank=True, unique=True) user = models.ForeignKey(User, null=True, blank=True, unique=True)
language = models.CharField(verbose_name=_('language'), blank=False, max_length=10, language = models.CharField(verbose_name=_('language'), blank=False,
choices=LANGUAGE_CHOICES, default=LANGUAGE_CODE) max_length=10, choices=LANGUAGE_CHOICES, default=LANGUAGE_CODE)
code = models.CharField(_('code'), max_length=30, unique=True) code = models.CharField(_('code'), max_length=30, unique=True)
def get_owned_shares(self): def get_owned_shares(self):
......
""" from django.test import TestCase
This file demonstrates writing tests using the unittest module. These will pass from models import create_user_profile, Person
when you run "manage.py test".
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_language_code_in_choices(self):
def test_basic_addition(self): """Test whether the default value for language is a valid choice."""
""" language_field = Person.objects.all()[0]._meta.get_field('language')
Tests that 1 + 1 always equals 2. choice_codes = [code for (code, _) in language_field.choices]
""" self.assertIn(language_field.default, choice_codes)
self.assertEqual(1 + 1, 2)
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