1
Fork 0

Test for Snippet Link Handler

This commit is contained in:
Parbhat Puri 2020-02-13 17:53:35 +05:30
parent 2266946165
commit 399ebd1d6f
14 changed files with 175 additions and 8 deletions

13
.gitignore vendored
View file

@ -1 +1,12 @@
.venv
.venv
.idea/
MANIFEST
*.pyc
*.egg-info
dist/
*.egg
*.tar.gz
.eggs/
.DS_Store
.cache/
.pytest_cache/

View file

@ -10,7 +10,7 @@ env:
install:
- pip install -U pip
- pip install .
- pip install pytest
- pip install pytest pytest-django pytest-pythonpath wagtail-factories
script:
- pytest
deploy:

4
pytest.ini Normal file
View file

@ -0,0 +1,4 @@
[pytest]
django_find_project = false
DJANGO_SETTINGS_MODULE = tests.test_settings
python_paths = tests

3
tests/conftest.py Normal file
View file

@ -0,0 +1,3 @@
pytest_plugins = [
'tests.fixtures'
]

8
tests/fixtures.py Normal file
View file

@ -0,0 +1,8 @@
import pytest
@pytest.fixture
def site():
from wagtail.core.models import Site
site = Site.objects.get(is_default_site=True)
return site

86
tests/test_settings.py Normal file
View file

@ -0,0 +1,86 @@
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ADMINS = (
('test@example.com', 'TEST-R'),
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'tests.db',
},
}
SECRET_KEY = '_uobce43e5osp8xgsffssffsds2_16%y$sf*5(12vfg25hpnxik_*'
INSTALLED_APPS = [
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
# App Under test
'wagtail_draftail_snippet',
# Test app
'tests.testapp'
]
MIDDLEWARE = (
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'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',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
},
},
]
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ROOT_URLCONF = 'tests.urls'
WAGTAIL_SITE_NAME = "Test Site",
SITE_ID = 1

View file

@ -0,0 +1,22 @@
import pytest
from tests.testapp import factories
from wagtail_draftail_snippet.richtext import SnippetLinkHandler
@pytest.mark.django_db
def test_snippet_link_handler():
advert = factories.AdvertFactory(text='advert', url='https://www.example.com')
advert.save()
assert advert.text == 'advert'
assert advert.url == 'https://www.example.com'
# Empty link created in case of exception
result = SnippetLinkHandler.expand_db_attributes({'id': 0})
assert result == "<a>"
# Test snippet template render correctly
attrs = {'id': 1, 'data-app-name': 'testapp', 'data-model-name': 'Advert'}
result = SnippetLinkHandler.expand_db_attributes(attrs)
assert result == f'<a href="{ advert.url }/{ advert.id }">'

View file

@ -1,5 +0,0 @@
from wagtail_draftail_snippet import __version__
def test_version():
assert __version__ == '0.1.0'

View file

View file

@ -0,0 +1,11 @@
import factory
from .models import Advert
class AdvertFactory(factory.Factory):
class Meta:
model = Advert
url = "https://www.example.com"
text = "Example"

18
tests/testapp/models.py Normal file
View file

@ -0,0 +1,18 @@
from django.db import models
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.snippets.models import register_snippet
@register_snippet
class Advert(models.Model):
url = models.URLField(null=True, blank=True)
text = models.CharField(max_length=255)
panels = [
FieldPanel('url'),
FieldPanel('text'),
]
def __str__(self):
return self.text

View file

@ -0,0 +1 @@
<a href="{{ object.url }}/{{ object.id }}">

9
tests/urls.py Normal file
View file

@ -0,0 +1,9 @@
from django.conf.urls import include, url
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
urlpatterns = [
url(r'^admin/', include(wagtailadmin_urls)),
url(r'', include(wagtail_urls)),
]

View file

@ -1 +0,0 @@
__version__ = '0.1.0'