So it's time to go and update our tests, and I have been somewhat negligent in doing so, largely because
I wanted to get the database stuff out of the way first.
So if you've tried to run your tests lately, you may have noticed that there are some problems.
Let's do that.
So I'm in my working directory at the root level of my project, and if I just type go test dash Vygotsky
to test everything, you'll see there are some errors.
So right away we have this error in render test, go undefined render template.
Well, that's an easy one.
Let's go to render test Dongo.
Render test Ortego and there are errors everywhere I called this function.
So let's find it render template.
And if you recall, we renamed that from render template to template.
So that'll fix that one and that'll fix that one.
And at the bottom, we have one more new templates.
Well, we didn't call it new templates anymore.
What did we actually call it?
We renamed it to New Renderer.
So let's copy that function name and replace that.
And that will fix those problems.
So also try your tests again and see what else is going on.
Go test drive, dot, dot, dot, dot, dot.
And now we have another problem.
And the problem is where is it?
Right here.
Set up test.
Don't go.
Not enough arguments and call to new repo.
So the problem with that one is that we now have new handlers requiring a database driver.
OK, well, how are we going to get around that?
Because if you think it through, what we're essentially asking ourselves to do is to every time we
run our tests, we want to have access to the actual database.
And what we're writing are called unit tests.
And unit tests are supposed to test one thing in isolation.
So when I test, for example, this handler, the home handler, all I want to test is what's inside
of this.
I don't care about anything else.
So in essence, what we need to do is to find a way to run our tests without creating a new database,
running all the migrations to populate the database with tables, then populating the tables with some
test data and then running our tests.
Now, there are situations where you want to do that and we'll be doing something very similar to that
before we finish this course.
But right now, I'm writing unit tests.
So what I'm going to do is take advantage of the fact that this is calling a repository.
And if you look in our repository folder for our application, we have this repository itself that describes
all the functions that must be available to a given repository.
But we also have the ability to swap out our database with another database or as is the case right
now, we're going to swap out the repoll the database repo and put in place for our tests only a repository
that thinks a database and it's really not that difficult.
So let's think about how this works.
Let's go back to may not go and see exactly how we're doing this.
So in our run function and our main function, it's up here.
So now in our run function, we register some things for the session.
We set up some values for values, for our application config.
And then down here we connect to a database.
OK, so it says connect to SQL.
That's fine.
Let's skip that.
Create template cache here.
New repo for the handlers.
So the handlers function has a new repository function where we're going to create a new function in
here for new test repo or something like that.
And that actually calls DB repo new postgrads repo.
And we're going to create another function here that returns a test repository.
So let's get started doing that.
Let's just get it set up and then we'll get around to actually making the whole thing work.
I'll create another type, which I will call test DB repo, which will be a struct and it will have
the same Fields app slash pointer to configure app config, the same fields as Postgres DB Repo.
It also has to have DB, which we're not going to populate with anything, but it needs to exist so
we can call database functions that don't actually have a database behind them.
Then we'll have to have a function just like this one.
So I'll copy this one.
I'll post it here and I will call it new test repo,
new test testing repo.
Let's call it new testing repo.
And it's only going to take one argument, just the app config.
OK, even though postgrads repo or this needs to become test DB repo,
we're never going to populate the database for we're just going to populate the application config.
So now this gives us an error and the error is one that you should already know about.
It says this type sorry database repo type does not implement a repository database repo as some methods
are missing.
And what it's talking about are all the methods inherent in postgrads repo.
We must have one entry for every one of these things.
So I want them to do is copy this and under the DB repo I will create a new file.
Which I will call test Repo Digo, and I will add that to get and I will paste everything in there and
then I'll go start changing things.
So to start with, um, let's see, I don't need this.
I need to get rid of this.
I didn't delete before I pasted there.
So I'm going to change this type or this receiver from postgrads DB repo to test DB repo and I'll do
that for all of the functions.
Paste.
Paiste.
Paced, and I think there's one more paced now that gets rid of the errors.
If we go back to DB, Repo now implements all of those methods, but we're not actually passing it a
database connection pool.
So let's go change these functions.
The first one is fine because it doesn't actually hit the database at all and we're not doing it.
That's going to be changed later, later on when we get to building the back end for our property owners.
But this function, for example, insert reservation, I want this function to exist.
I want to want it to have the receiver tested repo.
And I wanted to return an intent, an error, but I never wanted to hit the database.
So all I have to do is have it return an end and no error, which is sufficient for right now.
And we'll do the same thing here.
This just returns an error, so I'll just have it return nil and this returns a bool and an error.
So we return false and nil and this returns a slice of model's room.
So I'll leave my declaration of model's room there for my room's variable.
But just have it return that empty variable and no error and we'll change this when we get to writing
tests that actually do something.
But right now, I just want this to be able to run.
So this one returns a slice.
Now this one turns a model's room.
So I'll leave the declaration of that variable
and just have it returned the empty variable and to nil.
OK, this gets us closer.
So let's make sure there's no errors here.
No, no errors in DB repo.
No.
No errors in the render test.
No.
OK, so let's have a look and see what happens if I try to run the test, because we're not done yet.
But I just want to see how close we are.
Go test Dog-Eat-Dog.
And it says internals, handlers set up Tesco, not enough arguments in call to new repo.
OK, so that's in set up test under handlers.
Let's go there.
So handlers set up test and the error is right here.
So here this is calling the new repo and app, I'm going to instead create a copy of this function in
handler Stocco, which exists only to run test.
So I call it new testing or new test repo, new test repo.
And that's only going to take one argument, which is the app config.
And it's going to return a database of type new testing repo, which only requires the app config.
So now we've taken the database right out of the equation.
So let's copy this function name, go back to setup test and have it call that instead of new repo.
All right.
Let's see what happens now.
They're still going to be errors.
But don't despair.
We'll take care of those.
Go test dash v dot, dot, dot, dot, dot.
OK, so there are some errors which are expected.
Let's see what they are.
I'm going to clear the screen and run that again with some empty lines in front of it so I can see where
to start looking.
Right here, so fine, fine, fine, that's good.
And here we have our first fail in bookings, internal helpers, but it's bookings, internal handlers
and it says there's a problem and we're testing the reservation.
OK, so if you look at these, you'll notice some commonality.
And the commonality is pretty easy to identify.
All of the errors have to do with either the session because apparently we don't have access to the
session or whenever we're making a post request.
And those are problems and they're easy to fix.
And we'll take care of fixing those in the next few lectures.