Skip to content
Snippets Groups Projects
Commit 73c17cc7 authored by dominip89's avatar dominip89
Browse files

Merge branch '28-add-models-wissenssnacks-and-aktivitaten' into 'master'

Resolve "Add Django Models for Wissenssnacks and Aktivitäten"

Closes #28

See merge request swp-unisport/team-warumkeinrust/unisport-o-mat!37
parents 61ba6f37 2e06f477
No related branches found
No related tags found
No related merge requests found
#SOURCE: https://www.codingforentrepreneurs.com/blog/django-virtualenv-python-gitignore-file/
# Project related
/media
# Virtualenv related
bin/
include/
......
......@@ -9,3 +9,4 @@ requests==2.25.1
soupsieve==2.2.1
sqlparse==0.4.1
urllib3==1.26.4
Pillow==8.2.0
\ No newline at end of file
unisportomat/quiz/fixtures/images/test_image.png

21.7 KiB

# Generated by Django 3.2 on 2021-06-02 13:36
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
replaces = [
("quiz", "0002_auto_20210601_1537"),
("quiz", "0003_auto_20210601_1549"),
("quiz", "0004_auto_20210602_1247"),
]
dependencies = [
("quiz", "0001_refactor_sport_and_add_criteria"),
]
operations = [
migrations.AlterField(
model_name="criterionrating",
name="rating",
field=models.IntegerField(
validators=[
django.core.validators.MaxValueValidator(10),
django.core.validators.MinValueValidator(1),
]
),
),
migrations.CreateModel(
name="KnowledgeSnack",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("text", models.TextField()),
("image", models.ImageField(max_length=200, null=True, upload_to="")),
],
),
migrations.CreateModel(
name="CallToMove",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("image", models.ImageField(max_length=200, null=True, upload_to="")),
("text", models.TextField(default="")),
],
),
]
# Generated by Django 3.2 on 2021-06-02 13:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("quiz", "0002_add_knowledge_snacks_and_call_to_move"),
]
operations = [
migrations.AlterField(
model_name="calltomove",
name="text",
field=models.TextField(),
),
]
# Generated by Django 3.2 on 2021-06-02 13:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("quiz", "0003_alter_calltomove_text"),
("quiz", "0004_auto_20210526_2014"),
]
operations = []
......@@ -56,6 +56,20 @@ class Criterion(models.Model):
name = models.TextField()
class CallToMove(models.Model):
"""Defines text and image that are used to show a call to move between questions"""
text = models.TextField()
image = models.ImageField(null=True, max_length=200)
class KnowledgeSnack(models.Model):
"""Defines text and image that are used to show a KnowledgeSnack between questions"""
text = models.TextField()
image = models.ImageField(null=True, max_length=200)
class Question(models.Model):
"""Defines a Question that is assigned to exactly one Criterion"""
......
""" This module tests all our quiz models"""
from django.test import TestCase
from .models import Sport, Criterion, Question
import os
import shutil
import tempfile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase, override_settings
from django.conf import settings
from .models import Sport, Criterion, CallToMove, KnowledgeSnack, Question
class SportModelTest(TestCase):
......@@ -64,6 +71,83 @@ class CriterionRatingTest(TestCase):
self.assertEqual(self.test_sport.get_rating(criterion=self.criterion), 8)
FIXTURE_IMAGES = os.path.join(settings.BASE_DIR, "quiz", "fixtures", "images")
MEDIA_ROOT = tempfile.mkdtemp(
suffix="testing"
) # Create a temp directory for files created during tests
@override_settings(MEDIA_ROOT=MEDIA_ROOT)
class CallToMoveTest(TestCase):
"""Tests the Model for Call To Move"""
def setUp(self):
tempfile.mkdtemp(suffix="testing") # recreate tmp folder before each test
self.text = "Kreise deine Arme vor der nächsten Frage 3x nach hinten"
self.image_name = "test_image.png"
self.image_path = os.path.join(FIXTURE_IMAGES, self.image_name)
self.image = SimpleUploadedFile(
name=self.image_name,
content=open(self.image_path, "rb").read(),
content_type="image/png",
)
self.call_to_move = CallToMove(text=self.text, image=self.image)
self.call_to_move.save()
def tearDown(self) -> None:
"""Delete the temp dir after each test"""
shutil.rmtree(MEDIA_ROOT, ignore_errors=True)
def test_can_create_call_to_move(self):
"""A call to move can be correctly created"""
self.assertEqual(self.call_to_move.text, self.text)
self.assertEqual(self.call_to_move.image.name, self.image.name)
def test_can_save_and_load_call_to_move(self):
"""A saved Call to Move can be loaded"""
call_to_move = CallToMove.objects.first()
self.assertEqual(call_to_move.text, self.text)
self.assertEqual(call_to_move.image.name, self.image.name)
@override_settings(MEDIA_ROOT=MEDIA_ROOT)
class KnowledgeSnackTest(TestCase):
"""Tests the Model for Knowledge Snack"""
def setUp(self):
tempfile.mkdtemp(suffix="testing") # recreate tmp folder before each test
self.text = (
"Dass Treppensteigen fast 5x so viele Kalorien verbrennt,"
"als bei der Nutzung des Aufzuges?"
)
self.image_name = "test_image.png"
self.image_path = os.path.join(FIXTURE_IMAGES, self.image_name)
self.image = SimpleUploadedFile(
name=self.image_name,
content=open(self.image_path, "rb").read(),
content_type="image/png",
)
self.knowledge_snack = KnowledgeSnack(text=self.text, image=self.image)
self.knowledge_snack.save()
def tearDown(self) -> None:
"""Delete the temp dir after each test"""
shutil.rmtree(MEDIA_ROOT, ignore_errors=True)
def test_can_create_knowledge_snack(self):
"""A knowledge snack can be correctly created"""
self.assertEqual(self.knowledge_snack.text, self.text)
self.assertEqual(self.knowledge_snack.image.name, self.image.name)
def test_can_save_and_load_call_to_move(self):
"""A saved Knowledge Snack can be loaded"""
knowledge_snack = KnowledgeSnack.objects.first()
self.assertEqual(knowledge_snack.text, self.text)
self.assertEqual(knowledge_snack.image.name, self.image.name)
class CriterionAndQuestionModelTest(TestCase):
"""Tests the Criterion and the Question model which have a One to One Relation"""
......
......@@ -9,12 +9,14 @@ https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Where user uploaded files like images are stored
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
......@@ -27,7 +29,6 @@ DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
......@@ -70,7 +71,6 @@ TEMPLATES = [
WSGI_APPLICATION = "unisportomat.wsgi.application"
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
......@@ -81,7 +81,6 @@ DATABASES = {
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
......@@ -100,7 +99,6 @@ AUTH_PASSWORD_VALIDATORS = [
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
......@@ -114,7 +112,6 @@ USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment