Skip to content
Snippets Groups Projects
Commit 737df68c authored by fu2662cw's avatar fu2662cw :speech_balloon:
Browse files

Add docstrings and fix other linter warnings

parent c98b1826
No related branches found
No related tags found
No related merge requests found
""" Model definitions for the quiz """
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
class CriterionRating(models.Model):
"""
This is the relation between Sport and Criterion.
You can use it to add a rating for a specific criterion to a sport.
To see it's usage check Sport.rate() and Sport.get_rating()
"""
rating = models.IntegerField(
validators=[MaxValueValidator(10), MinValueValidator(0)] # validation on user level (not db level)
validators=[MaxValueValidator(10), MinValueValidator(0)]
)
criterion = models.ForeignKey("Criterion", on_delete=models.CASCADE)
sport = models.ForeignKey("Sport", on_delete=models.CASCADE)
class Sport(models.Model):
"""
Defines a Sport with name, url that leads to the booking page.
A sport includes ratings for all criterions.
(e.g. How much it corresponds to the criterion "Martial Arts")
"""
name = models.TextField()
url = models.URLField()
criteria_ratings = models.ManyToManyField("Criterion", through="CriterionRating")
......@@ -19,13 +33,20 @@ class Sport(models.Model):
return self.name
def rate(self, criterion, rating):
rating = CriterionRating(sport=self, criterion=criterion, rating=10)
"""Defines how much (rating) the sport meets the given criterion"""
rating = CriterionRating(sport=self, criterion=criterion, rating=rating)
rating.save()
def get_rating(self, criterion):
"""Returns how much the sport meets the given criterion"""
criterion_rating = CriterionRating.objects.get(sport=self, criterion=criterion)
return criterion_rating.rating
class Criterion(models.Model):
"""
Defines a Sport property that is used a a criterion for our quiz.
(e.g. Individual or Team sport)
"""
name = models.TextField()
from django.test import TestCase
""" This module tests all our quiz models"""
from django.test import TestCase
from .models import Sport, Criterion
class SportModelTest(TestCase):
"""Tests the sport model"""
def setUp(self):
self.name = "HIIT"
self.url = "https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_-_High_Intensity_Interval_Training___HOME.html"
self.url = "https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_" \
"-_High_Intensity_Interval_Training___HOME.html "
self.testSport = Sport(
self.test_sport = Sport(
name=self.name,
url=self.url,
)
self.testSport.save()
self.test_sport.save()
def test_sport_can_be_created(self):
self.assertEquals(self.testSport.name, self.name)
self.assertEquals(self.testSport.url, self.url)
""" New sport is written to the database """
test_sport = Sport.objects.first()
self.assertEqual(test_sport.name, self.name)
self.assertEqual(test_sport.url, self.url)
class CriterionRatingTest(TestCase):
"""Tests the Relation between Sport and Criterion"""
def setUp(self):
self.name = "HIIT"
self.url = "https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_-_High_Intensity_Interval_Training___HOME.html"
self.url = "https://www.buchsys.de/fu-berlin/angebote/aktueller_zeitraum/_HIIT_" \
"-_High_Intensity_Interval_Training___HOME.html "
self.testSport = Sport(
self.test_sport = Sport(
name=self.name,
url=self.url,
)
self.testSport.save()
self.test_sport.save()
self.criterion = Criterion(name="Einzelsport")
self.criterion.save()
def test_can_rate_criterion_for_sport(self):
self.testSport.rate(self.criterion, 10)
self.assertEquals(self.testSport.criteria_ratings.first(), self.criterion)
self.assertEquals(self.testSport.get_rating(self.criterion), 10)
""" A rating for a specific criterion can be added to a sport """
self.test_sport.rate(self.criterion, 10)
self.assertEqual(self.test_sport.criteria_ratings.first(), self.criterion)
self.assertEqual(self.test_sport.get_rating(self.criterion), 10)
class CriterionModelTest(TestCase):
"""Tests the Criterion model"""
def test_criterion_can_be_created(self):
"""New criterion is saved to the db"""
name = "Einzelsport"
test_criterion = Criterion(name=name)
self.assertEquals(test_criterion.name, name)
Criterion(name=name).save()
test_criterion=Criterion.objects.first()
self.assertEqual(test_criterion.name, name)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment