Django: Getting Started Transcripts
Chapter: Receiving Uploads in Django
Lecture: Testing an upload view

Login or purchase this course to watch this video and the rest of the course contents.
0:01 It's that time again, boys and girls. Let's figure out how to test all this new code. Your test suite isn't going to actually upload a file.
0:10 So how do you test an upload view. Well, Django has an answer to that.
0:14 It's the simple uploaded file object which mimics everything you need for testing. Managing tests is often about more than just managing the test code.
0:24 You often have to manage data to go with it. Up until now you've been creating users and books inside of the tests.
0:31 You're not going to want to do that with images. You're going to need a folder somewhere with some data that you can use to do your tests.
0:38 You should manage that data the same way you manage code. It should all be part of your code repo.
0:55 I've just pasted the usual imports and set up here. You've seen all this kind of code before.
1:01 Now for the test, I'm starting out by getting ready adding an author and getting
1:20 the relevant URLs that go with that author. The first thing I'm testing is to make sure you don't see the upload button on
1:35 the author page, if you're not logged in. At this point, I'm still not logged in and I'm making sure that you get redirected
1:51 if you try the upload page. Now I'm logging in and going to the upload page,
2:39 and this is the code that actually tests the upload. I open up an image file from the test data directory,
2:46 create a simple uploaded file object passing in the file I just read and then use that in the posts form dictionary.
2:55 An accepted file should result in a 302 going to the author's page. Now I'm refetching the author to check their picture field. This step is important.
3:12 Remember that these objects are just proxies to the database. If I don't re fetch I'll have a stale copy the one from when I created it.
3:20 The view will have changed the database but the original from a few lines back won't see that. Refetching the author fixes that problem.
3:31 With the new copy, I validate the name of the associated picture file using the user ID naming convention from the view.
3:45 Last I do a bit of cleanup removing the uploaded file. There are better ways of doing this. If the test failed between the upload and this cleanup,
3:53 you'd end up with this file sticking around. Better way to do this would be to create a special uploads directory
4:00 just for the test and use the tear down method, the pair of the setup method to clean up that temp directory.
4:07 I'm a bit lazy though and haven't gone through all that. I did want to put the file delete code here though so that you're aware that
4:13 a side effect of the test would be something in your uploads directory. The rest I'll leave as a homework assignment or you can be lazy like me and
4:21 just delete the files manually, if something goes wrong. Offscreen, I created the data directory and copied Michael.jpeg from my samples in here.
4:36 This is the photo that gets used in the test And there you go. I'm up to nine tests and thankfully they're all still passing.


Talk Python's Mastodon Michael Kennedy's Mastodon