Unverified Commit 53f1b45e authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Extend unit tests for changing existing thabloids

parent 8008a2a9
...@@ -20,6 +20,8 @@ pep8: ...@@ -20,6 +20,8 @@ pep8:
- mkdir -p pip-cache - mkdir -p pip-cache
- git log -1 - git log -1
- pip install tox coverage - pip install tox coverage
- apt-get update
- apt-get install -y ghostscript # required for Thabloid's tests
script: script:
- tox -e $PYTHON_VERSION - tox -e $PYTHON_VERSION
- cd website - cd website
......
...@@ -76,7 +76,7 @@ class Thabloid(models.Model): ...@@ -76,7 +76,7 @@ class Thabloid(models.Model):
if wait: # pragma: no cover if wait: # pragma: no cover
p.wait() p.wait()
def save(self, *args, nopages=False, wait=False, **kwargs): def save(self, *args, wait=False, **kwargs):
new_file = False new_file = False
if self.pk is None: if self.pk is None:
...@@ -115,5 +115,5 @@ class Thabloid(models.Model): ...@@ -115,5 +115,5 @@ class Thabloid(models.Model):
super(Thabloid, self).save(*args, **kwargs) super(Thabloid, self).save(*args, **kwargs)
if new_file and not nopages: if new_file:
self.extract_thabloid_pages(wait) self.extract_thabloid_pages(wait)
import shutil import shutil
import tempfile import tempfile
import os.path
from django.core.files import File from django.core.files import File
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings
from .models import Thabloid from .models import Thabloid
tmp_MEDIA_ROOT = tempfile.mkdtemp()
@override_settings(MEDIA_ROOT=tmp_MEDIA_ROOT)
class TestThabloid(TestCase): class TestThabloid(TestCase):
@classmethod def setUp(self):
def setUpClass(cls):
super().setUpClass()
cls._old_media_root = settings.MEDIA_ROOT
settings.MEDIA_ROOT = tempfile.mkdtemp()
with open('thabloid/fixtures/thabloid-1998-1999-1.pdf', 'rb') as f: with open('thabloid/fixtures/thabloid-1998-1999-1.pdf', 'rb') as f:
cls.thabloid = Thabloid( self.thabloid = Thabloid(
year=1998, year=1998,
issue=1, issue=1,
file=File(f)) file=File(f))
# Only generate pages if we have 'gs' # we should wait for gs to be done before we can do cleanup
if shutil.which('gs') is None: self.thabloid.save(wait=True)
cls.thabloid.save(nopages=True)
else: def tearDown(self):
# we should wait for gs to be done before we can do cleanup
cls.thabloid.save(wait=True)
@classmethod
def tearDownClass(cls):
"""Clean up remaining Thabloid files""" """Clean up remaining Thabloid files"""
shutil.rmtree(settings.MEDIA_ROOT) shutil.rmtree(settings.MEDIA_ROOT)
settings.MEDIA_ROOT = cls._old_media_root
super().tearDownClass()
def test_thaboid_get_absolute_url(self): def test_thaboid_get_absolute_url(self):
self.assertEqual(self.thabloid.get_absolute_url(), self.assertEqual(self.thabloid.get_absolute_url(),
...@@ -49,3 +43,52 @@ class TestThabloid(TestCase): ...@@ -49,3 +43,52 @@ class TestThabloid(TestCase):
self.assertEqual( self.assertEqual(
self.thabloid.page_url(20), self.thabloid.page_url(20),
'public/thabloids/pages/thabloid-1998-1999-1/020.jpg') '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))
Supports Markdown
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