Increase test coverage

This commit is contained in:
Gergely Polonkai 2015-10-20 11:16:58 +02:00
parent 2b2c91bab6
commit 77a5bf35de
2 changed files with 59 additions and 34 deletions

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8
from django.test import TestCase, Client from django.test import TestCase, Client
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
@ -18,31 +19,31 @@ class DuckClassTest(WebTest):
self.user = User.objects.create_user(username='test', self.user = User.objects.create_user(username='test',
password='test') password='test')
spec = Species.objects.create(name = 'duck') spec = Species.objects.create(name='duck')
loc = Location.objects.create(name = 'temp') loc = Location.objects.create(name='temp')
self.comp_bad = Competence.objects.create(name = 'test1', self.comp_bad = Competence.objects.create(name='test1',
added_by = self.user) added_by=self.user)
self.comp_good = Competence.objects.create(name = 'test2', self.comp_good = Competence.objects.create(name='test2',
added_by = self.user) added_by=self.user)
self.duck = Duck.objects.create(species = spec, self.duck = Duck.objects.create(species=spec,
name='test duck', name='test duck',
location = loc, location=loc,
donated_by = self.user, donated_by=self.user,
color='123456') color='123456')
DuckCompetence.objects.create(duck = self.duck, DuckCompetence.objects.create(duck=self.duck,
comp = self.comp_bad, comp=self.comp_bad,
up_minutes = bad_minutes, up_minutes=bad_minutes,
down_minutes = 0) down_minutes=0)
DuckCompetence.objects.create(duck = self.duck, DuckCompetence.objects.create(duck=self.duck,
comp = self.comp_good, comp=self.comp_good,
up_minutes = good_minutes, up_minutes=good_minutes,
down_minutes = 0) down_minutes=0)
def test_book_nonlogged(self): def test_book_nonlogged(self):
page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True) page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True)
@ -70,10 +71,14 @@ class DuckClassTest(WebTest):
self.assertEqual(404, page.status_code) self.assertEqual(404, page.status_code)
def test_book_warn(self): def test_book_warn(self):
test_data = {
'competence': self.comp_bad.pk,
}
url = '/api/v1/ducks/%d/book/' % self.duck.pk url = '/api/v1/ducks/%d/book/' % self.duck.pk
comp_none = Competence.objects.create(name='test3',
added_by=self.user)
# Book for a competence the duck doesnt have at all
test_data = {
'competence': comp_none.pk,
}
page = self.app.post(url, params=test_data, user=self.user) page = self.app.post(url, params=test_data, user=self.user)
self.assertEquals(200, page.status_code) self.assertEquals(200, page.status_code)
@ -81,6 +86,18 @@ class DuckClassTest(WebTest):
page_json = json.loads(page.content) page_json = json.loads(page.content)
self.assertEquals(page_json['status'], 'bad-comp') self.assertEquals(page_json['status'], 'bad-comp')
# Book for a competence with low level
test_data = {
'competence': self.comp_bad.pk,
}
page = self.app.post(url, params=test_data, user=self.user)
self.assertEquals(200, page.status_code)
page_json = json.loads(page.content)
self.assertEquals(page_json['status'], 'bad-comp')
# Forcibly book for a competence with low level
test_data['force'] = 1 test_data['force'] = 1
page = self.app.post(url, params=test_data, user=self.user) page = self.app.post(url, params=test_data, user=self.user)

View File

@ -271,29 +271,37 @@ class SimilarCompTest(TestCase):
class BookingTest(TestCase): class BookingTest(TestCase):
def setUp(self): def setUp(self):
spec = Species(name = 'test') spec = Species.objects.create(name='test')
spec.save()
loc = Location(name = 'test') loc = Location.objects.create(name='test')
loc.save()
user = User(username = 'test') self.user = User.objects.create_user(username='test')
user.save()
self.booked_duck = Duck(species = spec, location = loc, donated_by = user) self.booked_duck = Duck.objects.create(species=spec,
self.booked_duck.save() location=loc,
donated_by=self.user)
comp = Competence(name = 'test', added_by = user) self.comp = Competence.objects.create(name='test',
comp.save() added_by=self.user)
booking = Booking(duck = self.booked_duck, user = user, comp_req = comp) booking = Booking.objects.create(duck=self.booked_duck,
booking.save() user=self.user,
comp_req=self.comp)
self.unbooked_duck = Duck(species = spec, location = loc, donated_by = user) self.unbooked_duck = Duck.objects.create(species=spec,
self.unbooked_duck.save() location=loc,
donated_by=self.user)
def test_booked_duck(self): def test_booked_duck(self):
self.assertNotEqual(self.booked_duck.booked_by(), None) self.assertNotEqual(self.booked_duck.booked_by(), None)
def test_unbooked_duck(self): def test_unbooked_duck(self):
self.assertEqual(self.unbooked_duck.booked_by(), None) self.assertEqual(self.unbooked_duck.booked_by(), None)
def test_multiple_booking(self):
Booking.objects.create(duck=self.booked_duck,
user=self.user,
comp_req=self.comp)
with self.assertRaises(RuntimeError):
self.booked_duck.booked_by()