1
00:00:01,440 --> 00:00:07,150
So now we have a means of storing things in the session for our test, and that's really helpful.

2
00:00:07,620 --> 00:00:12,780
So what I want to do right now is just check our test coverage, which is going to be terrible because

3
00:00:12,780 --> 00:00:14,370
we've disabled most of our tests.

4
00:00:14,760 --> 00:00:17,670
But let's have a look at it and see what is covered and what isn't.

5
00:00:17,670 --> 00:00:23,100
And I'll do that by running my coverage script that I wrote some time ago, which saves me a little

6
00:00:23,100 --> 00:00:23,690
bit of typing.

7
00:00:24,990 --> 00:00:30,300
So as you can see, there's a lot of stuff not covered here, a lot of red in this file.

8
00:00:30,310 --> 00:00:34,210
But don't worry, we'll get this mostly green or entirely green by the time we're done.

9
00:00:34,440 --> 00:00:37,530
But what I want to look at right now is this one.

10
00:00:37,890 --> 00:00:39,470
It's our reservation function.

11
00:00:39,480 --> 00:00:42,060
This is the one we've been testing for the last lecture or so.

12
00:00:42,690 --> 00:00:46,950
And the first thing that's not covered is this Helper's server error.

13
00:00:46,980 --> 00:00:52,920
When we try to pull the model's reservation out of the session, if we can't get it, it fires well

14
00:00:52,920 --> 00:00:54,900
at a server error, which is not really helpful.

15
00:00:54,900 --> 00:00:58,680
We probably should make that a little more useful whenever possible.

16
00:00:58,680 --> 00:01:03,810
I like to avoid just throwing an error of 500 at people and instead give them some meaningful feedback

17
00:01:03,810 --> 00:01:05,020
as to what's gone wrong.

18
00:01:05,400 --> 00:01:12,270
So what I can do is change this and change this, change both of these functions such that if it fails,

19
00:01:12,540 --> 00:01:15,450
it doesn't actually just take you to an error screen.

20
00:01:15,450 --> 00:01:20,040
It takes you somewhere else, which is still part of the website, but gives you some meaningful, meaningful

21
00:01:20,040 --> 00:01:20,510
feedback.

22
00:01:21,090 --> 00:01:25,440
So I'll go back here, open up handlers and find the reservation function.

23
00:01:25,440 --> 00:01:26,790
And I'm going to change this part.

24
00:01:26,790 --> 00:01:31,830
I'm going to delete this error and instead I'm going to put something a little more useful.

25
00:01:31,830 --> 00:01:33,290
So I'll put something in the session.

26
00:01:33,290 --> 00:01:37,830
Emden session not put my app.

27
00:01:38,770 --> 00:01:39,300
Try that again.

28
00:01:39,750 --> 00:01:46,200
Emerg app dot session dot put and I will put in the context arg context.

29
00:01:48,700 --> 00:01:55,630
I will put an error message, which we called error, and I'll just put in the error message can't.

30
00:01:57,880 --> 00:02:04,990
Get reservation from session, which, you know, might that might not be that meaningful to a user,

31
00:02:05,380 --> 00:02:07,330
but this is just an exercise.

32
00:02:07,330 --> 00:02:09,340
You can put whatever error message you want in there.

33
00:02:09,590 --> 00:02:11,800
So I do that and then I will redirect them.

34
00:02:11,800 --> 00:02:14,170
I'll take them back to redirect.

35
00:02:14,950 --> 00:02:18,760
I'll take them to I have to put in my response writer and my request.

36
00:02:19,000 --> 00:02:24,610
I'll just take them to the home page and I'll put it in an error message, a status message here that

37
00:02:24,610 --> 00:02:30,970
says temporary redirect, HDP status, temporary redirect and then return because I don't want anything

38
00:02:30,970 --> 00:02:32,400
else to work after this.

39
00:02:32,950 --> 00:02:38,360
Now, I'll copy the same code and I'll replace this one as well.

40
00:02:39,370 --> 00:02:42,160
And in that, I will change the error message to.

41
00:02:47,430 --> 00:02:51,330
What am I going to call it, can't find room, can't find room?

42
00:02:51,730 --> 00:02:59,190
OK, so now when this test is run and I test it, what I want to test, what I want to do next is run

43
00:02:59,190 --> 00:03:06,510
another version of this test against the same handler and test the situation where there is no reservation

44
00:03:06,510 --> 00:03:07,820
variable in the session.

45
00:03:07,830 --> 00:03:08,800
So how am I going to do that?

46
00:03:08,820 --> 00:03:13,320
Well, I'll go back to my handlers test and I'll go to the very bottom of this.

47
00:03:13,650 --> 00:03:22,370
And what I want to do here is a test case where reservation is not in session.

48
00:03:23,100 --> 00:03:24,210
So I have to reset everything.

49
00:03:27,580 --> 00:03:34,130
And that's really what I'm going to do is reset my request and I can do that just by reinitialize in

50
00:03:34,150 --> 00:03:37,740
that request variable with with an HTTP new request.

51
00:03:38,170 --> 00:03:39,970
So request and an error.

52
00:03:39,970 --> 00:03:43,840
And this time I use the equals sign because I already declared this variable up here.

53
00:03:43,840 --> 00:03:49,020
I'm just reinitialize it and it will just be HGP dot new request.

54
00:03:50,230 --> 00:03:57,190
And again it's a get request, same you URL make reservation and an empty bottle.

55
00:03:57,760 --> 00:04:04,150
So now I've reinitialize that, but I still need to get the context with the session header so I can

56
00:04:04,150 --> 00:04:08,590
just say is equal to get with my request.

57
00:04:08,590 --> 00:04:14,680
And the reason I'm doing that is if I don't do that, I'm not going to be able to even test the situation

58
00:04:14,680 --> 00:04:18,480
where I can't find the value in the session because there is no session.

59
00:04:18,760 --> 00:04:24,460
So this gives me a context that I can then put back in the request, just like I did above.

60
00:04:24,850 --> 00:04:30,880
Request is equal to request with context and pass it the context I just created.

61
00:04:31,270 --> 00:04:37,810
So now I have a session, but that session doesn't have the reservation variable in it because I'm not

62
00:04:37,810 --> 00:04:38,260
going to add it.

63
00:04:39,580 --> 00:04:44,800
So once I've done that, I just create a new response recorder by initializing my our variable with

64
00:04:44,800 --> 00:04:54,850
HDB test, new recorder and then I call my handler handler don't serve HDP with my response recorder

65
00:04:55,150 --> 00:05:06,490
and my request and now I can just copy this, paste it down here, change this to status temporary redirect

66
00:05:06,490 --> 00:05:09,050
because that's what I'm expecting to find in this situation.

67
00:05:10,210 --> 00:05:13,120
Replace it down there and let's try running the test.

68
00:05:13,120 --> 00:05:19,600
So I will go back here, close this just so we can do our coverage in a minute, run our test, make

69
00:05:19,600 --> 00:05:20,380
sure it works.

70
00:05:20,380 --> 00:05:22,330
So I'll clear the screen and say go test.

71
00:05:23,390 --> 00:05:24,410
It should pass.

72
00:05:24,440 --> 00:05:25,130
It does.

73
00:05:25,400 --> 00:05:26,540
Now let's check our coverage.

74
00:05:28,050 --> 00:05:34,020
And when I do that, hopefully that first case will be taken care of because now I've tested a situation

75
00:05:34,350 --> 00:05:39,840
where there is something in the model's reservation, there's a reservation variable in the session,

76
00:05:40,110 --> 00:05:42,330
and I've tested a case where there is not.

77
00:05:42,330 --> 00:05:43,440
And that worked well.

78
00:05:43,920 --> 00:05:50,130
So the only thing left for this function appears to be getting a room by ID.

79
00:05:50,430 --> 00:05:52,590
So I try to get the Rambhai ID.

80
00:05:52,600 --> 00:06:02,640
Now, if we look at in our test database repo, which is under repository DB Repo Test repo, I'm looking

81
00:06:02,640 --> 00:06:08,120
for get Rambhai id find get room there is right there right now.

82
00:06:08,130 --> 00:06:09,460
This just returns an empty room.

83
00:06:09,870 --> 00:06:16,170
So how can I change this function such that I can either a return.

84
00:06:16,170 --> 00:06:21,980
Yes I found a room or B return an error while I'm having, I have this parameter ID that I'm not using.

85
00:06:21,990 --> 00:06:22,830
So let's try this.

86
00:06:23,820 --> 00:06:33,060
If ID is greater then to then do something else instead of returning a model's room, return a model's

87
00:06:33,060 --> 00:06:34,710
room, but an error too.

88
00:06:34,710 --> 00:06:40,110
So I'm going to move this declaration above my IF statement so I don't have to create a second variable

89
00:06:40,110 --> 00:06:41,130
just for this case.

90
00:06:41,490 --> 00:06:47,940
I will say return room and return an error and I have to make that error by hand.

91
00:06:47,940 --> 00:06:52,390
So I'll just say errors donu some error.

92
00:06:54,240 --> 00:06:58,380
Now if I pass it and ID greater than two, it will give me an error.

93
00:06:58,560 --> 00:07:01,560
But if I pass it a one or two it'll return no error.

94
00:07:01,560 --> 00:07:06,560
And that's all I need to do is to simulate the case where you're trying to get a non-existent room.

95
00:07:07,260 --> 00:07:11,250
So let's go back to our handlers test and test with a non-existent room.

96
00:07:12,630 --> 00:07:15,630
Once again, I'm going to want to do all of this stuff.

97
00:07:17,010 --> 00:07:26,950
So I will copy that and paste it here and I'll put a comment in test with non existent room, OK?

98
00:07:28,170 --> 00:07:33,330
And this time I want to put my sessional variable reservation back in there so I can get past that first

99
00:07:33,330 --> 00:07:33,770
case.

100
00:07:33,930 --> 00:07:37,560
So I already have that declared and filled with the necessary information.

101
00:07:37,950 --> 00:07:40,830
So I'll just put it here right after my response recorder.

102
00:07:41,130 --> 00:07:44,700
I'll put my sessional variable reservation in the room.

103
00:07:45,150 --> 00:07:48,980
And now I want to test for a case when there is a non-existent room.

104
00:07:50,400 --> 00:07:51,870
So how am I going to do that?

105
00:07:52,180 --> 00:07:57,720
Well, we need to specify a little one little change in our reservation model.

106
00:07:58,230 --> 00:08:01,950
I want to make sure that it has a remedy of greater than two.

107
00:08:01,950 --> 00:08:09,270
And I can do that just by overriding the value in there right now, by saying reservation room ID is

108
00:08:09,270 --> 00:08:12,300
equal to 100 and that's definitely greater than two.

109
00:08:13,780 --> 00:08:15,560
Room ID there.

110
00:08:16,360 --> 00:08:22,780
So the reason I'm doing that, let's go look at this handler that I'm calling, which is called reservations,

111
00:08:22,780 --> 00:08:27,250
all open up the reservation in this situation at line 70.

112
00:08:27,250 --> 00:08:33,370
In my code, I'm saying I've got the room from the session now get the room by I.D. and I'm checking

113
00:08:33,370 --> 00:08:35,200
reservation room ID.

114
00:08:35,560 --> 00:08:40,510
And I want that test to fail because I want to test the situation where you're trying to get a nonexistent

115
00:08:40,510 --> 00:08:40,840
room.

116
00:08:41,200 --> 00:08:47,760
So I'm populating the member room ID of that model reservation with a value greater than two.

117
00:08:48,010 --> 00:08:49,900
So this should work.

118
00:08:50,080 --> 00:08:50,800
Let's try that.

119
00:08:50,800 --> 00:08:55,270
Let's first of all, close this coverage in our browser, because we'll check that again in a moment.

120
00:08:55,570 --> 00:08:58,240
Clear this screen and go test.

121
00:09:00,780 --> 00:09:01,620
And it passed.

122
00:09:02,010 --> 00:09:07,620
Let's check our coverage and that function reservation should be entirely green now.

123
00:09:10,730 --> 00:09:17,480
And it is very good, so there we have managed to test the situations, enough situations are enough

124
00:09:17,480 --> 00:09:23,480
cases where every possibility is covered for that given handler reservation.

125
00:09:24,440 --> 00:09:29,150
And we can use exactly the same logic for every other one of our tests.

126
00:09:29,660 --> 00:09:35,750
But one of the things I noticed right away is that if I look at my tests, my handlers tests.

127
00:09:37,480 --> 00:09:42,400
There's a bunch of these things that I can test without a session, so I'm going to uncommented this

128
00:09:42,730 --> 00:09:45,200
and then delete the things that I don't want to test.

129
00:09:45,220 --> 00:09:47,050
Well, the first thing I don't want to test is this one.

130
00:09:47,530 --> 00:09:50,710
And I probably don't want to test these ones either.

131
00:09:51,010 --> 00:09:52,460
So I'll comment them back, actually.

132
00:09:52,540 --> 00:09:56,780
No, I don't want to, because these all use the session and they also take post variables.

133
00:09:57,670 --> 00:10:02,860
The other thing that I'll notice that I notice right away is that when I'm looking at these tests,

134
00:10:03,760 --> 00:10:08,230
I actually don't need to pass any post data here so I can delete this entirely.

135
00:10:08,230 --> 00:10:13,720
Just get rid of it, because I'm not going to bother testing the handlers in this table, test the ones

136
00:10:13,720 --> 00:10:14,560
that take a post.

137
00:10:14,890 --> 00:10:19,450
So I'm going to get rid of all of those data's.

138
00:10:22,790 --> 00:10:30,020
And I'll handle those with individual tests, just like I did for the reservation handler, so I can

139
00:10:30,020 --> 00:10:35,300
get rid of this and then get rid of the parameters posted, and that simplifies things immensely.

140
00:10:35,660 --> 00:10:37,520
So now let's try running this test again

141
00:10:40,280 --> 00:10:42,280
and we should see more coverage in her handlers.

142
00:10:42,380 --> 00:10:43,070
I got a typo.

143
00:10:44,600 --> 00:10:45,640
Expect the status code.

144
00:10:45,650 --> 00:10:47,320
It has no parameters.

145
00:10:47,330 --> 00:10:47,600
Right.

146
00:10:47,610 --> 00:10:50,440
So I better actually fix the actual handler itself.

147
00:10:51,650 --> 00:10:58,850
I'm just going to only get rid of this entirely and not bother checking to see if it's a post request,

148
00:10:59,640 --> 00:11:02,440
because I'm actually not running post requests in this test anymore.

149
00:11:03,790 --> 00:11:04,100
There.

150
00:11:05,500 --> 00:11:06,300
Let's try that again.

151
00:11:10,490 --> 00:11:11,270
Go test.

152
00:11:13,270 --> 00:11:18,160
That's better, and now let's check our coverage, which should be better than it was a moment ago.

153
00:11:21,060 --> 00:11:26,390
All right, so this is not getting tested new repo, but it will be before we're done.

154
00:11:28,640 --> 00:11:30,600
Post reservation is not being checked.

155
00:11:30,620 --> 00:11:33,110
I expected that, but these ones are, which is good.

156
00:11:33,110 --> 00:11:39,050
Post availability is not getting checked and availability, Jason is not and reservation summary is

157
00:11:39,050 --> 00:11:39,700
not OK.

158
00:11:40,100 --> 00:11:45,140
So there's still some to test, some that we had never written test before before because we just added

159
00:11:45,140 --> 00:11:45,800
them recently.

160
00:11:46,160 --> 00:11:48,350
But we definitely are getting closer than we were.

161
00:11:48,590 --> 00:11:53,540
We actually now have nineteen point seven percent of our statements tested, which is better than it

162
00:11:53,540 --> 00:11:54,830
was a few moments ago.

163
00:11:55,430 --> 00:11:55,880
All right.

164
00:11:55,880 --> 00:11:59,720
So in the next lecture, we'll start doing a few more of these tests.

165
00:11:59,720 --> 00:12:02,960
We've got our our reservation one done.

166
00:12:02,960 --> 00:12:06,020
The next one looks like it might be post search availability.

167
00:12:06,020 --> 00:12:08,870
So we'll add that and a few more in the next couple of lectures.
