Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Szaki committed Aug 21, 2024
1 parent a4f2015 commit 9a16b32
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions tests/test_binder_file_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ def test_get_unknown_extension(self):
'/zoo/{}/binder_picture/?h={}&content_type=&filename={}'.format(zoo.pk, UNKNOWN_TYPE_HASH, filename),
)

def test_get_direct(self):
filename = 'pic.jpg'
zoo = Zoo(name='Apenheul')
zoo.binder_picture_direct = ContentFile(JPG_CONTENT, name=filename)
zoo.save()

response = self.client.get('/zoo/{}/'.format(zoo.pk))
self.assertEqual(response.status_code, 200)
data = jsonloads(response.content)

# Remove once Django 3 lands with: https://docs.djangoproject.com/en/3.1/howto/custom-file-storage/#django.core.files.storage.get_alternative_name
zoo.refresh_from_db()
filename = basename(zoo.binder_picture_direct.name) # Without folders foo/bar/

self.assertEqual(
data['data']['binder_picture_direct'],
'/zoo/{}/binder_picture_direct/?h={}&content_type=image/jpeg&filename={}'.format(zoo.pk, JPG_HASH, filename),
)

def test_setting_blank(self):
zoo = Zoo(name='Apenheul')
zoo.binder_picture = ''
Expand Down
2 changes: 2 additions & 0 deletions tests/testapp/models/zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Binder:

binder_picture_custom_extensions = BinderImageField(allowed_extensions=['png'], blank=True, null=True)

binder_picture_direct = BinderImageField(serve_direct=True, blank=True, null=True)

def __str__(self):
return 'zoo %d: %s' % (self.pk, self.name)

Expand Down
3 changes: 2 additions & 1 deletion tests/testapp/views/zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class ZooView(PermissionView):
m2m_fields = ['contacts', 'zoo_employees', 'most_popular_animals']
model = Zoo
file_fields = ['floor_plan', 'django_picture', 'binder_picture', 'django_picture_not_null',
'binder_picture_not_null', 'binder_picture_custom_extensions']
'binder_picture_not_null', 'binder_picture_custom_extensions', 'binder_picture_direct']
shown_properties = ['animal_count']
image_resize_threshold = {
'floor_plan': 500,
'binder_picture': 500,
'binder_picture_custom_extensions': 500,
'binder_picture_direct': 500,
}
image_format_override = {
'floor_plan': 'jpeg',
Expand Down

0 comments on commit 9a16b32

Please sign in to comment.