Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Back-end tests #27

Merged
merged 4 commits into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import absolute_import, unicode_literals

import unittest

from django.test import override_settings

from wagtaildraftail import blocks, draft_text, forms, widgets


class DraftailTextBlockTestCase(unittest.TestCase):

def test_get_default_with_string_default(self):
class StringDefaultDraftailTextBlock(blocks.DraftailTextBlock):
class Meta:
default = '{}'

block = StringDefaultDraftailTextBlock()
default = block.get_default()
expected_default = draft_text.DraftText('{}')

self.assertIsInstance(default, draft_text.DraftText)
self.assertEqual(default, expected_default)

def test_get_default_with_node_default(self):
class NodeDefaultDraftailTextBlock(blocks.DraftailTextBlock):
class Meta:
default = draft_text.DraftText('{}')

block = NodeDefaultDraftailTextBlock()
default = block.get_default()
expected_default = draft_text.DraftText('{}')

self.assertIsInstance(default, draft_text.DraftText)
self.assertEqual(default, expected_default)

def test_field_class(self):
block = blocks.DraftailTextBlock()

self.assertIsInstance(block.field, forms.SerializedJSONField)

@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={
'test_editor': {
'WIDGET': 'wagtaildraftail.widgets.DraftailTextArea',
}
})
def test_field_is_initialized_with_widget(self):
block = blocks.DraftailTextBlock(editor='test_editor')

self.assertIsInstance(block.field.widget, widgets.DraftailTextArea)

def test_field_is_initialized_with_options(self):
options = {'required': False, 'help_text': 'weee'}
block = blocks.DraftailTextBlock(**options)

self.assertEqual(block.field.required, options['required'])
self.assertEqual(block.field.help_text, options['help_text'])

def test_to_python(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
python_value = blocks.DraftailTextBlock().to_python(value)
expected_python_value = draft_text.DraftText(value)

self.assertIsInstance(python_value, draft_text.DraftText)
self.assertEqual(python_value, expected_python_value)

def test_value_from_form(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
form_value = blocks.DraftailTextBlock().value_from_form(value)
expected_form_value = draft_text.DraftText(value)

self.assertIsInstance(form_value, draft_text.DraftText)
self.assertEqual(form_value, expected_form_value)

def test_get_searchable_content_with_string_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
searchable_content = blocks.DraftailTextBlock().get_searchable_content(value)
expected_searchable_content = [
'<p><strong>Cupcake</strong> ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...</p>']

self.assertEqual(searchable_content, expected_searchable_content)

def test_get_searchable_content_with_node_value(self):
value = draft_text.DraftText('{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}') # noqa: E501
searchable_content = blocks.DraftailTextBlock().get_searchable_content(value)
expected_searchable_content = [
'<p><strong>Cupcake</strong> ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...</p>']

self.assertEqual(searchable_content, expected_searchable_content)
97 changes: 97 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from __future__ import absolute_import, unicode_literals

import unittest

from django.forms import widgets as djwidgets
from django.test import override_settings

from wagtaildraftail import draft_text, fields, widgets


class DraftailTextFieldTestCase(unittest.TestCase):

def test_init_has_default_editor(self):
field = fields.DraftailTextField()

self.assertEqual(field.editor, 'default_draftail')

def test_init_with_custom_editor(self):
editor = 'custom_editor'
field = fields.DraftailTextField(editor=editor)

self.assertEqual(field.editor, editor)

@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={
'test_editor': {
'WIDGET': 'wagtaildraftail.widgets.DraftailTextArea',
}
})
def test_formfield_is_initialized_with_widget(self):
field = fields.DraftailTextField(editor='test_editor')
formfield = field.formfield()

self.assertIsInstance(formfield.widget, widgets.DraftailTextArea)

def test_formfield_can_overwrite_widget(self):
field = fields.DraftailTextField()
formfield = field.formfield(widget=djwidgets.Textarea)

self.assertIsInstance(formfield.widget, djwidgets.Textarea)

def test_to_python_with_string_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
python_value = fields.DraftailTextField().to_python(value)
expected_python_value = draft_text.DraftText(value)

self.assertIsInstance(python_value, draft_text.DraftText)
self.assertEqual(python_value, expected_python_value)

def test_to_python_with_node_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
python_value = fields.DraftailTextField().to_python(draft_text.DraftText(value))
expected_python_value = draft_text.DraftText(value)

self.assertIsInstance(python_value, draft_text.DraftText)
self.assertEqual(python_value, expected_python_value)

def test_get_prep_value_with_string_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
prep_value = fields.DraftailTextField().get_prep_value(value)

self.assertEqual(prep_value, value)

def test_get_prep_value_with_node_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
prep_value = fields.DraftailTextField().get_prep_value(draft_text.DraftText(value))

self.assertEqual(prep_value, value)

def test_from_db_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
from_db_value = fields.DraftailTextField().from_db_value(value)
expected_from_db_value = draft_text.DraftText(value)

self.assertIsInstance(from_db_value, draft_text.DraftText)
self.assertEqual(from_db_value, expected_from_db_value)

def test_value_to_string_with_none_value(self):
pass # TODO

def test_value_to_string_with_obj_value(self):
pass # TODO

def test_get_searchable_content_with_string_value(self):
value = '{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}' # noqa: E501
searchable_content = fields.DraftailTextField().get_searchable_content(value)
expected_searchable_content = [
'<p><strong>Cupcake</strong> ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...</p>']

self.assertEqual(searchable_content, expected_searchable_content)

def test_get_searchable_content_with_node_value(self):
value = draft_text.DraftText('{"entityMap": {}, "blocks": [{"entityRanges": [], "inlineStyleRanges": [{"style": "BOLD", "length": 7, "offset": 0}], "type": "unstyled", "text": "Cupcake ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...", "depth": 0, "key": "en564", "data": {}}]}') # noqa: E501
searchable_content = fields.DraftailTextField().get_searchable_content(value)
expected_searchable_content = [
'<p><strong>Cupcake</strong> ipsum dolor sit amet muffin drag\u00e9e cupcake biscuit...</p>']

self.assertEqual(searchable_content, expected_searchable_content)
29 changes: 29 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import absolute_import, unicode_literals

import unittest

from django.core.exceptions import ValidationError

from wagtaildraftail import forms


class SerializedJSONFieldTestCase(unittest.TestCase):

def setUp(self):
self.field = forms.SerializedJSONField(required=True)

def test_none_is_empty_value(self):
with self.assertRaises(ValidationError):
self.field.clean(None)

def test_empty_string_is_empty_value(self):
with self.assertRaises(ValidationError):
self.field.clean('')

def test_serialized_empty_list_is_empty_value(self):
with self.assertRaises(ValidationError):
self.field.clean('[]')

def test_serialized_empty_dict_is_empty_value(self):
with self.assertRaises(ValidationError):
self.field.clean('{}')
13 changes: 0 additions & 13 deletions tests/test_wagtail_blocks.py

This file was deleted.

10 changes: 10 additions & 0 deletions tests/test_wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import unittest

from wagtail.wagtailcore.hooks import get_hooks

from wagtaildraftail.wagtail_hooks import draftail_editor_css, draftail_editor_js


Expand All @@ -10,6 +12,14 @@ def test_editor_css(self):
self.assertEqual(
draftail_editor_css(), '<link rel="stylesheet" href="/static/wagtaildraftail/wagtaildraftail.css">')

def test_insert_editor_css_hook(self):
hooks = get_hooks('insert_editor_css')
self.assertIn(draftail_editor_css, hooks, 'Editor CSS should be inserted automatically.')

def test_editor_js(self):
self.assertEqual(
draftail_editor_js(), '<script src="/static/wagtaildraftail/wagtaildraftail.js"></script>')

def test_insert_editor_js(self):
hooks = get_hooks('insert_editor_js')
self.assertIn(draftail_editor_js, hooks, 'Editor JS should be inserted automatically.')
6 changes: 3 additions & 3 deletions wagtaildraftail/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def get_default(self):
else:
return DraftText(self.meta.default)

def to_python(self, value):
return DraftText(value)

@cached_property
def field(self):
from wagtail.wagtailadmin.rich_text import get_rich_text_editor_widget
return SerializedJSONField(widget=get_rich_text_editor_widget(self.editor), **self.field_options)

def to_python(self, value):
return DraftText(value)

def value_from_form(self, value):
return DraftText(value)

Expand Down