From cf84e4aa4a6a6acf4ea075df506036b04761f9d5 Mon Sep 17 00:00:00 2001 From: Adam Hill Date: Fri, 14 Feb 2020 22:23:14 -0500 Subject: [PATCH] Split up tests. --- README.md | 3 + pyproject.toml | 4 ++ tests/fixtures.py | 11 ++- tests/test_settings.py | 111 +++++++++++++---------------- tests/test_snippet_link_handler.py | 36 ++++++---- 5 files changed, 88 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index e571101..7f83f93 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,9 @@ The package requires Wagtail 2.5 or above. 1. `poetry build` +## Run tests +1. `poetry install --dev` +1. `poetry run pytest` ## Contributors diff --git a/pyproject.toml b/pyproject.toml index 794597c..0d9a814 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,10 @@ python = "^3.6" [tool.poetry.dev-dependencies] pytest = "^3.0" +wagtail = "^2.5" +pytest-django = "^3.8" +pytest-pythonpath = "^0.7.3" +wagtail-factories = "^2.0" [build-system] requires = ["poetry>=0.12"] diff --git a/tests/fixtures.py b/tests/fixtures.py index 3410018..95ca655 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -1,8 +1,17 @@ import pytest +from wagtail.core.models import Site + +from tests.testapp import factories @pytest.fixture def site(): - from wagtail.core.models import Site site = Site.objects.get(is_default_site=True) return site + + +@pytest.fixture() +def advert(): + advert = factories.AdvertFactory(text="advert", url="https://www.example.com") + advert.save() + yield advert diff --git a/tests/test_settings.py b/tests/test_settings.py index cb232f6..a05e410 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -2,85 +2,74 @@ import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -ADMINS = ( - ('test@example.com', 'TEST-R'), -) +ADMINS = (("test@example.com", "TEST-R"),) -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': 'tests.db', - }, -} +DATABASES = {"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 = [ - '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', - + "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', + "wagtail_draftail_snippet", # Test app - 'tests.testapp' + "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', + "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', + "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", ], - 'loaders': [ - 'django.template.loaders.filesystem.Loader', - 'django.template.loaders.app_directories.Loader', - ] }, - }, + } ] DEBUG = True TEMPLATE_DEBUG = DEBUG -ROOT_URLCONF = 'tests.urls' +ROOT_URLCONF = "tests.urls" -WAGTAIL_SITE_NAME = "Test Site", +WAGTAIL_SITE_NAME = ("Test Site",) SITE_ID = 1 diff --git a/tests/test_snippet_link_handler.py b/tests/test_snippet_link_handler.py index bdc3cde..efa7d84 100644 --- a/tests/test_snippet_link_handler.py +++ b/tests/test_snippet_link_handler.py @@ -1,22 +1,28 @@ 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() +class TestSnippetLinkHandler: + @pytest.mark.django_db + def test_advert_setup_correctly(self, advert): + assert advert.text == "advert" + assert advert.url == "https://www.example.com" - assert advert.text == 'advert' - assert advert.url == 'https://www.example.com' + @pytest.mark.django_db + 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}) - assert result == "" + result = SnippetLinkHandler.expand_db_attributes({"id": 0}) + assert result == "" - # 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'' + @pytest.mark.django_db + 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) + assert result == f''