82 lines
3.1 KiB
Python
82 lines
3.1 KiB
Python
import io
|
|
|
|
from django.contrib.auth.models import User
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
from django.test import TestCase
|
|
from PIL import Image
|
|
|
|
from crochet.models import Pattern, PatternImage, StitchType
|
|
|
|
|
|
def make_test_image_file(name='test.png'):
|
|
buffer = io.BytesIO()
|
|
Image.new('RGB', (1, 1), color='red').save(buffer, format='PNG')
|
|
return SimpleUploadedFile(name, buffer.getvalue(), content_type='image/png')
|
|
|
|
|
|
class PatternModelTests(TestCase):
|
|
def test_defaults(self):
|
|
pattern = Pattern.objects.create()
|
|
self.assertEqual(pattern.sections, [])
|
|
self.assertEqual(pattern.page_settings, {})
|
|
|
|
def test_str_falls_back_to_uuid_without_title(self):
|
|
pattern = Pattern.objects.create()
|
|
self.assertEqual(str(pattern), str(pattern.uuid))
|
|
|
|
def test_str_uses_title_when_present(self):
|
|
pattern = Pattern.objects.create(page_settings={'title': 'Ampharos'})
|
|
self.assertEqual(str(pattern), 'Ampharos')
|
|
|
|
def test_uuid_is_unique_per_pattern(self):
|
|
first = Pattern.objects.create()
|
|
second = Pattern.objects.create()
|
|
self.assertNotEqual(first.uuid, second.uuid)
|
|
|
|
def test_created_by_defaults_to_none(self):
|
|
pattern = Pattern.objects.create()
|
|
self.assertIsNone(pattern.created_by)
|
|
|
|
def test_created_by_survives_owner_deletion(self):
|
|
# SET_NULL (no CASCADE): borrar la cuenta no debe llevarse por delante
|
|
# el patrón, solo desasociarlo.
|
|
owner = User.objects.create_user(username='crocheter', password='s3cret-pw')
|
|
pattern = Pattern.objects.create(created_by=owner)
|
|
|
|
owner.delete()
|
|
pattern.refresh_from_db()
|
|
|
|
self.assertIsNone(pattern.created_by)
|
|
|
|
|
|
class PatternImageModelTests(TestCase):
|
|
def test_deleted_with_pattern(self):
|
|
pattern = Pattern.objects.create()
|
|
PatternImage.objects.create(pattern=pattern, image=make_test_image_file())
|
|
self.assertEqual(PatternImage.objects.count(), 1)
|
|
|
|
pattern.delete()
|
|
|
|
self.assertEqual(PatternImage.objects.count(), 0)
|
|
|
|
|
|
class StitchTypeModelTests(TestCase):
|
|
def test_str_uses_spanish_translation(self):
|
|
stitch_type = StitchType.objects.create(translations={'es': 'Punto bajo', 'en': 'SC'})
|
|
self.assertEqual(str(stitch_type), 'Punto bajo')
|
|
|
|
def test_str_falls_back_without_spanish_translation(self):
|
|
stitch_type = StitchType.objects.create(translations={})
|
|
self.assertEqual(str(stitch_type), f'StitchType #{stitch_type.pk}')
|
|
|
|
def test_ordered_by_order_then_id(self):
|
|
# El catálogo ya trae tipos de punto sembrados por las migraciones de
|
|
# datos (0004/0005), así que se filtra a los creados aquí en vez de
|
|
# comparar contra StitchType.objects.all() entero.
|
|
third = StitchType.objects.create(translations={'es': 'C'}, order=1)
|
|
first = StitchType.objects.create(translations={'es': 'A'}, order=0)
|
|
second = StitchType.objects.create(translations={'es': 'B'}, order=1)
|
|
|
|
created_ids = [first.id, third.id, second.id]
|
|
self.assertEqual(list(StitchType.objects.filter(id__in=created_ids)), [first, third, second])
|