Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Testing view methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now it's time to move one level up in our testing. What have we tested so far? We've tested that part right there. However, I would like to test
0:10 that if we go to this registration process successfully we get redirected back to the account page and if not, maybe we stay in the same place
0:20 something to that effect. What we're going to do is we're going to import this function and call it. Now you'll see there's a couple of other things
0:27 that are going to be happening so here when we have create_user we're going to have to make sure that actually get called in the same way.
0:35 So, let's get started on that and I'm just going to duplicate a little bit of this here. Test registration view new user.
0:50 Something to that effect, okay? So, we're going to come down here and we're going to say from pypi_org.views account_views import register_post
1:02 and our goal is going to be just to simply call that function. So, I'm not doing this here We're still passing in this data
1:11 but we're just going to try to call that function. If we look, it doesn't take any arguments it may, sometimes it does
1:17 like in the case of our package here. We might have to pass that but in this case we're just calling this one. It doesn't take any argument.
1:26 Everything is passed through the form. So, theoretically we have our data all set up to pass the validation. We already have to say hey, unittest
1:35 what we're going to do is we're going to return none for the user here and we don't need to do validate but we do need to do this up here like so.
1:48 We need to say hey when we do this test here we would like to make sure and before we call this function
1:54 we want to make sure that if that function happens to have all find user by email it's going to return none. We also need to do another one here.
2:02 Now this is going to look a little messy we should be able to clean it up in a sec. User service, what is the other user service
2:07 function we're calling? Create user, so if we're calling create user we want to have it return an object. Now if you look over here
2:18 it says if it doesn't get back a user there's some kind of problem so we need to make sure that we get back a user here as well
2:26 for this particular one, okay? So, that's a lot of with blocks here. Maybe we can clean it up. We'll see but we're going to do our post
2:34 this will be are our act here. I want to get a response and this is going to be a response object. It'll help us work with it. If we come down here
2:46 and we import flask response here and we're going to assert some stuff about it so we could go here and say that the location is /account
2:57 so I feel like that's probably what we're looking for when we're coming down here we've gone and we've gone and done this.
3:05 We've redirected over to there so that redirect action is to set a location header to be /account. Whoa, all right, let's see if this is going to work.
3:13 We can try our test again. It looked like it works, beautiful. Let's just verify that it does. So, we can come over here
3:25 if this part failed, we would see of course it comes back and says do we get this error? No, the expected was /account
3:39 but what we actually got is something down here the left was none, we expected this to be /account.
3:47 The reason it's not is it's actually an HTML response not a redirect response. Cool, so that worked over here.
3:55 And of course if the user can't be found again we get some kind of thing where it's showing that hey, the user couldn't be created as an error.
4:08 Super, now I had this nesting and nesting and nesting and with blocks. I'm not a big fan, so let's go do this. So, we'll say and do
4:21 a quick little organization here like that. Move this one up and so, we can create user. Let's just call this find user
4:36 and then this will be the request. And then we can do a much simpler version of find user, rate user request.
4:49 So, deindent that so it's not so terrible. Is that better? Oh, you decide here. It's just kind of messy and sometimes that's just how it goes
4:59 but let's just see that it still works. Yeah, it still works like a charm. So, we can at least not go super indented here by creating all those things
5:07 and then usually in a single context block for all them. So, that's how we test these view methods and we've already verified that that part works
5:16 so we don't have to write too many of them. We probably want to check that we do get the right error message back here and here in those two cases
5:24 and then in this case we're already testing that we get this redirect. Beautiful.


Talk Python's Mastodon Michael Kennedy's Mastodon