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

79 lines
2.6 KiB
Python
Raw Normal View History

2020-09-03 22:22:04 +02:00
import datetime
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-03 22:22:04 +02:00
from lostplaces_app.models import PlaceImage
2020-09-11 12:09:51 +02:00
from lostplaces_app.tests.models import SubmittableTestCase
2020-09-03 22:22:04 +02:00
from lostplaces_app.tests import mock_user
from lostplaces_app.tests.models.test_place_model import mock_place
from easy_thumbnails.fields import ThumbnailerImageField
def mock_place_image():
2020-09-11 12:09:51 +02:00
all_place_images = PlaceImage.objects.all()
if len(all_place_images) <= 0:
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')
)
return PlaceImage.objects.create(
description='Im a description',
filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'),
place=mock_place(),
submitted_when=datetime.datetime.now(),
submitted_by=mock_user().explorer
)
return all_place_images[0]
class TestPlaceImage(SubmittableTestCase, TestCase):
2020-09-03 22:22:04 +02:00
model_name = 'PlaceImage'
def setUp(self):
self.object = mock_place_image()
def test_description(self):
self._test_field('description', models.TextField)
def test_filename(self):
self._test_field('filename',ThumbnailerImageField)
def test_place(self):
field = self._test_field('place', models.ForeignKey)
self.assertEqual(field.remote_field.on_delete, models.CASCADE,
msg='%s.%s deleting of %s should be cascadinf' % (
self.model_name,
'place',
self.model_name
)
)
self.assertEqual(field.remote_field.related_name, 'images',
msg='%s.%s related name should be images' % (
self.model_name,
'place'
)
)
2020-09-03 22:32:28 +02:00
def test_str(self):
2020-09-11 12:09:51 +02:00
place_image = self.object
self.assertTrue(place_image.place.name.lower() in str(place_image).lower(),
msg='Expecting %s.__str__ to contain the name of the place' % (
self.model_name
)
)
def test_deletion(self):
# TODO
path = self.object.filename.path
self.object.delete()
self.assertFalse(
os.path.isfile(path),
msg='Expecting the file of an place_image to be deleteed when an place_image is deleted'
)