1
00:00:01,210 --> 00:00:04,670
OK, so let's get started with testing now.

2
00:00:04,690 --> 00:00:09,280
We talked about testing briefly way back at the beginning of this course, and you may have been saying

3
00:00:09,280 --> 00:00:10,500
to yourself, well, wait a second.

4
00:00:10,510 --> 00:00:12,750
He said testing was important, but he's not doing any.

5
00:00:13,300 --> 00:00:16,810
Well, I wanted to get enough code in there that we could actually test things without having to go

6
00:00:16,810 --> 00:00:18,640
back and change it over and over and over.

7
00:00:18,730 --> 00:00:22,180
And we're going to write tests now and we'll continue to write them for the remainder of this course.

8
00:00:23,950 --> 00:00:26,800
Now, the first thing I want to test is my main function.

9
00:00:26,830 --> 00:00:31,570
And if you recall, I said back in the beginning when I was going over testing in the introduction to

10
00:00:31,570 --> 00:00:37,750
go, that my goal would be to not test my main function, to make it as stupid as I possibly could,

11
00:00:37,900 --> 00:00:43,300
and instead test things outside of the main function and just leave the main function alone so we can

12
00:00:43,300 --> 00:00:49,290
do that and we can do it pretty easily and we can do it by creating a new function here on my main DOKO,

13
00:00:49,630 --> 00:00:57,730
I'm just going to call it Run, Func, Run, and it's going to take no parameters, but it's going to

14
00:00:57,730 --> 00:01:00,410
return an error or nil if there is no error.

15
00:01:00,820 --> 00:01:04,150
So at the end I can say return nil because I'm going to have to do that at some point.

16
00:01:04,870 --> 00:01:11,530
And what I'm going to do is take everything from here, from where I initialize my renders and initialize

17
00:01:11,530 --> 00:01:14,800
my handlers all the way up to the beginning and cut it out of there.

18
00:01:15,460 --> 00:01:16,870
And I'm going to just say run.

19
00:01:17,350 --> 00:01:19,630
I shall say error is equal to run.

20
00:01:21,190 --> 00:01:26,890
And I'll check for the error at a moment and I'll paste what I cut out of my main function into my room

21
00:01:26,890 --> 00:01:29,280
run function and it should catch up.

22
00:01:29,680 --> 00:01:35,170
And what I'm going to do here is say if error is not equal to nil, in other words, if the setup failed,

23
00:01:35,530 --> 00:01:37,480
then I want to return.

24
00:01:38,530 --> 00:01:39,940
I want to log fatele.

25
00:01:40,060 --> 00:01:42,340
I just want to stop and print out the error.

26
00:01:42,910 --> 00:01:47,770
OK, so Logoff will write to our terminal and it will stop the application.

27
00:01:47,770 --> 00:01:48,970
It won't go any further.

28
00:01:48,970 --> 00:01:50,030
And that's exactly what I want.

29
00:01:50,590 --> 00:01:55,730
So let's go down here and see are we actually getting any errors because we have to.

30
00:01:55,750 --> 00:01:58,030
Well, we register a God that doesn't throw errors.

31
00:01:58,630 --> 00:02:01,300
We initialize our apter in production.

32
00:02:01,310 --> 00:02:02,070
That's fine.

33
00:02:02,110 --> 00:02:02,980
This is all fine.

34
00:02:02,990 --> 00:02:04,090
None of this throws errors.

35
00:02:04,110 --> 00:02:04,480
Aha.

36
00:02:04,480 --> 00:02:06,370
Here's one where we create our template cash.

37
00:02:06,640 --> 00:02:14,260
Well, if that gets set to non nil, in other words, if that throws an error, I can just return the

38
00:02:14,260 --> 00:02:19,890
error rate here, return error and that won't bother trying to do anything else.

39
00:02:19,930 --> 00:02:23,800
So there now this is testable almost.

40
00:02:23,890 --> 00:02:24,970
There's still some more work to do.

41
00:02:25,510 --> 00:02:27,340
So let's write a test for this.

42
00:02:27,340 --> 00:02:28,690
Let's try to write a test for this.

43
00:02:28,690 --> 00:02:34,420
So I'll come in here to my main folder and create a new file and go file, which I will call main underscore

44
00:02:34,420 --> 00:02:35,540
test Dongo.

45
00:02:35,540 --> 00:02:42,700
And as you recall, anything that has underscore test before the go, that's considered to be a test

46
00:02:42,700 --> 00:02:42,970
file.

47
00:02:43,280 --> 00:02:47,350
OK, so what am I going to do inside my main test?

48
00:02:47,350 --> 00:02:53,050
Well, what I want to do, you think about it, is just func test run.

49
00:02:53,050 --> 00:02:58,570
I want to test that run function that exists over in my main go file this one right here.

50
00:02:59,950 --> 00:03:02,050
So let's write a test for that or see if we can.

51
00:03:02,170 --> 00:03:10,720
And of course, because of the test, it takes a pointer to testing Dorte and it automatically important

52
00:03:10,720 --> 00:03:11,350
testing for me.

53
00:03:11,350 --> 00:03:11,860
That's good.

54
00:03:11,980 --> 00:03:15,610
So let's just say error is assigned run.

55
00:03:17,410 --> 00:03:18,290
Does that work?

56
00:03:18,760 --> 00:03:23,490
Well, let's find out if Iraq is not equal to nil, then what do I do?

57
00:03:23,500 --> 00:03:27,220
T dot error f failed.

58
00:03:30,770 --> 00:03:39,710
I just, you know, er failed run and let's try running it now, I'm going to do all of my testing,

59
00:03:39,710 --> 00:03:42,100
all of the running of the tests in a terminal window.

60
00:03:42,110 --> 00:03:44,960
You can do it in the terminal in your idea if you want to.

61
00:03:44,960 --> 00:03:46,360
But I'm actually going to do it here.

62
00:03:48,020 --> 00:03:49,710
So let me run that test.

63
00:03:49,730 --> 00:03:50,810
Am I in the right folder.

64
00:03:52,420 --> 00:03:57,680
Oh, I am in the right level so I need to go to command web and go test.

65
00:03:59,750 --> 00:04:02,120
And a past perfect no problem.

66
00:04:02,300 --> 00:04:07,560
And if I go test Teshuvah just to get the verbose it runs test run and get past it.

67
00:04:07,610 --> 00:04:09,450
OK, so that was easy and I have not.

68
00:04:09,520 --> 00:04:12,020
Now I know how to test my mind test.

69
00:04:12,650 --> 00:04:16,460
How do I test, for example, middleware here?

70
00:04:16,460 --> 00:04:18,080
I have no surf in here.

71
00:04:18,080 --> 00:04:18,890
I have Sushila.

72
00:04:18,890 --> 00:04:20,440
Is there a way of testing the middleware?

73
00:04:21,110 --> 00:04:22,150
Well, we can try.

74
00:04:22,250 --> 00:04:24,110
Let's just create a new test file.

75
00:04:27,170 --> 00:04:29,930
Middle where underscore tests don't go.

76
00:04:30,640 --> 00:04:36,440
OK, and let's see what we have to test, we have to test no surf and we have to test session lows.

77
00:04:36,620 --> 00:04:37,700
Well, can I call this.

78
00:04:37,730 --> 00:04:38,370
Let's try that.

79
00:04:38,390 --> 00:04:42,500
Let's see if we can flunk test.

80
00:04:42,560 --> 00:04:43,430
No, sir.

81
00:04:44,900 --> 00:04:47,900
He pointed to testing Dorte.

82
00:04:50,330 --> 00:04:51,200
And let's see what we can do.

83
00:04:51,230 --> 00:04:54,890
Well, I could try saying h what what is it written?

84
00:04:54,910 --> 00:04:55,730
Let's see what it returns.

85
00:04:55,730 --> 00:04:57,170
It returns and it be handler.

86
00:04:57,180 --> 00:05:04,670
OK, can I just go h for my handler is assigned no surf and it has to know surf has to take a parameter

87
00:05:05,000 --> 00:05:07,380
and it takes a next HTP handler.

88
00:05:07,400 --> 00:05:09,160
Well where am I going to get one of those.

89
00:05:09,620 --> 00:05:15,710
I need to create a handler to pass to no surf so we can hand me back a handler.

90
00:05:17,330 --> 00:05:18,700
Why I need to create one of those.

91
00:05:20,120 --> 00:05:21,830
Fortunately it's not that difficult to do it.

92
00:05:21,920 --> 00:05:27,500
What I need to do is to take advantage of goes interfaces like we talked about way back in the introduction

93
00:05:27,500 --> 00:05:28,310
to the go language.

94
00:05:28,580 --> 00:05:34,850
So I'm to need some method of setting up the environment before this actual test runs, and that's not

95
00:05:34,850 --> 00:05:35,210
that hard.

96
00:05:35,220 --> 00:05:42,920
So let's go to our Web folder and create a file with a very specific name set up underscore test Testico.

97
00:05:43,370 --> 00:05:47,420
And whatever is in here, we'll actually run before our tests run.

98
00:05:47,420 --> 00:05:49,190
It won't run in our compile program.

99
00:05:49,190 --> 00:05:52,290
It's not part of that, but it will run before our test run.

100
00:05:52,640 --> 00:05:56,270
So in this file, I need to set certain things up.

101
00:05:56,780 --> 00:06:01,550
So what I needed the first of all, is any time you have a set up test, set up underscore test, OK,

102
00:06:01,700 --> 00:06:05,390
it has to have a function and that function has to be called test me.

103
00:06:05,570 --> 00:06:14,930
So func test main and M, it's just my variable name and it's have type testing, not T but M and it

104
00:06:14,930 --> 00:06:17,660
doesn't return anything, but it runs and does certain things.

105
00:06:17,660 --> 00:06:22,430
So what it does here is that we can set up our variables in there if we wanted to, and I'm going to

106
00:06:22,430 --> 00:06:23,810
put os x it.

107
00:06:24,560 --> 00:06:28,380
But before it exits that's supposed to be X exit.

108
00:06:28,850 --> 00:06:34,070
I just says stop but before it does that it's going to run certain things.

109
00:06:34,070 --> 00:06:36,050
Ambigram, it's going to run my tests.

110
00:06:36,440 --> 00:06:39,530
So this says before you start running the tests.

111
00:06:39,650 --> 00:06:47,150
So middleware test or main test, do something inside this function, then run the tests that x and

112
00:06:47,150 --> 00:06:51,650
it also gives me a place to store variables that I might need outside of the test main function.

113
00:06:51,650 --> 00:06:59,900
And here's where I'm going to create an object that satisfies the HTP handler interface.

114
00:06:59,900 --> 00:07:02,900
And it's going to be a type and the type will be my handler.

115
00:07:03,410 --> 00:07:05,630
That's just the name that I choose and it's just a struct.

116
00:07:07,340 --> 00:07:08,810
So that's the type.

117
00:07:08,930 --> 00:07:14,630
Now I need to satisfy the server or satisfy the HTP, the handler interface.

118
00:07:14,630 --> 00:07:20,990
Well, the HP handler interface, we can go look at it here under middleware and find an HTP handler

119
00:07:20,990 --> 00:07:25,250
and there it is and it tells you all about it here right now.

120
00:07:25,400 --> 00:07:30,110
And all we need to do is implement the same methods or the same functions that it does.

121
00:07:30,380 --> 00:07:32,590
And in this case, that's really easy.

122
00:07:32,600 --> 00:07:35,120
So let's go back to setup test and do precisely that.

123
00:07:36,440 --> 00:07:37,460
So I'll create a function.

124
00:07:37,880 --> 00:07:40,910
It will be tied to my handler or.

125
00:07:40,940 --> 00:07:41,780
Yeah, my handler.

126
00:07:41,960 --> 00:07:47,180
So I'll put my variable name in the receiver and it's just a pointer to my handler, OK?

127
00:07:47,510 --> 00:07:53,060
And now I need to have the function with exactly the same names as the ones that are implemented by

128
00:07:53,060 --> 00:07:54,500
HTP handler.

129
00:07:54,860 --> 00:07:56,060
So what does it have?

130
00:07:56,060 --> 00:07:59,960
It has one called Serve HTTP, which we've used in our main function I believe.

131
00:08:00,860 --> 00:08:08,960
And it takes two arguments, a W which is in each feed of response writer and a pointer to you to give

132
00:08:08,960 --> 00:08:12,950
a variable name or a pointer to HTP request.

133
00:08:13,760 --> 00:08:14,990
And I'm not going to return anything.

134
00:08:15,290 --> 00:08:16,340
I've implemented it.

135
00:08:16,340 --> 00:08:17,690
That's all I have to do.

136
00:08:17,690 --> 00:08:19,610
So now I've created this type.

137
00:08:20,300 --> 00:08:22,550
So what can I do with that type?

138
00:08:22,550 --> 00:08:31,250
Well, I can go back to my middleware test, which is over here, and I can actually now populate this

139
00:08:31,250 --> 00:08:34,940
with the information that it needs to have no serve requires.

140
00:08:35,600 --> 00:08:37,970
If I point out it, it requires an issue to be handler.

141
00:08:37,970 --> 00:08:43,730
So all I have to do is create a variable that satisfies the interface for HTP handler.

142
00:08:44,000 --> 00:08:50,210
And I can just go over my H, which is just a variable name that I'm picking and it's a type my handler,

143
00:08:50,210 --> 00:08:53,510
which I defined over in setup test, go right here.

144
00:08:54,500 --> 00:08:57,380
So now that I've done that, I can pass that as an argument.

145
00:08:57,530 --> 00:09:01,850
My H now so my H.

146
00:09:01,850 --> 00:09:02,360
Let's see here.

147
00:09:02,360 --> 00:09:03,050
What do I miss.

148
00:09:04,850 --> 00:09:10,250
Type does not implement HGP handler and serve HGP method has a pointer receiver.

149
00:09:10,370 --> 00:09:11,450
What did I do wrong.

150
00:09:11,450 --> 00:09:16,430
Well in here I need to make this a pointer now.

151
00:09:16,430 --> 00:09:18,520
It's a pointer to that variable so that works.

152
00:09:18,530 --> 00:09:24,980
So now I satisfy the requirements for no serve and I've received an HTP handler back here but I.

153
00:09:25,180 --> 00:09:25,870
I haven't done anything.

154
00:09:26,050 --> 00:09:31,060
How do I test that it's actually a hand that's really the only thing I need to test at this point for

155
00:09:31,060 --> 00:09:31,680
our purposes.

156
00:09:32,440 --> 00:09:39,730
When I call no serve, it should receive an HTP handler or something that satisfies that interface and

157
00:09:39,730 --> 00:09:42,470
it should return something that is also in a [REMOVED] behavior.

158
00:09:42,490 --> 00:09:46,140
So all I can do is just test for it and we can test for that very easily.

159
00:09:46,150 --> 00:09:50,860
We can use our switch statement, which we saw a long time ago, and I'll just use V as my variable

160
00:09:50,860 --> 00:09:56,040
name and that will be assigned the type of H.

161
00:09:56,380 --> 00:10:00,970
So I've now got a switch statement I'm storing in V whatever type h.s.

162
00:10:01,630 --> 00:10:07,630
And as is a switch date, but it has to have these parentheses and I'll put case htp dog handler.

163
00:10:07,870 --> 00:10:10,470
If the type is an HDB handler do nothing.

164
00:10:10,480 --> 00:10:11,620
So I'll just put a comment here.

165
00:10:11,800 --> 00:10:12,310
Do nothing.

166
00:10:12,460 --> 00:10:13,500
This is what we expect.

167
00:10:13,690 --> 00:10:19,780
So if it gets that this should be a code, not a semicolon, if it gets that we've in effect passed

168
00:10:19,780 --> 00:10:27,520
our test, otherwise the default is going to be something as simple as writing an error teig error and

169
00:10:27,520 --> 00:10:37,510
we'll write our error type is and in this case I'm going to type is not HTP dog handler and with that

170
00:10:37,510 --> 00:10:38,020
fails.

171
00:10:38,030 --> 00:10:39,370
So we should be able to run this test.

172
00:10:39,370 --> 00:10:40,950
Now, let's go back and give it a try.

173
00:10:42,100 --> 00:10:43,440
There's an error in here somewhere.

174
00:10:43,450 --> 00:10:43,990
Where is it.

175
00:10:44,440 --> 00:10:55,480
Over here and use variable V so we have V default case HDB handler so I should use it down here.

176
00:10:55,480 --> 00:10:56,320
I'll just put it in here.

177
00:10:56,320 --> 00:11:03,490
I've got to use that variable somewhere so I'll put changes to a format or s print F and inside of that

178
00:11:03,490 --> 00:11:06,040
type is not but is.

179
00:11:06,040 --> 00:11:12,880
And then I'll put in my syntax for that which is simply t you haven't seen that yet, but that just

180
00:11:12,880 --> 00:11:17,680
says print the type and that's what the placeholder for the type sameas percent D is a placeholder for

181
00:11:17,680 --> 00:11:20,560
instance percent s is a placeholder for string's.

182
00:11:20,980 --> 00:11:23,410
This is for V there.

183
00:11:23,560 --> 00:11:24,430
Now we have no error.

184
00:11:24,430 --> 00:11:25,990
So now I should be able to run this test.

185
00:11:25,990 --> 00:11:33,280
Let's try this or go back to my terminal window, clear the screen and go test and everything past.

186
00:11:33,280 --> 00:11:33,730
Good.

187
00:11:33,730 --> 00:11:35,290
So now I've tested that hammer.

188
00:11:35,320 --> 00:11:36,280
No problem.

189
00:11:36,970 --> 00:11:38,740
What else do I have to test in middleware.

190
00:11:38,770 --> 00:11:44,650
Well, I have session load and what does that do that receives and should it be handler at hands back

191
00:11:44,650 --> 00:11:45,580
in HTP handler.

192
00:11:45,580 --> 00:11:48,940
So let's just make sure that a run that's pretty easy to do is pretty much the same thing.

193
00:11:49,360 --> 00:11:55,990
I can just copy this and paste it and change it to test such and load.

194
00:11:58,390 --> 00:12:03,310
And in this case I want to create a handler, but instead of creating a No sirf, I'm going to call

195
00:12:03,830 --> 00:12:10,460
session load and session loads should give back a type and the type should be what it should be.

196
00:12:10,460 --> 00:12:11,470
It should be handler.

197
00:12:11,560 --> 00:12:12,730
Nothing else should change.

198
00:12:12,910 --> 00:12:13,930
So let's try this.

199
00:12:14,830 --> 00:12:17,230
Go run test or go test Dashti.

200
00:12:18,360 --> 00:12:22,860
Everything past, OK, so now I've tested my main function as much as I want to.

201
00:12:23,220 --> 00:12:28,880
I've tested by Middlemore where function as much as I want to now route's how do I test my roots?

202
00:12:28,890 --> 00:12:30,200
I have all these roots.

203
00:12:30,210 --> 00:12:32,020
I'd really like to test those.

204
00:12:32,400 --> 00:12:34,860
Well, it's not that difficult again.

205
00:12:35,520 --> 00:12:46,680
OK, let's go back to our here our web folder and create a new file called Test Ersoy Roots Underscore

206
00:12:46,680 --> 00:12:47,510
Test Dargo.

207
00:12:48,000 --> 00:12:49,670
So it creates that file for me.

208
00:12:50,310 --> 00:12:54,030
And in here it's a package name.

209
00:12:55,920 --> 00:12:57,170
I need to write some test for it.

210
00:12:57,180 --> 00:13:06,710
So let's read it function func test routes and it takes t testing Dorte and it doesn't return anything.

211
00:13:07,680 --> 00:13:09,330
So what do I need to run the routes.

212
00:13:09,360 --> 00:13:10,710
Well, let's go back here and look at them.

213
00:13:11,590 --> 00:13:14,730
I need an app config, a pointer to an app config.

214
00:13:14,880 --> 00:13:17,460
I definitely need that and it has to return a handler.

215
00:13:17,490 --> 00:13:19,720
OK, so what else do we need in here?

216
00:13:19,740 --> 00:13:21,270
Let's just try it out and see what happens.

217
00:13:22,020 --> 00:13:22,980
So route's test.

218
00:13:22,980 --> 00:13:28,380
First of all, I can say var app, which is a config app config.

219
00:13:28,710 --> 00:13:29,220
No problem.

220
00:13:29,220 --> 00:13:33,630
I've created a variable, does not have anything in it and then I can call mux.

221
00:13:33,640 --> 00:13:38,550
I'll just call out my variable name because that's what we're using the roots file and call and pass

222
00:13:38,550 --> 00:13:40,440
it a pointer to my app.

223
00:13:41,030 --> 00:13:48,230
OK, and then switch the monks dot type.

224
00:13:48,240 --> 00:13:52,140
I'm going to just test the type again and run this and say is it of type.

225
00:13:52,170 --> 00:13:54,900
Well what actual type does this return?

226
00:13:54,910 --> 00:13:57,810
Let's go back to our roots and see what it's returning.

227
00:13:58,080 --> 00:14:02,790
Its returning mux and Marks is a pointer to dot marks.

228
00:14:02,820 --> 00:14:04,760
OK, so that's what I should test for.

229
00:14:05,820 --> 00:14:13,320
So Case Star Chheda Mux and it does the import for me do nothing,

230
00:14:17,400 --> 00:14:21,750
test past and let's try our have our default case.

231
00:14:21,750 --> 00:14:33,090
If it's not that then I want to add an error t error and again I'll do format s print F type is not

232
00:14:33,720 --> 00:14:34,170
cheap.

233
00:14:34,170 --> 00:14:43,460
Dot marks type is Pacetti and in there I'll just put the.

234
00:14:44,700 --> 00:14:46,200
So let's see if this works.

235
00:14:46,200 --> 00:14:47,490
Let's try running this test.

236
00:14:48,450 --> 00:14:49,260
Clear the screen.

237
00:14:49,500 --> 00:14:51,300
Go test Dashti.

238
00:14:53,680 --> 00:14:56,690
Everything passed, that's not bad at all.

239
00:14:57,010 --> 00:14:59,480
So have we tested everything we need to test in here?

240
00:14:59,500 --> 00:15:04,500
Let's find out under our Web folder, we have mango.

241
00:15:04,990 --> 00:15:07,960
We're testing our run function and we're testing that right here.

242
00:15:07,960 --> 00:15:08,290
Good.

243
00:15:08,590 --> 00:15:12,730
Under our middleware test, we're testing test, no surf and test session load.

244
00:15:13,060 --> 00:15:15,760
And in middleware, we have no surf and session load.

245
00:15:16,330 --> 00:15:22,060
In route's test, we're only testing the roots while creating the roots of our roots called the roots

246
00:15:22,060 --> 00:15:22,540
function.

247
00:15:22,540 --> 00:15:25,990
So under roots, we have roots that seems good.

248
00:15:25,990 --> 00:15:28,410
So far, that seems to work just perfectly.

249
00:15:28,870 --> 00:15:32,060
So let's see now what our coverage looks like.

250
00:15:32,060 --> 00:15:36,450
A test or a go test cover.

251
00:15:39,080 --> 00:15:41,160
Eighty point nine percent of statements.

252
00:15:41,210 --> 00:15:42,050
That's pretty good.

253
00:15:42,320 --> 00:15:46,280
Well, what about testing a little more and a little more detail?

254
00:15:46,290 --> 00:15:50,330
How do we test to get our coverage as an HTML file?

255
00:15:51,170 --> 00:15:52,970
What if you recall, we do it like this.

256
00:15:52,970 --> 00:16:02,270
We go go and test and then cover profile equals coverage, dot out or whatever you want to call it.

257
00:16:03,020 --> 00:16:11,010
And then we run go tool cover HTML format using coverage out.

258
00:16:11,490 --> 00:16:12,320
We'll see how that works.

259
00:16:14,780 --> 00:16:18,090
Fired up my Web browser and it says, OK, you're not testing your main function.

260
00:16:18,440 --> 00:16:21,700
We knew that this is all tested except for that.

261
00:16:21,740 --> 00:16:25,110
I can live with that because we know that the test failed at that point.

262
00:16:25,130 --> 00:16:32,060
OK, and we look at our middleware that's entirely covered everything that has to be.

263
00:16:32,060 --> 00:16:35,430
And we look at our roots and that's entirely covered.

264
00:16:35,480 --> 00:16:36,640
That's pretty good.

265
00:16:36,650 --> 00:16:39,460
I think that's excellent test coverage for our main package.

266
00:16:39,920 --> 00:16:43,120
So we've tested our main and we've tested our handlers.

267
00:16:43,400 --> 00:16:45,050
Let's move on with some other tests.

268
00:16:45,080 --> 00:16:46,400
We haven't actually tested our hander's.

269
00:16:46,400 --> 00:16:47,150
We've tested aretz.

270
00:16:47,270 --> 00:16:49,700
Let's move on with our handlers in the next section.
