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