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

Added authors model, validation and testing #46

Merged
merged 6 commits into from
Jun 2, 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
26 changes: 26 additions & 0 deletions codechallenges/migrations/0009_auto_20210525_0030.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.1.4 on 2021-05-25 00:30

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('codechallenges', '0008_auto_20210522_1942'),
]

operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('firstname', models.CharField(max_length=64)),
('lastname', models.CharField(max_length=64)),
],
),
migrations.AddField(
model_name='question',
name='authors',
field=models.ManyToManyField(to='codechallenges.Author'),
),
]
14 changes: 14 additions & 0 deletions codechallenges/migrations/0010_merge_20210602_0049.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 3.1.4 on 2021-06-02 00:49

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('codechallenges', '0009_auto_20210525_0030'),
('codechallenges', '0009_auto_20210527_1757'),
]

operations = [
]
9 changes: 8 additions & 1 deletion codechallenges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class Category(models.Model):
def __str__(self):
return self.title

class Author(models.Model):
firstname = models.CharField(max_length=64)
lastname = models.CharField(max_length=64)

def __str__(self):
return f"{self.firstname} {self.lastname}"

class Question(models.Model):
title = models.CharField(max_length=150)
Expand Down Expand Up @@ -50,7 +56,8 @@ class Question(models.Model):
CodeChallengeEvent, on_delete=models.CASCADE, blank=True, null=True
)

categories = models.ManyToManyField(Category, blank=True)
categories = models.ManyToManyField(Category)
authors = models.ManyToManyField(Author)

def __str__(self):
return self.title
Expand Down
11 changes: 10 additions & 1 deletion codechallenges/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers
from codechallenges.models import Question, Submission, Category
from codechallenges.models import Author, Question, Submission, Category


class CategorySerializer(serializers.ModelSerializer):
Expand All @@ -8,8 +8,15 @@ class Meta:
fields = ["title"]


class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = ["firstname", "lastname"]


class QuestionSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True, required=False)
authors = AuthorSerializer(many=True, required=False)

class Meta:
model = Question
Expand All @@ -22,6 +29,7 @@ class Meta:
"expiration_date",
"difficulty",
"categories",
"authors",
)


Expand All @@ -38,6 +46,7 @@ class Meta:
"expiration_date",
"difficulty",
"categories",
"authors",
)


Expand Down
14 changes: 13 additions & 1 deletion codechallenges/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase
from rest_framework.test import APIRequestFactory
from codechallenges.models import Category, Question, Submission
from codechallenges.models import Author, Category, Question, Submission
from .views import SubmissionList

import datetime
Expand All @@ -16,6 +16,12 @@ def setUp(self) -> None:
self.category2 = Category.objects.create(
title="Cryptography", body="KEY IS KEY"
)
self.author1 = Author.objects.create(
firstname="John", lastname="Wick"
)
self.author2 = Author.objects.create(
firstname="Doom", lastname="Guy"
)
self.question1 = Question.objects.create(
title="Yeet",
body="Blaghjdklahgjfkl",
Expand All @@ -42,6 +48,12 @@ def test_categories(self):
self.question2.categories.add(self.category1) # Not Added
self.assertEquals(len(self.question1.categories.all()), 2)

def test_authors(self):
self.question1.authors.add(self.author1) # Added
self.question1.authors.add(self.author2) # Added
self.question2.authors.add(self.author1) # Not Added
self.assertEquals(len(self.question1.authors.all()), 2)


class SubmissionTestCase(TestCase):
def setUp(self) -> None:
Expand Down