Building data-driven web apps with Pyramid and SQLAlchemy Transcripts
Chapter: Testing web apps
Lecture: Fixing the 404 package error
0:00 So we've made it pretty far through this app.
0:02 We've built the whole web app without
0:04 running into this error I'm about to show you.
0:07 So, when we were looking at the testing
0:08 I said oh, I think there is a bug
0:10 if we come request a package that doesn't exist.
0:13 So, we can go request photo3
0:15 and it gives us all the details about it.
0:17 But what if we request photo300?
0:20 What's going to happen then?
0:22 Boom, None type has no releases.
0:24 We're not checking that, right?
0:25 So let's first, before we fix this
0:27 write a test that says we get proper 404 handling
0:31 and then let's fix it. How do we do that?
0:34 Well, it's going to look similar to this.
0:38 But when I put that as 404
0:40 and we're going to say we're asking for
0:43 doesn't really matter, but let's say SQLAlchemy missing
0:46 we don't need any of this test data
0:49 because we're going to return None, okay?
0:52 We'll call this function
0:54 and there's going to be nothing to assert, for now.
0:58 Let's just run the test again.
1:02 None type has no releases, okay, this is the bug.
1:05 What we want to do, is check and make sure
1:08 that this actually raises an exception.
1:11 Let's first fix the error in our view model.
1:14 So let's go over here, now we're going to say
1:17 if self.package and their releases.
1:23 That looks good to me, I think that'll fix the bug
1:25 that tiny little bit right there, should fix it.
1:28 Let's try again. Boom. Perfect. So it did make it work
1:33 and we got this exception, right there.
1:37 However, it failed our test
1:39 because our test did not expect it.
1:42 So let's go back to our test, and tell it
1:44 expect an error right here.
1:46 So we'll come down here and say with self.assertRaises
1:51 and we give it the exception type, and of those.
1:57 We're going to import that up there, HTTP exceptions
2:02 like that, and then we're going to run this code here.
2:07 We don't really care what the model is, we just want to say
2:10 if we pass it here None instead of crashing
2:15 we're going to get an HTTPNotFound error, as an exception.
2:19 Let's do it. Boom. Passes. So, there you go.
2:23 We actually found a bug in our little app
2:25 and we fixed it, and now we have a test for it
2:28 to make sure it's never coming back.
2:30 Because, if it ever comes back
2:32 this test right here will fail again. Beautifully.
2:35 By the way, if you're not familiar with this assertRaises
2:38 if, for some reason, this doesn't execute that error
2:42 if for some reason it passes that block
2:44 without an error, that itself fails the test.
2:47 Let's just see that real quick. Boom. Failure.
2:51 We did not get HTTPNotFound, right
2:55 that's sort of a double negative, but it says
2:57 you told us there should have been an error
2:59 there was no error, therefore this test fails
3:01 because you were testing for an error case
3:04 that didn't happen. Now it's back. Perfect.