diff --git a/api/serializers.py b/api/serializers.py index 96ef0cc..347bd65 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -14,7 +14,10 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer): """ def __init__(self, *args, **kwargs): - if not hasattr(self.Meta, 'url_namespace') or self.Meta.url_namespace is None: + if not hasattr(self, 'Meta') \ + or not hasattr(self.Meta, 'url_namespace') \ + or self.Meta.url_namespace is None \ + or self.Meta.url_namespace == '': raise ImproperlyConfigured("namespace must be set!") self.url_namespace = self.Meta.url_namespace diff --git a/api/tests.py b/api/tests.py index 45fa6d1..64acb65 100644 --- a/api/tests.py +++ b/api/tests.py @@ -3,15 +3,51 @@ Test cases for API calls """ -from django.contrib.auth.models import User from django.conf import settings +from django.contrib.auth.models import User +from django.core.exceptions import ImproperlyConfigured +from django.test import TestCase from django_webtest import WebTest import json +from .serializers import NamespacedSerializer from booking.ducklevel import level_to_up_minutes from booking.models import Species, Location, Duck, Competence, DuckCompetence +class MetalessNamespacedSerializer(NamespacedSerializer): + pass + +class MissingNamespacedSerializer(NamespacedSerializer): + class Meta: + pass + +class NoneNamespacedSerializer(NamespacedSerializer): + class Meta: + url_namespace = None + +class EmptyNamespacedSerializer(NamespacedSerializer): + class Meta: + url_namespace = '' + +class TestNamespacedSerializer(TestCase): + """ + Test namespaced Serializer + """ + + def test_no_namespace(self): + with self.assertRaises(ImproperlyConfigured): + serializer = MetalessNamespacedSerializer() + + with self.assertRaises(ImproperlyConfigured): + serializer = MissingNamespacedSerializer() + + with self.assertRaises(ImproperlyConfigured): + serializer = NoneNamespacedSerializer() + + with self.assertRaises(ImproperlyConfigured): + serializer = EmptyNamespacedSerializer() + class DuckClassTest(WebTest): """ Test case for duck related API calls diff --git a/booking/models.py b/booking/models.py index 977835a..30acf52 100644 --- a/booking/models.py +++ b/booking/models.py @@ -140,16 +140,24 @@ class DuckName(models.Model): """ duck = models.ForeignKey(Duck) - name = models.CharField(max_length=60) + name = models.CharField(max_length=60, null=False) suggested_by = models.ForeignKey(User) suggested_at = models.DateTimeField(default=timezone.now) - closed_by = models.ForeignKey(User, related_name='+') - closed_at = models.DateTimeField(null=True) + closed_by = models.ForeignKey(User, + related_name='+', + null=True, + blank=True) + closed_at = models.DateTimeField(null=True, blank=True) def __str__(self): - return "{0}, suggested by {1}".format(self.name, + ret = "{0}, suggested by {1}".format(self.name, self.suggested_by) + if self.closed_by: + ret += " " + + return ret + @python_2_unicode_compatible class DuckNameVote(models.Model): """ @@ -163,7 +171,7 @@ class DuckNameVote(models.Model): def __str__(self): return "{0} voted {1} for {2}".format(self.voter, - "up" if upvote else "down", + "up" if self.upvote else "down", self.duck_name) @python_2_unicode_compatible @@ -235,6 +243,7 @@ class Booking(models.Model): select_params=('%s', '%s'))[0].amount def __str__(self): - return "{0} booked by {1} since {2}".format(self.duck, - self.user, - self.start_ts) + return "{0} booked by {1} for {2} since {3}".format(self.duck, + self.user, + self.comp_req, + self.start_ts) diff --git a/booking/tests.py b/booking/tests.py index 02dc18a..8d6f4b0 100644 --- a/booking/tests.py +++ b/booking/tests.py @@ -12,7 +12,8 @@ from django.utils import timezone from .ducklevel import level_to_up_minutes, level_to_down_minutes, minutes_to_level from .templatetags import booking_tags -from .models import Duck, Competence, DuckCompetence, Species, Location, Booking +from .models import Duck, Competence, DuckCompetence, Species, \ + Location, Booking, DuckName, DuckNameVote class FrontTest(TestCase): """ @@ -415,3 +416,78 @@ class BookingTest(TestCase): with self.assertRaises(RuntimeError): self.booked_duck.booked_by() + +class StrTest(TestCase): + """ + Test case for models’ __str__() method + """ + + def setUp(self): + self.user = User.objects.create_user(username='test') + self.location = Location.objects.create(name="A Location") + self.species = Species.objects.create(name="Duck") + self.competence = Competence.objects.create(name="Testing", + added_by=self.user) + + self.duck = Duck.objects.create(name="First Duck", + species=self.species, + location=self.location, + donated_by=self.user) + + def test_location_str(self): + self.assertEquals("A Location", self.location.__str__()) + + def test_species_str(self): + self.assertEquals("Duck", self.species.__str__()) + + def test_competence_str(self): + self.assertEquals("Testing", self.competence.__str__()) + + def test_duck_str(self): + self.assertEquals("First Duck", self.duck.__str__()) + + def test_duckname_str(self): + name_suggestion = DuckName.objects.create(name="New Duck", + duck=self.duck, + suggested_by=self.user) + self.assertEquals("New Duck, suggested by test", + name_suggestion.__str__()) + + name_suggestion.closed_by = self.user + + self.assertEquals("New Duck, suggested by test ", + name_suggestion.__str__()) + + def test_ducknamevote_str(self): + name_suggestion = DuckName.objects.create(name="New Duck", + duck=self.duck, + suggested_by=self.user) + + vote = DuckNameVote.objects.create(duck_name=name_suggestion, + voter=self.user, + upvote=False) + + self.assertEquals("test voted down for New Duck, suggested by test", + vote.__str__()) + + vote.upvote = True + + self.assertEquals("test voted up for New Duck, suggested by test", + vote.__str__()) + + def test_duckcompetence_str(self): + dcomp = DuckCompetence.objects.create(duck=self.duck, + comp=self.competence) + + self.assertEquals("First Duck with +0/-0 minutes in Testing", + dcomp.__str__()) + + def test_booking_str(self): + start = timezone.now() + booking = Booking.objects.create(duck=self.duck, + user=self.user, + comp_req=self.competence, + start_ts=start) + + self.assertEquals("First Duck booked by test for Testing since {0}".format(start), + booking.__str__())