Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Testing the register viewmodel

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So, let's test this register view model here in the form of the post. So basically we're trying to simulate the essence of this register post here.
0:11 So, we're going to define another method called test and then, these methods they're never interacted with directly by other programmers
0:18 they're only called by the test framework which means they can be really descriptive because that will help us in our reports.
0:24 So, let's make this register validation when valid. Okay, so something like that. We want to test the registration view model
0:34 that it's working when the right validation when the right data is passed to it. Now, this whole test here is going to come with quite the challenge
0:43 that you may see coming you might not see coming but there's couple of things we have to run into. Working with Flask as well as some other stuff.
0:51 Now, I like to be a little explicit when I write my tests and typically I want to use this little pattern that's nice and mnemonic
0:58 on organizing the code within your test don't have to write it out usually but I find this helpful. So, it's called the three A's of testing.
1:07 I'll put it here. So, the three A's are arrange, assert, and act. So the first thing we're going to do is arrange
1:14 and what that means is we want to get everything setup the way that it needs to be. So, we're going to create some form data
1:20 and then we're going to do an act. That is, try to do the actual login. We're also going to need our view model up here.
1:28 So we'll say vm = RegisterViewModel. We need to import it like so it's not imported at the top. That's good, but here, we run into the first challenge.
1:41 Remember how the registration view model works. It's nice and simple, but dig into it you can see the first thing that it does
1:48 is it starts going to the request dictionary and asking for the name. Oh, well, how do we even get that into Flask? How's this going to work?
1:57 So, this actually takes a little bit of reorganization and a little bit of extra work here to make this happen.
2:03 So what we can use is this context manager. We're going to go ahead and say with something we're going to create this view model
2:14 and it's going to pull that data back. How is, what do we put here. Well, this is where we get into one of the challenges of testing the web.
2:23 So, what I'm going to do is I'm goin to define another file over here that's going to hold some of this data for us I'll call this test_client.
2:31 It's going to have a couple of things and this actually talked about over on the Flask documentation and what I'm going to do
2:38 is just drop some code in here that sort of pre-configures Flask and we'll just talk through it real quick.
2:43 In pytest you can create what's called a fixture also, that if you pass a variable called client it will actually run this function
2:52 and give back this object. So, we're not actually working with this here yet. We're just going to work with this part but
3:00 we'll need them both as we go through this class. So here, I'm going to say flask_app I guess I have to type it up here
3:08 from tests.test_client import flask_app We're going to get this and we're going to come down here and with this thing we can say
3:17 Okay, we'll say request_context and then here, what do we want to set. We basically want to say that the URL they're requesting is account/register
3:27 so, if we look at the URL it looks correct. Then we can say the data is going to be form data. Now, within this context manager
3:36 when this view model goes and says give me the data back get all of the data out of the form guess what? It's going to think that this is actually
3:45 the form that's being submitted. So, this should make everything work that we're hoping for. Then we go down here and say validate.
3:53 All right, that's the action that we're taking and then the assert is the third thing. The assert is, once we've set up everything
4:01 we've done our action and then we want to verify whatever test we want to test but this is where we do it.
4:07 So, we're going to say assert vm.error is None. We're going to say there's no error in this case. Why, because we provided all the data
4:16 and theoretically, this is a unique user. Now, it actually exists in the database but that's not a problem. We're not talking to the database.
4:25 Are we? Let's find out. Hmmm. Looks like that test failed. Let's go look and see what went wrong. register.failed TypeError.
4:38 Oh, I used the wrong thing here. This is not request_context, this is test_request_context. There we go. Let's try again. Now, we got another error.
4:48 Now, what's the error? The error is oh, the password must be five characters. I guess this is not going to work
4:57 be super explicit. There you go, six. Six a's. Now, we're getting to a different error here. What is the problem? NoneType is not callable.
5:09 That sounds like a problem. We're going to create this session here what are we doing? We're talking to the database and then we're calling the factory
5:20 but the factory itself remember we're trying to call this sessionmaker factory to create a session and we're going to return it
5:27 but we haven't setup the database on purpose. We don't want to talk to the database but here we are confronted with this error
5:33 trying to talk to the database. So, while everything looked like it was going pretty well it turns out that even though we're running our test
5:42 we need to do a little bit more work to make sure that we don't actually talk to the database. Let's make that happen.


Talk Python's Mastodon Michael Kennedy's Mastodon