dimanche 29 novembre 2015

Python, Django, Unit tests - Mock DEFAULT_FILE_STORAGE

I'm using S3 to upload images so I'm trying to mock the DEFAULT_FILE_STORAGE in my tests. I read a couple of articles(Link) and questions and it looks like I should use MagicMock.

The upload of the image works fine (creating a dummy image and uploading it) but it is failing to mock the DEFAULT_FILE_STORAGE.

class CreateGroup(TokenAuthTestCase):

    NAME = "My Group"
    SLUG = "mygroup"

    def setUp(self):
        super(CreateGroup, self).setUp()

        new_logo = BytesIO()
        gen_image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
        gen_image.save(new_logo, 'png')
        new_logo.name = 'test.png'
        new_logo.seek(0)
        self.LOGO = new_logo

        self.default_storage_mock = mock.MagicMock(spec=default_storage, name='default_storage_mock')

    def test_create_group_logged_in_api(self):
        """ Logged In Users can create groups through the api """

        with mock.patch('django.core.files.storage.default_storage._wrapped', self.default_storage_mock):
            response = self.client.post(reverse("organisations:create"), {
                "name": self.NAME,
                "slug": self.SLUG,
                "logo": SimpleUploadedFile('test.png', self.LOGO.read(), content_type="image/png")
            })

        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data['name'], self.NAME)
        self.assertEqual(response.data['slug'], self.SLUG)
        self.assertTrue(response.data['logo'])

The error I'm getting is:

...
...
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/django/views/generic/base.py", line 71, in view
  return self.dispatch(request, *args, **kwargs)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/views.py", line 466, in dispatch
  response = self.handle_exception(exc)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/views.py", line 463, in dispatch
  response = handler(request, *args, **kwargs)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/generics.py", line 192, in post
  return self.create(request, *args, **kwargs)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/mixins.py", line 22, in create
  headers = self.get_success_headers(serializer.data)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/serializers.py", line 503, in data
  ret = super(Serializer, self).data
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/serializers.py", line 239, in data
  self._data = self.to_representation(self.instance)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/serializers.py", line 472, in to_representation
  ret[field.field_name] = field.to_representation(attribute)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/rest_framework/fields.py", line 1381, in to_representation
  return request.build_absolute_uri(url)
File "/Users/Manos/Projects/project-name/backend/env/lib/python3.5/site-packages/django/http/request.py", line 148, in build_absolute_uri
  bits = urlsplit(location)
File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/parse.py", line 337, in urlsplit
  if i > 0:
TypeError: unorderable types: MagicMock() > int()

Aucun commentaire:

Enregistrer un commentaire