From 500a24ada1919e964af33496b7b9a24566d0c5b9 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 20 Feb 2014 21:04:12 +0000 Subject: [PATCH 1/8] Added test utils --- wagtail/{wagtailsearch => }/tests/utils.py | 4 +-- wagtail/wagtaildocs/tests.py | 27 ++++++------------- wagtail/wagtailembeds/tests.py | 18 +++---------- wagtail/wagtailimages/tests.py | 27 ++++++------------- .../wagtailsearch/tests/test_editorspicks.py | 10 +++---- wagtail/wagtailsearch/tests/test_frontend.py | 6 ++--- wagtail/wagtailsearch/tests/test_queries.py | 4 +-- 7 files changed, 32 insertions(+), 64 deletions(-) rename wagtail/{wagtailsearch => }/tests/utils.py (80%) diff --git a/wagtail/wagtailsearch/tests/utils.py b/wagtail/tests/utils.py similarity index 80% rename from wagtail/wagtailsearch/tests/utils.py rename to wagtail/tests/utils.py index 05a122f99f..08d5c0aec7 100644 --- a/wagtail/wagtailsearch/tests/utils.py +++ b/wagtail/tests/utils.py @@ -2,7 +2,7 @@ from wagtail.wagtailcore.models import Site from django.contrib.auth.models import User -def get_default_host(): +def get_host(): return Site.objects.filter(is_default_site=True).first().root_url.split('://')[1] @@ -11,4 +11,4 @@ def login(client): User.objects.create_superuser(username='test', email='test@email.com', password='password') # Login - client.login(username='test', password='password') + client.login(username='test', password='password') \ No newline at end of file diff --git a/wagtail/wagtaildocs/tests.py b/wagtail/wagtaildocs/tests.py index 1a8079aad2..172ebaa60d 100644 --- a/wagtail/wagtaildocs/tests.py +++ b/wagtail/wagtaildocs/tests.py @@ -1,6 +1,6 @@ from django.test import TestCase from wagtail.wagtaildocs import models -from wagtail.wagtailcore.models import Site +from wagtail.tests.utils import login, get_host from django.contrib.auth.models import User, Group, Permission from django.core.urlresolvers import reverse @@ -37,24 +37,13 @@ class TestDocumentPermissions(TestCase): ## ===== ADMIN VIEWS ===== -def get_default_host(): - return Site.objects.filter(is_default_site=True).first().root_url.split('://')[1] - - -def login(client): - # Create a user - User.objects.create_superuser(username='test', email='test@email.com', password='password') - - # Login - client.login(username='test', password='password') - class TestDocumentIndexView(TestCase): def setUp(self): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtaildocs_index'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_index'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -82,7 +71,7 @@ class TestDocumentAddView(TestCase): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtaildocs_add_document'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_add_document'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -96,7 +85,7 @@ class TestDocumentEditView(TestCase): self.document = models.Document.objects.create(title="Test document") def get(self, params={}): - return self.client.get(reverse('wagtaildocs_edit_document', args=(self.document.id,)), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_edit_document', args=(self.document.id,)), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -110,7 +99,7 @@ class TestDocumentDeleteView(TestCase): self.document = models.Document.objects.create(title="Test document") def get(self, params={}): - return self.client.get(reverse('wagtaildocs_delete_document', args=(self.document.id,)), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_delete_document', args=(self.document.id,)), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -121,7 +110,7 @@ class TestDocumentChooserView(TestCase): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtaildocs_chooser'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_chooser'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -146,7 +135,7 @@ class TestDocumentChooserChosenView(TestCase): self.document = models.Document.objects.create(title="Test document") def get(self, params={}): - return self.client.get(reverse('wagtaildocs_document_chosen', args=(self.document.id,)), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_document_chosen', args=(self.document.id,)), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -157,7 +146,7 @@ class TestDocumentChooserUploadView(TestCase): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtaildocs_chooser_upload'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtaildocs_chooser_upload'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) diff --git a/wagtail/wagtailembeds/tests.py b/wagtail/wagtailembeds/tests.py index c5c74f7c15..c21737aae8 100644 --- a/wagtail/wagtailembeds/tests.py +++ b/wagtail/wagtailembeds/tests.py @@ -1,5 +1,6 @@ from django.test import TestCase from django.test.client import Client +from wagtail.tests.utils import login, get_host from wagtail.wagtailembeds import get_embed @@ -45,24 +46,13 @@ class TestEmbeds(TestCase): } -def get_default_host(): - from wagtail.wagtailcore.models import Site - return Site.objects.filter(is_default_site=True).first().root_url.split('://')[1] - - class TestChooser(TestCase): def setUp(self): - # Create a user - from django.contrib.auth.models import User - User.objects.create_superuser(username='test', email='test@email.com', password='password') - - # Setup client - self.c = Client() - login = self.c.login(username='test', password='password') - self.assertEqual(login, True) + # login + login(self.client) def test_chooser(self): - r = self.c.get('/admin/embeds/chooser/', HTTP_HOST=get_default_host()) + r = self.client.get('/admin/embeds/chooser/', HTTP_HOST=get_host()) self.assertEqual(r.status_code, 200) # TODO: Test submitting diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py index 1c35f8af4b..42e26981b4 100644 --- a/wagtail/wagtailimages/tests.py +++ b/wagtail/wagtailimages/tests.py @@ -2,7 +2,7 @@ from django.test import TestCase from django import template from django.contrib.auth.models import User, Group, Permission from django.core.urlresolvers import reverse -from wagtail.wagtailcore.models import Site +from wagtail.tests.utils import login, get_host from wagtail.wagtailimages.models import get_image_model from wagtail.wagtailimages.templatetags import image_tags @@ -123,24 +123,13 @@ class TestImageTag(TestCase): ## ===== ADMIN VIEWS ===== -def get_default_host(): - return Site.objects.filter(is_default_site=True).first().root_url.split('://')[1] - - -def login(client): - # Create a user - User.objects.create_superuser(username='test', email='test@email.com', password='password') - - # Login - client.login(username='test', password='password') - class TestImageIndexView(TestCase): def setUp(self): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtailimages_index'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_index'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -168,7 +157,7 @@ class TestImageAddView(TestCase): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtailimages_add_image'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_add_image'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -185,7 +174,7 @@ class TestImageEditView(TestCase): ) def get(self, params={}): - return self.client.get(reverse('wagtailimages_edit_image', args=(self.image.id,)), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_edit_image', args=(self.image.id,)), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -202,7 +191,7 @@ class TestImageDeleteView(TestCase): ) def get(self, params={}): - return self.client.get(reverse('wagtailimages_delete_image', args=(self.image.id,)), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_delete_image', args=(self.image.id,)), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -213,7 +202,7 @@ class TestImageChooserView(TestCase): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtailimages_chooser'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_chooser'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -241,7 +230,7 @@ class TestImageChooserChosenView(TestCase): ) def get(self, params={}): - return self.client.get(reverse('wagtailimages_image_chosen', args=(self.image.id,)), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_image_chosen', args=(self.image.id,)), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -252,7 +241,7 @@ class TestImageChooserUploadView(TestCase): login(self.client) def get(self, params={}): - return self.client.get(reverse('wagtailimages_chooser_upload'), params, HTTP_HOST=get_default_host()) + return self.client.get(reverse('wagtailimages_chooser_upload'), params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) diff --git a/wagtail/wagtailsearch/tests/test_editorspicks.py b/wagtail/wagtailsearch/tests/test_editorspicks.py index 80c81f7a9c..d3879c6a94 100644 --- a/wagtail/wagtailsearch/tests/test_editorspicks.py +++ b/wagtail/wagtailsearch/tests/test_editorspicks.py @@ -1,5 +1,5 @@ from django.test import TestCase -from .utils import get_default_host, login +from wagtail.tests.utils import login, get_host from wagtail.wagtailsearch import models @@ -50,7 +50,7 @@ class TestEditorsPicksIndexView(TestCase): login(self.client) def get(self, params={}): - return self.client.get('/admin/search/editorspicks/', params, HTTP_HOST=get_default_host()) + return self.client.get('/admin/search/editorspicks/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -72,7 +72,7 @@ class TestEditorsPicksAddView(TestCase): login(self.client) def get(self, params={}): - return self.client.get('/admin/search/editorspicks/add/', params, HTTP_HOST=get_default_host()) + return self.client.get('/admin/search/editorspicks/add/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -87,7 +87,7 @@ class TestEditorsPicksEditView(TestCase): self.query.editors_picks.create(page_id=1, description="Root page") def get(self, params={}): - return self.client.get('/admin/search/editorspicks/' + str(self.query.id) + '/', params, HTTP_HOST=get_default_host()) + return self.client.get('/admin/search/editorspicks/' + str(self.query.id) + '/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -102,7 +102,7 @@ class TestEditorsPicksDeleteView(TestCase): self.query.editors_picks.create(page_id=1, description="Root page") def get(self, params={}): - return self.client.get('/admin/search/editorspicks/' + str(self.query.id) + '/delete/', params, HTTP_HOST=get_default_host()) + return self.client.get('/admin/search/editorspicks/' + str(self.query.id) + '/delete/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) diff --git a/wagtail/wagtailsearch/tests/test_frontend.py b/wagtail/wagtailsearch/tests/test_frontend.py index 4c09f27071..7172e6aa91 100644 --- a/wagtail/wagtailsearch/tests/test_frontend.py +++ b/wagtail/wagtailsearch/tests/test_frontend.py @@ -1,10 +1,10 @@ from django.test import TestCase -from .utils import get_default_host +from wagtail.tests.utils import get_host class TestSearchView(TestCase): def get(self, params={}): - return self.client.get('/search/', params, HTTP_HOST=get_default_host()) + return self.client.get('/search/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) @@ -23,7 +23,7 @@ class TestSearchView(TestCase): class TestSuggestionsView(TestCase): def get(self, params={}): - return self.client.get('/search/suggest/', params, HTTP_HOST=get_default_host()) + return self.client.get('/search/suggest/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) diff --git a/wagtail/wagtailsearch/tests/test_queries.py b/wagtail/wagtailsearch/tests/test_queries.py index d8a4008e33..a2843b2f65 100644 --- a/wagtail/wagtailsearch/tests/test_queries.py +++ b/wagtail/wagtailsearch/tests/test_queries.py @@ -1,7 +1,7 @@ from django.test import TestCase from django.core import management from wagtail.wagtailsearch import models -from .utils import get_default_host, login +from wagtail.tests.utils import login, get_host from StringIO import StringIO import unittest @@ -145,7 +145,7 @@ class TestQueryChooserView(TestCase): login(self.client) def get(self, params={}): - return self.client.get('/admin/search/queries/chooser/', params, HTTP_HOST=get_default_host()) + return self.client.get('/admin/search/queries/chooser/', params, HTTP_HOST=get_host()) def test_status_code(self): self.assertEqual(self.get().status_code, 200) From cca1fc0e529cfdd1b94a81ac0feaf1162def2c0d Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 20 Feb 2014 21:32:51 +0000 Subject: [PATCH 2/8] Added test models --- runtests.py | 1 + wagtail/tests/models.py | 87 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 wagtail/tests/models.py diff --git a/runtests.py b/runtests.py index 0c0427be28..ff59234319 100755 --- a/runtests.py +++ b/runtests.py @@ -61,6 +61,7 @@ if not settings.configured: 'wagtail.wagtailembeds', 'wagtail.wagtailsearch', 'wagtail.wagtailredirects', + 'wagtail.tests', ] ) diff --git a/wagtail/tests/models.py b/wagtail/tests/models.py new file mode 100644 index 0000000000..5757dd2bd7 --- /dev/null +++ b/wagtail/tests/models.py @@ -0,0 +1,87 @@ +from django.db import models +from modelcluster.fields import ParentalKey +from wagtail.wagtailcore.models import Page, Orderable +from wagtail.wagtailcore.fields import RichTextField +from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel, PageChooserPanel +from wagtail.wagtailimages.edit_handlers import ImageChooserPanel +from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel + + +COMMON_PANELS = ( + FieldPanel('slug'), + FieldPanel('seo_title'), + FieldPanel('show_in_menus'), + FieldPanel('search_description'), +) + + +class ChildObject(Orderable): + page = ParentalKey('tests.TestPage', related_name='child_objects') + chosen_page = models.ForeignKey( + 'wagtailcore.Page', + null=True, + blank=True, + related_name='+' + ) + chosen_document = models.ForeignKey( + 'wagtaildocs.Document', + null=True, + blank=True, + related_name='+' + ) + chosen_image = models.ForeignKey( + 'wagtailimages.Image', + null=True, + blank=True, + related_name='+' + ) + + panels = [ + PageChooserPanel('chosen_page'), + DocumentChooserPanel('chosen_document'), + ImageChooserPanel('chosen_image'), + ] + +class TestPage(Page): + value = models.CharField(max_length=255, blank=True) + body = RichTextField(blank=True) + date = models.DateField(null=True, blank=True) + time = models.TimeField(null=True, blank=True) + index_me = models.BooleanField(default=True) + + def object_indexed(self): + return self.index_me + + def calculated_indexed_field(self): + return self.value + + indexed_fields = ('body', ) + +TestPage.content_panels = [ + FieldPanel('title', classname="full title"), + FieldPanel('body', classname="full"), + FieldPanel('value'), + FieldPanel('date'), + FieldPanel('time'), + FieldPanel('index_me'), + InlinePanel(TestPage, 'child_objects', label="Child objects"), +] + +TestPage.promote_panels = [ + MultiFieldPanel([ + FieldPanel('slug'), + FieldPanel('seo_title'), + FieldPanel('show_in_menus'), + FieldPanel('search_description'), + ], "Common page configuration"), +] + + +class DerivedPage(TestPage): + extra_field = models.CharField(max_length=255, blank=True) + + indexed_fields = ('extra_field', ) + + +class NotIndexedPage(Page): + indexed = False From 576eb0830e7a5105b62fb62a7435ea4e35c97ad5 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 20 Feb 2014 21:40:42 +0000 Subject: [PATCH 3/8] Created a few admin tests --- wagtail/wagtailadmin/tests.py | 107 +++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 13 deletions(-) diff --git a/wagtail/wagtailadmin/tests.py b/wagtail/wagtailadmin/tests.py index 501deb776c..11d84b1a43 100644 --- a/wagtail/wagtailadmin/tests.py +++ b/wagtail/wagtailadmin/tests.py @@ -1,16 +1,97 @@ -""" -This file demonstrates writing tests using the unittest module. These will pass -when you run "manage.py test". - -Replace this with more appropriate tests for your application. -""" - from django.test import TestCase +import unittest +from wagtail.tests.models import TestPage +from wagtail.tests.utils import login, get_host +from wagtail.wagtailcore.models import Page +from django.core.urlresolvers import reverse -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) +class TestPageExplorer(TestCase): + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Add child page + self.child_page = TestPage() + self.child_page.title = "Hello world!" + self.child_page.slug = "hello-world" + self.root_page.add_child(self.child_page) + + # Login + login(self.client) + + def test_explore(self): + response = self.client.get(reverse('wagtailadmin_explore', args=(self.root_page.id, )), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) + self.assertEqual(self.root_page, response.context['parent_page']) + self.assertTrue(response.context['pages'].filter(id=self.child_page.id).exists()) + + +class TestPageCreation(TestCase): + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Login + login(self.client) + + def test_select_type(self): + response = self.client.get(reverse('wagtailadmin_pages_select_type'), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) + + @unittest.expectedFailure # For some reason, this returns a 302... + def test_select_location_testpage(self): + response = self.client.get(reverse('wagtailadmin_pages_select_location', args=('tests', 'testpage')), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) + + def test_select_location_nonexistanttype(self): + response = self.client.get(reverse('wagtailadmin_pages_select_location', args=('notanapp', 'notamodel')), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 404) + + def test_select_location_nonpagetype(self): + response = self.client.get(reverse('wagtailadmin_pages_select_location', args=('wagtailimages', 'image')), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 404) + + def test_add_subpage_root(self): + response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(self.root_page.id, )), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) + + def test_add_subpage_nonexistant(self): + response = self.client.get(reverse('wagtailadmin_pages_add_subpage', args=(100000, )), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 404) + + def test_create_testpage_root(self): + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'testpage', self.root_page.id)), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) + + def test_create_testpage_nonexistantparent(self): + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'testpage', 100000)), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 404) + + @unittest.expectedFailure # FIXME: Crashes! + def test_create_nonpagetype(self): + response = self.client.get(reverse('wagtailadmin_pages_create', args=('wagtailimages', 'image', self.root_page.id)), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 404) + + +class TestPageEditDelete(TestCase): + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Add child page + self.child_page = TestPage() + self.child_page.title = "Hello world!" + self.child_page.slug = "hello-world" + self.root_page.add_child(self.child_page) + + # Login + login(self.client) + + def test_edit(self): + response = self.client.get(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) + + def test_delete(self): + response = self.client.get(reverse('wagtailadmin_pages_delete', args=(self.child_page.id, )), HTTP_HOST=get_host()) + self.assertEqual(response.status_code, 200) From 28a77c6071faaa717dff990c06cc12b14858215a Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 20 Feb 2014 21:59:54 +0000 Subject: [PATCH 4/8] Added ElasticSearch backend to tests settings --- runtests.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/runtests.py b/runtests.py index ff59234319..4606994498 100755 --- a/runtests.py +++ b/runtests.py @@ -62,7 +62,16 @@ if not settings.configured: 'wagtail.wagtailsearch', 'wagtail.wagtailredirects', 'wagtail.tests', - ] + ], + WAGTAILSEARCH_BACKENDS={ + 'default': { + 'BACKEND': 'wagtail.wagtailsearch.backends.db.DBSearch', + }, + 'elasticsearch': { + 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch', + 'RUN_TESTS': True, + } + } ) From 553d718e5a82ec69a8e74381f85f0e10b41d979c Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 20 Feb 2014 22:08:44 +0000 Subject: [PATCH 5/8] Revert "Added ElasticSearch backend to tests settings" This reverts commit 28a77c6071faaa717dff990c06cc12b14858215a. --- runtests.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/runtests.py b/runtests.py index 4606994498..ff59234319 100755 --- a/runtests.py +++ b/runtests.py @@ -62,16 +62,7 @@ if not settings.configured: 'wagtail.wagtailsearch', 'wagtail.wagtailredirects', 'wagtail.tests', - ], - WAGTAILSEARCH_BACKENDS={ - 'default': { - 'BACKEND': 'wagtail.wagtailsearch.backends.db.DBSearch', - }, - 'elasticsearch': { - 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch', - 'RUN_TESTS': True, - } - } + ] ) From 451f133d3b1439864b0762f1c234d48303dbb7ac Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 23 Feb 2014 14:57:33 +0000 Subject: [PATCH 6/8] Added test-media folder --- runtests.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtests.py b/runtests.py index ff59234319..d928654f55 100755 --- a/runtests.py +++ b/runtests.py @@ -8,6 +8,7 @@ from django.core.management import execute_from_command_line WAGTAIL_ROOT = os.path.dirname(__file__) STATIC_ROOT = os.path.join(WAGTAIL_ROOT, 'test-static') +MEDIA_ROOT = os.path.join(WAGTAIL_ROOT, 'test-media') if not settings.configured: settings.configure( @@ -21,6 +22,7 @@ if not settings.configured: ROOT_URLCONF='wagtail.tests.urls', STATIC_URL='/static/', STATIC_ROOT=STATIC_ROOT, + MEDIA_ROOT=MEDIA_ROOT, STATICFILES_FINDERS=( 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', @@ -72,6 +74,7 @@ def runtests(): execute_from_command_line(argv) finally: shutil.rmtree(STATIC_ROOT, ignore_errors=True) + shutil.rmtree(MEDIA_ROOT, ignore_errors=True) if __name__ == '__main__': From f565f4b79bfa884c421934745badfceea42b958f Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 23 Feb 2014 14:58:04 +0000 Subject: [PATCH 7/8] Wagtailimages: Test image is now created on the fly --- .../static/wagtailimages/images/test.png | Bin 1946 -> 0 bytes wagtail/wagtailimages/tests.py | 9 ++++++++- 2 files changed, 8 insertions(+), 1 deletion(-) delete mode 100644 wagtail/wagtailimages/static/wagtailimages/images/test.png diff --git a/wagtail/wagtailimages/static/wagtailimages/images/test.png b/wagtail/wagtailimages/static/wagtailimages/images/test.png deleted file mode 100644 index e1c8170af514625cb25d552cfdfce00cfeadb833..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1946 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV0^&A1Qgk|*?Ths1AC&Ui(^Q|oVQmE85tCK z4j3HR|NLQ-oC?>k((`AS817k>GBEr((ZHyXaDYL;Ac28H!hnH=M}mQgjfa7enXRFL zk=fzEsPt&Cj3$TCY%p3njFyU{wE=1+*~tBO1>4@mjAE;RbuNRatDnm{r-UW|-Oxt$ diff --git a/wagtail/wagtailimages/tests.py b/wagtail/wagtailimages/tests.py index 42e26981b4..2265b93092 100644 --- a/wagtail/wagtailimages/tests.py +++ b/wagtail/wagtailimages/tests.py @@ -8,7 +8,14 @@ from wagtail.wagtailimages.templatetags import image_tags def get_test_image_file(): - return 'wagtail/wagtailimages/static/wagtailimages/images/test.png' + from StringIO import StringIO + from PIL import Image + from django.core.files.images import ImageFile + + f = StringIO() + image = Image.new('RGB', (640, 480), 'white') + image.save(f, 'PNG') + return ImageFile(f, name='test.png') Image = get_image_model() From df91d73644d6460800ce5a1e19d2a5e65f51898b Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 23 Feb 2014 15:06:59 +0000 Subject: [PATCH 8/8] Test models are now more human-friendly --- wagtail/tests/models.py | 201 +++++++++++++++++++++++++++------- wagtail/wagtailadmin/tests.py | 10 +- 2 files changed, 165 insertions(+), 46 deletions(-) diff --git a/wagtail/tests/models.py b/wagtail/tests/models.py index 5757dd2bd7..99fb813b41 100644 --- a/wagtail/tests/models.py +++ b/wagtail/tests/models.py @@ -7,6 +7,12 @@ from wagtail.wagtailimages.edit_handlers import ImageChooserPanel from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel +EVENT_AUDIENCE_CHOICES = ( + ('public', "Public"), + ('private', "Private"), +) + + COMMON_PANELS = ( FieldPanel('slug'), FieldPanel('seo_title'), @@ -15,73 +21,186 @@ COMMON_PANELS = ( ) -class ChildObject(Orderable): - page = ParentalKey('tests.TestPage', related_name='child_objects') - chosen_page = models.ForeignKey( +# Link fields + +class LinkFields(models.Model): + link_external = models.URLField("External link", blank=True) + link_page = models.ForeignKey( 'wagtailcore.Page', null=True, blank=True, related_name='+' ) - chosen_document = models.ForeignKey( + link_document = models.ForeignKey( 'wagtaildocs.Document', null=True, blank=True, related_name='+' ) - chosen_image = models.ForeignKey( + + @property + def link(self): + if self.link_page: + return self.link_page.url + elif self.link_document: + return self.link_document.url + else: + return self.link_external + + panels = [ + FieldPanel('link_external'), + PageChooserPanel('link_page'), + DocumentChooserPanel('link_document'), + ] + + class Meta: + abstract = True + + +# Carousel items + +class CarouselItem(LinkFields): + image = models.ForeignKey( 'wagtailimages.Image', null=True, blank=True, + on_delete=models.SET_NULL, + related_name='+' + ) + embed_url = models.URLField("Embed URL", blank=True) + caption = models.CharField(max_length=255, blank=True) + + panels = [ + ImageChooserPanel('image'), + FieldPanel('embed_url'), + FieldPanel('caption'), + MultiFieldPanel(LinkFields.panels, "Link"), + ] + + class Meta: + abstract = True + + +# Related links + +class RelatedLink(LinkFields): + title = models.CharField(max_length=255, help_text="Link title") + + panels = [ + FieldPanel('title'), + MultiFieldPanel(LinkFields.panels, "Link"), + ] + + class Meta: + abstract = True + + +# Event page + +class EventPageCarouselItem(Orderable, CarouselItem): + page = ParentalKey('tests.EventPage', related_name='carousel_items') + + +class EventPageRelatedLink(Orderable, RelatedLink): + page = ParentalKey('tests.EventPage', related_name='related_links') + + +class EventPageSpeaker(Orderable, LinkFields): + page = ParentalKey('tests.EventPage', related_name='speakers') + first_name = models.CharField("Name", max_length=255, blank=True) + last_name = models.CharField("Surname", max_length=255, blank=True) + image = models.ForeignKey( + 'wagtailimages.Image', + null=True, + blank=True, + on_delete=models.SET_NULL, related_name='+' ) + @property + def name_display(self): + return self.first_name + " " + self.last_name + panels = [ - PageChooserPanel('chosen_page'), - DocumentChooserPanel('chosen_document'), - ImageChooserPanel('chosen_image'), + FieldPanel('first_name'), + FieldPanel('last_name'), + ImageChooserPanel('image'), + MultiFieldPanel(LinkFields.panels, "Link"), ] -class TestPage(Page): - value = models.CharField(max_length=255, blank=True) + +class EventPage(Page): + date_from = models.DateField("Start date", null=True) + date_to = models.DateField( + "End date", + null=True, + blank=True, + help_text="Not required if event is on a single day" + ) + time_from = models.TimeField("Start time", null=True, blank=True) + time_to = models.TimeField("End time", null=True, blank=True) + audience = models.CharField(max_length=255, choices=EVENT_AUDIENCE_CHOICES) + location = models.CharField(max_length=255) body = RichTextField(blank=True) - date = models.DateField(null=True, blank=True) - time = models.TimeField(null=True, blank=True) - index_me = models.BooleanField(default=True) + cost = models.CharField(max_length=255) + signup_link = models.URLField(blank=True) + feed_image = models.ForeignKey( + 'wagtailimages.Image', + null=True, + blank=True, + on_delete=models.SET_NULL, + related_name='+' + ) - def object_indexed(self): - return self.index_me + indexed_fields = ('get_audience_display', 'location', 'body') + search_name = "Event" - def calculated_indexed_field(self): - return self.value + @property + def event_index(self): + # Find event index in ancestors + for ancestor in reversed(self.get_ancestors()): + if isinstance(ancestor.specific, EventIndexPage): + return ancestor - indexed_fields = ('body', ) + # No ancestors are event indexes, + # just return first event index in database + return EventIndexPage.objects.first() -TestPage.content_panels = [ + def serve(self, request): + if "format" in request.GET: + if request.GET['format'] == 'ical': + # Export to ical format + response = HttpResponse( + export_event(self, 'ical'), + content_type='text/calendar', + ) + response['Content-Disposition'] = 'attachment; filename=' + self.slug + '.ics' + return response + else: + # Unrecognised format error + message = 'Could not export event\n\nUnrecognised format: ' + request.GET['format'] + return HttpResponse(message, content_type='text/plain') + else: + # Display event page as usual + return super(EventPage, self).serve(request) + +EventPage.content_panels = [ FieldPanel('title', classname="full title"), + FieldPanel('date_from'), + FieldPanel('date_to'), + FieldPanel('time_from'), + FieldPanel('time_to'), + FieldPanel('location'), + FieldPanel('audience'), + FieldPanel('cost'), + FieldPanel('signup_link'), + InlinePanel(EventPage, 'carousel_items', label="Carousel items"), FieldPanel('body', classname="full"), - FieldPanel('value'), - FieldPanel('date'), - FieldPanel('time'), - FieldPanel('index_me'), - InlinePanel(TestPage, 'child_objects', label="Child objects"), + InlinePanel(EventPage, 'speakers', label="Speakers"), + InlinePanel(EventPage, 'related_links', label="Related links"), ] -TestPage.promote_panels = [ - MultiFieldPanel([ - FieldPanel('slug'), - FieldPanel('seo_title'), - FieldPanel('show_in_menus'), - FieldPanel('search_description'), - ], "Common page configuration"), +EventPage.promote_panels = [ + MultiFieldPanel(COMMON_PANELS, "Common page configuration"), + ImageChooserPanel('feed_image'), ] - - -class DerivedPage(TestPage): - extra_field = models.CharField(max_length=255, blank=True) - - indexed_fields = ('extra_field', ) - - -class NotIndexedPage(Page): - indexed = False diff --git a/wagtail/wagtailadmin/tests.py b/wagtail/wagtailadmin/tests.py index 11d84b1a43..ca75eb98a6 100644 --- a/wagtail/wagtailadmin/tests.py +++ b/wagtail/wagtailadmin/tests.py @@ -1,6 +1,6 @@ from django.test import TestCase import unittest -from wagtail.tests.models import TestPage +from wagtail.tests.models import EventPage from wagtail.tests.utils import login, get_host from wagtail.wagtailcore.models import Page from django.core.urlresolvers import reverse @@ -12,7 +12,7 @@ class TestPageExplorer(TestCase): self.root_page = Page.objects.get(id=2) # Add child page - self.child_page = TestPage() + self.child_page = EventPage() self.child_page.title = "Hello world!" self.child_page.slug = "hello-world" self.root_page.add_child(self.child_page) @@ -41,7 +41,7 @@ class TestPageCreation(TestCase): @unittest.expectedFailure # For some reason, this returns a 302... def test_select_location_testpage(self): - response = self.client.get(reverse('wagtailadmin_pages_select_location', args=('tests', 'testpage')), HTTP_HOST=get_host()) + response = self.client.get(reverse('wagtailadmin_pages_select_location', args=('tests', 'eventpage')), HTTP_HOST=get_host()) self.assertEqual(response.status_code, 200) def test_select_location_nonexistanttype(self): @@ -61,7 +61,7 @@ class TestPageCreation(TestCase): self.assertEqual(response.status_code, 404) def test_create_testpage_root(self): - response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'testpage', self.root_page.id)), HTTP_HOST=get_host()) + response = self.client.get(reverse('wagtailadmin_pages_create', args=('tests', 'eventpage', self.root_page.id)), HTTP_HOST=get_host()) self.assertEqual(response.status_code, 200) def test_create_testpage_nonexistantparent(self): @@ -80,7 +80,7 @@ class TestPageEditDelete(TestCase): self.root_page = Page.objects.get(id=2) # Add child page - self.child_page = TestPage() + self.child_page = EventPage() self.child_page.title = "Hello world!" self.child_page.slug = "hello-world" self.root_page.add_child(self.child_page)