├── pages
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── admin.py
├── apps.py
├── urls.py
├── views.py
└── tests.py
├── posts
├── __init__.py
├── migrations
│ ├── __init__.py
│ └── 0001_initial.py
├── apps.py
├── admin.py
├── urls.py
├── views.py
├── models.py
└── tests.py
├── myproject
├── __init__.py
├── urls.py
├── wsgi.py
└── settings.py
├── templates
├── home.html
├── about.html
└── posts.html
├── db.sqlite3
├── README.md
├── Pipfile
├── manage.py
└── Pipfile.lock
/pages/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/posts/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/myproject/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/pages/migrations/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/posts/migrations/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/templates/home.html:
--------------------------------------------------------------------------------
1 |
Homepage
--------------------------------------------------------------------------------
/templates/about.html:
--------------------------------------------------------------------------------
1 | About page
--------------------------------------------------------------------------------
/pages/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 |
3 | # Create your models here.
4 |
--------------------------------------------------------------------------------
/db.sqlite3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wsvincent/django-testing-tutorial/HEAD/db.sqlite3
--------------------------------------------------------------------------------
/pages/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 |
3 | # Register your models here.
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Source code
2 |
3 | For [Django Testing Tutorial](https://wsvincent.com/django-testing-tutorial).
4 |
--------------------------------------------------------------------------------
/pages/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class PagesConfig(AppConfig):
5 | name = 'pages'
6 |
--------------------------------------------------------------------------------
/posts/apps.py:
--------------------------------------------------------------------------------
1 | from django.apps import AppConfig
2 |
3 |
4 | class PostsConfig(AppConfig):
5 | name = 'posts'
6 |
--------------------------------------------------------------------------------
/posts/admin.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 |
3 | from .models import Post
4 |
5 | admin.site.register(Post)
6 |
--------------------------------------------------------------------------------
/templates/posts.html:
--------------------------------------------------------------------------------
1 | Message board homepage
2 |
3 | {% for post in object_list %}
4 | - {{ post.text }}
5 | {% endfor %}
6 |
--------------------------------------------------------------------------------
/Pipfile:
--------------------------------------------------------------------------------
1 | [[source]]
2 | url = "https://pypi.org/simple"
3 | verify_ssl = true
4 | name = "pypi"
5 |
6 | [packages]
7 | django = "*"
8 |
9 | [dev-packages]
10 |
--------------------------------------------------------------------------------
/posts/urls.py:
--------------------------------------------------------------------------------
1 | from django.urls import path
2 |
3 | from .views import PostPageView
4 |
5 | urlpatterns = [
6 | path('', PostPageView.as_view(), name='posts'),
7 | ]
8 |
--------------------------------------------------------------------------------
/posts/views.py:
--------------------------------------------------------------------------------
1 | from django.views.generic import ListView
2 | from .models import Post
3 |
4 |
5 | class PostPageView(ListView):
6 | model = Post
7 | template_name = 'posts.html'
8 |
--------------------------------------------------------------------------------
/pages/urls.py:
--------------------------------------------------------------------------------
1 | from django.urls import path
2 |
3 | from .views import HomePageView, AboutPageView
4 |
5 | urlpatterns = [
6 | path('', HomePageView.as_view(), name='home'),
7 | path('about/', AboutPageView.as_view(), name='about'),
8 | ]
--------------------------------------------------------------------------------
/pages/views.py:
--------------------------------------------------------------------------------
1 | from django.views.generic import TemplateView
2 |
3 |
4 | class HomePageView(TemplateView):
5 | template_name = 'home.html'
6 |
7 |
8 | class AboutPageView(TemplateView):
9 | template_name = 'about.html'
10 |
--------------------------------------------------------------------------------
/posts/models.py:
--------------------------------------------------------------------------------
1 | from django.db import models
2 |
3 |
4 | class Post(models.Model):
5 | text = models.TextField()
6 |
7 | def __str__(self):
8 | """A string representation of the model."""
9 | return self.text
10 |
--------------------------------------------------------------------------------
/myproject/urls.py:
--------------------------------------------------------------------------------
1 | from django.contrib import admin
2 | from django.urls import path, include
3 |
4 | urlpatterns = [
5 | path('', include('pages.urls')),
6 | path('admin/', admin.site.urls),
7 | path('posts/', include('posts.urls')),
8 | ]
9 |
--------------------------------------------------------------------------------
/myproject/wsgi.py:
--------------------------------------------------------------------------------
1 | """
2 | WSGI config for myproject project.
3 |
4 | It exposes the WSGI callable as a module-level variable named ``application``.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
8 | """
9 |
10 | import os
11 |
12 | from django.core.wsgi import get_wsgi_application
13 |
14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
15 |
16 | application = get_wsgi_application()
17 |
--------------------------------------------------------------------------------
/posts/migrations/0001_initial.py:
--------------------------------------------------------------------------------
1 | # Generated by Django 2.0.5 on 2018-05-07 13:26
2 |
3 | from django.db import migrations, models
4 |
5 |
6 | class Migration(migrations.Migration):
7 |
8 | initial = True
9 |
10 | dependencies = [
11 | ]
12 |
13 | operations = [
14 | migrations.CreateModel(
15 | name='Post',
16 | fields=[
17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18 | ('text', models.TextField()),
19 | ],
20 | ),
21 | ]
22 |
--------------------------------------------------------------------------------
/manage.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import os
3 | import sys
4 |
5 | if __name__ == "__main__":
6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
7 | try:
8 | from django.core.management import execute_from_command_line
9 | except ImportError as exc:
10 | raise ImportError(
11 | "Couldn't import Django. Are you sure it's installed and "
12 | "available on your PYTHONPATH environment variable? Did you "
13 | "forget to activate a virtual environment?"
14 | ) from exc
15 | execute_from_command_line(sys.argv)
16 |
--------------------------------------------------------------------------------
/posts/tests.py:
--------------------------------------------------------------------------------
1 | from django.test import TestCase
2 | from django.urls import reverse
3 |
4 | from .models import Post
5 |
6 |
7 | class PostTests(TestCase):
8 |
9 | def setUp(self):
10 | Post.objects.create(text='just a test')
11 |
12 | def test_text_content(self):
13 | post = Post.objects.get(id=1)
14 | expected_object_name = f'{post.text}'
15 | self.assertEquals(expected_object_name, 'just a test')
16 |
17 | def test_post_list_view(self):
18 | response = self.client.get(reverse('posts'))
19 | self.assertEqual(response.status_code, 200)
20 | self.assertContains(response, 'just a test')
21 | self.assertTemplateUsed(response, 'posts.html')
22 |
--------------------------------------------------------------------------------
/Pipfile.lock:
--------------------------------------------------------------------------------
1 | {
2 | "_meta": {
3 | "hash": {
4 | "sha256": "e34336994c9a9a3f014e74492b8c604374a11b34db7a5fc7b9dcb2e1bdc5b79e"
5 | },
6 | "pipfile-spec": 6,
7 | "requires": {},
8 | "sources": [
9 | {
10 | "name": "pypi",
11 | "url": "https://pypi.org/simple",
12 | "verify_ssl": true
13 | }
14 | ]
15 | },
16 | "default": {
17 | "django": {
18 | "hashes": [
19 | "sha256:1ffab268ada3d5684c05ba7ce776eaeedef360712358d6a6b340ae9f16486916",
20 | "sha256:dd46d87af4c1bf54f4c926c3cfa41dc2b5c15782f15e4329752ce65f5dad1c37"
21 | ],
22 | "index": "pypi",
23 | "version": "==2.1.3"
24 | },
25 | "pytz": {
26 | "hashes": [
27 | "sha256:31cb35c89bd7d333cd32c5f278fca91b523b0834369e757f4c5641ea252236ca",
28 | "sha256:8e0f8568c118d3077b46be7d654cc8167fa916092e28320cde048e54bfc9f1e6"
29 | ],
30 | "version": "==2018.7"
31 | }
32 | },
33 | "develop": {}
34 | }
35 |
--------------------------------------------------------------------------------
/pages/tests.py:
--------------------------------------------------------------------------------
1 | from django.http import HttpRequest
2 | from django.test import SimpleTestCase
3 | from django.urls import reverse
4 |
5 | from . import views
6 |
7 |
8 | class HomePageTests(SimpleTestCase):
9 |
10 | def test_home_page_status_code(self):
11 | response = self.client.get('/')
12 | self.assertEquals(response.status_code, 200)
13 |
14 | def test_view_url_by_name(self):
15 | response = self.client.get(reverse('home'))
16 | self.assertEquals(response.status_code, 200)
17 |
18 | def test_view_uses_correct_template(self):
19 | response = self.client.get(reverse('home'))
20 | self.assertEquals(response.status_code, 200)
21 | self.assertTemplateUsed(response, 'home.html')
22 |
23 | def test_home_page_contains_correct_html(self):
24 | response = self.client.get('/')
25 | self.assertContains(response, 'Homepage
')
26 |
27 | def test_home_page_does_not_contain_incorrect_html(self):
28 | response = self.client.get('/')
29 | self.assertNotContains(
30 | response, 'Hi there! I should not be on the page.')
31 |
32 |
33 | class AboutPageTests(SimpleTestCase):
34 |
35 | def test_about_page_status_code(self):
36 | response = self.client.get('/about/')
37 | self.assertEquals(response.status_code, 200)
38 |
39 | def test_view_url_by_name(self):
40 | response = self.client.get(reverse('about'))
41 | self.assertEquals(response.status_code, 200)
42 |
43 | def test_view_uses_correct_template(self):
44 | response = self.client.get(reverse('about'))
45 | self.assertEquals(response.status_code, 200)
46 | self.assertTemplateUsed(response, 'about.html')
47 |
48 | def test_about_page_contains_correct_html(self):
49 | response = self.client.get('/about/')
50 | self.assertContains(response, 'About page
')
51 |
52 | def test_about_page_does_not_contain_incorrect_html(self):
53 | response = self.client.get('/')
54 | self.assertNotContains(
55 | response, 'Hi there! I should not be on the page.')
56 |
--------------------------------------------------------------------------------
/myproject/settings.py:
--------------------------------------------------------------------------------
1 | """
2 | Django settings for myproject project.
3 |
4 | Generated by 'django-admin startproject' using Django 2.0.1.
5 |
6 | For more information on this file, see
7 | https://docs.djangoproject.com/en/2.0/topics/settings/
8 |
9 | For the full list of settings and their values, see
10 | https://docs.djangoproject.com/en/2.0/ref/settings/
11 | """
12 |
13 | import os
14 |
15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 |
18 |
19 | # Quick-start development settings - unsuitable for production
20 | # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21 |
22 | # SECURITY WARNING: keep the secret key used in production secret!
23 | SECRET_KEY = 'q^yjpb!z-^5de=foku(-%k=67a8th&u3w6%p=i8n21_2i#rt'
24 |
25 | # SECURITY WARNING: don't run with debug turned on in production!
26 | DEBUG = True
27 |
28 | ALLOWED_HOSTS = []
29 |
30 |
31 | # Application definition
32 |
33 | INSTALLED_APPS = [
34 | 'django.contrib.admin',
35 | 'django.contrib.auth',
36 | 'django.contrib.contenttypes',
37 | 'django.contrib.sessions',
38 | 'django.contrib.messages',
39 | 'django.contrib.staticfiles',
40 | 'pages.apps.PagesConfig',
41 | 'posts.apps.PostsConfig',
42 | ]
43 |
44 | MIDDLEWARE = [
45 | 'django.middleware.security.SecurityMiddleware',
46 | 'django.contrib.sessions.middleware.SessionMiddleware',
47 | 'django.middleware.common.CommonMiddleware',
48 | 'django.middleware.csrf.CsrfViewMiddleware',
49 | 'django.contrib.auth.middleware.AuthenticationMiddleware',
50 | 'django.contrib.messages.middleware.MessageMiddleware',
51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware',
52 | ]
53 |
54 | ROOT_URLCONF = 'myproject.urls'
55 |
56 | TEMPLATES = [
57 | {
58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates',
59 | 'DIRS': [os.path.join(BASE_DIR, 'templates')],
60 | 'APP_DIRS': True,
61 | 'OPTIONS': {
62 | 'context_processors': [
63 | 'django.template.context_processors.debug',
64 | 'django.template.context_processors.request',
65 | 'django.contrib.auth.context_processors.auth',
66 | 'django.contrib.messages.context_processors.messages',
67 | ],
68 | },
69 | },
70 | ]
71 |
72 | WSGI_APPLICATION = 'myproject.wsgi.application'
73 |
74 |
75 | # Database
76 | # https://docs.djangoproject.com/en/2.0/ref/settings/#databases
77 |
78 | DATABASES = {
79 | 'default': {
80 | 'ENGINE': 'django.db.backends.sqlite3',
81 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
82 | }
83 | }
84 |
85 |
86 | # Password validation
87 | # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
88 |
89 | AUTH_PASSWORD_VALIDATORS = [
90 | {
91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
92 | },
93 | {
94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
95 | },
96 | {
97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
98 | },
99 | {
100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
101 | },
102 | ]
103 |
104 |
105 | # Internationalization
106 | # https://docs.djangoproject.com/en/2.0/topics/i18n/
107 |
108 | LANGUAGE_CODE = 'en-us'
109 |
110 | TIME_ZONE = 'UTC'
111 |
112 | USE_I18N = True
113 |
114 | USE_L10N = True
115 |
116 | USE_TZ = True
117 |
118 |
119 | # Static files (CSS, JavaScript, Images)
120 | # https://docs.djangoproject.com/en/2.0/howto/static-files/
121 |
122 | STATIC_URL = '/static/'
123 |
--------------------------------------------------------------------------------