Commit d9b9640b by Dudás Ádám

school: some refactoring and more test for Person

parent 235ff37f
......@@ -31,7 +31,7 @@ def create_user_profile(sender, instance, created, **kwargs):
p = Person.objects.get(code=instance.username)
except Person.DoesNotExist:
p = Person.objects.create(code=instance.username)
except Exception as e:
except Exception as e: # pragma: no cover
logger.warning("Couldn't create profile for user: %(username)s"
"\nReason: %(exception)s",
{"username": instance.username,
......@@ -42,6 +42,9 @@ def create_user_profile(sender, instance, created, **kwargs):
post_save.connect(create_user_profile, sender=User)
class Person(models.Model):
"""
Personal settings and attributes of a user.
"""
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)
......@@ -59,9 +62,9 @@ class Person(models.Model):
def short_name(self):
if self.user:
if self.user.last_name:
if self.user.last_name: # pragma: no cover
return self.user.last_name
else:
else: # pragma: no cover
return self.user.username
else:
return self.code
......
from datetime import datetime, timedelta
from django.test import TestCase
from django.contrib.auth.models import User
from models import create_user_profile, Person, Course, Semester, Group
from datetime import datetime, timedelta
class MockUser:
username = "testuser"
class CreateUserProfileTestCase(TestCase):
def setUp(self):
self.user = MockUser()
for p in Person.objects.all():
p.delete()
self.user = User(username="testuser", password="testpass",
email="test@mail.com", first_name="Test", last_name="User")
Person.objects.all().delete()
with self.assertRaises(Person.DoesNotExist):
Person.objects.get(code=self.user.username)
def test_new_profile(self):
"""Test profile creation functionality for new user."""
create_user_profile(self.user.__class__, self.user, True)
self.assertEqual(Person.objects.filter(
code=self.user.username).count(), 1)
self.assertIsNotNone(Person.objects.get(code=self.user.username))
def test_existing_profile(self):
"""Test profile creation functionality when it already exists."""
Person.objects.create(code=self.user.username)
create_user_profile(self.user.__class__, self.user, True)
self.assertEqual(Person.objects.filter(
code=self.user.username).count(), 1)
self.assertIsNotNone(Person.objects.get(code=self.user.username))
class PersonTestCase(TestCase):
def setUp(self):
self.testperson = Person.objects.create(code='testperson')
"""Test 'static' Person facts."""
def test_language_code_in_choices(self):
"""Test whether the default value for language is a valid choice."""
# TODO
language_field = self.testperson._meta.get_field('language')
person = Person(code="test")
language_field = person._meta.get_field('language')
choice_codes = [code for (code, _) in language_field.choices]
self.assertIn(language_field.default, choice_codes)
class PersonWithUserTestCase(TestCase):
"""Test Person entities which have their user attribute set."""
def setUp(self):
self.user = User(username="testuser", password="testpass",
email="test@mail.com", first_name="Test", last_name="User")
Person.objects.all().delete()
self.person = Person.objects.create(code='testcode', user=self.user)
def test_get_owned_shares(self):
self.assertIsNotNone(self.person.get_owned_shares())
def test_get_shares(self):
self.assertIsNotNone(self.person.get_shares())
def test_short_name(self):
self.assertIsNotNone(self.person.short_name())
# without first or last name
self.person.user.first_name = None
self.person.user.last_name = None
self.assertIsNotNone(self.person.short_name())
def test_unicode(self):
self.assertIsNotNone(self.person.__unicode__())
class PersonWithoutUserTestCase(TestCase):
"""Test Person entities which doesn't have their user attribute set."""
def setUp(self):
Person.objects.all().delete()
self.person = Person.objects.create(code='testcode')
def test_get_owned_shares(self):
# TODO
self.testperson.get_owned_shares()
self.assertIsNotNone(self.person.get_owned_shares())
def test_get_shares(self):
# TODO
self.testperson.get_shares()
self.assertIsNotNone(self.person.get_shares())
def test_short_name(self):
# TODO
self.testperson.short_name()
self.assertIsNotNone(self.person.short_name())
def test_unicode(self):
# TODO
self.testperson.__unicode__()
self.assertIsNotNone(self.person.__unicode__())
class CourseTestCase(TestCase):
def setUp(self):
......
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