From the course: Test-Driven Development in Django

Testing page data

From the course: Test-Driven Development in Django

Start my 1-month free trial

Testing page data

- [Instructor] Now, if we're going to have a website that's going to take in some text and then ultimately spit back out a hash, or we've got to have some sort of form where someone can enter in that information. So I think that should be our next unit test is testing whether we have a HashForm on the home page. So, let's go ahead and create a new test. We'll do def, and let go ahead and choose a name for this, like test underscore hash underscore form, and we'll pass self in there, use our colon. Remember, you've got to have a test start with the next test if you want it to be run. Inside of this test, what we're going to be looking for is if we can create a new form. And so, what form that we're going to eventually be creating is going to be called the HashForm. So let's go ahead and go to the top here and import that. We're going to say, from dot forms import, and we want hash capital Form. Now, this doesn't exist yet, but it will in a second here. We'll come back down to our function, and we'll say, form is equal to capital HashForm, and we'll just pass inside of here some data equal to, we'll make a quick dictionary here with curly brackets, text colon hello. So the idea here is that we should be able to make a form out of this HashForm class and pass in some data text will pass in hello for the text field in that form. And with this, we then want to assert that this form is valid. So we're going to do a new kind of assert here. We're going to say, self dot assert capital True, True we have two S's there, assertTrue. And then, in our parentheses here, we're going to say, form dot is underscore valid. So we're going to test essentially two things, one, that this HashForm exists. If HashForm does not exist, we're going to get an error on that line. But if it does exist, then we're going to test that it's valid, meaning that if we pass in hello, that that's a valid piece of information for the field text. So let's go ahead and save that. With our new test that we have here, let's go ahead and try it out. So we'll run our manage.py test, and wouldn't you know it, we have an error, and that's expected, right? You write errors that fail, then we write the code to pass them. So let's go ahead and move back. We're going to now create a new file called forms.py. So here in our hashing directory, we're going to right-click new New File. We're going to create forms.py. Inside of this file, we need to import the code to create a form. So we're going to say from django import forms. With that in place, let's create our new class, class HashForm, make sure you have the capital F there, and then, we want to type inside of our parentheses forms dot form, that's where we're coming from, and we'll do our colon. With that, let's go ahead and add our text field here. We'll say, text is equal to forms dot capital Char, and then capital Field, and then, inside of the parentheses, we want our label to be equal to what. Well, remember that we wrote a test about what we wanted this particular form to say. So if we go back to our test.py, and then, we go to our test_there_is_homepage, you should see that we are looking for Enter hash here. So I'm going to copy this exactly so that we're able to pass this test. I'm going to come back to my forms, paste that right inside of there. So now, our label is going to display that. Then we'll say comma, and then, widget is equal to forms dot capital Textarea. And the reason I'm using the Textarea here is because, with hashing, sometimes, people will hash a huge chunk of information, like, we could do the Gettysburg Address, or anything, really, that we wanted. But we want them to have a lot of space that someone could input lots of text. So, we've gone ahead and create our form here. Next, let's go ahead and move to our Views so that we can create this form. So, inside of our Views, first thing that we got to do is import that form, so we're going to say, from dot forms import capital HashForm. Now that we have them in place inside of our home, let's create an instance of that form. We'll say, form is equal to capital HashForm with the parentheses. And then, when we pass back the template, let's throw in this new form. So we're going to do our curly brackets here, we're going to pass in for the key form as a string, the actual then form value. We'll go ahead and save that. Now, there's just one last piece of the puzzle here, and that's that we have to update our template to show this form. So let's go to our home.html. Inside of here, let's delete that hello and now replace with a new opening form tag, and then, we'll have an ending form tag. Inside of this opening tag, we got to specify a few things. The first one is where we want this to go, so we're going to say, action. This should essentially just go to our home page, so we're going to say, inside of these double quotes, curlies, percentage signs, url, single quote, Home, so that's going to take us to the home page, and the method that we're looking for is having this equal to a post. Now, if we create a form with a post, we need to have a CSRF token, so I'm going to do my curly bracket with percentage signs, we need a csrf underscore token. Now with that in place, we need to display the form. So let's do two curly brackets, and inside of there, we'll say, form dot as_p, so that's going to display it as paragraph tags. And then finally, we need a Submit button. So we're do that via an input. So we'll say input name is equal to submit. We're going to have type equal to, you guessed it, submit. Let's do double quotes on that, excellent. And then, finally, we're going to have the value, this is the text that shows up inside the button box, have something like Hash, right? That way, the user knows, oh, if I hit this button, it's going to hash whatever is inside of that box. So with this all in place, let's go ahead and save that. We've saved all of our code. Let's go ahead and move over into our Terminal. Let's run this test again, and look at this, we have an issue. So, let's use this as a teaching moment to see if our test can't tell us why this isn't working. So it's telling us inside of our forms.py on line three that we have a class, and look at that, I use three S's for class. So we're going to go ahead and move back to our forms.py, we'll get rid of that S, Save. Let's try rerunning these tests, well, and look at that, we get out two tests to pass with flying colors, things are looking great.

Contents