So let's start running some tests for a render package, and I'm looking at the render package right
now and that's in internal render and the file is called Render Echo.
And the very first thing I want to test is this function.
Add default data.
So let's get started, first of all, to create the two files I know I'm probably going to need.
The first one is to set up our environment set up, underscore Testico and the second one I will call
render underscore test dutko.
OK, so let's get our setups first.
What do we need in order to test this function?
Well it says add default data takes TD, which is a pointer to models template data.
That's no problem.
I can create a variable that's of type models, template data and pass a pointer to this function.
That's easy.
The second one is the request.
Now, when this application is actually running, I have access to that request.
But in this package I don't because I'm running a test.
I'm not actually testing the entire application from Maine go right through to the very end of whatever
the request might be.
I'm just testing this one function at default data in isolation.
And that means I need to build this request and I can't just build an empty request.
That's trivial.
What I need to do is build a request that has session data.
So let's try building it first, just using a standard way of building an empty request object and see
what happens without the session data, it will fail.
Let's get started and try it anyway.
So I'm going to, first of all, in my setup, underscore Testico.
I'm going to create two variables.
I'll create a session which I'm not going to use right now, but I will momentarily.
So I'll set it up right now.
That's a pointer to Excession manager and I'll also create a variable called Test App, and I'll tell
you why I'm doing that in a minute.
And that's a type config app config.
The reason I'm creating test app is because if you look at render Dongo, the application variable is
actually stored or created right here in the package.
OK, so I'm going to create my own copy of that and then make this variable app a pointer to the one
that I create and I'll do that in setup test.
I go now.
Last time we created a function called Get Roots and that works fine and we could use the same approach
right now and create a function that creates everything we need.
Or we can use the built in one which we did much earlier in the course, and we'll do it again now.
But just to remind you that it exists and that's the function called Test Me, and it takes a parameter
which I call M, and that's a pointer to testing done m not testing that T.
And then at the very end of this function we call os x it not executable exit, but before it exits,
it actually runs our tests.
M not run.
So remember this function gets called before any of the tests are run.
It does whatever we tell it to do in the body of the function.
Then just before it closes in is the application.
It runs our tests.
So in here I need to set up certain things and I can get that information again from Command Mango.
And just getting the top part of what we have in here.
This is what I need.
I need the sessional information, so I'll copy that and then I'll paste it and I need to make a few
changes so I'm not going to be working with App directly.
Instead, I'll use this variable test app, put it in there, put it in there.
And I can't call app in production, but I'll just say this fault because we're not running an HDTV
server so we can leave that set of faults for testing purposes.
And then just before the application runs the tests, I say app is equal to a to test app and that populates
this this one command right here after I set everything up and test out it, make sure that app which
is declared in rendered ago, it makes sure that this variable has the things I needed to have.
So that's my setup right now.
OK, so that's probably sufficient for the moment.
Let's go back to our render test and start writing test.
And the application function that we want to test is called a default data.
So let's copy its name back to render, test and create a function function test with a capital T,
add default data and add default data is going to be of type T that takes a parameter a pointer to testing
T and it doesn't return anything.
So what do we do in this function?
Well, if you actually look at the application function itself, it's pretty straightforward.
It takes a couple of arguments and it's over in the render package.
It takes TD, which is a pointer to models template data, and then it takes a request.
So let's go back and call it so we can call it just like this.
We can first of all create an empty TD variable, which is a type models template data.
That's his first argument, and then we need a request now, we could do it like this, we could say
our.
I don't know if it is an error or not, I think it does is a sign the value of HDP new request.
Yes, that returns an error and it requires a method.
Get her post.
What would she get?
It requires you earn.
That can be anything.
Just some.
You are we're not going to do anything with you IRL, so we can just put whatever we want in there.
And it requires a body.
But I'm not going to pass to the body.
I want to pass it nil because really I'm just making a request to you, Earl.
I'm trying to get a request.
So that will create one for us.
And I can check for an error.
If error is not equal to nil, then teig error error.
And that will that will fail our test.
So now I have a request.
So let's try calling that that function.
This one over here render an rendered.
OK, we're going to add default data.
So let's call it we can say result is assigned, add default data and we can pass it our TD and our
request.
And then of course we need to do something with that.
So I can say if result is equal to nil, then teig error.
It doesn't matter what I'm doing here.
It's just tittered error failed.
OK, I know this isn't going to work, so I'm not too worried about getting too much right here in terms
of error messages.
So let's try this now.
It wants a pointer to TD, so I have to make that a pointer there.
Let's try running this test.
Go, run.
I'm in the right direction, am I?
Yes, I'm in my internal render go past Dashty and it should fail and it will fail because it can't
find any session data in the context.
And that is a really serious clue as to what's going on.
I actually panicked when I said no session data in context, and it tells me that error takes place
in on a particular line.
What I can show you where the error is, it's right here when it's trying to call this function right
here at default data.
I'm using the session here, all these pop string methods, and it's actually creating an error because
there is no session data in that context.
So it fails when it tries to get the data for flash or for error or for warning.
So what we need to do is to change our test so that it has a request that has session data.
And fortunately, that's not too difficult.
So let's go back to our test and I'm going to create a new function below here.
I'm going to call that func get session.
OK, and that's going to do what?
Well, when I call this, it's going to return to possible things.
First of all, it's going to return a pointer to an HTTP request and it's potentially going to return
an error.
Something goes wrong.
So let's see how we do this.
To start with, I can cut this section out of here where I create a request and paste it in down there.
Now, I can't do a t er I can just return nil an error because this isn't a testing function.
This is not part of the testing package.
This is a function we're creating ourself.
Therefore I don't have access to that T variable that I do up here.
So first of all, create this request.
I know that's going to work because this is a request.
This new request function is built right into the standard library.
It's not going to give me an error, but it's good to get in the habit of trapping for those things.
So at this point, I have a request object.
I have a pointer to an HTP request, which is what I want to return, but I can't just return it or
I'll be right back where I was before with no session data available.
So what I'm going to do instead is create a context.
I'll get the context from the request that we just built and I'll call that.
That's my variable that's going to be pulled out of our request object that we just made.
And it's just called context.
And we use that every time we write to or read from the session.
So you're familiar with that?
Now that I have this, I need to put session data in there.
And really, that's a one line command.
So the syntax is as follows X and I don't care about the second parameter, so I'm going to ignore it
is equal to session.
That's our station manager load, not lifetime load.
And it takes context, which is the variable that I'm actually working with right here.
It requires the context.
And then this is the part that makes it an active session.
Our header douget and the key that it's looking for is X Dasch session exactly like that with a capital
aspects and a capital S.
And when I've done that, when I've actually put that into my context now I need to put my context back
into my request.
And again, that's a one line command.
My request is now equal to my request with context and the context and putting it is the one on one
that I just built up, the one that I added session data to, and now I can return error and nil.
So I now have this function get session and back up here.
I can actually make that work as follows by calling it error and error are equal to get the session.
If error is not equal to nil, then I want to fill my test teda error here and that will fail the test.
So at this point now when I call add default data, I can call it with my empty TD, my partner to my
template data and the request that now has session information in it.
But before I do that, I need something to test.
So let's put something in our flash, for example, session.
Put context is our context.
I want to store in the variable special variable flash, I want to put the values one to three.
So now that I've put it in the session and called add default data, at this point, I instead of checking
to see if result is nil, I can say if results flash is not equal to one, two, three, then I can
give it a more meaningful error message.
Flash value of one, two, three, not found in session.
And this should work unless we missed something.
So let's give it a try back over here, clear the screen, go test ASV and it failed flash value of
one, two, three, not found in session.
So what did I miss?
Let's go back here.
Well, that was stupid, if result flash is not equal to one, two, three.
Let's try that again.
Go back over to here and run the test.
And that's exactly what I wanted.
All right.
So now I've tested the first function in the next lecture.
Let's work on template cash and all the other functions that we need to test.