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

Add tests and model for call to move

parent 052a1e0e
No related branches found
No related tags found
No related merge requests found
#SOURCE: https://www.codingforentrepreneurs.com/blog/django-virtualenv-python-gitignore-file/ #SOURCE: https://www.codingforentrepreneurs.com/blog/django-virtualenv-python-gitignore-file/
# Project related
/media
# Virtualenv related # Virtualenv related
bin/ bin/
include/ include/
......
unisportomat/quiz/fixtures/images/test_image.png

21.7 KiB

# Generated by Django 3.2 on 2021-06-01 15:37
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("quiz", "0001_refactor_sport_and_add_criteria"),
]
operations = [
migrations.CreateModel(
name="CallToMove",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
],
),
migrations.AlterField(
model_name="criterionrating",
name="rating",
field=models.IntegerField(
validators=[
django.core.validators.MaxValueValidator(10),
django.core.validators.MinValueValidator(1),
]
),
),
]
# Generated by Django 3.2 on 2021-06-01 15:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("quiz", "0002_auto_20210601_1537"),
]
operations = [
migrations.AddField(
model_name="calltomove",
name="image",
field=models.ImageField(null=True, upload_to=""),
),
migrations.AddField(
model_name="calltomove",
name="text",
field=models.TextField(default=""),
preserve_default=False,
),
]
...@@ -54,3 +54,8 @@ class Criterion(models.Model): ...@@ -54,3 +54,8 @@ class Criterion(models.Model):
""" """
name = models.TextField() name = models.TextField()
class CallToMove(models.Model):
text = models.TextField()
image = models.ImageField(null=True, max_length=200)
""" This module tests all our quiz models""" """ This module tests all our quiz models"""
from django.test import TestCase import os
from .models import Sport, Criterion import shutil
import tempfile
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase, override_settings
from .models import Sport, Criterion, CallToMove
from django.conf import settings
class SportModelTest(TestCase): class SportModelTest(TestCase):
...@@ -74,3 +81,41 @@ class CriterionModelTest(TestCase): ...@@ -74,3 +81,41 @@ class CriterionModelTest(TestCase):
Criterion(name=name).save() Criterion(name=name).save()
test_criterion = Criterion.objects.first() test_criterion = Criterion.objects.first()
self.assertEqual(test_criterion.name, name) self.assertEqual(test_criterion.name, name)
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 InfoScreenTest(TestCase):
"""Tests the Model for the info screen (Call To Move or Knowledge Snack)"""
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)
...@@ -9,12 +9,14 @@ https://docs.djangoproject.com/en/3.2/topics/settings/ ...@@ -9,12 +9,14 @@ https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/ https://docs.djangoproject.com/en/3.2/ref/settings/
""" """
import os
from pathlib import Path from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent 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 # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
...@@ -27,7 +29,6 @@ DEBUG = True ...@@ -27,7 +29,6 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
...@@ -70,7 +71,6 @@ TEMPLATES = [ ...@@ -70,7 +71,6 @@ TEMPLATES = [
WSGI_APPLICATION = 'unisportomat.wsgi.application' WSGI_APPLICATION = 'unisportomat.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases # https://docs.djangoproject.com/en/3.2/ref/settings/#databases
...@@ -81,7 +81,6 @@ DATABASES = { ...@@ -81,7 +81,6 @@ DATABASES = {
} }
} }
# Password validation # Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
...@@ -100,7 +99,6 @@ AUTH_PASSWORD_VALIDATORS = [ ...@@ -100,7 +99,6 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/ # https://docs.djangoproject.com/en/3.2/topics/i18n/
...@@ -114,7 +112,6 @@ USE_L10N = True ...@@ -114,7 +112,6 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/ # 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.
Finish editing this message first!
Please register or to comment