1
00:00:00,390 --> 00:00:07,080
All right, it's time for an exercise you may have noticed at this point that for all of these packages,

2
00:00:07,470 --> 00:00:12,000
there is one that we've written no test for whatsoever, and that's generally a bad thing.

3
00:00:12,420 --> 00:00:16,110
So I'm going to ask you to write some tests for the forms package.

4
00:00:16,440 --> 00:00:20,640
We don't need one for the config package because this is just the type that there's no way to test that

5
00:00:20,640 --> 00:00:23,850
either a type is syntactically correct or it's not.

6
00:00:23,850 --> 00:00:26,160
And this one is syntactically correct or it wouldn't compile.

7
00:00:26,790 --> 00:00:32,340
But the forms package actually has, as you can see here, quite a few functions and there are no tests

8
00:00:32,340 --> 00:00:32,990
for any of them.

9
00:00:33,390 --> 00:00:38,430
So to get you started, I'm actually going to take you through the process for writing a couple of quick

10
00:00:38,430 --> 00:00:38,910
tests.

11
00:00:39,510 --> 00:00:46,200
So in the forms package, for example, we have this function valid and it is taken as a receiver of

12
00:00:46,200 --> 00:00:52,710
type type pointer to form and it returns either true or false, either the form is valid or it is not.

13
00:00:52,980 --> 00:00:56,190
And we can write a really simple test for that as I've done here.

14
00:00:56,790 --> 00:01:00,720
And the syntax for the name this is the convention is test.

15
00:01:00,930 --> 00:01:06,510
It would normally be test valid if the function didn't have a receiver, but because this function has

16
00:01:06,510 --> 00:01:12,780
a receiver of type form, the naming convention is test and then form or the receiver name, followed

17
00:01:12,780 --> 00:01:14,610
by an underscore, followed by the function.

18
00:01:15,540 --> 00:01:22,200
So the test that we need to give it, if you can look at the form of the function itself, we need to

19
00:01:22,200 --> 00:01:24,360
have a form to test in the first place.

20
00:01:24,360 --> 00:01:26,340
So we'll create a request.

21
00:01:26,850 --> 00:01:34,530
And I'm just creating a new HDB test, new request of type post with any new URL I want and Knill for

22
00:01:34,530 --> 00:01:37,980
the body that gives me the variable error, which is of type request.

23
00:01:38,490 --> 00:01:44,730
And then I just say create a new form by passing in our post form, which is of type you are of values

24
00:01:44,730 --> 00:01:46,980
and in this case it's empty, but that's immaterial.

25
00:01:47,610 --> 00:01:52,500
And then I just call form dot valid and store the result is valid.

26
00:01:52,890 --> 00:01:57,300
And since there's nothing in the form to cause it to fail validation, it's empty.

27
00:01:57,810 --> 00:02:00,810
I just checked to see if it's valid is false.

28
00:02:00,960 --> 00:02:02,490
If it's false, I throw an error.

29
00:02:02,670 --> 00:02:09,360
So there's a really simple test for one now a slightly more complex one is this one called required

30
00:02:09,690 --> 00:02:16,410
and required is again it has a receiver of type pointer to form and it takes it's a very ADIC function

31
00:02:16,410 --> 00:02:23,610
that takes none or many, so as many as you want strings and then it determines whether they're valid

32
00:02:23,610 --> 00:02:24,910
or not, whether they're there or not.

33
00:02:25,830 --> 00:02:32,550
So in this case, again, I create a pointer to a request by calling HDB test new request and just passing

34
00:02:32,550 --> 00:02:37,920
in post for the type and anything I want for the URL and no body whatsoever.

35
00:02:38,400 --> 00:02:44,400
Then I instantiate a new form object by passing in our post form, which is of type zero values.

36
00:02:44,940 --> 00:02:48,810
And then I say for this form, require the fields A, B and C.

37
00:02:49,620 --> 00:02:57,090
If the form is valid, then it should fail the test because form shows valid when required fields are

38
00:02:57,090 --> 00:02:57,450
missing.

39
00:02:58,080 --> 00:03:02,040
Then I just create my own type of U.S. values.

40
00:03:02,040 --> 00:03:08,670
So I create a variable called posted data that has is of type orld up values and I add three three values

41
00:03:08,670 --> 00:03:11,610
in there, A, B and C and then I call the request again.

42
00:03:11,610 --> 00:03:16,260
This time I don't have to assign it because I already have the variable error which I declared up here.

43
00:03:16,260 --> 00:03:19,020
So I'm just overrating it with a new value, a new request.

44
00:03:19,350 --> 00:03:26,340
And then I set the post form field on that request object to posted data which we defined right here.

45
00:03:27,060 --> 00:03:28,170
Then I run the test again.

46
00:03:28,530 --> 00:03:30,710
So we create a new form, archtop post form.

47
00:03:30,720 --> 00:03:33,120
This time our post form has these values.

48
00:03:33,120 --> 00:03:34,710
So we actually have the values in there.

49
00:03:34,950 --> 00:03:45,510
And I say form required A, B, C and I should return valid if the form is is valid and not valid or

50
00:03:45,510 --> 00:03:45,900
false.

51
00:03:45,900 --> 00:03:47,070
If the form is not valid.

52
00:03:47,070 --> 00:03:49,980
If we get a false from here, then we have an error.

53
00:03:49,980 --> 00:03:51,900
So let's run these tests and see what happens.

54
00:03:52,230 --> 00:03:58,650
And I'll tell you right now, when we wrote the forms package I introduced to deliberate errors that

55
00:03:58,650 --> 00:04:02,790
are not immediately obvious until you write the necessary test to find them.

56
00:04:02,940 --> 00:04:04,680
And that's part of what we're doing right now.

57
00:04:04,920 --> 00:04:06,420
So I'm in the forms package.

58
00:04:06,600 --> 00:04:07,260
Let's right.

59
00:04:07,320 --> 00:04:09,930
Go test Dashty and see what we get.

60
00:04:11,240 --> 00:04:18,290
And I got a pass for everything that's great, so no surprise here, I didn't fix one of the deliberate

61
00:04:18,290 --> 00:04:19,130
errors for you.

62
00:04:19,310 --> 00:04:20,590
I'm hoping that you'll find it.

63
00:04:20,780 --> 00:04:27,440
So you need to go through and look at the functions we've not tested that would be has min length and

64
00:04:27,440 --> 00:04:29,720
is email and write tests for them.

65
00:04:29,720 --> 00:04:34,220
And when you write the test, bear in mind you're going to test two cases, one, when you should get

66
00:04:34,220 --> 00:04:39,650
a valid response from the validator and one when you should get an invalid response from the validator.

67
00:04:40,250 --> 00:04:40,770
Good luck.

68
00:04:40,820 --> 00:04:44,840
So take some time, work through it on your own, bang your head against the wall for a little while.

69
00:04:44,930 --> 00:04:49,550
And once that callus on your forehead is nice and thick, if you haven't figured it out, go to the

70
00:04:49,550 --> 00:04:51,650
next lecture where I give you the solutions.
