test models
This commit is contained in:
parent
1c3867367d
commit
a46a484b85
2
Makefile
2
Makefile
|
@ -12,5 +12,5 @@ help:
|
|||
@echo " coverage to run coverage check of the source files."
|
||||
|
||||
coverage:
|
||||
coverage run --source='gwift' gwift/manage.py test; coverage report; coverage html;
|
||||
coverage run --source='.' src/manage.py test; coverage report; coverage html;
|
||||
@echo "Testing of coverage in the sources finished."
|
|
@ -14,7 +14,7 @@ class AbstractModel(models.Model):
|
|||
class UnknownUser(models.Model):
|
||||
|
||||
name = models.CharField(max_length=255)
|
||||
email = models.CharField(max_length=255)
|
||||
email = models.CharField(max_length=255, unique=True)
|
||||
|
||||
class Wishlist(AbstractModel):
|
||||
|
||||
|
@ -22,14 +22,6 @@ class Wishlist(AbstractModel):
|
|||
description = models.TextField()
|
||||
external_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
@staticmethod
|
||||
def create(name, description):
|
||||
w = Wishlist()
|
||||
w.name = name
|
||||
w.description = description
|
||||
w.save()
|
||||
return w
|
||||
|
||||
|
||||
class Wish(AbstractModel):
|
||||
|
||||
|
@ -42,31 +34,20 @@ class Wish(AbstractModel):
|
|||
estimated_price = models.DecimalField(max_digits=19, decimal_places=2,
|
||||
null=True)
|
||||
|
||||
@staticmethod
|
||||
def create(name, description, wishlist):
|
||||
i = Wish()
|
||||
i.name = name
|
||||
i.description = description
|
||||
i.wishlist = wishlist
|
||||
i.save()
|
||||
return i
|
||||
|
||||
@property
|
||||
def percentage(self):
|
||||
def percentage_of_completion(self):
|
||||
"""
|
||||
Calcule le pourcentage de complétion pour un élément.
|
||||
"""
|
||||
number_of_linked_parts = Part.objects.filter(wish=self).count()
|
||||
number_of_linked_parts = WishPart.objects.filter(wish=self).count()
|
||||
total = self.number_of_parts * self.numbers_available
|
||||
percentage = (number_of_linked_parts / total)
|
||||
return percentage * 100
|
||||
|
||||
class WishPart(models.Model):
|
||||
wish = models.ForeignKey(Wish)
|
||||
user = models.ForeignKey(User)
|
||||
unknown_user = models.ForeignKey('UnknownUser')
|
||||
comment = models.TextField()
|
||||
done_at = models.DateTimeField()
|
||||
|
||||
def save(self, force_insert=False, force_update=False, commit=True):
|
||||
pass
|
||||
wish = models.ForeignKey(Wish)
|
||||
user = models.ForeignKey(User, null=True)
|
||||
unknown_user = models.ForeignKey('UnknownUser', null=True)
|
||||
comment = models.TextField(null=True, blank=True)
|
||||
done_at = models.DateTimeField(auto_now_add=True)
|
||||
|
|
|
@ -1,3 +1,35 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from .models import Wishlist, Wish, WishPart
|
||||
|
||||
class TestWishModel(TestCase):
|
||||
def test_percentage_of_completion(self):
|
||||
"""
|
||||
Vérifie que le pourcentage de complétion d'un souhait
|
||||
est correctement calculé.
|
||||
"""
|
||||
wishlist = Wishlist(name='Fake WishList',
|
||||
description='This is a faked wishlist')
|
||||
wishlist.save()
|
||||
|
||||
wish = Wish(wishlist=wishlist,
|
||||
name='Fake Wish',
|
||||
description='This is a faked wish',
|
||||
number_of_parts=4)
|
||||
wish.save()
|
||||
|
||||
part1 = WishPart(wish=wish, comment='part1')
|
||||
part1.save()
|
||||
self.assertEqual(25, wish.percentage_of_completion)
|
||||
|
||||
part2 = WishPart(wish=wish, comment='part2')
|
||||
part2.save()
|
||||
self.assertEqual(50, wish.percentage_of_completion)
|
||||
|
||||
part3 = WishPart(wish=wish, comment='part3')
|
||||
part3.save()
|
||||
self.assertEqual(75, wish.percentage_of_completion)
|
||||
|
||||
part4 = WishPart(wish=wish, comment='part4')
|
||||
part4.save()
|
||||
self.assertEqual(100, wish.percentage_of_completion)
|
Loading…
Reference in New Issue