Skip to content

Commit

Permalink
Increase test coverage (django-cms#178)
Browse files Browse the repository at this point in the history
* added more tests

* further adaptions

* fix tests

* replace with helper files

* further adaptions

* fix tests

* fix tests

* simplify code
  • Loading branch information
FinalAngel authored Aug 22, 2019
1 parent 88b6c28 commit 2712ce4
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 329 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.log
*.pot
.DS_Store
.coverage
.coverage/
.eggs/
.idea/
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: python

dist: xenial
sudo: false

matrix:
Expand Down Expand Up @@ -27,8 +28,6 @@ matrix:
env: DJANGO='dj22' CMS='cms37'
- python: 3.7
env: DJANGO='dj22' CMS='cms37'
dist: xenial
sudo: true

install:
- pip install coverage isort tox
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ Changelog
=========


2.6.0 (unreleased)
==================

* Pinned ``django-filer`` to 1.5.0
* Added further tests to raise coverage
* Fixed smaller issues found during testing
* Dropped support for django-select2 <= 4
* Dropped support for django-filer <= 1.4


2.5.0 (2019-07-09)
==================

Expand Down
11 changes: 4 additions & 7 deletions djangocms_link/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ def render(self, context, instance, placeholder):
def get_form(self, request, obj=None, **kwargs):
form_class = super(LinkPlugin, self).get_form(request, obj, **kwargs)

try:
if obj and obj.page and obj.page.site:
site = obj.page.site
elif self.page and self.page.site:
site = self.page.site
except: # noqa
site = Site.objects.get_current()
if obj and obj.page and obj.page.site:
site = obj.page.site
elif self.page and self.page.site:
site = self.page.site
else:
site = Site.objects.get_current()

Expand Down
13 changes: 7 additions & 6 deletions djangocms_link/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from django.conf import settings


ENABLE_SELECT2 = getattr(settings, 'DJANGOCMS_LINK_USE_SELECT2', False)
def is_select2_enabled():
use_select2 = getattr(settings, 'DJANGOCMS_LINK_USE_SELECT2', False)
is_installed = 'django_select2' in settings.INSTALLED_APPS
return use_select2 and is_installed

if ENABLE_SELECT2 and 'django_select2' in settings.INSTALLED_APPS:
try:
from djangocms_link.fields_select2 import Select2PageSearchField as PageSearchField # noqa
except ImportError:
from djangocms_link.fields_select2_legacy import Select2LegacyPageSearchField as PageSearchField # noqa

if is_select2_enabled():
from djangocms_link.fields_select2 import Select2PageSearchField as PageSearchField # noqa
else:
from cms.forms.fields import PageSelectFormField as PageSearchField # noqa
25 changes: 0 additions & 25 deletions djangocms_link/fields_select2_legacy.py

This file was deleted.

2 changes: 0 additions & 2 deletions djangocms_link/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,13 @@ class AbstractLink(CMSPlugin):
on_delete=models.SET_NULL,
help_text=_('If provided, overrides the external link.'),
)

file_link = FilerFileField(
verbose_name=_('File link'),
blank=True,
null=True,
on_delete=models.SET_NULL,
help_text=_('If provided links a file from the filer app.'),
)

# other link types
anchor = models.CharField(
verbose_name=_('Anchor'),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

REQUIREMENTS = [
'django-cms>=3.4.5',
'django-filer>=1.3.0',
'django-filer>=1.5.0',
'djangocms-attributes-field>=0.4.0',
]

Expand Down
123 changes: 123 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
from tempfile import mkdtemp

from django.core.files import File

from filer.models.filemodels import File as FilerFile
from filer.models.foldermodels import Folder as FilerFolder
from filer.models.imagemodels import Image as FilerImage
from filer.utils.compatibility import PILImage, PILImageDraw


# from https://github.com/divio/django-filer/blob/develop/tests/helpers.py#L46-L52
def create_image(mode="RGB", size=(800, 600)):
"""
Creates a usable image file using PIL
:returns: PIL Image instance
"""
image = PILImage.new(mode, size)
draw = PILImageDraw.Draw(image)
x_bit, y_bit = size[0] // 10, size[1] // 10
draw.rectangle((x_bit, y_bit * 2, x_bit * 7, y_bit * 3), "red")
draw.rectangle((x_bit * 2, y_bit, x_bit * 3, y_bit * 8), "red")

return image


def get_image(image_name="test_file.jpg"):
"""
Creates and stores an image to the file system using PILImage
:param image_name: the name for the file (default "test_file.jpg")
:returns: dict {name, image, path}
"""
image = create_image()
image_path = os.path.join(
mkdtemp(),
image_name,
)
image.save(image_path, "JPEG")

return {
"name": image_name,
"image": image,
"path": image_path,
}


def get_file(file_name="test_file.pdf"):
"""
Creates and stores an arbitrary file into a temporary dir
:param file_name: the name for the file (default "test_file.pdf")
:returns: dict {name, image, path}
"""
file_path = os.path.join(
mkdtemp(),
file_name,
)
data = open(file_path, "a")

return {
"name": file_name,
"file": data,
"path": file_path,
}


def get_filer_image(image_name="test_file.jpg"):
"""
Creates and stores an image to filer and returns it
:param image_name: the name for the file (default "test_file.jpg")
:returns: filer image instance
"""
image = get_image(image_name)
filer_file = File(
open(image.get("path"), "rb"),
name=image.get("name"),
)
filer_object = FilerImage.objects.create(
original_filename=image.get("name"),
file=filer_file,
)

return filer_object


def get_filer_file(file_name="test_file.pdf", folder=None):
"""
Creates and stores a file to filer and returns it
:param file_name: the name for the file (default "test_file.pdf")
:param folder: optionally provide a folder instance
:returns: filer file instance
"""
data = get_file(file_name)
filer_file = File(
open(data.get("path"), "rb"),
name=data.get("name"),
)
filer_object = FilerFile.objects.create(
original_filename=data.get("name"),
file=filer_file,
folder=folder,
)

return filer_object


def get_filer_folder(folder_name="test_folder", parent=None):
"""
Creates and returns a filer folder
:param folder_name: the name of the folder to be used (default "folder_name")
:param parent: optionally provide a parent folder
:returns: filer folder instance
"""
filer_object = FilerFolder.objects.create(
parent=parent,
name=folder_name,
)

return filer_object
8 changes: 5 additions & 3 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# requirements from setup.py
django-select2>=5.11,<7 # 7 works in Django 2+
django-filer>=1.3.0
django-filer>=1.5.0
djangocms-text-ckeditor
html5lib<0.99999999
# other requirements
djangocms-helper
tox
coverage
isort
flake8
# override "pyflakes<2.1" from djangocms-helper
pyflakes>=2.1.0
10 changes: 0 additions & 10 deletions tests/test_field.py

This file was deleted.

16 changes: 16 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.test import TestCase

from djangocms_link.fields import is_select2_enabled, PageSearchField
from djangocms_link.fields_select2 import Select2PageSearchField


class LinkFieldTestCase(TestCase):

def test_field_with_django_select2_extension(self):
self.assertTrue('django_select2' in settings.INSTALLED_APPS)
self.assertTrue(is_select2_enabled())
settings.INSTALLED_APPS.remove("django_select2")
self.assertFalse(is_select2_enabled())
self.assertEqual(PageSearchField, Select2PageSearchField)
62 changes: 0 additions & 62 deletions tests/test_file_plugin.py

This file was deleted.

Loading

0 comments on commit 2712ce4

Please sign in to comment.