1
00:00:00,820 --> 00:00:06,250
So we need to write some more tests to complete testing our handlers, those specifically that either

2
00:00:06,250 --> 00:00:10,890
access the session or that hit the database.

3
00:00:11,320 --> 00:00:16,960
And I said last time, I'm probably going to start with this one, which is the post to search dash

4
00:00:16,960 --> 00:00:17,530
availability.

5
00:00:17,530 --> 00:00:22,960
But I'm actually not going to I'm going to start with this one instead, the make dash reservation page

6
00:00:22,960 --> 00:00:24,070
when we post to that.

7
00:00:24,070 --> 00:00:29,710
And the reason I'm going to start with that one is because this one actually includes everything you

8
00:00:29,710 --> 00:00:32,350
need to know in order to write the tests yourself.

9
00:00:32,650 --> 00:00:38,920
So it covers every possible possible situation for all of the tests that I have to write for the remainder

10
00:00:38,920 --> 00:00:39,700
of my handlers.

11
00:00:40,270 --> 00:00:45,310
And I don't want to force you to watch me writing these tests one at a time because it gets extremely

12
00:00:45,310 --> 00:00:46,440
repetitive after a while.

13
00:00:46,480 --> 00:00:51,700
And what I'll do instead is after I've done the necessary coding to write the tests for the post to

14
00:00:51,700 --> 00:00:53,530
the make desk reservation.

15
00:00:53,530 --> 00:01:00,220
You, Earl, I will post the complete source code as it exists at this point in the course.

16
00:01:00,220 --> 00:01:05,140
Write to the next lecture and you can use that as a reference to write your own tests and I'll talk

17
00:01:05,140 --> 00:01:05,980
more about that later.

18
00:01:05,980 --> 00:01:10,410
But right now I want to test the Make Reservation, the posting to make Dasch reservation.

19
00:01:10,420 --> 00:01:11,260
So how am I going to do that?

20
00:01:11,290 --> 00:01:15,220
Well, I'm going to go to the bottom here and I'll create a new function func.

21
00:01:15,550 --> 00:01:23,940
And it is for the repository test repository, test repository and post reservation right there.

22
00:01:23,950 --> 00:01:29,890
So I'm testing the post reservation method or function in that has the receiver repository and it's

23
00:01:29,890 --> 00:01:30,310
a test.

24
00:01:30,940 --> 00:01:32,570
So let's go look at that and see what it does.

25
00:01:32,590 --> 00:01:36,810
So let's go over to handlers and here is the function post reservation.

26
00:01:37,300 --> 00:01:39,610
So this, first of all, passes a form.

27
00:01:39,610 --> 00:01:44,620
And you may recall some time ago I said it's always good practice to call parsed form whenever you're

28
00:01:44,620 --> 00:01:45,560
parsing form data.

29
00:01:45,580 --> 00:01:46,660
In fact, you have to call it.

30
00:01:46,930 --> 00:01:48,340
But it's good practice for a reason.

31
00:01:48,340 --> 00:01:52,570
And I'm going to draw your attention to this because we need to pay attention to what happens in this

32
00:01:52,570 --> 00:01:54,220
function a little bit later on.

33
00:01:54,230 --> 00:01:56,140
So that's the first thing I have to test.

34
00:01:56,740 --> 00:02:02,770
And if I have problems with that, well, it's going to right now just throw a server error.

35
00:02:02,770 --> 00:02:06,130
And as was the case in our previous test, I don't want that to happen.

36
00:02:06,130 --> 00:02:10,040
I want instead to give more meaningful feedback to our users.

37
00:02:10,040 --> 00:02:11,350
So let's make that change right now.

38
00:02:11,350 --> 00:02:18,160
And I'm going to go up to reservation and do the same thing I did here, a copy this and post it down

39
00:02:18,160 --> 00:02:20,410
in here to replace this server error.

40
00:02:20,800 --> 00:02:27,100
And I'll change the error message and the error messages can't pass form, and I'll just take them back

41
00:02:27,100 --> 00:02:33,190
to the home page with a status temporary redirect that a little bit further down, I try to pass the

42
00:02:33,190 --> 00:02:34,300
start and end date.

43
00:02:34,330 --> 00:02:39,880
Now, in theory, those should always be OK because we're using a date picker to put them in the correct

44
00:02:39,880 --> 00:02:40,360
format.

45
00:02:40,750 --> 00:02:42,760
But I never trust user input.

46
00:02:42,760 --> 00:02:50,200
So again, I'm going to test for that and I'll change the error message to can't pass start date.

47
00:02:50,440 --> 00:02:51,430
And I better still pass.

48
00:02:51,430 --> 00:02:51,630
Right.

49
00:02:52,230 --> 00:02:55,480
And again, these error messages are just to get the error message in here.

50
00:02:55,480 --> 00:03:00,940
For our purposes, you would probably have more meaningful error messages in a live application will

51
00:03:00,940 --> 00:03:11,900
do the same thing for end date, can't pass and date and get rid of that and room ID, same idea.

52
00:03:11,950 --> 00:03:15,220
So we write here, we are saying error invalid data.

53
00:03:15,220 --> 00:03:15,990
So that one's OK.

54
00:03:16,000 --> 00:03:17,050
That's giving me my status.

55
00:03:17,050 --> 00:03:18,010
Temporary redirect.

56
00:03:18,310 --> 00:03:18,970
I want that.

57
00:03:19,330 --> 00:03:23,540
Then we build our reservation model up and we pass our form.

58
00:03:23,620 --> 00:03:28,960
We generate our form and start testing some things and then we test for first name, last name and email

59
00:03:28,960 --> 00:03:29,860
which have to be there.

60
00:03:30,520 --> 00:03:35,380
We make sure that first name is three characters along at least that email is in the form of email.

61
00:03:35,590 --> 00:03:42,160
And if it's not, we take them back to the form, repopulate it with the data that they've entered and

62
00:03:42,160 --> 00:03:43,930
highlight the errors that they have.

63
00:03:44,140 --> 00:03:50,530
OK, assuming we get past that, we then attempt to insert something into the database and get a reservation.

64
00:03:50,530 --> 00:03:54,940
So we attempt to assert the reservation into the database and get a reservation back.

65
00:03:54,940 --> 00:03:58,060
And again here I don't want to helper's server error.

66
00:03:58,270 --> 00:04:07,600
I want to take them back somewhere and I'll say can't insert reservation into a database, which is

67
00:04:07,600 --> 00:04:11,230
just an error message, and we'll take them back to the homepage, which is sufficient for our purposes

68
00:04:11,230 --> 00:04:11,710
right now.

69
00:04:12,400 --> 00:04:14,980
And lastly, we try it with the room restriction.

70
00:04:15,010 --> 00:04:16,870
So again, I don't want a server error.

71
00:04:16,900 --> 00:04:21,420
I'm going to say can't insert room restriction.

72
00:04:23,500 --> 00:04:25,480
Try that again there.

73
00:04:27,460 --> 00:04:30,990
So now I have things I can actually write test for.

74
00:04:31,150 --> 00:04:36,070
So let's go back and start writing this test and look at the very first thing we need to do.

75
00:04:38,080 --> 00:04:41,980
So the first thing we need to do in our test is to pass the form.

76
00:04:42,310 --> 00:04:47,260
And here is where you need to draw your attention back to what pass form does.

77
00:04:47,830 --> 00:04:50,080
I said a while ago, good practice to do this.

78
00:04:50,080 --> 00:04:55,210
And here's what it actually does, is read on the screen in front of us pass form, populates our form

79
00:04:55,210 --> 00:04:57,690
and our post form for all requests.

80
00:04:57,880 --> 00:05:00,130
Pass form passes the raw query.

81
00:05:00,240 --> 00:05:07,950
From the URL and updates our form, so what I need to do then is to generate a post request that actually

82
00:05:07,950 --> 00:05:11,280
supplies a body that will pass the form data.

83
00:05:11,310 --> 00:05:13,020
So let's go back and start writing our tests.

84
00:05:13,290 --> 00:05:16,770
So here, what's the very first thing I need to do?

85
00:05:16,800 --> 00:05:22,550
Well, it's pretty much the same as what I did in my reservation here, my repository in a repository

86
00:05:22,570 --> 00:05:23,130
reservation.

87
00:05:23,340 --> 00:05:28,500
I need to create a request to get a context for the session so I can write to that, generate a new

88
00:05:28,500 --> 00:05:29,130
recorder.

89
00:05:29,160 --> 00:05:36,030
So let's copy that and come down here and paste it in and I'm going to post not get.

90
00:05:36,030 --> 00:05:43,710
So I'm creating a request that says create a post to make reservation and I can't pass the Knill for

91
00:05:43,710 --> 00:05:44,130
the body.

92
00:05:44,670 --> 00:05:48,960
You would think you could you would think you could just maybe construct a U URL values and put that

93
00:05:48,960 --> 00:05:49,480
somewhere.

94
00:05:49,560 --> 00:05:50,610
No, that won't work.

95
00:05:50,610 --> 00:05:52,580
You actually need to construct the body.

96
00:05:53,220 --> 00:05:54,810
So let's think about how we can do that.

97
00:05:54,860 --> 00:05:59,820
Well, you know that when you pull anything out of a post request, the art form and a handler, when

98
00:05:59,820 --> 00:06:04,830
you say ARGT form aget first underscore name, you're getting that as a string.

99
00:06:04,830 --> 00:06:07,920
And that's actually pretty indicative of what we need to do here.

100
00:06:07,920 --> 00:06:12,960
We need to generate a string and then pass that as an argument to new request.

101
00:06:13,050 --> 00:06:19,710
Now, if we look at new request, a new request, it says it requires three parameters method, which

102
00:06:19,710 --> 00:06:20,370
is a string.

103
00:06:20,370 --> 00:06:24,900
We have that it's post you URL, which is a string make reservation.

104
00:06:24,900 --> 00:06:25,860
We have that as well.

105
00:06:26,370 --> 00:06:33,030
And then it requires a body o reader or something that satisfies the requirements in order to be taken

106
00:06:33,030 --> 00:06:34,710
as an iReport reader.

107
00:06:34,710 --> 00:06:38,250
And we have something that does for that does that for us writing the Strings package.

108
00:06:38,340 --> 00:06:39,290
But we'll get to that in a moment.

109
00:06:39,540 --> 00:06:42,300
First of all, let's build our post request.

110
00:06:42,870 --> 00:06:48,180
There are other ways to do this, but I think this is a good exercise for you to figure out how post

111
00:06:48,180 --> 00:06:49,550
requests actually work.

112
00:06:49,800 --> 00:06:52,230
First of all, I'm going to create a string which I'll call recorded.

113
00:06:54,420 --> 00:06:55,410
That's going to be a string.

114
00:06:55,710 --> 00:07:00,330
And right now I'm just going to put one thing in here, one of the required form parameters that we

115
00:07:00,330 --> 00:07:02,550
know have to be here and that start date.

116
00:07:03,750 --> 00:07:06,750
And that's going to be equal to and I'll put it way in the future.

117
00:07:06,750 --> 00:07:10,800
So I know that it's going to be a date, that it's never going to cause me problems for a long time,

118
00:07:11,460 --> 00:07:12,930
20, 50 zero one zero one.

119
00:07:13,110 --> 00:07:20,580
So now I have a string and now I'm going to append to that one line will append every request parameter

120
00:07:20,580 --> 00:07:23,880
that we need to make this a valid post for our handler.

121
00:07:23,880 --> 00:07:30,240
So I'll say Rech Body and I will use the format package, which is part of the standard library, and

122
00:07:30,240 --> 00:07:36,270
I will use the method or the function as print F and I'm just going to build up my string one line at

123
00:07:36,270 --> 00:07:36,690
a time.

124
00:07:36,690 --> 00:07:42,780
So this will take two placeholders a percent s followed by an ampersand followed by a percent.

125
00:07:42,780 --> 00:07:46,550
S and those placeholders are replaced with request body.

126
00:07:46,800 --> 00:07:51,510
So the first part of this string will be what the request bodies currently equal to, which is this

127
00:07:51,510 --> 00:07:52,290
line right here.

128
00:07:52,860 --> 00:07:58,380
And the second one will just be my next parameter, which is Endi, and that will be equal to 20 50

129
00:07:59,040 --> 00:08:00,510
zero one zero two.

130
00:08:01,530 --> 00:08:02,910
And then I'll duplicate that line.

131
00:08:03,570 --> 00:08:07,920
And my next parameter that I require is my first name, which has to be three characters long.

132
00:08:07,920 --> 00:08:14,670
So I'll make it John, which is four characters then the last name, which will be Smith.

133
00:08:15,330 --> 00:08:17,690
I'm just putting valid data in here quite deliberately.

134
00:08:18,270 --> 00:08:23,970
My next one is my email, which can be anything in the form of an email John at Smith dot com.

135
00:08:24,690 --> 00:08:27,200
And the last one we need right now is phone.

136
00:08:27,840 --> 00:08:31,590
There's one more actually, and that can be anything.

137
00:08:31,590 --> 00:08:34,220
One, two, three, four, five, six, seven, eight, nine.

138
00:08:35,310 --> 00:08:39,180
And lastly, we need a remedy, which I will make one.

139
00:08:39,600 --> 00:08:43,650
OK, now I've built up a string which will read as follows.

140
00:08:43,890 --> 00:08:51,030
Start date equals 20 50 dash zero one zero one ampersand and date equals twenty fifty dash zero one

141
00:08:51,030 --> 00:08:52,590
zero two ampersand.

142
00:08:52,590 --> 00:08:56,220
First name equals John and all the way down to we get to remedy.

143
00:08:57,540 --> 00:09:02,820
So now I have everything I need to make my post and what I need to supply here.

144
00:09:02,820 --> 00:09:05,750
As you can see, when you look at new request, I need an IO reader.

145
00:09:05,760 --> 00:09:08,300
Well fortunately String's has a really nice function.

146
00:09:08,790 --> 00:09:13,890
The Strings package, which is built right into the standard library, has a function called New Reader,

147
00:09:14,100 --> 00:09:19,800
and it just takes one argument, which is a string, which is request body, in our case, our Rikyu

148
00:09:19,800 --> 00:09:20,190
body.

149
00:09:20,910 --> 00:09:27,210
So now I've built up a request and that request has this post data as the body.

150
00:09:27,780 --> 00:09:31,800
So now that I've built this up, the next thing I do is the same thing I did before.

151
00:09:32,010 --> 00:09:37,860
I just create a context by calling get get context, this function down here passing at the request

152
00:09:37,860 --> 00:09:38,940
that I created right here.

153
00:09:39,270 --> 00:09:42,150
And it gives me back a context that knows about the session.

154
00:09:42,720 --> 00:09:48,570
And then we just add that context right back into the request by calling request dot with context and

155
00:09:48,570 --> 00:09:50,400
passing it the context we just created.

156
00:09:50,910 --> 00:09:53,340
So now I'm pretty much ready to make that request.

157
00:09:53,890 --> 00:09:57,540
There's only one more thing I'm going to add here, and it's something that may not actually be required,

158
00:09:57,720 --> 00:09:59,460
but I think it's excellent practice.

159
00:09:59,950 --> 00:10:05,190
That is to set the header for the request, so I'm going to call one of the methods or one of the functions

160
00:10:05,190 --> 00:10:11,760
built into my request called header, and I'm going to set the header and I'm going to set it to tell

161
00:10:11,760 --> 00:10:15,660
the Web server about the kind of request that is coming its way.

162
00:10:15,960 --> 00:10:19,440
And I want to tell the Web server that it is a form post.

163
00:10:19,440 --> 00:10:26,130
And I do that by specifying this header content dash type with a capital C and a capital T, that's

164
00:10:26,130 --> 00:10:27,330
the name of the header.

165
00:10:27,360 --> 00:10:36,870
And the value is application slash x dash w w w dash form dash u url encoded.

166
00:10:37,440 --> 00:10:42,360
And that's part of the web standard and what that actually says to the web server is, hey, the request

167
00:10:42,360 --> 00:10:48,090
you're about to get, it is a form post and this is the header that tells a Web server that this is

168
00:10:48,090 --> 00:10:48,860
a form post.

169
00:10:50,160 --> 00:10:55,740
So now I create my my new recorder, just as I did before, and then I need my handler.

170
00:10:55,740 --> 00:11:00,810
So we'll turn our our handler, which in this case is called post reservation into a handler func.

171
00:11:00,810 --> 00:11:07,680
So I'll again, just like I did before, create a variable and assign at the value of http dot handler

172
00:11:07,680 --> 00:11:08,070
func.

173
00:11:09,780 --> 00:11:11,100
Handler func.

174
00:11:13,290 --> 00:11:20,580
And pointed to the correct correct handler, which in our case is repo post reservation there.

175
00:11:21,120 --> 00:11:24,740
Now I have a handler, so that means I can actually call this handler.

176
00:11:24,750 --> 00:11:30,670
So let's try that handler dot serve HTP and pass it.

177
00:11:30,690 --> 00:11:35,370
Our response writer, which is not really a response writer, but it's something that satisfies the

178
00:11:35,370 --> 00:11:37,620
requirements for being a response writer.

179
00:11:37,880 --> 00:11:44,560
Our and our pointer to an HDB request, which is just to request the one we built up above.

180
00:11:45,180 --> 00:11:48,780
So now that I've built this, I can actually do the same thing I did up here.

181
00:11:48,840 --> 00:11:57,840
I can copy this code, paste it down here and find out what do I want this to have for a return type

182
00:11:58,200 --> 00:11:59,220
and the return type.

183
00:11:59,220 --> 00:12:06,090
If we go back to our our handlers and assume that everything worked OK, our return type at the very

184
00:12:06,090 --> 00:12:08,240
end of this function is status the other.

185
00:12:08,250 --> 00:12:13,230
So I'll copy that, go back to my tests and not look for a temporary redirect.

186
00:12:13,500 --> 00:12:20,010
I will look for status the other and I will say change this to post reservation post reservation handler

187
00:12:20,020 --> 00:12:21,420
returned wrong response code.

188
00:12:21,420 --> 00:12:28,050
Got this, wanted that and substitute what we got from our, our test server and what we expected which

189
00:12:28,050 --> 00:12:28,890
is the other.

190
00:12:28,890 --> 00:12:29,820
So let's see if that worked.

191
00:12:30,030 --> 00:12:33,750
So we'll go back to our terminal, make sure that we're in the right direction.

192
00:12:33,930 --> 00:12:34,500
We are.

193
00:12:34,500 --> 00:12:36,180
And run our test go test.

194
00:12:37,570 --> 00:12:38,440
And that past.

195
00:12:38,470 --> 00:12:40,850
OK, so this one actually works.

196
00:12:40,870 --> 00:12:49,030
So now we know how to actually manually build up the body of a post request and send it to the server

197
00:12:49,030 --> 00:12:50,260
and make sure that it all worked.

198
00:12:50,740 --> 00:12:56,770
But all we're testing here right now is that every situation is correct, that we've done everything

199
00:12:56,770 --> 00:12:57,130
right.

200
00:12:57,520 --> 00:13:01,490
So what we need to do is to actually test other situations.

201
00:13:01,720 --> 00:13:03,710
So what do we need to test?

202
00:13:03,730 --> 00:13:09,020
Let's go back to our handlers and start at the very top of the post reservation, which is right here.

203
00:13:09,580 --> 00:13:14,090
So the first thing we want to fail is what if there's no body to the request?

204
00:13:14,110 --> 00:13:16,040
What if we don't we can't pass the form.

205
00:13:16,060 --> 00:13:23,260
In other words, so I've already changed this from a helper's server error to a nice redirect, which

206
00:13:23,260 --> 00:13:29,650
means we'll be looking for the status of temporary redirect, but we need to make another call to our

207
00:13:29,680 --> 00:13:30,390
handler.

208
00:13:30,400 --> 00:13:34,600
But this time we want to make sure that there's nothing in the body of the request.

209
00:13:35,110 --> 00:13:39,700
So what I can do is just reset everything so I can copy all of this stuff.

210
00:13:39,730 --> 00:13:48,280
I'll just copy all of it and put a comment here, test for missing post body and paste it in there.

211
00:13:49,030 --> 00:13:51,850
And of course, this becomes an equal sign.

212
00:13:52,240 --> 00:13:55,030
And I'm not going to pass the request body this time.

213
00:13:55,030 --> 00:13:59,590
I'm going to pass in a mail, which should cause that to fail.

214
00:14:00,040 --> 00:14:02,220
And this context is not a sign anymore.

215
00:14:02,230 --> 00:14:06,580
It's a new we're getting a new sessional context just to make sure everything is starting from a known

216
00:14:06,730 --> 00:14:09,910
proper value and will make the response writer.

217
00:14:09,910 --> 00:14:11,110
We don't need an assigned there.

218
00:14:11,110 --> 00:14:13,900
I'm going to get rid of these spaces just to make things a little closer.

219
00:14:14,290 --> 00:14:15,970
And we're not assigning a new handler.

220
00:14:15,970 --> 00:14:21,670
We're simply redefining our handler with our new we don't even need this line, but I'm going to put

221
00:14:21,670 --> 00:14:22,570
it in to make it readable.

222
00:14:23,260 --> 00:14:24,520
And then we make a request.

223
00:14:24,670 --> 00:14:31,780
And what we want to see here is status C other and put it down here.

224
00:14:32,260 --> 00:14:39,310
Post Reservation will change our message to handle returned wrong response code for missing post body.

225
00:14:40,310 --> 00:14:41,710
Let's try that and see what happens.

226
00:14:42,730 --> 00:14:46,060
So I'll clear my screen go test.

227
00:14:47,090 --> 00:14:51,440
And it failed and I got three or seven wanted 303, let's see what we have here.

228
00:14:51,890 --> 00:14:54,020
So did I get that wrong in my handlers?

229
00:14:54,380 --> 00:14:55,400
That's temporary redirect.

230
00:14:55,430 --> 00:14:55,870
That's right.

231
00:14:56,300 --> 00:14:57,060
It's not the other.

232
00:14:57,770 --> 00:15:01,940
So my test told me exactly what I wanted to find out, which is good.

233
00:15:02,520 --> 00:15:03,250
Let's try that again.

234
00:15:04,130 --> 00:15:05,060
Clear the screen.

235
00:15:05,420 --> 00:15:05,930
Run it.

236
00:15:07,340 --> 00:15:07,760
Good.

237
00:15:07,760 --> 00:15:13,000
Now, let's try our coverage, see what's missing so far and there'll be a lot missing.

238
00:15:14,450 --> 00:15:15,350
So what is missing?

239
00:15:15,380 --> 00:15:16,590
Well, we haven't got this yet.

240
00:15:16,610 --> 00:15:18,110
We'll take care of that later on.

241
00:15:18,110 --> 00:15:18,900
That's no problem.

242
00:15:19,520 --> 00:15:20,760
All of this is green.

243
00:15:20,870 --> 00:15:22,630
I mean, the first one is great.

244
00:15:22,640 --> 00:15:23,810
The second one is great.

245
00:15:24,410 --> 00:15:25,640
And here's the one I'm looking at.

246
00:15:25,640 --> 00:15:26,210
Reservation.

247
00:15:26,210 --> 00:15:26,680
That's great.

248
00:15:26,690 --> 00:15:28,570
Now, here's the one I'm looking at post reservation.

249
00:15:28,580 --> 00:15:31,910
OK, so we managed to handle the first case, which is great.

250
00:15:32,180 --> 00:15:38,480
Now we want to handle the cases where the start date and then the end date and then the remedy are all

251
00:15:38,480 --> 00:15:39,190
invalid.

252
00:15:39,350 --> 00:15:40,370
OK, well that's easy.

253
00:15:40,370 --> 00:15:44,900
We know how to do that already and it just becomes a matter of copy and paste and changing content.

254
00:15:45,980 --> 00:15:54,410
So what I'm going to do is copy this entire thing, create a new comment here that says test for invalid

255
00:15:55,100 --> 00:16:00,320
start date and paste that in there and get rid of this comment.

256
00:16:00,590 --> 00:16:07,070
And now I need to change this data and I'm going to do this by copying the entire thing and changing

257
00:16:07,070 --> 00:16:08,390
the one value that I want.

258
00:16:08,870 --> 00:16:16,700
So I'll come back down to where my comment is and I need to reset my post body and make this a string's

259
00:16:16,910 --> 00:16:17,630
new reader.

260
00:16:17,780 --> 00:16:24,620
And in body, this becomes an equal sign because we've already declared that variable above.

261
00:16:24,620 --> 00:16:32,030
And now I want to put the start date in an invalid format and the word invalid is a perfect invalid

262
00:16:32,030 --> 00:16:32,510
format.

263
00:16:33,050 --> 00:16:36,530
OK, so that should fail.

264
00:16:36,920 --> 00:16:41,110
This part should pass when we actually make the first request.

265
00:16:41,120 --> 00:16:44,150
So over here in our handlers, this should pass.

266
00:16:44,150 --> 00:16:45,020
We should get past that.

267
00:16:45,020 --> 00:16:47,930
No problem, because we have a form data that we can pass.

268
00:16:48,500 --> 00:16:52,100
This is no problem because I'm just getting values from the form post.

269
00:16:52,100 --> 00:16:57,440
If there's nothing in there that just becomes an empty string, but this part should fail and we're

270
00:16:57,440 --> 00:16:59,570
again going to be getting a temporary redirect.

271
00:16:59,570 --> 00:17:04,280
So let's make sure we're actually testing for a temporary redirect at the end of this.

272
00:17:04,280 --> 00:17:10,640
We are, but we need to change this part of our error message for invalid start date.

273
00:17:13,250 --> 00:17:16,460
OK, so let's run that test and see if we missed anything.

274
00:17:18,500 --> 00:17:19,220
Go test.

275
00:17:20,120 --> 00:17:21,200
It should pass.

276
00:17:21,200 --> 00:17:25,040
And it does check our coverage and make sure it's actually testing what we think it's testing.

277
00:17:25,790 --> 00:17:31,550
So here it is all the way down to post data and that got tested properly.

278
00:17:31,550 --> 00:17:31,880
Good.

279
00:17:32,570 --> 00:17:38,810
Now I once again, just to make this clear, what I'm doing, I'm going to test for an invalid date,

280
00:17:38,900 --> 00:17:42,980
a copy and paste the whole thing invalid and date.

281
00:17:43,100 --> 00:17:45,440
And this time I'll make sure the start date is valid.

282
00:17:46,070 --> 00:17:48,710
Twenty fifty dash zero one zero one.

283
00:17:48,950 --> 00:17:52,670
The end date is invalid and I'll just make it again the word invalid.

284
00:17:53,870 --> 00:17:57,770
Change my error message to end date and run the test.

285
00:17:58,070 --> 00:18:01,250
And there's of course copying pasting the whole blocks of code like this.

286
00:18:01,550 --> 00:18:05,630
Really not a good idea, but I'm just trying to make it eminently readable for you.

287
00:18:05,960 --> 00:18:12,860
What I would do once I figured out how I'm writing this test is make this a table test and just build

288
00:18:12,860 --> 00:18:18,410
up my data as a slice of whatever format I need and run it through a for loop.

289
00:18:18,410 --> 00:18:23,900
But this works just fine for our purposes today and we're trying to make it clear how everything is

290
00:18:23,900 --> 00:18:24,590
getting tested.

291
00:18:24,590 --> 00:18:34,510
So now that coverage close, close, close, that go back to here and go test, it should passed.

292
00:18:34,760 --> 00:18:35,450
It does.

293
00:18:35,780 --> 00:18:36,800
And now coverage.

294
00:18:40,390 --> 00:18:41,260
And there it is.

295
00:18:41,320 --> 00:18:45,620
OK, so let's go down here and make sure that it actually tested what we thought it would end date.

296
00:18:45,640 --> 00:18:46,510
It does.

297
00:18:46,870 --> 00:18:52,360
So now we need to do the same thing for our remedy.

298
00:18:53,410 --> 00:19:03,420
So let's close this and go back to our editor and once again, copy all of this, paste it in here.

299
00:19:05,140 --> 00:19:07,330
And this time we're testing for invalid remedy.

300
00:19:08,360 --> 00:19:13,270
Now, what's going to make it an invalid remedy is if it's not something that can be converted into

301
00:19:13,270 --> 00:19:13,390
an.

302
00:19:13,720 --> 00:19:20,380
So, again, I can put the word invalid here and change the end date to a valid end date, 20, 50 zero

303
00:19:20,380 --> 00:19:23,500
one zero two and change my error down here.

304
00:19:23,500 --> 00:19:24,970
Invalid remedy.

305
00:19:28,000 --> 00:19:31,950
Save that and let's actually run our test again.

306
00:19:33,610 --> 00:19:34,330
Go test.

307
00:19:35,880 --> 00:19:41,160
It passes and lets do our coverage and make sure that it tests what we think it's testing.

308
00:19:44,210 --> 00:19:47,900
And this is post preservation and here it is remedy.

309
00:19:47,920 --> 00:19:49,240
Now that is tested as well.

310
00:19:49,610 --> 00:19:51,290
So what's left to test?

311
00:19:51,470 --> 00:19:56,780
What's left to test is we need to make sure that it passes form validation.

312
00:19:56,930 --> 00:20:00,320
And if it's not valid, we want to take them back to that screen.

313
00:20:00,320 --> 00:20:01,580
So let's test that.

314
00:20:01,580 --> 00:20:04,850
How can we make sure that our form validation fails?

315
00:20:05,120 --> 00:20:10,940
What we can do that by either leaving something out like first name or making sure that first name is

316
00:20:10,940 --> 00:20:12,360
not at least three characters long.

317
00:20:12,380 --> 00:20:21,100
So let's go back to let's close this window again, go back to our code, copy everything here, paste

318
00:20:21,110 --> 00:20:24,200
it down and say test for invalid data.

319
00:20:25,430 --> 00:20:29,390
So we'll leave everything the same except for first name, which I'll make one character long.

320
00:20:29,390 --> 00:20:35,690
OK, and that should test it for us for invalid data instead of remedy.

321
00:20:37,040 --> 00:20:37,730
Save that.

322
00:20:38,300 --> 00:20:38,780
Go back.

323
00:20:38,780 --> 00:20:39,740
Run our tests.

324
00:20:44,780 --> 00:20:51,500
The test passed was checker coverage to make sure we have the right thing being tested up, it comes

325
00:20:51,710 --> 00:20:57,800
down to post reservation, which is right here, and we want to make sure, no, that did not actually

326
00:20:57,800 --> 00:20:58,170
work.

327
00:20:58,580 --> 00:21:00,000
So what do we have here?

328
00:21:00,020 --> 00:21:01,320
Why did it pass?

329
00:21:01,340 --> 00:21:02,480
Let's go back and look at our data.

330
00:21:02,500 --> 00:21:05,830
I'll close this window, go back and look and see what I got wrong here.

331
00:21:06,320 --> 00:21:07,580
Test for invalid data.

332
00:21:07,580 --> 00:21:08,590
First name is Jay.

333
00:21:08,600 --> 00:21:09,710
Last name is Smith.

334
00:21:10,160 --> 00:21:11,320
Posted in there.

335
00:21:11,330 --> 00:21:15,490
Get context if it's not equal to temporary redirect.

336
00:21:15,540 --> 00:21:15,920
Aha.

337
00:21:16,400 --> 00:21:17,690
So let's see.

338
00:21:18,140 --> 00:21:18,800
That's the part.

339
00:21:18,800 --> 00:21:19,520
That's the problem.

340
00:21:20,140 --> 00:21:23,780
It's a set up test handlers don't go hendershott go.

341
00:21:23,990 --> 00:21:27,650
What happens when we fail our validation test.

342
00:21:29,800 --> 00:21:31,840
If form is not valid, status the other.

343
00:21:33,220 --> 00:21:35,060
So let's see what happens here.

344
00:21:35,230 --> 00:21:38,080
This should be status C other status the other.

345
00:21:38,770 --> 00:21:39,850
I think that's still going to fail.

346
00:21:39,850 --> 00:21:41,140
I think I missed something else.

347
00:21:41,470 --> 00:21:43,240
Start date, end date.

348
00:21:44,050 --> 00:21:45,470
First name is equal to J.

349
00:21:45,490 --> 00:21:46,660
Is that what I'm actually checking.

350
00:21:46,660 --> 00:21:47,290
First name.

351
00:21:49,630 --> 00:21:54,880
First name has to be at least three characters along, OK, if not form is valid, then it takes us

352
00:21:54,880 --> 00:21:57,470
back there and returns and that should be status the other.

353
00:21:57,490 --> 00:21:58,560
OK, let's see what happens.

354
00:21:59,830 --> 00:22:00,610
Go test.

355
00:22:03,280 --> 00:22:07,060
Got three or seven, wanted three or three, why did that fail?

356
00:22:07,450 --> 00:22:13,880
I must have left something, set the wrong way back in our test start date and date.

357
00:22:13,900 --> 00:22:14,260
Oh, yeah.

358
00:22:14,260 --> 00:22:16,150
Remedy changed that to one.

359
00:22:16,390 --> 00:22:17,270
That's my own fault.

360
00:22:17,280 --> 00:22:18,630
I can't see what's in front of me.

361
00:22:18,670 --> 00:22:19,660
Let's try that one more time.

362
00:22:20,410 --> 00:22:21,220
Go test.

363
00:22:22,570 --> 00:22:29,890
And now it passes and now if we look at our coverage, that should actually test.

364
00:22:31,630 --> 00:22:33,650
Right here, the form is not valid.

365
00:22:33,670 --> 00:22:34,180
Perfect.

366
00:22:34,630 --> 00:22:36,590
So now we have to do two more.

367
00:22:36,760 --> 00:22:41,130
We have to do our insert reservation and our insert room restriction.

368
00:22:41,140 --> 00:22:42,040
How are we going to do that?

369
00:22:42,070 --> 00:22:45,820
Well, we'll do it much the same way we did for one of our earlier tests.

370
00:22:45,850 --> 00:22:52,450
We'll go look at the test repo and we want to test insert reservation and we want to test insert room

371
00:22:52,450 --> 00:22:52,980
restriction.

372
00:22:52,990 --> 00:22:58,220
So what we can do is use this reservations model and we can just do arbitrary things.

373
00:22:58,600 --> 00:23:07,640
So what I'm going to say is if the test if the room ID is to then fail, otherwise pass.

374
00:23:08,770 --> 00:23:14,290
So if restroom ID is equal to two, I'll return.

375
00:23:14,800 --> 00:23:16,810
It doesn't matter what I return for the int.

376
00:23:17,170 --> 00:23:26,410
I want an error errors donu some error and I'll do the same thing down here, but I'll do that next

377
00:23:26,410 --> 00:23:27,710
to make sure I get the right data in there.

378
00:23:27,730 --> 00:23:29,820
So let's go back and do this part of the test first.

379
00:23:30,460 --> 00:23:40,930
Once again, I'm just going to cheat and say copy all of this and put in a comment test for failure

380
00:23:41,680 --> 00:23:46,060
to insert reservation into a database.

381
00:23:46,780 --> 00:23:49,210
And this time I'll make sure all of the data is valid.

382
00:23:49,420 --> 00:23:54,760
So, John, change this comment to say this was built.

383
00:23:54,760 --> 00:23:55,030
Right?

384
00:23:56,160 --> 00:23:58,420
Uh, reservation handler.

385
00:24:01,680 --> 00:24:08,600
Failed when trying to fail in inserting reservation.

386
00:24:08,700 --> 00:24:10,790
Not the best comment in the world, but it gets the idea.

387
00:24:11,460 --> 00:24:14,210
And what response code should that return at this point?

388
00:24:14,550 --> 00:24:17,380
So it's at this point status temporary redirect.

389
00:24:17,430 --> 00:24:18,390
So let's go back here.

390
00:24:18,840 --> 00:24:20,970
Change this to status, temporary direct.

391
00:24:20,970 --> 00:24:26,790
Change this to status, temporary redirect and make sure that we give it a room ID of two, which is

392
00:24:26,790 --> 00:24:27,990
going to generate the failure.

393
00:24:28,320 --> 00:24:29,150
So this should work.

394
00:24:29,820 --> 00:24:30,590
That's valid.

395
00:24:31,080 --> 00:24:32,090
All of that is valid.

396
00:24:32,220 --> 00:24:36,090
Let's give it a try go test.

397
00:24:39,040 --> 00:24:39,890
And it passed.

398
00:24:39,940 --> 00:24:42,070
Let's get our coverage and make sure we tested the right thing.

399
00:24:44,820 --> 00:24:47,880
Scroll on down, this is reservation.

400
00:24:47,910 --> 00:24:54,060
This is post reservation, so hopefully, yes, we're down to only one thing left to check and that

401
00:24:54,060 --> 00:24:55,740
isn't inserting the room restriction.

402
00:24:56,250 --> 00:25:03,030
So let's close that window and close this window and go back to our editor and do our last test for

403
00:25:03,030 --> 00:25:04,200
this particular handler.

404
00:25:04,530 --> 00:25:05,460
So I will copy.

405
00:25:05,880 --> 00:25:07,670
Get rid of that comic that doesn't need to be there.

406
00:25:08,040 --> 00:25:09,360
Copy all of this.

407
00:25:11,010 --> 00:25:16,470
And this time I want to get past the room restriction or the the saving of the reservation.

408
00:25:16,860 --> 00:25:25,440
And I want to test for failed to insert restriction into database, which means this has to be remedy

409
00:25:25,440 --> 00:25:25,830
one.

410
00:25:26,130 --> 00:25:28,290
So we'll pass the saving of the reservation.

411
00:25:28,560 --> 00:25:32,790
But now we want to return something that's going to fail no matter what we do.

412
00:25:33,360 --> 00:25:37,800
So let's go back here to our our test repo and see how we can do this.

413
00:25:38,370 --> 00:25:41,780
We're saying here, if the reservation ID is equal to two, then fail.

414
00:25:42,480 --> 00:25:44,820
And here I can say insert room restriction.

415
00:25:44,820 --> 00:25:49,530
I want to check the model's room restriction that needs to fail somehow.

416
00:25:49,540 --> 00:25:52,310
So I need some arbitrary value that's actually going to fail.

417
00:25:52,320 --> 00:25:53,160
So let me try this.

418
00:25:53,790 --> 00:26:02,910
If our DOT remedy is equal to one thousand, there's one that I know is is just not likely to ever cause

419
00:26:02,920 --> 00:26:03,420
me grief.

420
00:26:03,690 --> 00:26:05,280
Then I want to return a new error.

421
00:26:05,730 --> 00:26:06,180
Errors.

422
00:26:06,180 --> 00:26:08,660
Dot knew some error.

423
00:26:09,360 --> 00:26:17,160
So what I'm doing here is saying if when I tried to insert a room restriction for room ID 1000, then

424
00:26:17,160 --> 00:26:17,450
fail.

425
00:26:17,910 --> 00:26:21,090
So let's go back to our handlers test and see how we can do that.

426
00:26:22,080 --> 00:26:23,550
We have validated it here.

427
00:26:23,580 --> 00:26:29,280
Let's give it remedy of one thousand and this should return a temporary redirect.

428
00:26:29,280 --> 00:26:29,790
I think.

429
00:26:29,790 --> 00:26:35,070
Let's make sure when it fails to insert the room restriction, I get a temporary redirect.

430
00:26:35,070 --> 00:26:35,820
So this should work.

431
00:26:36,210 --> 00:26:36,900
Let's run it.

432
00:26:38,250 --> 00:26:39,060
Go test.

433
00:26:40,530 --> 00:26:42,540
It passes coverage.

434
00:26:47,080 --> 00:26:52,690
Go down to post reservation, which is right here, and that should be entirely green at this point.

435
00:26:54,480 --> 00:26:55,810
And it is all right.

436
00:26:56,310 --> 00:27:02,730
So what I'm going to do now is I'm going to go finish writing these tests myself and then I'll post

437
00:27:02,730 --> 00:27:08,730
a very short lecture, which will just be a slide probably that has all of the code necessary for you

438
00:27:08,730 --> 00:27:12,930
to refer to when you go and try to write the test for the remaining functions.

439
00:27:12,930 --> 00:27:16,140
And you'll see there's quite a few that you have to test yet, some that we've never written a test

440
00:27:16,140 --> 00:27:16,380
for.

441
00:27:16,920 --> 00:27:22,490
But you should have at your disposal now everything you need in order to write the tests yourself.

442
00:27:22,920 --> 00:27:25,460
And I encourage you to try to do it yourself.

443
00:27:25,890 --> 00:27:31,890
And if you get stuck, go look at my source code, but just look at enough that you can verify are you

444
00:27:31,890 --> 00:27:37,050
can solve your problem of the moment, because if all you're doing is copying and pasting someone else's

445
00:27:37,050 --> 00:27:38,470
code, you're really not learning anything.

446
00:27:38,490 --> 00:27:44,160
The only way you learn to write tests or the only way you learn to write code is to write tests or write

447
00:27:44,160 --> 00:27:44,600
code.

448
00:27:45,030 --> 00:27:49,320
So I encourage you to try that on your own and you will bang your head against the wall every once in

449
00:27:49,320 --> 00:27:54,990
a while and you'll be pulling your hair out and you'll be having, you know, some real problems, but

450
00:27:54,990 --> 00:27:55,950
you'll be able to solve them.

451
00:27:55,980 --> 00:28:00,420
You do have right now in front of you, if you've written the code up to this point in the course,

452
00:28:00,420 --> 00:28:05,520
you have everything you need in order to write tests that will give you one hundred percent coverage

453
00:28:06,750 --> 00:28:08,560
for your handler's package.

454
00:28:08,610 --> 00:28:09,930
So good luck.

455
00:28:09,930 --> 00:28:11,550
If you get stuck, look at my code.

456
00:28:11,550 --> 00:28:13,770
And if not, then you've done a great job.

457
00:28:14,850 --> 00:28:19,470
So this gives us enough that we can move on to the next major topic of this course.
