1
Fork 0

Split up tests.

This commit is contained in:
Adam Hill 2020-02-14 22:23:14 -05:00
parent 399ebd1d6f
commit cf84e4aa4a
5 changed files with 88 additions and 77 deletions

View file

@ -31,6 +31,9 @@ The package requires Wagtail 2.5 or above.
1. `poetry build` 1. `poetry build`
## Run tests
1. `poetry install --dev`
1. `poetry run pytest`
## Contributors ## Contributors

View file

@ -12,6 +12,10 @@ python = "^3.6"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^3.0" pytest = "^3.0"
wagtail = "^2.5"
pytest-django = "^3.8"
pytest-pythonpath = "^0.7.3"
wagtail-factories = "^2.0"
[build-system] [build-system]
requires = ["poetry>=0.12"] requires = ["poetry>=0.12"]

View file

@ -1,8 +1,17 @@
import pytest import pytest
from wagtail.core.models import Site
from tests.testapp import factories
@pytest.fixture @pytest.fixture
def site(): def site():
from wagtail.core.models import Site
site = Site.objects.get(is_default_site=True) site = Site.objects.get(is_default_site=True)
return site return site
@pytest.fixture()
def advert():
advert = factories.AdvertFactory(text="advert", url="https://www.example.com")
advert.save()
yield advert

View file

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

View file

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