1
00:00:01,270 --> 00:00:06,790
So it's time to go and update our tests, and I have been somewhat negligent in doing so, largely because

2
00:00:06,790 --> 00:00:08,930
I wanted to get the database stuff out of the way first.

3
00:00:09,940 --> 00:00:14,460
So if you've tried to run your tests lately, you may have noticed that there are some problems.

4
00:00:14,470 --> 00:00:15,130
Let's do that.

5
00:00:15,820 --> 00:00:23,080
So I'm in my working directory at the root level of my project, and if I just type go test dash Vygotsky

6
00:00:23,320 --> 00:00:26,200
to test everything, you'll see there are some errors.

7
00:00:26,680 --> 00:00:31,610
So right away we have this error in render test, go undefined render template.

8
00:00:31,630 --> 00:00:32,480
Well, that's an easy one.

9
00:00:32,500 --> 00:00:34,180
Let's go to render test Dongo.

10
00:00:35,590 --> 00:00:42,760
Render test Ortego and there are errors everywhere I called this function.

11
00:00:42,770 --> 00:00:44,830
So let's find it render template.

12
00:00:44,830 --> 00:00:49,710
And if you recall, we renamed that from render template to template.

13
00:00:50,260 --> 00:00:54,120
So that'll fix that one and that'll fix that one.

14
00:00:54,730 --> 00:00:58,410
And at the bottom, we have one more new templates.

15
00:00:58,420 --> 00:01:00,190
Well, we didn't call it new templates anymore.

16
00:01:00,190 --> 00:01:01,380
What did we actually call it?

17
00:01:01,420 --> 00:01:03,060
We renamed it to New Renderer.

18
00:01:03,730 --> 00:01:06,400
So let's copy that function name and replace that.

19
00:01:07,300 --> 00:01:08,730
And that will fix those problems.

20
00:01:08,730 --> 00:01:11,080
So also try your tests again and see what else is going on.

21
00:01:11,410 --> 00:01:14,760
Go test drive, dot, dot, dot, dot, dot.

22
00:01:16,120 --> 00:01:17,920
And now we have another problem.

23
00:01:17,920 --> 00:01:19,750
And the problem is where is it?

24
00:01:20,680 --> 00:01:21,310
Right here.

25
00:01:23,110 --> 00:01:23,900
Set up test.

26
00:01:23,900 --> 00:01:24,520
Don't go.

27
00:01:24,520 --> 00:01:26,830
Not enough arguments and call to new repo.

28
00:01:32,440 --> 00:01:37,330
So the problem with that one is that we now have new handlers requiring a database driver.

29
00:01:37,540 --> 00:01:40,330
OK, well, how are we going to get around that?

30
00:01:40,990 --> 00:01:46,990
Because if you think it through, what we're essentially asking ourselves to do is to every time we

31
00:01:46,990 --> 00:01:50,380
run our tests, we want to have access to the actual database.

32
00:01:51,130 --> 00:01:53,110
And what we're writing are called unit tests.

33
00:01:53,110 --> 00:01:56,630
And unit tests are supposed to test one thing in isolation.

34
00:01:56,650 --> 00:02:03,190
So when I test, for example, this handler, the home handler, all I want to test is what's inside

35
00:02:03,190 --> 00:02:03,510
of this.

36
00:02:03,520 --> 00:02:05,140
I don't care about anything else.

37
00:02:06,040 --> 00:02:13,600
So in essence, what we need to do is to find a way to run our tests without creating a new database,

38
00:02:14,010 --> 00:02:19,150
running all the migrations to populate the database with tables, then populating the tables with some

39
00:02:19,150 --> 00:02:21,710
test data and then running our tests.

40
00:02:21,750 --> 00:02:26,350
Now, there are situations where you want to do that and we'll be doing something very similar to that

41
00:02:26,350 --> 00:02:27,730
before we finish this course.

42
00:02:28,090 --> 00:02:29,860
But right now, I'm writing unit tests.

43
00:02:30,610 --> 00:02:36,430
So what I'm going to do is take advantage of the fact that this is calling a repository.

44
00:02:36,460 --> 00:02:43,330
And if you look in our repository folder for our application, we have this repository itself that describes

45
00:02:43,360 --> 00:02:47,290
all the functions that must be available to a given repository.

46
00:02:47,740 --> 00:02:53,920
But we also have the ability to swap out our database with another database or as is the case right

47
00:02:53,920 --> 00:03:01,570
now, we're going to swap out the repoll the database repo and put in place for our tests only a repository

48
00:03:01,570 --> 00:03:05,020
that thinks a database and it's really not that difficult.

49
00:03:05,590 --> 00:03:07,030
So let's think about how this works.

50
00:03:07,040 --> 00:03:12,450
Let's go back to may not go and see exactly how we're doing this.

51
00:03:12,460 --> 00:03:15,870
So in our run function and our main function, it's up here.

52
00:03:15,880 --> 00:03:21,280
So now in our run function, we register some things for the session.

53
00:03:21,280 --> 00:03:25,450
We set up some values for values, for our application config.

54
00:03:25,870 --> 00:03:27,700
And then down here we connect to a database.

55
00:03:27,760 --> 00:03:29,740
OK, so it says connect to SQL.

56
00:03:29,740 --> 00:03:30,310
That's fine.

57
00:03:30,310 --> 00:03:31,150
Let's skip that.

58
00:03:31,390 --> 00:03:33,190
Create template cache here.

59
00:03:33,220 --> 00:03:35,020
New repo for the handlers.

60
00:03:35,320 --> 00:03:41,140
So the handlers function has a new repository function where we're going to create a new function in

61
00:03:41,140 --> 00:03:43,810
here for new test repo or something like that.

62
00:03:44,530 --> 00:03:48,850
And that actually calls DB repo new postgrads repo.

63
00:03:49,150 --> 00:03:52,800
And we're going to create another function here that returns a test repository.

64
00:03:52,810 --> 00:03:53,880
So let's get started doing that.

65
00:03:53,890 --> 00:03:57,240
Let's just get it set up and then we'll get around to actually making the whole thing work.

66
00:03:57,820 --> 00:04:04,780
I'll create another type, which I will call test DB repo, which will be a struct and it will have

67
00:04:04,780 --> 00:04:13,060
the same Fields app slash pointer to configure app config, the same fields as Postgres DB Repo.

68
00:04:13,220 --> 00:04:18,460
It also has to have DB, which we're not going to populate with anything, but it needs to exist so

69
00:04:18,460 --> 00:04:21,760
we can call database functions that don't actually have a database behind them.

70
00:04:23,110 --> 00:04:25,950
Then we'll have to have a function just like this one.

71
00:04:25,960 --> 00:04:26,950
So I'll copy this one.

72
00:04:27,760 --> 00:04:32,050
I'll post it here and I will call it new test repo,

73
00:04:35,260 --> 00:04:36,850
new test testing repo.

74
00:04:36,850 --> 00:04:38,260
Let's call it new testing repo.

75
00:04:38,950 --> 00:04:42,130
And it's only going to take one argument, just the app config.

76
00:04:42,440 --> 00:04:47,950
OK, even though postgrads repo or this needs to become test DB repo,

77
00:04:51,070 --> 00:04:55,220
we're never going to populate the database for we're just going to populate the application config.

78
00:04:55,720 --> 00:04:58,950
So now this gives us an error and the error is one that you should already know about.

79
00:04:58,960 --> 00:05:05,590
It says this type sorry database repo type does not implement a repository database repo as some methods

80
00:05:05,590 --> 00:05:06,190
are missing.

81
00:05:06,580 --> 00:05:10,870
And what it's talking about are all the methods inherent in postgrads repo.

82
00:05:10,870 --> 00:05:13,450
We must have one entry for every one of these things.

83
00:05:13,450 --> 00:05:19,510
So I want them to do is copy this and under the DB repo I will create a new file.

84
00:05:21,610 --> 00:05:29,980
Which I will call test Repo Digo, and I will add that to get and I will paste everything in there and

85
00:05:29,980 --> 00:05:31,380
then I'll go start changing things.

86
00:05:31,390 --> 00:05:36,100
So to start with, um, let's see, I don't need this.

87
00:05:36,400 --> 00:05:37,650
I need to get rid of this.

88
00:05:38,200 --> 00:05:40,870
I didn't delete before I pasted there.

89
00:05:41,560 --> 00:05:48,460
So I'm going to change this type or this receiver from postgrads DB repo to test DB repo and I'll do

90
00:05:48,460 --> 00:05:49,600
that for all of the functions.

91
00:05:50,680 --> 00:05:51,310
Paste.

92
00:05:53,580 --> 00:05:54,240
Paiste.

93
00:05:55,970 --> 00:05:56,600
Paiste.

94
00:05:58,950 --> 00:06:05,070
Paced, and I think there's one more paced now that gets rid of the errors.

95
00:06:05,100 --> 00:06:09,510
If we go back to DB, Repo now implements all of those methods, but we're not actually passing it a

96
00:06:09,520 --> 00:06:10,890
database connection pool.

97
00:06:11,610 --> 00:06:13,440
So let's go change these functions.

98
00:06:13,440 --> 00:06:17,040
The first one is fine because it doesn't actually hit the database at all and we're not doing it.

99
00:06:17,050 --> 00:06:22,620
That's going to be changed later, later on when we get to building the back end for our property owners.

100
00:06:23,070 --> 00:06:28,140
But this function, for example, insert reservation, I want this function to exist.

101
00:06:28,230 --> 00:06:31,170
I want to want it to have the receiver tested repo.

102
00:06:31,680 --> 00:06:35,720
And I wanted to return an intent, an error, but I never wanted to hit the database.

103
00:06:35,730 --> 00:06:43,110
So all I have to do is have it return an end and no error, which is sufficient for right now.

104
00:06:43,440 --> 00:06:44,800
And we'll do the same thing here.

105
00:06:44,910 --> 00:06:52,350
This just returns an error, so I'll just have it return nil and this returns a bool and an error.

106
00:06:52,560 --> 00:07:00,120
So we return false and nil and this returns a slice of model's room.

107
00:07:00,120 --> 00:07:03,840
So I'll leave my declaration of model's room there for my room's variable.

108
00:07:04,500 --> 00:07:10,320
But just have it return that empty variable and no error and we'll change this when we get to writing

109
00:07:10,320 --> 00:07:11,640
tests that actually do something.

110
00:07:12,070 --> 00:07:14,130
But right now, I just want this to be able to run.

111
00:07:15,420 --> 00:07:17,830
So this one returns a slice.

112
00:07:18,220 --> 00:07:19,890
Now this one turns a model's room.

113
00:07:19,890 --> 00:07:21,750
So I'll leave the declaration of that variable

114
00:07:25,830 --> 00:07:28,390
and just have it returned the empty variable and to nil.

115
00:07:29,180 --> 00:07:30,650
OK, this gets us closer.

116
00:07:31,590 --> 00:07:33,810
So let's make sure there's no errors here.

117
00:07:33,840 --> 00:07:36,000
No, no errors in DB repo.

118
00:07:36,000 --> 00:07:36,480
No.

119
00:07:37,200 --> 00:07:39,360
No errors in the render test.

120
00:07:39,870 --> 00:07:40,290
No.

121
00:07:40,310 --> 00:07:45,180
OK, so let's have a look and see what happens if I try to run the test, because we're not done yet.

122
00:07:45,390 --> 00:07:46,690
But I just want to see how close we are.

123
00:07:47,040 --> 00:07:50,520
Go test Dog-Eat-Dog.

124
00:07:53,060 --> 00:07:59,240
And it says internals, handlers set up Tesco, not enough arguments in call to new repo.

125
00:07:59,290 --> 00:08:02,360
OK, so that's in set up test under handlers.

126
00:08:02,380 --> 00:08:02,880
Let's go there.

127
00:08:03,860 --> 00:08:10,100
So handlers set up test and the error is right here.

128
00:08:12,890 --> 00:08:21,470
So here this is calling the new repo and app, I'm going to instead create a copy of this function in

129
00:08:21,470 --> 00:08:25,100
handler Stocco, which exists only to run test.

130
00:08:25,110 --> 00:08:30,170
So I call it new testing or new test repo, new test repo.

131
00:08:30,740 --> 00:08:35,210
And that's only going to take one argument, which is the app config.

132
00:08:38,470 --> 00:08:46,770
And it's going to return a database of type new testing repo, which only requires the app config.

133
00:08:46,810 --> 00:08:50,340
So now we've taken the database right out of the equation.

134
00:08:51,130 --> 00:08:56,650
So let's copy this function name, go back to setup test and have it call that instead of new repo.

135
00:08:57,490 --> 00:08:57,820
All right.

136
00:08:57,820 --> 00:08:58,830
Let's see what happens now.

137
00:08:58,840 --> 00:08:59,800
They're still going to be errors.

138
00:09:00,070 --> 00:09:00,870
But don't despair.

139
00:09:00,880 --> 00:09:01,900
We'll take care of those.

140
00:09:02,740 --> 00:09:06,180
Go test dash v dot, dot, dot, dot, dot.

141
00:09:09,320 --> 00:09:11,870
OK, so there are some errors which are expected.

142
00:09:12,590 --> 00:09:13,670
Let's see what they are.

143
00:09:15,110 --> 00:09:20,300
I'm going to clear the screen and run that again with some empty lines in front of it so I can see where

144
00:09:20,300 --> 00:09:21,080
to start looking.

145
00:09:24,260 --> 00:09:32,280
Right here, so fine, fine, fine, that's good.

146
00:09:32,300 --> 00:09:39,170
And here we have our first fail in bookings, internal helpers, but it's bookings, internal handlers

147
00:09:39,410 --> 00:09:42,680
and it says there's a problem and we're testing the reservation.

148
00:09:42,870 --> 00:09:46,130
OK, so if you look at these, you'll notice some commonality.

149
00:09:46,130 --> 00:09:48,230
And the commonality is pretty easy to identify.

150
00:09:48,500 --> 00:09:53,450
All of the errors have to do with either the session because apparently we don't have access to the

151
00:09:53,450 --> 00:09:56,570
session or whenever we're making a post request.

152
00:09:56,670 --> 00:09:59,300
And those are problems and they're easy to fix.

153
00:09:59,300 --> 00:10:02,630
And we'll take care of fixing those in the next few lectures.
