diff --git a/codechallenges/migrations/0004_submission_attempts.py b/codechallenges/migrations/0004_submission_attempts.py new file mode 100644 index 0000000..280a76f --- /dev/null +++ b/codechallenges/migrations/0004_submission_attempts.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.4 on 2021-04-30 01:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("codechallenges", "0003_submission_answer"), + ] + + operations = [ + migrations.AddField( + model_name="submission", + name="attempts", + field=models.IntegerField(default=0), + ), + ] diff --git a/codechallenges/migrations/0005_auto_20210430_0158.py b/codechallenges/migrations/0005_auto_20210430_0158.py new file mode 100644 index 0000000..f16ec36 --- /dev/null +++ b/codechallenges/migrations/0005_auto_20210430_0158.py @@ -0,0 +1,17 @@ +# Generated by Django 3.1.4 on 2021-04-30 01:58 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("codechallenges", "0004_submission_attempts"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="submission", + unique_together={("email", "question")}, + ), + ] diff --git a/codechallenges/models.py b/codechallenges/models.py index ec2c88c..2633a7d 100644 --- a/codechallenges/models.py +++ b/codechallenges/models.py @@ -38,3 +38,10 @@ class Submission(models.Model): question = models.ForeignKey( Question, on_delete=models.CASCADE, blank=True, null=True ) + attempts = models.IntegerField(default=0) + + class Meta: + unique_together = ( + "email", + "question", + ) diff --git a/codechallenges/tests.py b/codechallenges/tests.py index 7ce503c..f5811b6 100644 --- a/codechallenges/tests.py +++ b/codechallenges/tests.py @@ -1,3 +1,53 @@ from django.test import TestCase +from codechallenges.models import Question, Submission +import datetime # Create your tests here. + + +class SubmissionTestCase(TestCase): + def test_unique_together(self): + question1 = Question.objects.create( + title="Yeet", + body="Blaghjdklahgjfkl", + format="t", + answer="Yote", + release_date=datetime.date.today(), + expiration_date=datetime.date(2022, 4, 29), + ) + + # Verify question 1 is created + self.assertNotEquals(question1, None) + + submission1 = Submission.objects.create( + email="test123@gmail.com", + answer="Yote", + correct=True, + question=question1, + attempts=5, + ) + + # Verify submission 1 is created + self.assertNotEquals(submission1, None) + + # This should pass because it's not a dupe! + submission2 = Submission.objects.create( + email="test124@gmail.com", + answer="Y0te", + correct=False, + question=question1, + attempts=5, + ) + + # Verify submission 2 is created + self.assertNotEquals(submission2, None) + + # This should fail due to duplicate checking + with self.assertRaises(Exception): + submission3 = Submission.objects.create( + email="test123@gmail.com", + answer="Yate", + correct=False, + question=question1, + attempts=5, + )