'IOError: [Errno 2] No such file or directory' when testing file uploads
527 views
Skip to first unread message
Kevin Harvey
unread,
Nov 25, 2011, 3:19:16 PM11/25/11
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
I'm trying to write a test for a FileField on a model Photo. The FieldField is inherited from an base class Asset. I need to save the dimensions of the photo in the database, so I've got my own save method on my Photo model:
def save(self, *args, **kwargs): # save the instance, so we can use the uploaded photo super(Photo, self).save(*args, **kwargs) im = Image.open(os.path.join(MEDIA_ROOT, self.file.name)) self.width, self.height = im.size super(Photo, self).save(*args, **kwargs)
This works, but I can't test it. When I try to create an instance of this model in the test suite, it appears that the file is never moved to the MEDIA_ROOT, and self.file.name refers to the original file in the test directory of my app.
====================================================================== ERROR: testPhoto (caramel.content.tests.modelTests.ContentTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "../myproject/myproject/myapp/tests/modelTests.py", line 22, in setUp credit="John Smith") File "/Users/username/workspace/eclipse/lib/python2.7/site-packages/django/db/models/manager.py", line 138, in create return self.get_query_set().create(**kwargs) File "/Users/username/workspace/eclipse/lib/python2.7/site-packages/django/db/models/query.py", line 360, in create obj.save(force_insert=True, using=self.db) File "../myproject/myproject/myapp/models.py", line 97, in save im = Image.open(os.path.join(MEDIA_ROOT, self.file.name)) File "/Users/username/workspace/eclipse/lib/python2.7/site-packages/PIL/Image.py", line 1952, in open fp = __builtin__.open(fp, "rb") IOError: [Errno 2] No such file or directory: '/Users/username/workspace/eclipse/project/media/../myproject/myproject/myapp/tests/assets/test.jpg'
I'm using PIL's width/height methods here because I can't use Django's own get_image_dimensions, as it's not an ImageField. Thanks in advance.