diff --git a/ponder/models.py b/ponder/models.py index 2398aae..7cf2575 100644 --- a/ponder/models.py +++ b/ponder/models.py @@ -62,7 +62,9 @@ def email_categorizer(self): class Categorizer(models.Model): name = models.CharField(unique=True, max_length=254) initials = models.CharField(unique=True, max_length=3) - user = models.CharField(max_length=254) + + # The Django user this categorizer is related to. User is the Django user. + user = models.OneToOneField(User, to_field="username", db_column='user', on_delete=models.DO_NOTHING) class Meta: db_table = 'categorizers' diff --git a/ponder/tests.py b/ponder/tests.py index 6ae0ac7..afd8f7e 100644 --- a/ponder/tests.py +++ b/ponder/tests.py @@ -5,7 +5,8 @@ from ponder.models import Categorization, Categorizer, Commit, Dataset, ProblemCategory, ProblemCause, ProblemSymptom, ProblemFix from django.contrib.auth.models import User from ponder.forms import CategorizationForm -# Create your tests here. + +from django.db import IntegrityError class AddCategorizationFormTests(TestCase): ''' @@ -231,3 +232,53 @@ def test_category_text_not_null_and_func_fix_null(self): symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": False}, user='testUser') self.assertFalse(form.is_valid()) # The form should not be valid. self.assertEqual(form.errors["is_func_fix"], ["This field should be checked. An existing problem category indicates a bug fix."]) + +class CategorizerTests(TestCase): + @classmethod + def setUpTestData(self): + # Create two new Django users. + self.user1 = User.objects.create_user(username='testUser1', password='testpassword') + self.user2 = User.objects.create_user(username='testUser2', password='testpassword') + + # Case when a new categorizer is being added when the given name already exists in the table. + def test_same_name(self): + # Make sure there are no other categorizers. + Categorizer.objects.all().delete() + + # Create a categorizer John Smith. + Categorizer.objects.create(name='John Smith', initials='JS', user=self.user1) + + # Second categorizer is inserted with the same name and different initials and username. + Categorizer.objects.create(name='John Smith', initials='AB', user=self.user2) + + # Both testUser1 and testUser2 with the name "John Smith" should be in the table because two categorziers can have the same name + self.assertTrue(Categorizer.objects.filter(user='testUser1').exists()) + self.assertTrue(Categorizer.objects.filter(user='testUser2').exists()) + + # Case when a new categorizer is being added when the given initial already exists in the table. + def test_same_initials(self): + # Make sure there are no other categorizers. + Categorizer.objects.all().delete() + + # Create a categorizer John Smith. + Categorizer.objects.create(name='John Smith', initials='JS', user=self.user1) + + # Second categorizer is inserted with the same initials and different name and username. + # Exception here because the two users have the same initials. + self.assertRaises(IntegrityError, Categorizer.objects.create, name='Jane Scott', initials='JS', user=self.user2) + + # testUser1 with the initials "JS" should be in the table but not testUser2 because two categorziers can't have the same initials. + self.assertTrue(Categorizer.objects.filter(user='testUser1').exists()) + self.assertFalse(Categorizer.objects.filter(user='testUser2').exists()) + + # Case when a new categorizer is being added when the given user already exists in the table + def test_same_user(self): + # Make sure there are no other categorizers. + Categorizer.objects.all().delete() + + # Create a categorizer John Smith. + Categorizer.objects.create(name='John Smith', initials='JS', user=self.user1) + + # The same user is inserted again with different name and initials. + # Expecting an exception because two categorizers can't be related to the same Django user. + self.assertRaises(IntegrityError, Categorizer.objects.create, name='Michelle Reed', initials='MR', user=self.user1)