From 5e2f64346d46dad6b1cca1a5c3f6850ada129f4b Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Fri, 6 Nov 2020 12:34:46 +1100 Subject: [PATCH] Finished up to 3.5 --- .gitmodules | 3 +++ python-tdd-book-src | 1 + textbook/chap3.md | 59 ++++++++++++++++++++++++++++++++++++++++++--- textbook/summary.md | 6 +++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 .gitmodules create mode 160000 python-tdd-book-src create mode 100644 textbook/summary.md diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..40215dd --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "src"] + path = python-tdd-book-src + url = ssh://git@lj918plus.dsmynas.com:2223/Jason/python-tdd-book-src.git diff --git a/python-tdd-book-src b/python-tdd-book-src new file mode 160000 index 0000000..76a8bf8 --- /dev/null +++ b/python-tdd-book-src @@ -0,0 +1 @@ +Subproject commit 76a8bf8aed1c901a3ff6a1f04c743b8230b79f50 diff --git a/textbook/chap3.md b/textbook/chap3.md index 00b60f5..52701ef 100644 --- a/textbook/chap3.md +++ b/textbook/chap3.md @@ -6,7 +6,7 @@ Chap2 introduced writing a functional test using unittest module (expected failu * Django encourage structure the code into apps. We can reuse app developed by us or others -Create an app `lists` (sub-directory) via `python manage.py startapp lists` along other `functional_tests.py` & `manage.py`, this subdir should include `tests.py` be default +Create an app `lists` (sub-directory) via `python manage.py startapp lists` in the directory which contains `functional_tests.py` & `manage.py`, this subdir should include `tests.py` be default ## 3.2 Unit Tests, and How They Differ from Functional Tests @@ -47,11 +47,11 @@ python manage.py test ## 3.4 Django's MVC, URLs, and View Functions -Django is structured along a classic [**Model-View-Controller (MVC)** pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). +Django is structured along classic [**Model-View-Controller (MVC)** pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). Django's main job is to decide what to do when a user asks for a particular URL on our site. -Django's workflow is like: +Django's workflow: 1. An HTTP `request` comes in for a particular URL 2. *Resolving the URL*: Django uses some rules to decide which `view` function should deal with the request. @@ -62,9 +62,33 @@ Let's design a tests: 1. Verify we can resolve the URL for the root of site ("/") to a particular view function we've made? 2. Verify view function return some HTML which will get the functional test to pass? +```python +from django.test import TestCase +from django.urls import resolve +from lists.views import home_page + +class HomePageTest(TestCase): + + def test_root_url_resolves_to_home_page_view(self): + found = resolve('/') + self.assertEqual(found.func, home_page) +``` + +Where: +* The resolve() function if `django.url` package can be used for resolving URL paths to the corresponding view functions. The function returns a [`ResolverMatch` object](https://docs.djangoproject.com/en/3.1/ref/urlresolvers/#django.urls.ResolverMatch) that allows you to access various metadata about the resolved URL. +* In `assertEqual(...)`, we try to test that `found.func` is `home_page` (a view function we haven't defined yet) + +Result of running test: + +``` +ImportError: cannot import name 'home_page' +``` + ## 3.5 At Last! We Actually Write Some Application Code! -Add content in `lists/views.py`, to pass the test +In 3.4, we created a `test_root_url_resolves_to_home_page_view()` test case that check "django can resolve request to root and return a view function" + +Hence, add content in `lists/views.py`, we can pass the test ```python from django.shortcuts import render @@ -72,6 +96,33 @@ from django.shortcuts import render home_page = None ``` +Running the test generate following info + +``` +Creating test database for alias 'default'... +System check identified no issues (0 silenced). +E +====================================================================== +ERROR: test_root_url_resolves_to_home_page_view (lists.tests.HomePageTest) +---------------------------------------------------------------------- +Traceback (most recent call last): + File "/home/jason/HomeWorkstation/SynologyGiteaSpace/python-tdd-book-src/src/lists/tests.py", line 8, in test_root_url_resolves_to_home_page_view + found = resolve('/') + File "/home/jason/miniconda3/envs/python-tdd-book/lib/python3.6/site-packages/django/urls/base.py", line 27, in resolve + return get_resolver(urlconf).resolve(path) + File "/home/jason/miniconda3/envs/python-tdd-book/lib/python3.6/site-packages/django/urls/resolvers.py", line 394, in resolve + raise Resolver404({'tried': tried, 'path': new_path}) +django.urls.exceptions.Resolver404: {'tried': [[ (admin:admin) ^admin/>]], 'path': ''} + +---------------------------------------------------------------------- +Ran 1 test in 0.001s +``` + +* error due to `django.urls.exceptions.Resolver404: {'tried': [[ (admin:admin) ^admin/>]], 'path': ''}` +* Happen in `python-tdd-book-src/src/lists/tests.py", line 8, in test_root_url_resolves_to_home_page_view` + +Overall, the traceback can be interpreted as: "when trying to resolve `/`, Django raised a 404 error". i.e. Django can't find a URL mapping for "/" + ## 3.6 urls.py diff --git a/textbook/summary.md b/textbook/summary.md new file mode 100644 index 0000000..4f2e4b5 --- /dev/null +++ b/textbook/summary.md @@ -0,0 +1,6 @@ +# Summary + +Summarize learning, for quick reference + +* Run Django server: `python manage.py runserver` +* If test cases are created by framework, run it via: `python manage.py test` \ No newline at end of file