Extend unit tests for changing existing thabloids

parent 8008a2a9
......@@ -20,6 +20,8 @@ pep8:
- mkdir -p pip-cache
- git log -1
- pip install tox coverage
- apt-get update
- apt-get install -y ghostscript # required for Thabloid's tests
script:
- tox -e $PYTHON_VERSION
- cd website
......
......@@ -76,7 +76,7 @@ class Thabloid(models.Model):
if wait: # pragma: no cover
p.wait()
def save(self, *args, nopages=False, wait=False, **kwargs):
def save(self, *args, wait=False, **kwargs):
new_file = False
if self.pk is None:
......@@ -115,5 +115,5 @@ class Thabloid(models.Model):
super(Thabloid, self).save(*args, **kwargs)
if new_file and not nopages:
if new_file:
self.extract_thabloid_pages(wait)
import shutil
import tempfile
import os.path
from django.core.files import File
from django.conf import settings
from django.test import TestCase
from django.test.utils import override_settings
from .models import Thabloid
tmp_MEDIA_ROOT = tempfile.mkdtemp()
@override_settings(MEDIA_ROOT=tmp_MEDIA_ROOT)
class TestThabloid(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._old_media_root = settings.MEDIA_ROOT
settings.MEDIA_ROOT = tempfile.mkdtemp()
def setUp(self):
with open('thabloid/fixtures/thabloid-1998-1999-1.pdf', 'rb') as f:
cls.thabloid = Thabloid(
self.thabloid = Thabloid(
year=1998,
issue=1,
file=File(f))
# Only generate pages if we have 'gs'
if shutil.which('gs') is None:
cls.thabloid.save(nopages=True)
else:
# we should wait for gs to be done before we can do cleanup
cls.thabloid.save(wait=True)
@classmethod
def tearDownClass(cls):
# we should wait for gs to be done before we can do cleanup
self.thabloid.save(wait=True)
def tearDown(self):
"""Clean up remaining Thabloid files"""
shutil.rmtree(settings.MEDIA_ROOT)
settings.MEDIA_ROOT = cls._old_media_root
super().tearDownClass()
def test_thaboid_get_absolute_url(self):
self.assertEqual(self.thabloid.get_absolute_url(),
......@@ -49,3 +43,52 @@ class TestThabloid(TestCase):
self.assertEqual(
self.thabloid.page_url(20),
'public/thabloids/pages/thabloid-1998-1999-1/020.jpg')
@staticmethod
def _pdf_exist(pdf):
pdf = pdf.lstrip(settings.MEDIA_URL)
return os.path.isfile(os.path.join(settings.MEDIA_ROOT, pdf))
@staticmethod
def _jpgs_exist(pages, inverse=False):
jpgs = [url.lstrip(settings.MEDIA_URL) for url in pages]
for jpg in jpgs:
jpgpath = os.path.join(settings.MEDIA_ROOT, jpg)
if ((not inverse and not os.path.isfile(jpgpath)) or
(inverse and os.path.isfile(jpgpath))):
return False
return True
def test_pdf_existence(self):
TestThabloid._pdf_exist(self.thabloid.file.url)
def test_jpg_existence(self):
TestThabloid._jpgs_exist(self.thabloid.pages)
def test_change_year(self):
self.thabloid.year += 1
self.thabloid.save()
self.assertTrue(TestThabloid._pdf_exist(self.thabloid.file.url))
self.assertTrue(TestThabloid._jpgs_exist(self.thabloid.pages))
def test_change_year_cleanup(self):
oldpages = self.thabloid.pages
oldurl = self.thabloid.file.url
self.thabloid.year += 1
self.thabloid.save()
self.assertFalse(TestThabloid._pdf_exist(oldurl))
self.assertTrue(TestThabloid._jpgs_exist(oldpages, inverse=True))
def test_change_issue(self):
self.thabloid.issue += 1
self.thabloid.save()
self.assertTrue(TestThabloid._pdf_exist(self.thabloid.file.url))
self.assertTrue(TestThabloid._jpgs_exist(self.thabloid.pages))
def test_change_issue_cleanup(self):
oldpages = self.thabloid.pages
oldurl = self.thabloid.file.url
self.thabloid.issue += 1
self.thabloid.save()
self.assertFalse(TestThabloid._pdf_exist(oldurl))
self.assertTrue(TestThabloid._jpgs_exist(oldpages, inverse=True))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment