Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure the user field in categorizers table is one to one #148

Merged
merged 10 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ponder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
53 changes: 52 additions & 1 deletion ponder/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand Down Expand Up @@ -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)