Skip to content

Instantly share code, notes, and snippets.

@leofaster
Last active October 17, 2016 19:11
Show Gist options
  • Save leofaster/3afbf66b9b01636af68f306ea461a195 to your computer and use it in GitHub Desktop.
Save leofaster/3afbf66b9b01636af68f306ea461a195 to your computer and use it in GitHub Desktop.
Django Image Upload Unit Test
from PIL import Image
from rest_framework.test import APIRequestFactory, force_authenticate
class TestPostVetListCreateView:
factory = APIRequestFactory()
def test_request_post(self):
image = Image.new('RGB', (1000, 1000), 'white')
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file)
user = mixer.blend('users.user')
data = {
'description': 'BLAh blah',
'image_1': tmp_file
}
tmp_file.seek(0)
req = self.factory.post('/', data=data)
force_authenticate(req, user=user)
resp = views.PostListCreateView.as_view()(req)
assert resp.status_code == 201, (
'Should return HTTP 201 CREATED'
)
p = models.Post.objects.last()
image = p.images.first()
img_s = Image.open(image.standard)
img_t = Image.open(image.thumbnail)
assert img_s.size == (612, 612)
assert img_t.size == (150, 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment