diff --git a/functional_tests.py b/functional_tests.py index f3702400be8f04d11874fe7f7ce96992eedcf8bf..bafd537d9da29657620f7059bb66e7d5746292a4 100644 --- a/functional_tests.py +++ b/functional_tests.py @@ -11,6 +11,11 @@ class NewVisitorTest(unittest.TestCase): def tearDown(self): self.browser.quit() + def check_for_row_in_list_table(self, row_text): + table = self.browser.find_element_by_id('id_list_table') + rows = table.find_elements_by_tag_name('tr') + self.assertIn(row_text, [row.text for row in rows]) + def test_can_start_a_list_and_retrieve_it_later(self): self.browser.get('http://localhost:8000') @@ -32,17 +37,23 @@ class NewVisitorTest(unittest.TestCase): # When she hits enter, the page updates, and now the page lists # "1: Buy peacock feathers" as an item in a to-do list table inputbox.send_keys(Keys.ENTER) + self.check_for_row_in_list_table('1: Buy peacock feathers') - table = self.browser.find_element_by_id('id_list_table') - rows = table.find_elements_by_tag_name('tr') - self.assertTrue( - - any(row.text == '1: Buy peacock feathers' for row in rows), - "New to-do item did not appear in table" - ) # There is still a text box inviting her to add another item. She # enters "Use peacock feathers to make a fly" (Edith is very # methodical) + inputbox = self.browser.find_element_by_id('id_new_item') + inputbox.send_keys('Use peacock feathers to make a fly') + inputbox.send_keys(Keys.ENTER) + + # The page updates again, and now shows both items on her list + self.check_for_row_in_list_table('1: Buy peacock feathers') + self.check_for_row_in_list_table('2: Use peacock feathers to make a fly') + + + # Edith wonders whether the site will remember her list. Then she sees + # that the site has generated a unique URL for her -- there is some + # explanatory text to that effect. self.fail('Finish the test!') #h = self.browser.page_source diff --git a/lists/migrations/0001_initial.py b/lists/migrations/0001_initial.py new file mode 100644 index 0000000000000000000000000000000000000000..ac2a7928f77332d363ef15b5f960a944f69981da --- /dev/null +++ b/lists/migrations/0001_initial.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.5 on 2019-09-25 15:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Item', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + ), + ] diff --git a/lists/migrations/0002_item_text.py b/lists/migrations/0002_item_text.py new file mode 100644 index 0000000000000000000000000000000000000000..468321c16e3dc0b33c8b2107183b9da1c36330aa --- /dev/null +++ b/lists/migrations/0002_item_text.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.5 on 2019-09-25 15:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lists', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='item', + name='text', + field=models.TextField(default=''), + ), + ] diff --git a/lists/models.py b/lists/models.py index 71a836239075aa6e6e4ecb700e9c42c95c022d91..59abf095c1c4c75e88f61335ce39f3671c4a8ea8 100644 --- a/lists/models.py +++ b/lists/models.py @@ -1,3 +1,5 @@ from django.db import models # Create your models here. +class Item(models.Model): + text = models.TextField(default='') diff --git a/lists/templates/home.html b/lists/templates/home.html index 5474b018681f18bf147d180d35b6a439c58a84ea..e6b05e363e84153190d5a3d0e16e95326dba5b5b 100644 --- a/lists/templates/home.html +++ b/lists/templates/home.html @@ -4,7 +4,15 @@

Your To-Do list

- +
+ {% csrf_token %} + + +
+ {% for item in items %} + + {% endfor %} +
{{ forloop.counter }}: {{ item.text }}
diff --git a/lists/tests.py b/lists/tests.py index b02f54c9934326334b518e5e35d70ed9c2052797..91610b7f18479bf15c0200a421a0253fba71f6f2 100644 --- a/lists/tests.py +++ b/lists/tests.py @@ -2,14 +2,18 @@ from django.test import TestCase from django.urls import resolve from django.http import HttpRequest from lists.views import home_page +from lists.models import Item + from django.template.loader import render_to_string + + # Create your tests here. class HomePageTest(TestCase): def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertEqual(found.func, home_page) - +#"" %csrf_string def test_home_page_returns_correct_html(self): request = HttpRequest() # response = home_page(request) # @@ -19,17 +23,57 @@ class HomePageTest(TestCase): #self.assertTrue(response.content.startswith(b'')) # #self.assertTrue(response.content.strip().endswith(b'')) # - #def test_home_page_returns_correct_html(self): - # request = HttpRequest() # - # response = home_page(request) # - # self.assertTrue(response.content.startswith(b'')) # - # self.assertIn(b'Rumah', response.content) # - # self.assertIn(b'

Jordan Muhammad Andrianda

', response.content) # - # self.assertIn(b'

1506722765

', response.content) # - # self.assertTrue(response.content.endswith(b'')) # + def test_home_page_can_save_a_POST_request(self): + request = HttpRequest() + request.method = 'POST' + request.POST['item_text'] = 'A new list item' + + response = home_page(request) + + self.assertEqual(Item.objects.count(), 1) + new_item = Item.objects.first() + self.assertEqual(new_item.text, 'A new list item') + + def test_home_page_redirects_after_POST(self): + request = HttpRequest() + request.method = 'POST' + request.POST['item_text'] = 'A new list item' + response = home_page(request) + + self.assertEqual(response.status_code, 302) + self.assertEqual(response['location'], '/') + + + def test_home_page_only_saves_items_when_necessary(self): + request = HttpRequest() + home_page(request) + self.assertEqual(Item.objects.count(), 0) + + def test_home_page_displays_all_list_items(self): + Item.objects.create(text='itemey 1') + Item.objects.create(text='itemey 2') + + request = HttpRequest() + response = home_page(request) + + self.assertIn('itemey 1', response.content.decode()) + self.assertIn('itemey 2', response.content.decode()) + +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) -# class SmokeTest(TestCase): -# def test_bad_maths(self): -# self.assertEqual(1 + 1, 3) + 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') diff --git a/lists/views.py b/lists/views.py index 8410f5b25f2e3a777b30098a3f3f69718b6fb11f..ff3897544a45f44d48de4f4c9d145ecc32ac95aa 100644 --- a/lists/views.py +++ b/lists/views.py @@ -1,6 +1,13 @@ -from django.shortcuts import render -from django.http import HttpResponse +from django.shortcuts import redirect, render +from lists.models import Item -def home_page(request): +#def home_page(request): #return HttpResponse('Rumah

Jordan Muhammad Andrianda

1506722765

') - return render(request,'home.html') + +def home_page(request): + if request.method == 'POST': + Item.objects.create(text=request.POST['item_text']) + return redirect('/') + items = Item.objects.all() + return render(request, 'home.html', {'items': items}) +