Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Concept Testing view methods
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now let's move up one level in the amount that we're testing at once and the part of the application that we're testing. 'Cause we're using view models
0:08
we can basically assume that all the validation is working just fine and the data exchange is working fine as long as we test that
0:15
but what we do need to test is that the view method is working with the view model's response correctly. If an error comes back
0:23
that it actually shows the page with an error rather than just ignores it or that if it uses that data to try to create our user
0:31
if that user can't be created it still shows the error instead of just redirecting you. Things like that. So, that's the layer we just tested
0:37
and we're going to review right now. So, we're going to test the home page and we're just going to ignore the database aspect for just a moment.
0:44
We'll see a more realistic one in a second. So, let's imagine that we don't need to talk to the database we're going to come in here
0:49
we're going to import the index view method from home views and we're going to create a context manager that is a test request just to slash.
0:58
Then we're going to do act which is simply to call the function and capture the response so what we get back from these Flask methods
1:05
is always a response and then we want to do some kind of assert like that the status code is 200 and that the model contains packages
1:13
and that the length because that's a list, the length of it is greater than zero. Right, that part actually probably came from the database
1:19
so keeping it simple right now. But this is a totally reasonable test that we might write for the index home page. One quick side note that model
1:28
on the response, that is set by the @response decorator that I created for this course where you automatically have it pick up the view template.
1:38
If you don't do that you have to find another day to capture that data. You can look in the decorator, see how it's done there and so on, all right?
1:45
So, we have this and it looks like it's working. It does work just fine but if we want a more realistic version well, we need more space
1:54
because this time we don't want to talk to the database and we're going to talk to a fairly complicated function. We haven't seen this test written yet
2:00
so imagine we want to test if we go to our package details page. I want to go to like /project/sqlalchemy and see what comes back
2:10
but actually not talk to the database. Come over here, we're going to import project the method project from the package views
2:18
and we're going to import the Package object itself because we're going to have to create some test data that we can then mock out
2:25
so the project view gets that from the mocked method calls. Then we're going to create our test package I'm going to set the id to sqlalchemy
2:34
we're going to give it a couple of releases. The reason we want a beta release is because the basically that package method assumes
2:44
that there are releases. And then we'll going to mock a bunch of stuff out we're going to say if you call package
2:50
package_service.get_package_by_id, you get this test SQLAlchemy package. If you all releases for package you get the releases that we set here
2:58
and the context is going to be set to /project/sqlalchemy. The next thing to do is act. We're going to call our project function
3:08
and remember, in Flask, it takes the argument right, it's up to the Flask infrastructure to pass that along but we're calling it directly
3:15
so it's now our job to pass that along and internally, that's going to be calling get package by ID
3:21
form package service and all the other stuff that happens inside. That's why we had to mock it up. And then we can just assert.
3:27
I say the web package, the one that we got back inside the model we're going to capture it and I'm going to ask some questions.
3:34
Is the package.id actually sqlalchemy and does it actually have two releases? So, like this, we can do legitimate tests against complex methods
3:43
that interact with the database and inbound arguments, all that kind of stuff.