105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
import datetime
|
|
import os
|
|
import shutil
|
|
from unittest import mock
|
|
|
|
from django.test import TestCase
|
|
from django.db import models
|
|
from django.core.files import File
|
|
from django.conf import settings
|
|
from django.contrib.auth.models import User
|
|
|
|
from lostplaces_app.models import PlaceImage, Place
|
|
from lostplaces_app.tests.models import SubmittableTestCase
|
|
from lostplaces_app.tests.models.test_place_model import PlaceTestCase
|
|
|
|
from easy_thumbnails.fields import ThumbnailerImageField
|
|
|
|
class TestPlaceImage(SubmittableTestCase, TestCase):
|
|
model = PlaceImage
|
|
|
|
@classmethod
|
|
def setUpTestData(cls):
|
|
user = User.objects.create_user(
|
|
username='testpeter',
|
|
password='Develop123'
|
|
)
|
|
|
|
place = Place.objects.create(
|
|
name='Im a place',
|
|
submitted_when=datetime.datetime.now(),
|
|
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()
|
|
|
|
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')
|
|
)
|
|
|
|
shutil.copyfile(
|
|
os.path.join(current_dir, 'im_a_image.jpeg'),
|
|
os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
|
|
)
|
|
|
|
PlaceImage.objects.create(
|
|
description='Im a description',
|
|
filename=os.path.join(settings.MEDIA_ROOT, 'im_a_image_copy.jpeg'),
|
|
place=place,
|
|
submitted_when=datetime.datetime.now(),
|
|
submitted_by=user.explorer
|
|
)
|
|
|
|
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'
|
|
)
|
|
)
|
|
|
|
def test_str(self):
|
|
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_change_filename(self):
|
|
path = self.object.filename.path
|
|
self.object.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg')
|
|
self.object.save()
|
|
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'
|
|
)
|
|
|
|
def test_deletion(self):
|
|
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'
|
|
) |