diff --git a/.gitignore b/.gitignore index b694934..fc5a5e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,12 @@ -.venv \ No newline at end of file +.venv +.idea/ +MANIFEST +*.pyc +*.egg-info +dist/ +*.egg +*.tar.gz +.eggs/ +.DS_Store +.cache/ +.pytest_cache/ diff --git a/.travis.yml b/.travis.yml index 29d99e4..297bad6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..e83038f --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +django_find_project = false +DJANGO_SETTINGS_MODULE = tests.test_settings +python_paths = tests diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..28c8b9a --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,3 @@ +pytest_plugins = [ + 'tests.fixtures' +] diff --git a/tests/fixtures.py b/tests/fixtures.py new file mode 100644 index 0000000..3410018 --- /dev/null +++ b/tests/fixtures.py @@ -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 diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 0000000..cb232f6 --- /dev/null +++ b/tests/test_settings.py @@ -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 diff --git a/tests/test_snippet_link_handler.py b/tests/test_snippet_link_handler.py new file mode 100644 index 0000000..bdc3cde --- /dev/null +++ b/tests/test_snippet_link_handler.py @@ -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 == "" + + # 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'' diff --git a/tests/test_wagtail_draftail_snippet.py b/tests/test_wagtail_draftail_snippet.py deleted file mode 100644 index 7e66b31..0000000 --- a/tests/test_wagtail_draftail_snippet.py +++ /dev/null @@ -1,5 +0,0 @@ -from wagtail_draftail_snippet import __version__ - - -def test_version(): - assert __version__ == '0.1.0' diff --git a/tests/testapp/__init__.py b/tests/testapp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/testapp/factories.py b/tests/testapp/factories.py new file mode 100644 index 0000000..587e517 --- /dev/null +++ b/tests/testapp/factories.py @@ -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" diff --git a/tests/testapp/models.py b/tests/testapp/models.py new file mode 100644 index 0000000..fb02e27 --- /dev/null +++ b/tests/testapp/models.py @@ -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 diff --git a/tests/testapp/templates/testapp/advert_snippet.html b/tests/testapp/templates/testapp/advert_snippet.html new file mode 100644 index 0000000..bc08175 --- /dev/null +++ b/tests/testapp/templates/testapp/advert_snippet.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/urls.py b/tests/urls.py new file mode 100644 index 0000000..ab167cc --- /dev/null +++ b/tests/urls.py @@ -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)), +] diff --git a/wagtail_draftail_snippet/__init__.py b/wagtail_draftail_snippet/__init__.py index b794fd4..e69de29 100644 --- a/wagtail_draftail_snippet/__init__.py +++ b/wagtail_draftail_snippet/__init__.py @@ -1 +0,0 @@ -__version__ = '0.1.0'