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