From de1087c0dec15a6075a985e2222f2c7471f11ea8 Mon Sep 17 00:00:00 2001 From: Geert Kapteijns <ghkapteijns@gmail.com> Date: Thu, 15 Dec 2016 00:42:18 +0100 Subject: [PATCH] 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! --- theses/factories.py | 15 +++++++++++---- theses/models.py | 12 ++++++------ theses/test_models.py | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/theses/factories.py b/theses/factories.py index 926d1c192..60e67d1ae 100644 --- a/theses/factories.py +++ b/theses/factories.py @@ -1,10 +1,17 @@ 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') diff --git a/theses/models.py b/theses/models.py index b2bfe6242..139948b91 100644 --- a/theses/models.py +++ b/theses/models.py @@ -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) diff --git a/theses/test_models.py b/theses/test_models.py index 2e9cb5f6b..4308ab25f 100644 --- a/theses/test_models.py +++ b/theses/test_models.py @@ -1 +1,15 @@ +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) -- GitLab