Adds tests for remaining nontrivial model fields

This commit is contained in:
Mouse Reeve 2020-12-05 14:40:23 -08:00
parent 8a900689d3
commit 05cde33a0c

View file

@ -1,11 +1,20 @@
''' testing models ''' ''' testing models '''
from io import BytesIO
from collections import namedtuple from collections import namedtuple
import pathlib
import re
from PIL import Image
import responses
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
from django.db import models from django.db import models
from django.test import TestCase from django.test import TestCase
from django.utils import timezone
from bookwyrm.models import fields from bookwyrm.models import fields, User
from bookwyrm.settings import DOMAIN
class ActivitypubFields(TestCase): class ActivitypubFields(TestCase):
def test_validate_remote_id(self): def test_validate_remote_id(self):
@ -103,3 +112,78 @@ class ActivitypubFields(TestCase):
instance.field_to_activity(items), instance.field_to_activity(items),
'example.com/snake_case' 'example.com/snake_case'
) )
def test_tag_field(self):
instance = fields.TagField('User')
Serializable = namedtuple(
'Serializable',
('to_activity', 'remote_id', 'name_field', 'name')
)
Queryset = namedtuple('Queryset', ('all', 'instance'))
item = Serializable(
lambda: {'a': 'b'}, 'https://e.b/c', 'name', 'Name')
another_item = Serializable(
lambda: {}, 'example.com', '', '')
items = Queryset(lambda: [item], another_item)
result = instance.field_to_activity(items)
self.assertIsInstance(result, list)
self.assertEqual(len(result), 1)
self.assertEqual(result[0].href, 'https://e.b/c')
self.assertEqual(result[0].name, 'Name')
self.assertEqual(result[0].type, 'Serializable')
@responses.activate
def test_image_field(self):
user = User.objects.create_user(
'mouse', 'mouse@mouse.mouse', 'mouseword', local=True)
image_file = pathlib.Path(__file__).parent.joinpath(
'../../static/images/default_avi.jpg')
image = Image.open(image_file)
output = BytesIO()
image.save(output, format=image.format)
user.avatar.save(
'test.jpg',
ContentFile(output.getvalue())
)
output = fields.image_serializer(user.avatar)
self.assertIsNotNone(
re.match(
r'https://%s/images/avatars/test_[A-z0-9]+\.jpg' % DOMAIN,
output.url,
)
)
self.assertEqual(output.type, 'Image')
instance = fields.ImageField()
self.assertEqual(instance.field_to_activity(user.avatar), output)
responses.add(
responses.GET,
'http://www.example.com/image.jpg',
body=user.avatar.file.read(),
status=200)
loaded_image = instance.field_from_activity(
'http://www.example.com/image.jpg')
self.assertIsInstance(loaded_image, list)
self.assertIsNotNone(re.match(r'.*\.jpg', loaded_image[0]))
self.assertIsInstance(loaded_image[1], ContentFile)
def test_datetime_field(self):
instance = fields.DateTimeField()
now = timezone.now()
self.assertEqual(instance.field_to_activity(now), now.isoformat())
self.assertEqual(
instance.field_from_activity(now.isoformat()), now
)
self.assertEqual(instance.field_from_activity('bip'), None)
def test_array_field(self):
instance = fields.ArrayField(fields.IntegerField)
self.assertEqual(instance.field_to_activity([0, 1]), ['0', '1'])