1
00:00:01,130 --> 00:00:08,090
So now I want to test this function that's part of my render package, render template, and the very

2
00:00:08,090 --> 00:00:12,890
first thing I notice about this is that this doesn't actually throw an error.

3
00:00:13,340 --> 00:00:14,990
And that's a bit of a problem.

4
00:00:15,110 --> 00:00:21,350
I mean, it does generate an error down here, which is, you know, when I when I say buff right to

5
00:00:21,350 --> 00:00:25,790
the at the response rate, it generates an error, but I don't return one.

6
00:00:25,820 --> 00:00:30,410
So the very first thing I'm going to do is modify this to make it easier to test.

7
00:00:30,980 --> 00:00:32,150
I'm going to have a return on error.

8
00:00:32,630 --> 00:00:34,860
And if there is no error, it will return nil.

9
00:00:35,120 --> 00:00:39,890
So down here at the very end, if everything worked, I can say return nil.

10
00:00:41,240 --> 00:00:47,260
And here, where it generates an error, I'm going to return an error in the same way.

11
00:00:47,270 --> 00:00:51,770
If this is not OK, if I can't get the template from the cache, I'll return

12
00:00:55,310 --> 00:01:01,940
errors dot new can't get template from cache.

13
00:01:03,440 --> 00:01:05,050
Is there anywhere else I'm throwing an error?

14
00:01:05,090 --> 00:01:05,510
No.

15
00:01:05,600 --> 00:01:07,320
So now this throws an error.

16
00:01:07,340 --> 00:01:08,710
Now does this break our handlers?

17
00:01:08,720 --> 00:01:14,120
Let's go to our handlers and find out when I generate a page calling render template.

18
00:01:14,120 --> 00:01:17,570
All I'm getting is a warning that there's an unhandled error and I can live with that for now.

19
00:01:17,660 --> 00:01:21,470
OK, so now I have something I can actually look for.

20
00:01:21,470 --> 00:01:23,320
I can see if there's an error or not.

21
00:01:23,810 --> 00:01:27,140
So let's go back to our render underscore test.

22
00:01:27,330 --> 00:01:32,130
Go and think about how we're going to write our test render template function.

23
00:01:33,110 --> 00:01:35,560
Well, there's one thing I know I know I need to do straight away.

24
00:01:36,140 --> 00:01:40,280
You recall a couple of lectures ago in our render package.

25
00:01:40,670 --> 00:01:44,660
We actually created this variable at the top, which was path to templates.

26
00:01:45,110 --> 00:01:49,660
And when I run my tests in the render package, this is not the correct path.

27
00:01:49,820 --> 00:01:56,060
Fortunately, since I've extracted this to a variable, a package level variable in my render test or

28
00:01:56,060 --> 00:02:03,620
my render test when I create my testing function, which will do right now func test render template,

29
00:02:04,190 --> 00:02:10,400
which is of type testing t I can set that variable path to templates is equal to and I'll make it what

30
00:02:10,400 --> 00:02:16,020
it needs to be for the purposes of this test, which is two levels up from our current directory.

31
00:02:16,040 --> 00:02:18,710
Dot, dot, dot, dot, dot.

32
00:02:18,860 --> 00:02:20,240
And then templates.

33
00:02:22,640 --> 00:02:23,000
All right.

34
00:02:23,180 --> 00:02:26,120
So now we know we have a means of getting to the templates.

35
00:02:27,080 --> 00:02:30,340
So then I need to what what's the first thing I have to do?

36
00:02:30,350 --> 00:02:35,600
Let's we need to have a template cache before we can render anything so we can just say take for template

37
00:02:35,600 --> 00:02:40,910
cache and error are assigned the value of call the create template cache.

38
00:02:41,120 --> 00:02:43,760
So now I have a template cache and potentially an error.

39
00:02:44,210 --> 00:02:48,170
If there's an error, if error is not equal to nil, I don't need to fail.

40
00:02:48,590 --> 00:02:53,180
So I can say dot error and the error is just the error that it was passed.

41
00:02:53,360 --> 00:02:54,590
So that will fail the test.

42
00:02:55,340 --> 00:03:02,030
Now that I've created the template cache, I need to put that into my variable app and that's a package

43
00:03:02,030 --> 00:03:05,290
level variable so I can just assign a template cache equals T.C..

44
00:03:06,110 --> 00:03:06,590
All right.

45
00:03:06,590 --> 00:03:08,180
We're almost ready to call this function.

46
00:03:08,690 --> 00:03:09,950
There's one problem, though.

47
00:03:10,310 --> 00:03:12,680
We actually want to call render template.

48
00:03:12,770 --> 00:03:14,030
So let's look at that for a minute.

49
00:03:14,630 --> 00:03:17,240
Render template is right here.

50
00:03:17,270 --> 00:03:24,320
It wants a response writer, a request the string to the name of the template we want to render and

51
00:03:24,320 --> 00:03:27,080
then our template data and then it throws an error, which we just edit.

52
00:03:27,260 --> 00:03:28,130
Here's the problem.

53
00:03:28,130 --> 00:03:28,960
The response writer.

54
00:03:29,000 --> 00:03:30,640
How do I create a response writer?

55
00:03:31,130 --> 00:03:37,670
Well, if we try to actually call this right now, error equals render template.

56
00:03:38,330 --> 00:03:39,390
We need a response writer.

57
00:03:39,410 --> 00:03:45,890
So let's just call that response rate or I'm calling it two WS for a reason and our request.

58
00:03:46,190 --> 00:03:52,940
And we need to render some templates, say home page dot temple, and it needs to have some template

59
00:03:52,940 --> 00:03:58,640
that which we can just pass in is an empty variable, a pointer to model start template data.

60
00:03:59,720 --> 00:04:02,620
So that should be enough to call it.

61
00:04:02,900 --> 00:04:07,850
The problem is I don't have a response and I don't have a request.

62
00:04:07,850 --> 00:04:09,440
So where am I going to get these things?

63
00:04:09,740 --> 00:04:13,610
While the request is easy, we already have this get session function down here.

64
00:04:13,610 --> 00:04:15,440
That gives me something back.

65
00:04:15,440 --> 00:04:22,310
It gives me an HP request and an error so I can just come up here before I call, render template and

66
00:04:22,310 --> 00:04:26,810
say hour and an error are equal to get session.

67
00:04:28,670 --> 00:04:36,740
If error is not equal to nil, then fail the test error and error, so now I have my error right here.

68
00:04:36,930 --> 00:04:40,370
OK, this has to be a sign there.

69
00:04:40,730 --> 00:04:47,050
Now I have my my my other variable, so I have my request, which is a pointer to an HTP request, but

70
00:04:47,060 --> 00:04:51,590
I still don't have a response rate or I need to have a writer of some sort.

71
00:04:51,920 --> 00:04:57,050
So if we look at the render template package, it says it has to have an HTTP response, right.

72
00:04:57,200 --> 00:04:59,100
Well, is that built into the testing package?

73
00:04:59,420 --> 00:05:00,230
Can I just go?

74
00:05:00,440 --> 00:05:06,190
W-W is assigned the value of each test dot and then we had a new request.

75
00:05:06,200 --> 00:05:07,670
Is there a new response writer?

76
00:05:07,760 --> 00:05:09,030
New response?

77
00:05:09,680 --> 00:05:10,630
No, I don't see one.

78
00:05:11,140 --> 00:05:15,590
Well, there's a number of ways of doing this, but what I'm going to do is actually create an object

79
00:05:15,590 --> 00:05:22,760
using interfaces or create a type using interfaces that satisfies the requirements for a response.

80
00:05:22,760 --> 00:05:23,030
Right.

81
00:05:23,360 --> 00:05:25,400
So I'm going to go back over to setup test.

82
00:05:25,400 --> 00:05:27,440
I'll put them all in here just for the sake of doing it.

83
00:05:28,640 --> 00:05:35,720
So the first thing I want to do is create a type which I'll just call my writer and that's just going

84
00:05:35,720 --> 00:05:36,590
to be a struct.

85
00:05:36,860 --> 00:05:40,870
OK, no members, just an empty struct.

86
00:05:41,390 --> 00:05:42,350
So that's my writer.

87
00:05:42,950 --> 00:05:47,220
And then if we look at the response, let's see HTP response.

88
00:05:47,220 --> 00:05:49,330
Let me find an example where I can just point at it.

89
00:05:49,940 --> 00:05:55,270
So, for example, it should be a response rate.

90
00:05:55,310 --> 00:06:01,940
I'll just point out that when I pointed that it says the type response writer is an interface that has

91
00:06:01,940 --> 00:06:04,730
a header, a right and a right header method.

92
00:06:04,850 --> 00:06:11,720
If we can create a type that satisfies all of those requirements, we can use that in in place of an

93
00:06:12,450 --> 00:06:13,850
HTTP response rate.

94
00:06:13,970 --> 00:06:14,660
So let's go do that.

95
00:06:14,870 --> 00:06:15,710
It's really not that hurt.

96
00:06:16,400 --> 00:06:18,040
So we need to create three things.

97
00:06:18,050 --> 00:06:20,760
So the very first one we created are our type here.

98
00:06:20,870 --> 00:06:22,590
Now we need to create methods on that.

99
00:06:22,910 --> 00:06:28,790
So one was header, for example, func, and I'll call this T.W. just because I have to have a name

100
00:06:28,790 --> 00:06:29,200
for it.

101
00:06:29,600 --> 00:06:37,700
This will be a pointer to my writer and the function has to be called header and it has to return.

102
00:06:37,910 --> 00:06:44,750
And HGP dot header, which we can see if you actually go over again and point at this method, which

103
00:06:44,750 --> 00:06:50,120
is right here, this type, it says it's a header that returns a header.

104
00:06:50,120 --> 00:06:51,200
So we can do that.

105
00:06:51,260 --> 00:06:52,130
That's not hard at all.

106
00:06:53,030 --> 00:06:57,980
So all we have to do to satisfy that interface is create an empty header variable.

107
00:06:57,980 --> 00:07:06,110
VAR H HDP var H HDP dot header and return it.

108
00:07:07,280 --> 00:07:08,590
So there's one method down.

109
00:07:09,200 --> 00:07:09,900
No problem.

110
00:07:10,280 --> 00:07:15,800
Now we need the second one and the second one was a right method, right header method.

111
00:07:15,800 --> 00:07:17,180
So func.

112
00:07:18,020 --> 00:07:21,830
And again this has to have a receiver that links it with my right or type

113
00:07:24,740 --> 00:07:25,880
right header.

114
00:07:26,570 --> 00:07:27,740
And did that take parameters.

115
00:07:28,550 --> 00:07:29,530
Let's take a look at this again.

116
00:07:32,470 --> 00:07:36,640
So right heter takes a parameter of type int and doesn't return anything, well, that's easy.

117
00:07:37,450 --> 00:07:38,800
So we'll go back over here.

118
00:07:39,520 --> 00:07:44,180
So I intend it doesn't return anything and that should be sufficient for that one.

119
00:07:44,290 --> 00:07:50,530
So we have that the last method renderer is right, I believe.

120
00:07:51,580 --> 00:07:56,510
And Wright takes a slice of bytes and returns into an error.

121
00:07:56,620 --> 00:07:57,820
OK, all we can do that.

122
00:07:58,780 --> 00:08:06,550
So the last one is func T.W. Pointer to my writer and it's called Right.

123
00:08:06,790 --> 00:08:15,110
And it takes a slice of bytes, so be slice of byte and it returns an inch and an error.

124
00:08:16,000 --> 00:08:19,900
Now in this case, we actually can't just return a random event.

125
00:08:20,230 --> 00:08:22,800
And this is an area would discover the hard way.

126
00:08:23,080 --> 00:08:24,700
But I will just tell you how to do this.

127
00:08:24,700 --> 00:08:27,400
So we need to figure out the length of the slice of bytes.

128
00:08:27,820 --> 00:08:32,830
So length is assigned the value of Lyndon B..

129
00:08:33,820 --> 00:08:39,090
And I return length and no error take no.

130
00:08:40,030 --> 00:08:41,170
And that should be sufficient.

131
00:08:41,320 --> 00:08:47,920
So now I've created all of the three methods or all of three functions that are required to satisfy

132
00:08:47,920 --> 00:08:54,420
the interface that will allow us to use our own type, my writer, instead of a response rate.

133
00:08:54,970 --> 00:08:57,460
So this W-W is the issue.

134
00:08:58,060 --> 00:09:07,600
So let's make this now what we can actually fix this we can say via WW two WS is of type my return and

135
00:09:07,600 --> 00:09:13,090
then we can just say, well, we've got there right now, we've actually created it so we can actually

136
00:09:13,090 --> 00:09:14,020
just put it right here.

137
00:09:14,230 --> 00:09:16,330
But instead of this, it needs to be a pointer.

138
00:09:17,020 --> 00:09:18,160
It does not implement.

139
00:09:18,160 --> 00:09:19,240
Some methods are missing.

140
00:09:19,240 --> 00:09:19,630
Right.

141
00:09:19,630 --> 00:09:20,770
Byte error.

142
00:09:21,270 --> 00:09:22,870
Let's go back to what do we get wrong here?

143
00:09:23,170 --> 00:09:26,890
Setup test of right there.

144
00:09:29,060 --> 00:09:29,410
All right.

145
00:09:29,650 --> 00:09:34,480
Now, we've created the necessary variable to satisfy the requirements for a response.

146
00:09:34,480 --> 00:09:34,680
Right.

147
00:09:35,890 --> 00:09:39,010
So now we can actually render our template and check for the error.

148
00:09:39,040 --> 00:09:43,470
So we've rendered this template and we've passed it the necessary information.

149
00:09:43,690 --> 00:09:45,100
There are no parentheses after this.

150
00:09:45,100 --> 00:09:46,180
I don't know where those came from.

151
00:09:47,560 --> 00:09:48,730
And we can check for our error.

152
00:09:49,840 --> 00:09:53,950
If error is not equal to nil, we can just say

153
00:09:57,610 --> 00:10:04,670
Teda error, error rating template to the browser there.

154
00:10:06,640 --> 00:10:08,830
And then again, we probably should check for something else.

155
00:10:08,830 --> 00:10:09,850
We can do it right here.

156
00:10:09,850 --> 00:10:13,300
I'm going to do this again, but this time I'm going to call it exactly the same.

157
00:10:13,300 --> 00:10:19,510
But I would call a non existent template something that I know is not in my template cache.

158
00:10:19,900 --> 00:10:24,490
And this time I want to say if the error is equal to nil, in other words, if it successfully renders

159
00:10:24,490 --> 00:10:26,860
a non-existent template, well, it ought not to be doing that.

160
00:10:27,190 --> 00:10:35,680
So I'll say got a template that did not exist or rendered template that does not exist.

161
00:10:35,830 --> 00:10:36,210
All right.

162
00:10:37,000 --> 00:10:39,850
So that should be sufficient, which is clean this up a little bit.

163
00:10:40,210 --> 00:10:42,610
What's that should be sufficient to actually try to run this test?

164
00:10:42,620 --> 00:10:43,270
So let's do that.

165
00:10:43,480 --> 00:10:43,960
Go.

166
00:10:44,140 --> 00:10:45,160
Am I in the right package?

167
00:10:46,220 --> 00:10:48,610
I'm in render go test drive.

168
00:10:48,760 --> 00:10:49,840
Let's see what we got.

169
00:10:51,180 --> 00:10:55,630
And everything passed, so it says here, can't get template from cash.

170
00:10:55,650 --> 00:10:56,550
Where did that come from?

171
00:10:56,670 --> 00:11:00,930
That comes from can't get comtemplate from cash.

172
00:11:00,930 --> 00:11:03,650
That actually comes from a vendor package, I believe, yet right here.

173
00:11:04,230 --> 00:11:05,210
So that is fine.

174
00:11:05,220 --> 00:11:06,140
I don't mind that at all.

175
00:11:06,150 --> 00:11:07,200
That air is just fine.

176
00:11:07,200 --> 00:11:10,740
I can actually get it out of here entirely if I want to and run it again.

177
00:11:12,850 --> 00:11:18,170
And everything passed the way that it should, so now we have tested rendering our template.

178
00:11:18,310 --> 00:11:20,390
There's a few other things we have to try to test as well.

179
00:11:21,400 --> 00:11:25,770
So let's go back to our render package and see what's not tested so far.

180
00:11:25,780 --> 00:11:27,820
So we haven't tested new templates.

181
00:11:27,850 --> 00:11:28,520
Can we do that?

182
00:11:28,540 --> 00:11:29,010
Sure.

183
00:11:29,020 --> 00:11:30,850
That one is absolutely trivial.

184
00:11:30,850 --> 00:11:38,440
We just go back to our render test, create a new test function, which we will call function test new

185
00:11:38,710 --> 00:11:39,370
templates.

186
00:11:40,540 --> 00:11:44,800
And all we have to do is say new templates app.

187
00:11:46,150 --> 00:11:49,630
And that should actually fire that that that function.

188
00:11:49,870 --> 00:11:50,950
And it's not doing anything.

189
00:11:50,960 --> 00:11:51,850
It doesn't return anything.

190
00:11:51,880 --> 00:11:54,880
So this should be sufficient to force it to go through the testing process.

191
00:11:55,810 --> 00:11:56,270
All right.

192
00:11:56,320 --> 00:11:57,540
So we've tested that.

193
00:11:57,550 --> 00:11:59,410
Is there anything else we have to test?

194
00:12:00,430 --> 00:12:02,050
Have we tested adding default data?

195
00:12:02,080 --> 00:12:04,740
Yes, we've tested rendering our templates.

196
00:12:04,750 --> 00:12:05,200
Yes.

197
00:12:05,500 --> 00:12:07,560
We've tested create template cache.

198
00:12:07,570 --> 00:12:09,540
No, I don't see that at all.

199
00:12:09,550 --> 00:12:10,440
So we have to do that.

200
00:12:10,720 --> 00:12:14,040
So let's go look at test template cash or create template cash.

201
00:12:15,760 --> 00:12:17,290
That is right here.

202
00:12:18,040 --> 00:12:20,310
So this takes a map of string template template.

203
00:12:20,890 --> 00:12:21,910
No, it doesn't take anything.

204
00:12:21,910 --> 00:12:23,240
It returns those two things.

205
00:12:23,260 --> 00:12:23,560
All right.

206
00:12:23,560 --> 00:12:24,640
So let's create that test.

207
00:12:24,770 --> 00:12:25,600
That's the last one.

208
00:12:25,600 --> 00:12:31,930
I think we have to do func and we will test create template cache.

209
00:12:33,040 --> 00:12:37,840
And what we have to do is, again, specify our path, the template path to templates.

210
00:12:39,010 --> 00:12:44,590
And that's going to be equal to Doug's templates.

211
00:12:44,800 --> 00:12:46,720
So now we've got the correct value set.

212
00:12:46,750 --> 00:12:51,960
So when this function is called, we will look in the correct location and then we just call it create

213
00:12:51,970 --> 00:12:52,630
template cache.

214
00:12:52,630 --> 00:12:57,430
T.S. error is equal to create template cache.

215
00:12:58,990 --> 00:13:05,500
And we say if error is not equal to nil, then Teda error error and that will fail the test.

216
00:13:05,650 --> 00:13:08,950
Let's try running that now and see if these new tests work.

217
00:13:09,100 --> 00:13:14,320
Cleared the screen run and we have t declared but not used.

218
00:13:14,560 --> 00:13:17,780
OK, so Turkey declared is not used.

219
00:13:17,800 --> 00:13:22,800
OK, well we can't do that then so we don't actually need that turkey variable.

220
00:13:22,840 --> 00:13:23,860
We just needed to run.

221
00:13:26,140 --> 00:13:26,880
Let's try that again.

222
00:13:29,140 --> 00:13:34,720
And everything past perfect, so it looks like we've covered pretty much everything we have to now,

223
00:13:34,720 --> 00:13:37,100
we're not sure yet because we haven't checked our coverage.

224
00:13:37,450 --> 00:13:41,720
We will do that in the next lecture and see if we missed anything that needs to be tested.

225
00:13:42,850 --> 00:13:43,710
That's enough for now.
