Unit Test and named urls
This commit is contained in:
@@ -3,6 +3,6 @@ from .views import manifest, service_worker
|
|||||||
|
|
||||||
# Serve up serviceworker.js and manifest.json at the root
|
# Serve up serviceworker.js and manifest.json at the root
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url('^serviceworker.js$', service_worker),
|
url('^serviceworker.js$', service_worker, name='serviceworker'),
|
||||||
url('^manifest.json$', manifest)
|
url('^manifest.json$', manifest, name='manifest')
|
||||||
]
|
]
|
||||||
|
|||||||
14
runtests.py
Normal file
14
runtests.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import django
|
||||||
|
from django.conf import settings
|
||||||
|
from django.test.utils import get_runner
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
|
||||||
|
django.setup()
|
||||||
|
TestRunner = get_runner(settings)
|
||||||
|
test_runner = TestRunner()
|
||||||
|
failures = test_runner.run_tests(["tests"])
|
||||||
|
sys.exit(bool(failures))
|
||||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
36
tests/settings.py
Normal file
36
tests/settings.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
SECRET_KEY = 'fake-key'
|
||||||
|
INSTALLED_APPS = [
|
||||||
|
'django.contrib.admin',
|
||||||
|
'django.contrib.auth',
|
||||||
|
'django.contrib.contenttypes',
|
||||||
|
'django.contrib.sessions',
|
||||||
|
'django.contrib.messages',
|
||||||
|
'django.contrib.staticfiles',
|
||||||
|
"pwa",
|
||||||
|
]
|
||||||
|
|
||||||
|
ROOT_URLCONF = 'pwa.urls'
|
||||||
|
|
||||||
|
TEMPLATES = [
|
||||||
|
{
|
||||||
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
|
'DIRS': [],
|
||||||
|
'APP_DIRS': True,
|
||||||
|
'OPTIONS': {
|
||||||
|
'context_processors': [
|
||||||
|
'django.template.context_processors.debug',
|
||||||
|
'django.template.context_processors.request',
|
||||||
|
'django.contrib.auth.context_processors.auth',
|
||||||
|
'django.contrib.messages.context_processors.messages',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
DATABASES = {
|
||||||
|
'default': {
|
||||||
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
|
'NAME': 'mydatabase',
|
||||||
|
}
|
||||||
|
}
|
||||||
21
tests/test_settings_attr.py
Normal file
21
tests/test_settings_attr.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from pwa import app_settings
|
||||||
|
|
||||||
|
|
||||||
|
class AppSettingsTest(TestCase):
|
||||||
|
def test_has_defined(self):
|
||||||
|
"""Must have the attributes defined in app_settings.py"""
|
||||||
|
attributes = [
|
||||||
|
'PWA_SERVICE_WORKER_PATH',
|
||||||
|
'PWA_APP_NAME',
|
||||||
|
'PWA_APP_DESCRIPTION',
|
||||||
|
'PWA_APP_ROOT_URL',
|
||||||
|
'PWA_APP_THEME_COLOR',
|
||||||
|
'PWA_APP_BACKGROUND_COLOR',
|
||||||
|
'PWA_APP_DISPLAY',
|
||||||
|
'PWA_APP_START_URL',
|
||||||
|
'PWA_APP_ICONS'
|
||||||
|
]
|
||||||
|
for attr in attributes:
|
||||||
|
with self.subTest():
|
||||||
|
self.assertTrue(hasattr(app_settings, attr))
|
||||||
45
tests/test_template_tag_meta.py
Normal file
45
tests/test_template_tag_meta.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from django.template import Context, Template
|
||||||
|
|
||||||
|
|
||||||
|
class CreateMetaTemplateTagTest(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
context = Context({})
|
||||||
|
template_to_render = Template(
|
||||||
|
'{% load pwa %}'
|
||||||
|
'{% progressive_web_app_meta %}'
|
||||||
|
)
|
||||||
|
self.rendered_template = template_to_render.render(context)
|
||||||
|
|
||||||
|
def test_has_tags(self):
|
||||||
|
"""Must contains the tags in HTML"""
|
||||||
|
tags = [
|
||||||
|
'<link rel="apple-touch-icon" href="/" sizes="160x160">',
|
||||||
|
'<link rel="manifest" href="/manifest.json">',
|
||||||
|
'<meta name="theme-color" content="#000">',
|
||||||
|
'<meta name="apple-mobile-web-app-capable" content="yes">',
|
||||||
|
'<meta name="apple-mobile-web-app-title" content="MyApp">',
|
||||||
|
'<meta name="apple-mobile-web-app-status-bar-style" content="default">'
|
||||||
|
]
|
||||||
|
for text in tags:
|
||||||
|
with self.subTest():
|
||||||
|
self.assertInHTML(text, self.rendered_template)
|
||||||
|
|
||||||
|
def test_has_serviceworker(self):
|
||||||
|
"""Must have the script tag with serviceworker registration"""
|
||||||
|
contents = [
|
||||||
|
'<script type="text/javascript">',
|
||||||
|
"if ('serviceWorker' in navigator) {",
|
||||||
|
"navigator.serviceWorker.register('/serviceworker.js', {",
|
||||||
|
"scope: '.'",
|
||||||
|
"}).then(function (registration) {",
|
||||||
|
"console.log('django-progressive-web-app: ServiceWorker registration successful with scope: ', registration.scope);",
|
||||||
|
"}, function (err) {",
|
||||||
|
"console.log('django-progressive-web-app: ServiceWorker registration failed: ', err);",
|
||||||
|
"});",
|
||||||
|
"</script>"
|
||||||
|
]
|
||||||
|
|
||||||
|
for expected in contents:
|
||||||
|
with self.subTest():
|
||||||
|
self.assertIn(expected, self.rendered_template)
|
||||||
40
tests/test_view.py
Normal file
40
tests/test_view.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from django.shortcuts import resolve_url as r
|
||||||
|
|
||||||
|
|
||||||
|
class ServiceWorkerTest(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.response = self.client.get(r('serviceworker'))
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
"""GET /serviceworker.js Should return status code 200"""
|
||||||
|
self.assertEqual(200, self.response.status_code)
|
||||||
|
|
||||||
|
|
||||||
|
class ManifestTest(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.response = self.client.get(r('manifest'), format='json')
|
||||||
|
|
||||||
|
def test_get(self):
|
||||||
|
"""GET /manifest.json Should return status code 200"""
|
||||||
|
self.assertEqual(self.response.status_code, 200)
|
||||||
|
|
||||||
|
def test_template(self):
|
||||||
|
"""Must have the template manifest.json"""
|
||||||
|
self.assertTemplateUsed(self.response, 'manifest.json')
|
||||||
|
|
||||||
|
def test_manifest_contains(self):
|
||||||
|
"""Must be the attributes to manitesf.json"""
|
||||||
|
contents = [
|
||||||
|
'"name":',
|
||||||
|
'"short_name":',
|
||||||
|
'"description":',
|
||||||
|
'"start_url":',
|
||||||
|
'"display":',
|
||||||
|
'"background_color":',
|
||||||
|
'"theme_color":',
|
||||||
|
'"icons":',
|
||||||
|
]
|
||||||
|
for expected in contents:
|
||||||
|
with self.subTest():
|
||||||
|
self.assertContains(self.response, expected)
|
||||||
Reference in New Issue
Block a user