1
00:00:00,540 --> 00:00:04,920
So let's start running some tests for a render package, and I'm looking at the render package right

2
00:00:04,920 --> 00:00:09,420
now and that's in internal render and the file is called Render Echo.

3
00:00:10,140 --> 00:00:13,500
And the very first thing I want to test is this function.

4
00:00:13,500 --> 00:00:14,870
Add default data.

5
00:00:15,000 --> 00:00:19,530
So let's get started, first of all, to create the two files I know I'm probably going to need.

6
00:00:19,540 --> 00:00:26,760
The first one is to set up our environment set up, underscore Testico and the second one I will call

7
00:00:27,060 --> 00:00:28,920
render underscore test dutko.

8
00:00:32,920 --> 00:00:35,940
OK, so let's get our setups first.

9
00:00:36,660 --> 00:00:40,440
What do we need in order to test this function?

10
00:00:40,470 --> 00:00:46,160
Well it says add default data takes TD, which is a pointer to models template data.

11
00:00:46,170 --> 00:00:46,860
That's no problem.

12
00:00:46,860 --> 00:00:52,290
I can create a variable that's of type models, template data and pass a pointer to this function.

13
00:00:52,290 --> 00:00:52,840
That's easy.

14
00:00:53,460 --> 00:00:54,910
The second one is the request.

15
00:00:55,260 --> 00:00:59,940
Now, when this application is actually running, I have access to that request.

16
00:01:00,420 --> 00:01:05,250
But in this package I don't because I'm running a test.

17
00:01:05,250 --> 00:01:10,920
I'm not actually testing the entire application from Maine go right through to the very end of whatever

18
00:01:10,920 --> 00:01:11,940
the request might be.

19
00:01:12,540 --> 00:01:17,130
I'm just testing this one function at default data in isolation.

20
00:01:17,580 --> 00:01:21,870
And that means I need to build this request and I can't just build an empty request.

21
00:01:21,870 --> 00:01:22,590
That's trivial.

22
00:01:23,250 --> 00:01:27,380
What I need to do is build a request that has session data.

23
00:01:27,600 --> 00:01:33,690
So let's try building it first, just using a standard way of building an empty request object and see

24
00:01:33,690 --> 00:01:36,210
what happens without the session data, it will fail.

25
00:01:36,270 --> 00:01:37,530
Let's get started and try it anyway.

26
00:01:38,190 --> 00:01:42,350
So I'm going to, first of all, in my setup, underscore Testico.

27
00:01:42,360 --> 00:01:43,740
I'm going to create two variables.

28
00:01:43,770 --> 00:01:47,070
I'll create a session which I'm not going to use right now, but I will momentarily.

29
00:01:47,070 --> 00:01:48,210
So I'll set it up right now.

30
00:01:48,930 --> 00:01:54,090
That's a pointer to Excession manager and I'll also create a variable called Test App, and I'll tell

31
00:01:54,090 --> 00:01:55,260
you why I'm doing that in a minute.

32
00:01:55,260 --> 00:01:57,230
And that's a type config app config.

33
00:01:57,900 --> 00:02:02,910
The reason I'm creating test app is because if you look at render Dongo, the application variable is

34
00:02:02,910 --> 00:02:05,850
actually stored or created right here in the package.

35
00:02:05,980 --> 00:02:13,770
OK, so I'm going to create my own copy of that and then make this variable app a pointer to the one

36
00:02:13,770 --> 00:02:16,740
that I create and I'll do that in setup test.

37
00:02:16,740 --> 00:02:17,490
I go now.

38
00:02:17,490 --> 00:02:22,800
Last time we created a function called Get Roots and that works fine and we could use the same approach

39
00:02:22,800 --> 00:02:25,290
right now and create a function that creates everything we need.

40
00:02:25,590 --> 00:02:29,790
Or we can use the built in one which we did much earlier in the course, and we'll do it again now.

41
00:02:29,790 --> 00:02:35,430
But just to remind you that it exists and that's the function called Test Me, and it takes a parameter

42
00:02:35,850 --> 00:02:40,950
which I call M, and that's a pointer to testing done m not testing that T.

43
00:02:41,580 --> 00:02:50,100
And then at the very end of this function we call os x it not executable exit, but before it exits,

44
00:02:50,100 --> 00:02:51,360
it actually runs our tests.

45
00:02:51,360 --> 00:02:52,110
M not run.

46
00:02:52,890 --> 00:02:57,750
So remember this function gets called before any of the tests are run.

47
00:02:58,050 --> 00:03:01,500
It does whatever we tell it to do in the body of the function.

48
00:03:01,830 --> 00:03:05,070
Then just before it closes in is the application.

49
00:03:05,070 --> 00:03:06,110
It runs our tests.

50
00:03:06,480 --> 00:03:13,080
So in here I need to set up certain things and I can get that information again from Command Mango.

51
00:03:13,350 --> 00:03:16,680
And just getting the top part of what we have in here.

52
00:03:16,920 --> 00:03:17,790
This is what I need.

53
00:03:17,790 --> 00:03:22,800
I need the sessional information, so I'll copy that and then I'll paste it and I need to make a few

54
00:03:22,800 --> 00:03:26,610
changes so I'm not going to be working with App directly.

55
00:03:26,610 --> 00:03:33,000
Instead, I'll use this variable test app, put it in there, put it in there.

56
00:03:33,480 --> 00:03:38,640
And I can't call app in production, but I'll just say this fault because we're not running an HDTV

57
00:03:38,660 --> 00:03:41,760
server so we can leave that set of faults for testing purposes.

58
00:03:42,780 --> 00:03:51,930
And then just before the application runs the tests, I say app is equal to a to test app and that populates

59
00:03:51,930 --> 00:03:57,450
this this one command right here after I set everything up and test out it, make sure that app which

60
00:03:57,450 --> 00:04:02,640
is declared in rendered ago, it makes sure that this variable has the things I needed to have.

61
00:04:03,120 --> 00:04:04,920
So that's my setup right now.

62
00:04:04,950 --> 00:04:07,530
OK, so that's probably sufficient for the moment.

63
00:04:08,220 --> 00:04:11,430
Let's go back to our render test and start writing test.

64
00:04:11,430 --> 00:04:15,840
And the application function that we want to test is called a default data.

65
00:04:15,840 --> 00:04:22,710
So let's copy its name back to render, test and create a function function test with a capital T,

66
00:04:23,310 --> 00:04:30,780
add default data and add default data is going to be of type T that takes a parameter a pointer to testing

67
00:04:31,080 --> 00:04:33,450
T and it doesn't return anything.

68
00:04:33,960 --> 00:04:36,420
So what do we do in this function?

69
00:04:36,430 --> 00:04:41,520
Well, if you actually look at the application function itself, it's pretty straightforward.

70
00:04:42,300 --> 00:04:45,450
It takes a couple of arguments and it's over in the render package.

71
00:04:45,450 --> 00:04:49,680
It takes TD, which is a pointer to models template data, and then it takes a request.

72
00:04:50,700 --> 00:04:55,380
So let's go back and call it so we can call it just like this.

73
00:04:55,380 --> 00:04:59,910
We can first of all create an empty TD variable, which is a type models template data.

74
00:05:00,070 --> 00:05:05,770
That's his first argument, and then we need a request now, we could do it like this, we could say

75
00:05:05,770 --> 00:05:06,340
our.

76
00:05:07,390 --> 00:05:13,330
I don't know if it is an error or not, I think it does is a sign the value of HDP new request.

77
00:05:13,750 --> 00:05:16,960
Yes, that returns an error and it requires a method.

78
00:05:17,380 --> 00:05:18,160
Get her post.

79
00:05:18,160 --> 00:05:19,180
What would she get?

80
00:05:20,290 --> 00:05:21,390
It requires you earn.

81
00:05:21,430 --> 00:05:22,270
That can be anything.

82
00:05:22,510 --> 00:05:23,260
Just some.

83
00:05:23,260 --> 00:05:27,010
You are we're not going to do anything with you IRL, so we can just put whatever we want in there.

84
00:05:27,610 --> 00:05:29,050
And it requires a body.

85
00:05:29,050 --> 00:05:30,430
But I'm not going to pass to the body.

86
00:05:30,430 --> 00:05:34,150
I want to pass it nil because really I'm just making a request to you, Earl.

87
00:05:34,150 --> 00:05:35,950
I'm trying to get a request.

88
00:05:36,700 --> 00:05:38,380
So that will create one for us.

89
00:05:38,380 --> 00:05:39,340
And I can check for an error.

90
00:05:39,340 --> 00:05:44,320
If error is not equal to nil, then teig error error.

91
00:05:44,860 --> 00:05:46,480
And that will that will fail our test.

92
00:05:46,900 --> 00:05:48,130
So now I have a request.

93
00:05:48,130 --> 00:05:51,790
So let's try calling that that function.

94
00:05:51,790 --> 00:05:53,710
This one over here render an rendered.

95
00:05:53,710 --> 00:05:55,570
OK, we're going to add default data.

96
00:05:55,660 --> 00:06:04,990
So let's call it we can say result is assigned, add default data and we can pass it our TD and our

97
00:06:04,990 --> 00:06:05,560
request.

98
00:06:06,160 --> 00:06:08,740
And then of course we need to do something with that.

99
00:06:08,740 --> 00:06:13,990
So I can say if result is equal to nil, then teig error.

100
00:06:13,990 --> 00:06:15,310
It doesn't matter what I'm doing here.

101
00:06:15,310 --> 00:06:17,650
It's just tittered error failed.

102
00:06:17,980 --> 00:06:24,340
OK, I know this isn't going to work, so I'm not too worried about getting too much right here in terms

103
00:06:24,340 --> 00:06:25,150
of error messages.

104
00:06:25,990 --> 00:06:27,220
So let's try this now.

105
00:06:27,670 --> 00:06:30,670
It wants a pointer to TD, so I have to make that a pointer there.

106
00:06:31,030 --> 00:06:32,230
Let's try running this test.

107
00:06:34,340 --> 00:06:35,450
Go, run.

108
00:06:35,480 --> 00:06:36,620
I'm in the right direction, am I?

109
00:06:37,130 --> 00:06:44,150
Yes, I'm in my internal render go past Dashty and it should fail and it will fail because it can't

110
00:06:44,150 --> 00:06:47,450
find any session data in the context.

111
00:06:48,260 --> 00:06:51,010
And that is a really serious clue as to what's going on.

112
00:06:51,020 --> 00:06:57,320
I actually panicked when I said no session data in context, and it tells me that error takes place

113
00:06:57,320 --> 00:06:59,030
in on a particular line.

114
00:06:59,060 --> 00:07:06,410
What I can show you where the error is, it's right here when it's trying to call this function right

115
00:07:06,410 --> 00:07:08,870
here at default data.

116
00:07:08,870 --> 00:07:15,680
I'm using the session here, all these pop string methods, and it's actually creating an error because

117
00:07:15,680 --> 00:07:18,680
there is no session data in that context.

118
00:07:18,680 --> 00:07:22,740
So it fails when it tries to get the data for flash or for error or for warning.

119
00:07:23,420 --> 00:07:31,190
So what we need to do is to change our test so that it has a request that has session data.

120
00:07:31,190 --> 00:07:34,100
And fortunately, that's not too difficult.

121
00:07:34,730 --> 00:07:38,750
So let's go back to our test and I'm going to create a new function below here.

122
00:07:38,920 --> 00:07:41,940
I'm going to call that func get session.

123
00:07:42,840 --> 00:07:45,290
OK, and that's going to do what?

124
00:07:45,410 --> 00:07:48,770
Well, when I call this, it's going to return to possible things.

125
00:07:49,400 --> 00:07:55,760
First of all, it's going to return a pointer to an HTTP request and it's potentially going to return

126
00:07:55,760 --> 00:07:56,240
an error.

127
00:07:56,270 --> 00:07:57,140
Something goes wrong.

128
00:07:57,890 --> 00:08:00,680
So let's see how we do this.

129
00:08:00,680 --> 00:08:06,480
To start with, I can cut this section out of here where I create a request and paste it in down there.

130
00:08:07,010 --> 00:08:14,550
Now, I can't do a t er I can just return nil an error because this isn't a testing function.

131
00:08:14,570 --> 00:08:16,160
This is not part of the testing package.

132
00:08:16,190 --> 00:08:17,800
This is a function we're creating ourself.

133
00:08:17,810 --> 00:08:20,640
Therefore I don't have access to that T variable that I do up here.

134
00:08:21,770 --> 00:08:23,600
So first of all, create this request.

135
00:08:23,600 --> 00:08:26,110
I know that's going to work because this is a request.

136
00:08:26,120 --> 00:08:29,020
This new request function is built right into the standard library.

137
00:08:29,030 --> 00:08:32,940
It's not going to give me an error, but it's good to get in the habit of trapping for those things.

138
00:08:33,470 --> 00:08:35,600
So at this point, I have a request object.

139
00:08:35,600 --> 00:08:41,180
I have a pointer to an HTP request, which is what I want to return, but I can't just return it or

140
00:08:41,180 --> 00:08:44,410
I'll be right back where I was before with no session data available.

141
00:08:45,020 --> 00:08:47,420
So what I'm going to do instead is create a context.

142
00:08:47,430 --> 00:08:51,950
I'll get the context from the request that we just built and I'll call that.

143
00:08:52,550 --> 00:08:56,990
That's my variable that's going to be pulled out of our request object that we just made.

144
00:08:57,200 --> 00:08:58,490
And it's just called context.

145
00:08:59,000 --> 00:09:02,050
And we use that every time we write to or read from the session.

146
00:09:02,060 --> 00:09:03,170
So you're familiar with that?

147
00:09:03,890 --> 00:09:09,140
Now that I have this, I need to put session data in there.

148
00:09:09,500 --> 00:09:11,540
And really, that's a one line command.

149
00:09:12,110 --> 00:09:18,170
So the syntax is as follows X and I don't care about the second parameter, so I'm going to ignore it

150
00:09:18,500 --> 00:09:20,510
is equal to session.

151
00:09:20,750 --> 00:09:24,530
That's our station manager load, not lifetime load.

152
00:09:25,760 --> 00:09:30,410
And it takes context, which is the variable that I'm actually working with right here.

153
00:09:30,560 --> 00:09:31,940
It requires the context.

154
00:09:33,020 --> 00:09:35,450
And then this is the part that makes it an active session.

155
00:09:35,460 --> 00:09:44,180
Our header douget and the key that it's looking for is X Dasch session exactly like that with a capital

156
00:09:44,180 --> 00:09:46,040
aspects and a capital S.

157
00:09:46,670 --> 00:09:52,580
And when I've done that, when I've actually put that into my context now I need to put my context back

158
00:09:52,580 --> 00:09:53,750
into my request.

159
00:09:54,050 --> 00:09:55,670
And again, that's a one line command.

160
00:09:55,820 --> 00:10:02,300
My request is now equal to my request with context and the context and putting it is the one on one

161
00:10:02,300 --> 00:10:08,600
that I just built up, the one that I added session data to, and now I can return error and nil.

162
00:10:09,620 --> 00:10:14,390
So I now have this function get session and back up here.

163
00:10:14,750 --> 00:10:22,940
I can actually make that work as follows by calling it error and error are equal to get the session.

164
00:10:23,360 --> 00:10:30,780
If error is not equal to nil, then I want to fill my test teda error here and that will fail the test.

165
00:10:31,310 --> 00:10:40,010
So at this point now when I call add default data, I can call it with my empty TD, my partner to my

166
00:10:40,010 --> 00:10:43,910
template data and the request that now has session information in it.

167
00:10:44,390 --> 00:10:46,880
But before I do that, I need something to test.

168
00:10:47,000 --> 00:10:51,450
So let's put something in our flash, for example, session.

169
00:10:51,830 --> 00:10:55,370
Put context is our context.

170
00:10:58,390 --> 00:11:03,310
I want to store in the variable special variable flash, I want to put the values one to three.

171
00:11:04,600 --> 00:11:11,620
So now that I've put it in the session and called add default data, at this point, I instead of checking

172
00:11:11,620 --> 00:11:19,390
to see if result is nil, I can say if results flash is not equal to one, two, three, then I can

173
00:11:19,390 --> 00:11:21,010
give it a more meaningful error message.

174
00:11:22,060 --> 00:11:27,720
Flash value of one, two, three, not found in session.

175
00:11:28,570 --> 00:11:31,300
And this should work unless we missed something.

176
00:11:31,330 --> 00:11:39,730
So let's give it a try back over here, clear the screen, go test ASV and it failed flash value of

177
00:11:39,730 --> 00:11:41,500
one, two, three, not found in session.

178
00:11:41,510 --> 00:11:42,630
So what did I miss?

179
00:11:42,670 --> 00:11:43,510
Let's go back here.

180
00:11:44,780 --> 00:11:49,160
Well, that was stupid, if result flash is not equal to one, two, three.

181
00:11:49,280 --> 00:11:50,130
Let's try that again.

182
00:11:50,330 --> 00:11:53,390
Go back over to here and run the test.

183
00:11:54,890 --> 00:11:57,650
And that's exactly what I wanted.

184
00:11:58,010 --> 00:11:58,550
All right.

185
00:11:58,550 --> 00:12:01,360
So now I've tested the first function in the next lecture.

186
00:12:01,370 --> 00:12:06,260
Let's work on template cash and all the other functions that we need to test.
