lostplaces-backend/django_lostplaces/lostplaces/tests/models/test_place_image_model.py

104 lines
3.4 KiB
Python
Raw Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2020-09-11 12:09:51 +02:00
import os
import shutil
2020-09-03 22:22:04 +02:00
from unittest import mock
from django.test import TestCase
from django.db import models
from django.core.files import File
2020-09-11 12:09:51 +02:00
from django.conf import settings
2020-09-12 08:39:06 +02:00
from django.contrib.auth.models import User
from django.utils import timezone
2020-09-03 22:22:04 +02:00
from lostplaces.models import PlaceImage, Place
from lostplaces.tests.models import ModelTestCase
2020-09-03 22:22:04 +02:00
from easy_thumbnails.fields import ThumbnailerImageField
2020-09-13 19:12:32 +02:00
class PlaceImageTestCase(ModelTestCase):
2020-09-11 23:07:19 +02:00
model = PlaceImage
@classmethod
def setUpTestData(cls):
2020-09-12 08:39:06 +02:00
user = User.objects.create_user(
username='testpeter',
password='Develop123'
)
place = Place.objects.create(
name='Im a place',
submitted_when=timezone.now(),
2020-09-12 08:39:06 +02:00
submitted_by=User.objects.get(username='testpeter').explorer,
location='Testtown',
latitude=50.5,
longitude=7.0,
description='This is just a test, do not worry'
)
place.tags.add('I a tag', 'testlocation')
place.save()
2020-09-11 23:07:19 +02:00
2020-09-12 11:02:01 +02:00
if not os.path.isdir(settings.MEDIA_ROOT):
os.mkdir(settings.MEDIA_ROOT)
2020-09-11 12:09:51 +02:00
current_dir = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')):
shutil.copyfile(
os.path.join(current_dir, 'im_a_image.jpeg'),
os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg')
)
2020-09-12 08:39:06 +02:00
2020-09-12 08:48:53 +02:00
shutil.copyfile(
os.path.join(current_dir, 'im_a_image.jpeg'),
os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
)
2020-09-11 23:07:19 +02:00
PlaceImage.objects.create(
2020-09-11 12:09:51 +02:00
description='Im a description',
filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'),
2020-09-12 08:39:06 +02:00
place=place,
submitted_when=timezone.now(),
2020-09-12 08:39:06 +02:00
submitted_by=user.explorer
2020-09-11 12:09:51 +02:00
)
2020-09-03 22:22:04 +02:00
2020-09-13 19:12:32 +02:00
def setUp(self):
self.place_image = PlaceImage.objects.get(id=1)
2020-09-03 22:22:04 +02:00
def test_description(self):
2020-09-13 13:29:27 +02:00
self.assertField('description', models.TextField)
2020-09-03 22:22:04 +02:00
def test_filename(self):
2020-09-13 13:29:27 +02:00
self.assertField('filename',ThumbnailerImageField)
2020-09-03 22:22:04 +02:00
def test_place(self):
2020-09-13 13:29:27 +02:00
field = self.assertField('place', models.ForeignKey)
2020-09-03 22:22:04 +02:00
self.assertEqual(field.remote_field.on_delete, models.CASCADE,
2020-09-13 19:12:32 +02:00
msg='Expecting the deletion of %s to be cascading' % (
str(field)
2020-09-03 22:22:04 +02:00
)
)
2020-09-13 19:12:32 +02:00
expected_related_name = 'placeimages'
self.assertEqual(field.remote_field.related_name, expected_related_name,
msg='Expecting the related name of %s to be %s' % (
str(field),
expected_related_name
2020-09-03 22:22:04 +02:00
)
)
2020-09-12 08:48:53 +02:00
def test_change_filename(self):
2020-09-13 19:12:32 +02:00
path = self.place_image.filename.path
self.place_image.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
self.place_image.save()
2020-09-12 08:48:53 +02:00
self.assertFalse(
os.path.isfile(path),
msg='Expecting the old file of an place_image to be deleteed when an place_image file is changed'
)
2020-09-11 12:09:51 +02:00
def test_deletion(self):
2020-09-13 19:12:32 +02:00
path = self.place_image.filename.path
self.place_image.delete()
2020-09-11 12:09:51 +02:00
self.assertFalse(
os.path.isfile(path),
msg='Expecting the file of an place_image to be deleteed when an place_image is deleted'
)