# Chapter 5. Saving User Input: Testing the Database
Benefit of TDD: iterative style of development.
Our target: take to-do item input from user and sent it to server
## 5.1 Wiring Up Our Form to Send a POST Request
To let browser send a standard HTML POST request, steps needed:
1. Give `<input>` element a `name=` attribute
2. Wrap it in a `<form>` tag with `method="POST"`
several tricks used to debug unexpected failure in functional test:
* Add print statements, to show, for example, what the current page text is.
* Improve the error message to show more info about the current state.
* Manually visit the site yourself.
* Use `time.sleep` to pause the test during execution (Chosen to be used)
![error](img/5-1.jpg)
* Reason of failure: CSRF token missed.
* What's CSRF token:
* > a unique, secret, unpredictable value that is generated by the server-side application and transmitted to the client in such a way that it is included in a subsequent HTTP request made by the client.
* One of Django's CSRF protection methods: placing a little auto-generated token into each generated form, so the form can be identified as POST request coming from original site.
Embed CSRF token using `{% csrf_token %}
```html
<formmethod="POST">
<inputname="item_text"id="id_new_item"placeholder="Enter a to-do item"/>
{% csrf_token %}
</form>
```
Result of running FT, page is layed with content but failed test, as server haven't wired to deal with POST request yet.
## 5.2 Processing a POST Request on the Server
Current `home.html` don't have `action=` attribute in `<form>`. Hence, it is submitting back to the `/` page (the same URL it was rendered from). It means the request is dealt by `home_page` function.
Add a test case into `lists/tests.py`
```python
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
```
Test it will return error
```
AssertionError: 'A new list item' not found in '<html>\n <head>\n <title>To-Do lists</title>\n </head>\n <body>\n <h1>Your To-Do list</h1>\n <formmethod="POST">\n <inputname="item_text"id="id_new_item"placeholder="Enter a to-do item"/>\n <inputtype=\'hidden\' name=\'csrfmiddlewaretoken\' value=\'U3BKUu5Xl50FQMi8JNz5ZCCIkHKISJJ2QtVuLuZ3zdRmOWXh7hvwZfXIh76CQiN2\' />\n </form>\n\n <tableid="id_list_table"></table>\n </body>\n</html>'
```
We will pass variables from Python view code into HTML templates
## 5.3 Passing Python Variables to Be Rendered in the Template
* Django's template syntax lets us include a python object in template using notation `{{ ... }}`
* renderer will display the object as a string
```html
<body>
<h1>Your To-Do list</h1>
<formmethod="POST">
<inputname="item_text"id="id_new_item"placeholder="Enter a to-do item"/>
{% csrf_token %}
</form>
<tableid="id_list_table">
<tr><td>{{ new_item_text }}</td></tr>
</table>
</body>
```
*`new_item_text` is the variable name for the user input
Modify test case in `lists/tests.py`, `assertTemplateUsed` will check whether we are still using template
```python
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
```
return error
```
self.assertTemplateUsed(response, 'home.html')
File "/home/jason/miniconda3/envs/python-tdd-book/lib/python3.6/site-packages/django/test/testcases.py", line 578, in assertTemplateUsed
self.fail(msg_prefix + "No templates used to render the response")
AssertionError: No templates used to render the response
```
QUES: Why fail?
ANS: In `lists/views.py`, `home_page` function no longer return rendered page. When facing `POST` request, it return a HttpResponse
```python
if request.method == 'POST':
return HttpResponse(request.POST['item_text'])
return render(request, 'home.html')
```
QUES: How to fix it?
ANS: Modify `home_page` method to return rendered page.
QUES: How to use `render` function?
ANS: `render` function takes, (3rd arguments), a dictionary which maps template variable names to their values.
Error is from `test_uses_home_template` test case. We broke the code path where there is no POST request; i.e. explain below
*`test_can_save_a_POST_request` get POST request with `'/', data={'item_text': 'A new list item'}`
*`test_uses_home_template` get POST request with only `'/'`, while `request.POST['item_text']` cannot find it.
QUES: How to fix it?
ANS: type of `request.POST` is 'django.http.request.QueryDict' is a dictionary. So dictionary method `dict.get` can be used to supply a default value `''`
self.assertIn(row_text, [row.text for row in rows])
```
## 5.5 The Django ORM and Our First Model
* **Object-Relational Mapper (ORM)** is a layer of abstraction of data stored in database (tables, rows, and columns)
* ORM let use work with database using OOP.
* *Classes map to database tables*
* *Attributes map to columns*
* *Individual instance of the class is mapped to a row of data in database*
Django comes with ORM. Using which, creating a new record in data is easy:
1. creating an object
2. assigning some attributes
3. calling `.save()` function.
Django also provide API for:
* querying the database using class attributes of object `.object`
* use simplest possible query `.all()` to retrieves all records for the table
* returned object is a list-like object called 'QuerySet'
* Using 'QuerySet', we can extract individual objects
All these can be used in test case
```python
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.save()
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(second_saved_item.text, 'Item the second')
```
### 5.5.1 Our First Database Migration
* In Django, ORM's job is to model the database,
* **migration** is a system that build the database. It has ability to add/remove tables and columns, based changes on `models.py` file. Like a version control system for the database.
Steps to update database:
1.`python manage.py makemigrations` to create `migrations/000x_initials.py`
2. Make migration via `python manage.py migrate`
### 5.5.2 The Test Gets Surprisingly Far
To pass the test, add class `Item`:
```python
class Item(models.Model):
text = models.TextField(default='')
```
Then migrate the database again, and then we can pass the test
## 5.6 Saving the POST to the Database
Modify the POST request test case that view will save a new item to the database instead of just passing to response.
```python
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1) # check that one new Item has been saved to the database
new_item = Item.objects.first() # same as doing objects.all()[0]
self.assertEqual(new_item.text, 'A new list item') # check that the item's text is correct
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
```
First step to pass the test: modify `home_page` function to save request (including empty request just to '/')
"Always redirect after a POST", change test case to save a POST request. So redirect back to home page, instead of rendering a response with item in it
```python
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
```
* QUES: What mean redirect?
* ANS: After sending POST, the returned response does not contain `.content` that rendered by a template, instead it's HTTP redirect that point to another URL
Modify `views.py` to pass this UT
```python
def home_page(request):
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text']) # Processing user input