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