Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: Testing Django forms

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You wrote some code, time to write some tests. A few things to think about with the new features.
0:07 First you'll want to test a view that is behind the login wall, which means you'll have to log in. Next you're going to be accessing pages
0:15 and just like in the templates you shouldn't hard code your URLs. So you'll use reverse in the test to look them up.
0:23 You've seen the test client for getting a page. Well it also supports posting and to properly test your form process.
0:31 You'll want to verify that you get the correct redirect on successful form submission. Once again I have pre prepared a file for this.
0:40 Here's test review. To test things behind the login wall you're going to need a user or two and as the reviews are per user.
0:49 I've created a few of those as well. The setup method gets called before each test and this one creates a list of users
0:56 who are a named user dash number all with the same password. To test a review, I'll need a book, to get a book I need an author, so lines 22 through
1:09 24 create an author and book. The first chunk of this test makes sure that a book page that has no reviews comes back saying that there are no reviews.
1:20 On line 27, I used the reverse method to look up the URL named book with the book ID as an argument. I then use the client get method to get that URL
1:30 and assert that it came back as 200 status code, with the no reviews yet string showing up in the content.
1:40 The next chunk tests that the review page is behind the login wall. I look up the review URL and hit it.
1:47 I'm not logged in so I should get redirected to the login page. This is always a good test to have.
1:54 It's easy to forget or accidentally delete a login decorator and you'd be exposing a page you didn't mean to.
2:02 So validating it in your tests so you can do regression is a good idea. The third part of this test tests the submission of a review.
2:11 Lines 39 through 42 create a dictionary. That will be used as the posts submission.
2:17 Line 44 logs the client in while the line after does the actual post. The post is to the URL
2:27 that was looked up earlier and the data being sent is the equivalent of a form submission. As this data is valid,
2:34 what should come back is a redirect corresponding to the book's page. I checked the 302 status code that's redirect and I checked that the URL
2:43 inside of it is correct. After that online 49 I fetched the first review object associated with the book.
2:52 As there should only be one, it should be the right one. I then checked that the rating and text was set correctly from the form submission.
3:02 Not satisfied to do this once, on line 53, I do the process again using a different rating and review text. Next I test the book page.
3:12 On line 67 I log out. I don't technically have to do this, but this page is not behind the login wall so it doesn't hurt.
3:19 On line 68, I get the book page then validate that there are two reviews on the page, one from each of users, 0 and 1.
3:28 Finally, I double checked that those were the only two reviews in the content. More code, more tests, and everything still passes. That's great.

Talk Python's Mastodon Michael Kennedy's Mastodon