Increase test coverage

This commit is contained in:
Gergely Polonkai 2015-10-20 17:13:07 +02:00
parent d3ec0e8998
commit 54aa411019
4 changed files with 135 additions and 11 deletions

View File

@ -14,7 +14,10 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
""" """
def __init__(self, *args, **kwargs): 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!") raise ImproperlyConfigured("namespace must be set!")
self.url_namespace = self.Meta.url_namespace self.url_namespace = self.Meta.url_namespace

View File

@ -3,15 +3,51 @@
Test cases for API calls Test cases for API calls
""" """
from django.contrib.auth.models import User
from django.conf import settings 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 from django_webtest import WebTest
import json import json
from .serializers import NamespacedSerializer
from booking.ducklevel import level_to_up_minutes from booking.ducklevel import level_to_up_minutes
from booking.models import Species, Location, Duck, Competence, DuckCompetence 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): class DuckClassTest(WebTest):
""" """
Test case for duck related API calls Test case for duck related API calls

View File

@ -140,16 +140,24 @@ class DuckName(models.Model):
""" """
duck = models.ForeignKey(Duck) duck = models.ForeignKey(Duck)
name = models.CharField(max_length=60) name = models.CharField(max_length=60, null=False)
suggested_by = models.ForeignKey(User) suggested_by = models.ForeignKey(User)
suggested_at = models.DateTimeField(default=timezone.now) suggested_at = models.DateTimeField(default=timezone.now)
closed_by = models.ForeignKey(User, related_name='+') closed_by = models.ForeignKey(User,
closed_at = models.DateTimeField(null=True) related_name='+',
null=True,
blank=True)
closed_at = models.DateTimeField(null=True, blank=True)
def __str__(self): def __str__(self):
return "{0}, suggested by {1}".format(self.name, ret = "{0}, suggested by {1}".format(self.name,
self.suggested_by) self.suggested_by)
if self.closed_by:
ret += " <closed>"
return ret
@python_2_unicode_compatible @python_2_unicode_compatible
class DuckNameVote(models.Model): class DuckNameVote(models.Model):
""" """
@ -163,7 +171,7 @@ class DuckNameVote(models.Model):
def __str__(self): def __str__(self):
return "{0} voted {1} for {2}".format(self.voter, return "{0} voted {1} for {2}".format(self.voter,
"up" if upvote else "down", "up" if self.upvote else "down",
self.duck_name) self.duck_name)
@python_2_unicode_compatible @python_2_unicode_compatible
@ -235,6 +243,7 @@ class Booking(models.Model):
select_params=('%s', '%s'))[0].amount select_params=('%s', '%s'))[0].amount
def __str__(self): def __str__(self):
return "{0} booked by {1} since {2}".format(self.duck, return "{0} booked by {1} for {2} since {3}".format(self.duck,
self.user, self.user,
self.comp_req,
self.start_ts) self.start_ts)

View File

@ -12,7 +12,8 @@ from django.utils import timezone
from .ducklevel import level_to_up_minutes, level_to_down_minutes, minutes_to_level from .ducklevel import level_to_up_minutes, level_to_down_minutes, minutes_to_level
from .templatetags import booking_tags 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): class FrontTest(TestCase):
""" """
@ -415,3 +416,78 @@ class BookingTest(TestCase):
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
self.booked_duck.booked_by() 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 <closed>",
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__())