Skip to content
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
18 changes: 18 additions & 0 deletions codechallenges/migrations/0004_submission_attempts.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
17 changes: 17 additions & 0 deletions codechallenges/migrations/0005_auto_20210430_0158.py
Original file line number Diff line number Diff line change
@@ -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")},
),
]
7 changes: 7 additions & 0 deletions codechallenges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
50 changes: 50 additions & 0 deletions codechallenges/tests.py
Original file line number Diff line number Diff line change
@@ -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,
)