From 1bb710cd298ff265c482f18c953a248ffbf8847a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 27 Jul 2018 21:37:11 +0100 Subject: [PATCH] Add tests and linter --- Dockerfile | 4 +-- Pipfile | 1 + Pipfile.lock | 39 +++++++++++++++++++++- project/__init__.py | 0 project/common/__init__.py | 0 project/common/blocks.py | 2 +- project/common/context.py | 2 ++ project/common/models.py | 2 +- project/common/tests.py | 68 ++++++++++++++++++++++++++++++++++++++ project/home/models.py | 2 -- project/pages/models.py | 2 +- project/settings.py | 1 - scripts/tests.sh | 14 ++++++++ 13 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 project/__init__.py create mode 100644 project/common/__init__.py create mode 100644 project/common/tests.py create mode 100755 scripts/tests.sh diff --git a/Dockerfile b/Dockerfile index db5275e..ae2eb36 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ WORKDIR /app COPY Pipfile Pipfile COPY Pipfile.lock Pipfile.lock -RUN pipenv install --deploy --system +RUN pipenv install --deploy --system --dev COPY package.json package.json COPY package-lock.json package-lock.json @@ -30,6 +30,6 @@ COPY static/ /app/static/ COPY scripts/ /app/scripts/ COPY templates/ /app/templates/ -RUN ./scripts/build-static.sh +RUN BASE_URL= ./scripts/build-static.sh EXPOSE 8000 diff --git a/Pipfile b/Pipfile index d212930..94bb650 100644 --- a/Pipfile +++ b/Pipfile @@ -20,6 +20,7 @@ wagtail-metadata = "*" [dev-packages] mypy = "*" "flake8" = "*" +coverage = "*" [requires] python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock index f639f0c..fb75949 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6398b48732255dbfe41aeaace62f3f4e09fd282f2521716bdb43d388cee0e928" + "sha256": "2065b3b947b0a7713aba72b55493723789062834628c34bff544e3ef72f184df" }, "pipfile-spec": 6, "requires": { @@ -358,6 +358,43 @@ } }, "develop": { + "coverage": { + "hashes": [ + "sha256:03481e81d558d30d230bc12999e3edffe392d244349a90f4ef9b88425fac74ba", + "sha256:0b136648de27201056c1869a6c0d4e23f464750fd9a9ba9750b8336a244429ed", + "sha256:10a46017fef60e16694a30627319f38a2b9b52e90182dddb6e37dcdab0f4bf95", + "sha256:198626739a79b09fa0a2f06e083ffd12eb55449b5f8bfdbeed1df4910b2ca640", + "sha256:23d341cdd4a0371820eb2b0bd6b88f5003a7438bbedb33688cd33b8eae59affd", + "sha256:28b2191e7283f4f3568962e373b47ef7f0392993bb6660d079c62bd50fe9d162", + "sha256:2a5b73210bad5279ddb558d9a2bfedc7f4bf6ad7f3c988641d83c40293deaec1", + "sha256:2eb564bbf7816a9d68dd3369a510be3327f1c618d2357fa6b1216994c2e3d508", + "sha256:337ded681dd2ef9ca04ef5d93cfc87e52e09db2594c296b4a0a3662cb1b41249", + "sha256:3a2184c6d797a125dca8367878d3b9a178b6fdd05fdc2d35d758c3006a1cd694", + "sha256:3c79a6f7b95751cdebcd9037e4d06f8d5a9b60e4ed0cd231342aa8ad7124882a", + "sha256:3d72c20bd105022d29b14a7d628462ebdc61de2f303322c0212a054352f3b287", + "sha256:3eb42bf89a6be7deb64116dd1cc4b08171734d721e7a7e57ad64cc4ef29ed2f1", + "sha256:4635a184d0bbe537aa185a34193898eee409332a8ccb27eea36f262566585000", + "sha256:56e448f051a201c5ebbaa86a5efd0ca90d327204d8b059ab25ad0f35fbfd79f1", + "sha256:5a13ea7911ff5e1796b6d5e4fbbf6952381a611209b736d48e675c2756f3f74e", + "sha256:69bf008a06b76619d3c3f3b1983f5145c75a305a0fea513aca094cae5c40a8f5", + "sha256:6bc583dc18d5979dc0f6cec26a8603129de0304d5ae1f17e57a12834e7235062", + "sha256:701cd6093d63e6b8ad7009d8a92425428bc4d6e7ab8d75efbb665c806c1d79ba", + "sha256:7608a3dd5d73cb06c531b8925e0ef8d3de31fed2544a7de6c63960a1e73ea4bc", + "sha256:76ecd006d1d8f739430ec50cc872889af1f9c1b6b8f48e29941814b09b0fd3cc", + "sha256:7aa36d2b844a3e4a4b356708d79fd2c260281a7390d678a10b91ca595ddc9e99", + "sha256:7d3f553904b0c5c016d1dad058a7554c7ac4c91a789fca496e7d8347ad040653", + "sha256:7e1fe19bd6dce69d9fd159d8e4a80a8f52101380d5d3a4d374b6d3eae0e5de9c", + "sha256:8c3cb8c35ec4d9506979b4cf90ee9918bc2e49f84189d9bf5c36c0c1119c6558", + "sha256:9d6dd10d49e01571bf6e147d3b505141ffc093a06756c60b053a859cb2128b1f", + "sha256:be6cfcd8053d13f5f5eeb284aa8a814220c3da1b0078fa859011c7fffd86dab9", + "sha256:c1bb572fab8208c400adaf06a8133ac0712179a334c09224fb11393e920abcdd", + "sha256:de4418dadaa1c01d497e539210cb6baa015965526ff5afc078c57ca69160108d", + "sha256:e05cb4d9aad6233d67e0541caa7e511fa4047ed7750ec2510d466e806e0255d6", + "sha256:f3f501f345f24383c0000395b26b726e46758b71393267aeae0bd36f8b3ade80" + ], + "index": "pypi", + "version": "==4.5.1" + }, "flake8": { "hashes": [ "sha256:7253265f7abd8b313e3892944044a365e3f4ac3fcdcfb4298f55ee9ddf188ba0", diff --git a/project/__init__.py b/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/common/__init__.py b/project/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/common/blocks.py b/project/common/blocks.py index 15b4b45..148aa7d 100644 --- a/project/common/blocks.py +++ b/project/common/blocks.py @@ -36,4 +36,4 @@ def build_stream_field(): ('raw_html', blocks.RawHTMLBlock(label="Raw HTML")), ('ul', blocks.ListBlock(blocks.CharBlock(label="List Item"), icon="list-ul", label="Unordered List")), ('video', VideoBlock()) -]) + ]) diff --git a/project/common/context.py b/project/common/context.py index 32e1fc1..8205719 100644 --- a/project/common/context.py +++ b/project/common/context.py @@ -1,6 +1,7 @@ from django.conf import settings from project.home.models import HomePage + SETTINGS_KEYS = [ 'BASE_URL', 'STATIC_URL', @@ -17,6 +18,7 @@ def settings_injector(request): setting: getattr(settings, setting) for setting in SETTINGS_KEYS }} + def get_home_page(request): return { 'homepage': HomePage.objects.live().first() diff --git a/project/common/models.py b/project/common/models.py index 9ded08f..64c7d85 100644 --- a/project/common/models.py +++ b/project/common/models.py @@ -36,7 +36,7 @@ class Entity(MetadataPageMixin, Page): def get_short_body(self): body_words = BeautifulSoup(str(self.body), 'html5lib').get_text().split(' ') ending = '...' if len(body_words) > SHORT_BODY_LENGTH else '' - return ' '.join(body_words[:SHORT_BODY_LENGTH]) + ending # limit to 30 words (ish) + return ' '.join(body_words[:SHORT_BODY_LENGTH]) + ending # limit to 30 words (ish) class Meta: abstract = True diff --git a/project/common/tests.py b/project/common/tests.py new file mode 100644 index 0000000..3b30e45 --- /dev/null +++ b/project/common/tests.py @@ -0,0 +1,68 @@ +from wagtail.tests.utils import WagtailPageTests +from .context import SETTINGS_KEYS +from django.conf import settings +from project.home.models import HomePage +from wagtail.core.models import Site, Page +from django.urls import reverse +from rest_framework.test import APIClient +from django.utils.text import slugify +from bs4 import BeautifulSoup + + +class BaseTestCase(WagtailPageTests): + client = APIClient() + + def setUp(self): + super().setUp() + + self.root = self.create_initial_homepage() + + def create_model(self, model, data={}): + add_url = reverse('wagtailadmin_pages:add', args=[ + model._meta.app_label, model._meta.model_name, self.root.pk + ]) + data.update({ + 'action-publish': 'action-publish', + 'body-count': 1, + 'body-0-deleted': '', + 'body-0-order': 0, + 'body-0-type': 'raw_html', + 'body-0-value': data['body'], + 'slug': slugify(data['title']) + }) + return self.client.post(add_url, data) + + def create_test_user(self): + self.user = super().create_test_user() + return self.user + + def parse_content(self, content): + return BeautifulSoup(content, 'html.parser') + + def create_initial_homepage(self): + """ + from https://github.com/wagtail/wagtail/blob/master/wagtail/project_template/home/migrations/0002_create_homepage.py + """ + Page.objects.filter(id=2).delete() + HomePage.objects.all().delete() + + # Create a new homepage + homepage = HomePage.objects.create( + title="Homepage", + body="Home Page", + slug='home', + path='00010001', + depth=2, + numchild=0, + url_path='/', + ) + Site.objects.create(hostname='localhost', root_page=homepage, is_default_site=True) + return homepage + + +class ContextInjectorTestCase(BaseTestCase): + def test_has_keys(self): + response = self.client.get('/') + for key in SETTINGS_KEYS: + self.assertIn(key, response.context['settings']) + self.assertEqual(response.context['settings'][key], getattr(settings, key)) diff --git a/project/home/models.py b/project/home/models.py index 2a13760..ed8f237 100644 --- a/project/home/models.py +++ b/project/home/models.py @@ -1,5 +1,3 @@ -from wagtail.core.models import Page -from wagtail.core.fields import RichTextField from wagtail.admin.edit_handlers import FieldPanel from project.common.models import Entity from django.db import models diff --git a/project/pages/models.py b/project/pages/models.py index 9ad2f77..2fae351 100644 --- a/project/pages/models.py +++ b/project/pages/models.py @@ -19,4 +19,4 @@ class SectionIndexPage(Entity): content_panels = Entity.content_panels + [ StreamFieldPanel('body'), FieldPanel('hide_list') -] + ] diff --git a/project/settings.py b/project/settings.py index 4b3191d..7f9c3e6 100644 --- a/project/settings.py +++ b/project/settings.py @@ -119,7 +119,6 @@ if not DEBUG: del TEMPLATES[0]['APP_DIRS'] - WSGI_APPLICATION = 'project.wsgi.application' diff --git a/scripts/tests.sh b/scripts/tests.sh new file mode 100755 index 0000000..274b624 --- /dev/null +++ b/scripts/tests.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e + +source ./scripts/common.sh + +export IN_TEST=true + +echo ">> Running flake8..." +flake8 project --ignore=E128,E501 --exclude=migrations,wsgi.py + +echo ">> Running backend unit tests..." +coverage run --source=project --omit='*/wsgi.py,*/settings.py,*/migrations/*.py,*__init__.py' manage.py test $@ +coverage report