import datetime from unittest import mock from django.test import TestCase from django.db import models from django.core.files import File from lostplaces_app.models import PlaceImage from lostplaces_app.tests.models import TestSubmittable from lostplaces_app.tests import mock_user from lostplaces_app.tests.models import TestModel from lostplaces_app.tests.models.test_place_model import mock_place from easy_thumbnails.fields import ThumbnailerImageField def mock_place_image(): return PlaceImage( description='Im a description', filename=mock.MagicMock(spec=File, name='FileMock'), place=mock_place(), submitted_when=datetime.datetime.now(), submitted_by=mock_user().explorer ) class TestPlaceImage(TestSubmittable, TestCase): 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' ) ) def test_str(self): place_image = mock_place_image() self.assertEqual(str(place_image), ' '.join([place_image.place.name, str(place_image.pk)]))