SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit de1087c0 authored by Geert Kapteijns's avatar Geert Kapteijns
Browse files

Set blank=False on domain field of ThesisLink

A blank domain throws an error in /theses/.
I've also installed factory_boy (pip install -r requirements.txt
to get it) and set up a ThesisLinkFactory and a unit tests that
tests whether ThesisLink validates for a blank domain field.

Not sure if this is exactly how you're supposed to validate
a model, but it's a start. First unit test of the project, whoohoo!
parent 9b7fbbd5
No related branches found
No related tags found
No related merge requests found
import factory
from . import models
from .models import ThesisLink
class ThesisLinkFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.ThesisLink
model = ThesisLink
vetted = True
type = models
# requested_by = factory.SubFactory(ContributorFactory)
type = ThesisLink.MASTER_THESIS
title = factory.Sequence(lambda n: "thesis {0}".format(n))
pub_link = factory.Faker('uri')
author = factory.Faker('name')
supervisor = factory.Faker('name')
institution = factory.Faker('company')
defense_date = factory.Faker('date_time_this_century')
abstract = factory.Faker('text')
......@@ -18,7 +18,7 @@ class ThesisLink(models.Model):
(PHD_THESIS, 'Ph.D.'),
(HABILITATION_THESIS, 'Habilitation'),
)
thesis_type_dict = dict(THESIS_TYPES)
THESIS_TYPES_DICT = dict(THESIS_TYPES)
""" An URL pointing to a thesis """
requested_by = models.ForeignKey(
......@@ -29,13 +29,13 @@ class ThesisLink(models.Model):
vetted_by = models.ForeignKey(
Contributor, blank=True, null=True,
on_delete=models.CASCADE)
type = models.CharField(choices=THESIS_TYPES)
type = models.CharField(choices=THESIS_TYPES, max_length=3)
discipline = models.CharField(
max_length=20, choices=SCIPOST_DISCIPLINES,
default='physics')
domain = models.CharField(
max_length=3, choices=SCIPOST_JOURNALS_DOMAINS,
blank=True)
blank=False)
subject_area = models.CharField(
max_length=10,
choices=SCIPOST_SUBJECT_AREAS,
......@@ -88,7 +88,7 @@ class ThesisLink(models.Model):
header += '<td>(not claimed)</td>'
header += (
'</tr>'
'<tr><td>Type: </td><td></td><td>' + thesis_type_dict[self.type] +
'<tr><td>Type: </td><td></td><td>' + self.THESIS_TYPES_DICT[self.type] +
'</td></tr>'
'<tr><td>Discipline: </td><td></td><td>' +
disciplines_dict[self.discipline] + '</td></tr>'
......@@ -119,7 +119,7 @@ class ThesisLink(models.Model):
'<li><div class="flex-container">'
'<div class="flex-whitebox0"><p><a href="/thesis/{{ id }}" '
'class="pubtitleli">{{ title }}</a></p>'
'<p>' + thesis_type_dict[self.type] + ' thesis by {{ author }} '
'<p>' + self.THESIS_TYPES_DICT[self.type] + ' thesis by {{ author }} '
'(supervisor(s): {{ supervisor }}) in ' +
disciplines_dict[self.discipline] + ', ' +
journals_domains_dict[self.domain] + ' ' +
......@@ -138,7 +138,7 @@ class ThesisLink(models.Model):
'<li><div class="flex-container">'
'<div class="flex-whitebox0"><p><a href="/thesis/{{ id }}" '
'class="pubtitleli">{{ title }}</a></p>'
'<p>' + thesis_type_dict[self.type] +
'<p>' + self.THESIS_TYPES_DICT[self.type] +
' thesis by {{ author }} </div></div></li>')
template = Template(header)
return template.render(context)
import re
from django.test import TestCase
from django.core.exceptions import ValidationError
from .models import ThesisLink
from .factories import ThesisLinkFactory
class ThesisLinkTestCase(TestCase):
def test_domain_cannot_be_blank(self):
thesis_link = ThesisLinkFactory()
thesis_link.domain = ""
self.assertRaisesRegexp(ValidationError, re.compile(r'domain'),
thesis_link.full_clean)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment