Building Data-Driven Web Apps with Pyramid 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 Next we want to test these view methods or action methods as I sometimes call them, and again we're going to use
0:07 a fake request that we pass to it, but the result is going to be different. Now this view model we're going to look at, but we're going to
0:13 get whatever model would be sent to the template or whatever response is sent back to Pyramid. So we're going to see how to do that right now.
0:22 Let's do it in demo form first. In some sense it's going to look a lot like what we just did with our controller, and let's go and do a test
0:31 for packages here, let's go and do a test for packages over here. So we're going to have the same imports as we had before
0:38 and we'll have a class called PackageControllerTests and it'll be unittest.testcase We're going to define a test somethin' or other
0:49 let's say package_details, we could test for things like when we request one that exists, we'd get the details
0:56 and when we request one that doesn't exist, we'd get a 404 things like that. So, call this Success.
1:03 So again, Arrange what are we going to do here, request This time we're going to pass data over in the URL so we need to set something, and let's see
1:18 we're here in our PackageController what we think we're working with. So we're going to get this, and it's going to go to vm.package.
1:25 How does it know that? Well it thinks package name is being passed. So, great. Let's go and set that to SQLAlchemy.
1:35 That seems pretty solid. So our fake request is set up now we need to actually import that method. So we're going to go over here to controllers.
1:46 let's say, from that, PackageController, we want to import details. Alright let's just go look real quick. Yeah that's the one we want.
1:56 So we're going to send the details, and we should either get this back or "Not Found." Now the Act part is interesting, what are we going to do here?
2:06 We're going to come over and call details, we'll say model equals details of request, and the Assert
2:17 want to assert model package, actually let's get the package back. I'm going to say self.assert is not None and that the
2:35 that's assert equal we'll get a better message here assert equals SQLAlchemy as a string. Again, we'll get this annoying little type check.
2:47 So press it like so. Alright, this almost works. This almost works, let's run it and find out whether it works. Hmm, it doesn't work.
2:55 Again this DBSession is not call-able, again what is happening is we're calling this, and it's going to the database, let's fix that.
3:11 So we need to come up with something to patch and some test data that will return. So let's see, first of all, what the problem is.
3:22 We're calling PackageService.find_package_by_name. So that's what we're going to patch. PackageService.find_by_name, that looks good.
3:32 And let's just have it return nothing for a minute. See at least it's getting there, it's changing the error this is not going to work all the way.
3:42 Beautiful, dict has no releases. So it made it through, it made it through down to right here, notice that it's going through releases
3:50 when it gets it back, right there. It checks do we have it, come back and so on. I think we might have found a bug. Awesome, we found a bug!
4:01 Notice this, we're checking here for our 404, but we're actually not checking in the view model here whether no package was returned.
4:10 So we'll test for that in just a minute but we'll write a test that finds it and then we'll fix it. But for now let's stay focused
4:17 on actually working with real data here. So the problem is this thing that we return has to be test_package, a real honest-to-goodness, test_package
4:29 we can do, so let's go over here and say test_package = Package(), like so. Import that locally, again to say
4:41 = 'SQLAlchemy', releases = we could put a release or two in here if we really wanted like that, probably we should set the values
4:55 but let's just go with this for a minute. Now, when they call this function we're going to give them this SQLAlchemy back
5:01 I'm going to check, hey, we actually got this thing returned to us, that is the package. We got a model passed to us that contains the package
5:08 that is the right one. So let's try to run this and assert that it works. Oh, yeah, it's working! Fabulous!
5:15 Run our package tests, and that one's great. That's the one we're looking for. self.assert equals such and such.
5:23 Oh it says assert equals is deprecated please use that. Fine, still passes.

Talk Python's Mastodon Michael Kennedy's Mastodon