00:0.14 When we look at our code here,
00:2.07 both the tests and the actual program itself.
00:4.74 The library. This core library.
00:6.27 It really works well when you go down the specified path,
00:10.98 I'm supposed to be able to go and see if there's a table get it.
00:13.52 I'm supposed to be able to book a table but it's missing all of its error
00:17.24 handling. And one thing that I've learned over time is that when people are new
00:21.42 to programming they just focus on getting it started making sure the thing is supposed to
00:26.28 do. It does. But the real difference of polished software is that it deals
00:31.0 with data that's not correct inputs that are not valid situations that didn't make sense.
00:36.34 Like trying to book a table that already has been booked or trying to book a
00:39.84 table that doesn't exist or something like that.
00:42.44 So what we're gonna do is going to write two more tests that verify that we're
00:46.3 checking for those things. So let's go over here and write a test.
00:53.64 The first one says you can't book a table that doesn't exist.
00:58.44 We want this to respond in a meaningful way.
01:1.35 How are we going to do that?
01:3.14 We're just gonna go down here and say core right?
01:4.88 There's a book a table. And if you look at what this value here is
01:8.5 supposed to be it's a string.
01:11.34 So we're gonna come down here and let's give it some 
01:13.12 ID. That's not going to work.
01:16.2 Right? So obviously this should fail.
01:19.64 What's going to happen. How should it fail?
01:21.53 Should it return none should it raise an exception.
01:24.54 I'm gonna go with raises exception.
01:25.89 Right? That's a pretty python equate to go.
01:28.23 In fact, if we go down here and look at book a table notice there's
01:33.37 a integer not found table unavailable error and all those kinds of things to the error
01:38.83 handling. An exception support should be there for us to do this.
01:42.24 Right? Let's give it a run and see what happens.
01:44.94 Uh It didn't pass what is what is going on here?
01:48.4 All right. So what we got was it this failed?
01:51.91 And why did it fail? But an attribute error?
01:56.14 This is probably the most common error in all the python attribute or none.
01:59.41 Type object has no attribute is booked.
02:2.44 This is not a meaningful error.
02:4.67 This is just our code crashing halfway through its method.
02:8.54 That's what we did is we had no table.
02:9.95 Then we tried to book it and obviously if it doesn't exist,
02:13.2 this is going to crash. So what we need to do is actually write some
02:17.38 tests in our code to check for this.
02:19.83 For example, we should say if not table.
02:24.04 What do we do? Let's raise an entity not found error.
02:29.54 All right. So that will avoid.
02:30.95 If there's no table us getting that attribute error here,
02:33.41 let's try that. Okay, It's still crashed.
02:36.22 What's going on. Let's see.
02:38.65 Oh, perfect. We did get this core attribute not found error,
02:44.54 but why did it crash? Well,
02:45.91 this comes down to how we're writing our test.
02:48.84 What's supposed to happen here in a normal test.
02:51.72 If there's an exception that's a failure.
02:53.72 But in one of these negative error condition tests,
02:57.02 that's exactly what we want is a failure.
02:59.73 If there's not an error, it's like a double negative of testing.
03:3.14 So what we can do is we can come over here and now notice so far
03:6.28 we've not even used pytest but now we can start working with py tests for
03:10.7 its extra features and we can create a context manager.
03:14.4 So we say with pytest raises and then you specify the type of exception.
03:20.34 Then anything that happens in there.
03:21.87 If it leaves us with block without raising that error,
03:25.59 that is a error. That's a failed test.
03:28.02 So that that's the double negative.
03:29.74 So now if we run it,
03:30.6 let's see what happens. Boom,
03:32.4 it passes. This one passes.
03:34.19 Why? Because we said it must raise an error and guess what it did.
03:38.34 Let's do one more of these negative tests and that is that.
03:41.92 You cannot book a already booked table.
03:47.44 They were down here again. Let's get a table.
03:50.64 We'll go down the course a all tables Bracket zero.
03:54.43 I'll just give us the first one quote up,
03:56.74 book the table the table id and actually let's again just to make sure so we
04:2.75 can make sure that we're not going to get one that's already booked as we want
04:7.02 to make sure this works smooth every time.
04:11.14 So we're going to book the table and then we want to try to book at
04:15.32 a second time. So we'll go to the flow of somebody,
04:18.71 books it not us and then later table id just like before this should be
04:28.22 an error. Let's say this time.
04:31.82 We want a core table unavailable error.
04:36.04 We like that. Okay, so this should work pretty easy.
04:39.74 Let's go ahead and do a little time to clean up,
04:42.02 make it run it failed again.
04:45.05 Why did it fail? Because it didn't match the double negative this time it did
04:48.54 not raise any error and it did not raise that one in particular.
04:53.14 So let's go back in and see what's going on here.
04:55.67 Book a table again. Look,
04:59.8 it seems like we might have just double booked it,
05:1.87 it was booked before and now it's still booked.
05:4.84 So we don't want to let that happen.
05:6.12 We're gonna say if table dot is booked,
05:9.44 raise that error, Run our tests again.
05:15.14 Hey, that did it now are two negative scenarios in our two positive scenarios pass
05:19.27 right? We cannot book a book table.
05:21.86 We cannot book a non existent table but we can book one and there are some that could be booked beautiful.
