From 655700e4cde69f00f698a06ca18991eef784ccda Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 29 Mar 2012 11:53:35 +0200 Subject: [PATCH] update tests --- apps/catalogue/models.py | 47 ++++++++++++------ apps/catalogue/templatetags/catalogue_tags.py | 5 +- apps/catalogue/tests/book_import.py | 23 ++++----- apps/catalogue/tests/tags.py | 45 +++++++++-------- apps/picture/tests/picture_import.py | 10 +--- wolnelektury/settings/__init__.py | 49 ++++++++++--------- 6 files changed, 98 insertions(+), 81 deletions(-) diff --git a/apps/catalogue/models.py b/apps/catalogue/models.py index 53caa956d..8b8aa0ac6 100644 --- a/apps/catalogue/models.py +++ b/apps/catalogue/models.py @@ -244,7 +244,7 @@ class BookMedia(models.Model): try: old = BookMedia.objects.get(pk=self.pk) - except BookMedia.DoesNotExist, e: + except BookMedia.DoesNotExist: old = None else: # if name changed, change the file name, too @@ -398,18 +398,18 @@ class Book(models.Model): book_tag.save() return book_tag - def has_media(self, type): - if type in Book.formats: - return bool(getattr(self, "%s_file" % type)) + def has_media(self, type_): + if type_ in Book.formats: + return bool(getattr(self, "%s_file" % type_)) else: - return self.media.filter(type=type).exists() + return self.media.filter(type=type_).exists() - def get_media(self, type): - if self.has_media(type): - if type in Book.formats: - return getattr(self, "%s_file" % type) + def get_media(self, type_): + if self.has_media(type_): + if type_ in Book.formats: + return getattr(self, "%s_file" % type_) else: - return self.media.filter(type=type) + return self.media.filter(type=type_) else: return None @@ -531,6 +531,16 @@ class Book(models.Model): return True return False + # Thin wrappers for builder tasks + def build_pdf(self, *args, **kwargs): + return tasks.build_pdf.delay(self.pk, *args, **kwargs) + def build_epub(self, *args, **kwargs): + return tasks.build_epub.delay(self.pk, *args, **kwargs) + def build_mobi(self, *args, **kwargs): + return tasks.build_mobi.delay(self.pk, *args, **kwargs) + def build_txt(self, *args, **kwargs): + return tasks.build_txt.delay(self.pk, *args, **kwargs) + @staticmethod def zip_format(format_): def pretty_file_name(book): @@ -592,7 +602,7 @@ class Book(models.Model): for part_url in book_info.parts: try: children.append(Book.objects.get(slug=part_url.slug)) - except Book.DoesNotExist, e: + except Book.DoesNotExist: raise Book.DoesNotExist(_('Book "%s" does not exist.') % part_url.slug) @@ -640,18 +650,18 @@ class Book(models.Model): if book.build_html(): if not settings.NO_BUILD_TXT and build_txt: - tasks.build_txt.delay(book.pk) + book.build_txt() book.build_cover(book_info) if not settings.NO_BUILD_EPUB and build_epub: - tasks.build_epub.delay(book.pk) + book.build_epub() if not settings.NO_BUILD_PDF and build_pdf: - tasks.build_pdf.delay(book.pk) + book.build_pdf() if not settings.NO_BUILD_MOBI and build_mobi: - tasks.build_mobi.delay(book.pk) + book.build_mobi() if not settings.NO_SEARCH_INDEX and search_index: book.search_index(index_tags=search_index_tags, reuse_index=search_index_reuse) @@ -711,6 +721,13 @@ class Book(models.Model): type(self).objects.filter(pk=self.pk).update(_related_info=rel) return rel + def related_themes(self): + theme_counter = self.theme_counter + book_themes = Tag.objects.filter(pk__in=theme_counter.keys()) + for tag in book_themes: + tag.count = theme_counter[tag.pk] + return book_themes + def reset_tag_counter(self): if self.id is None: return diff --git a/apps/catalogue/templatetags/catalogue_tags.py b/apps/catalogue/templatetags/catalogue_tags.py index e4ed6e82a..e5e4d4fa0 100644 --- a/apps/catalogue/templatetags/catalogue_tags.py +++ b/apps/catalogue/templatetags/catalogue_tags.py @@ -295,10 +295,7 @@ def book_info(book): @register.inclusion_tag('catalogue/book_wide.html', takes_context=True) def book_wide(context, book): - theme_counter = book.theme_counter - book_themes = Tag.objects.filter(pk__in=theme_counter.keys()) - for tag in book_themes: - tag.count = theme_counter[tag.pk] + book_themes = book.related_themes() extra_info = book.get_extra_info_value() hide_about = extra_info.get('about', '').startswith('http://wiki.wolnepodreczniki.pl') diff --git a/apps/catalogue/tests/book_import.py b/apps/catalogue/tests/book_import.py index 3af1bb486..6ece3287e 100644 --- a/apps/catalogue/tests/book_import.py +++ b/apps/catalogue/tests/book_import.py @@ -7,8 +7,7 @@ from catalogue import models from librarian import WLURI from nose.tools import raises -import tempfile -from os import unlink, path, makedirs +from os import path, makedirs class BookImportLogicTests(WLTestCase): @@ -237,9 +236,7 @@ class ChildImportTests(WLTestCase): """ child = models.Book.from_text_and_meta(ContentFile(CHILD_TEXT), self.child_info, overwrite=True) - - themes = self.client.get(parent.get_absolute_url()).context['book_themes'] - + themes = parent.related_themes() self.assertEqual(['Kot'], [tag.name for tag in themes], 'wrong related theme list') @@ -283,26 +280,30 @@ class MultilingualBookImportTest(WLTestCase): class BookImportGenerateTest(WLTestCase): def setUp(self): WLTestCase.setUp(self) - input = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml') - self.book = models.Book.from_xml_file(input) + xml = path.join(path.dirname(__file__), 'files/fraszka-do-anusie.xml') + self.book = models.Book.from_xml_file(xml) def test_gen_pdf(self): self.book.build_pdf() - self.assertTrue(path.exists(self.book.pdf_file.path)) + book = models.Book.objects.get(pk=self.book.pk) + self.assertTrue(path.exists(book.pdf_file.path)) def test_gen_pdf_parent(self): """This book contains a child.""" - input = path.join(path.dirname(__file__), "files/fraszki.xml") - parent = models.Book.from_xml_file(input) + xml = path.join(path.dirname(__file__), "files/fraszki.xml") + parent = models.Book.from_xml_file(xml) parent.build_pdf() + parent = models.Book.objects.get(pk=parent.pk) self.assertTrue(path.exists(parent.pdf_file.path)) def test_custom_pdf(self): + from catalogue.tasks import build_custom_pdf out = models.get_dynamic_path(None, 'test-custom', ext='pdf') absoulute_path = path.join(settings.MEDIA_ROOT, out) if not path.exists(path.dirname(absoulute_path)): makedirs(path.dirname(absoulute_path)) - self.book.build_pdf(customizations=['nofootnotes', '13pt', 'a4paper'], file_name=out) + build_custom_pdf(self.book.id, + customizations=['nofootnotes', '13pt', 'a4paper'], file_name=out) self.assertTrue(path.exists(absoulute_path)) diff --git a/apps/catalogue/tests/tags.py b/apps/catalogue/tests/tags.py index a47e426a5..3eab3da4d 100644 --- a/apps/catalogue/tests/tags.py +++ b/apps/catalogue/tests/tags.py @@ -1,7 +1,9 @@ # -*- coding: utf-8 -*- +from django.core.files.base import ContentFile +from django.test import Client from catalogue import models from catalogue.test_utils import * -from django.core.files.base import ContentFile + class BooksByTagTests(WLTestCase): """ tests the /katalog/category/tag page for found books """ @@ -63,7 +65,6 @@ class BooksByTagTests(WLTestCase): ['Child']) -from django.test import Client class TagRelatedTagsTests(WLTestCase): """ tests the /katalog/category/tag/ page for related tags """ @@ -173,7 +174,7 @@ class CleanTagRelationTests(WLTestCase): """ - book = models.Book.from_text_and_meta(ContentFile(book_text), book_info) + self.book = models.Book.from_text_and_meta(ContentFile(book_text), book_info) def test_delete_objects(self): """ there should be no related tags left after deleting some objects """ @@ -190,8 +191,8 @@ class CleanTagRelationTests(WLTestCase): """ there should be no tag relations left after deleting tags """ models.Tag.objects.all().delete() - cats = self.client.get('/katalog/lektura/book/').context['categories'] - self.assertEqual(cats, {}) + self.assertEqual(len(self.book.related_info()['tags']), 0) + self.assertEqual(len(self.book.related_themes()), 0) self.assertEqual(models.Tag.intermediary_table_model.objects.all().count(), 0, "orphaned TagRelation objects left") @@ -219,13 +220,14 @@ class TestIdenticalTag(WLTestCase): def test_book_tags(self): """ there should be all related tags in relevant categories """ - models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info) + book = models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info) - context = self.client.get('/katalog/lektura/tag/').context + related_info = book.related_info() + related_themes = book.related_themes() for category in 'author', 'kind', 'genre', 'epoch': - self.assertTrue('tag' in [tag.slug for tag in context['categories'][category]], + self.assertTrue('tag' in [tag[1] for tag in related_info['tags'][category]], 'missing related tag for %s' % category) - self.assertTrue('tag' in [tag.slug for tag in context['book_themes']]) + self.assertTrue('tag' in [tag.slug for tag in related_themes]) def test_qualified_url(self): models.Book.from_text_and_meta(ContentFile(self.book_text), self.book_info) @@ -259,27 +261,28 @@ class BookTagsTests(WLTestCase): """ % info.title.encode('utf-8') - book = models.Book.from_text_and_meta(ContentFile(book_text), info) + models.Book.from_text_and_meta(ContentFile(book_text), info) def test_book_tags(self): """ book should have own tags and whole tree's themes """ - context = self.client.get('/katalog/lektura/parent/').context + book = models.Book.objects.get(slug='parent') + related_info = book.related_info() + related_themes = book.related_themes() - self.assertEqual([tag.name for tag in context['categories']['author']], - ['Common Man']) - self.assertEqual([tag.name for tag in context['categories']['kind']], - ['Kind']) - self.assertEqual([(tag.name, tag.count) for tag in context['book_themes']], + self.assertEqual(related_info['tags']['author'], + [('Common Man', 'common-man')]) + self.assertEqual(related_info['tags']['kind'], + [('Kind', 'kind')]) + self.assertEqual([(tag.name, tag.count) for tag in related_themes], [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)]) def test_main_page_tags(self): """ test main page tags and counts """ - - context = self.client.get('/katalog/').context - - self.assertEqual([(tag.name, tag.count) for tag in context['categories']['author']], + from catalogue.templatetags.catalogue_tags import catalogue_menu + menu = catalogue_menu() + self.assertEqual([(tag.name, tag.book_count) for tag in menu['author']], [('Jim Lazy', 1), ('Common Man', 1)]) - self.assertEqual([(tag.name, tag.count) for tag in context['fragment_tags']], + self.assertEqual([(tag.name, tag.book_count) for tag in menu['theme']], [('ChildTheme', 1), ('ParentTheme', 1), ('Theme', 2)]) diff --git a/apps/picture/tests/picture_import.py b/apps/picture/tests/picture_import.py index 91fb35f71..202acdd65 100644 --- a/apps/picture/tests/picture_import.py +++ b/apps/picture/tests/picture_import.py @@ -1,16 +1,10 @@ # -*- coding: utf-8 -*- from __future__ import with_statement -from django.core.files.base import ContentFile, File -from catalogue.test_utils import * -from catalogue import models -from librarian import WLURI +from os import path +from django.test import TestCase from picture.models import Picture -from nose.tools import raises -import tempfile -from os import unlink, path, makedirs - class PictureTest(TestCase): diff --git a/wolnelektury/settings/__init__.py b/wolnelektury/settings/__init__.py index 20057974e..679abec40 100644 --- a/wolnelektury/settings/__init__.py +++ b/wolnelektury/settings/__init__.py @@ -42,8 +42,32 @@ MIDDLEWARE_CLASSES = [ ROOT_URLCONF = 'wolnelektury.urls' -INSTALLED_APPS = [ +# These are the ones we should test. +INSTALLED_APPS_OUR = [ 'wolnelektury_core', + # our + 'ajaxable', + 'api', + 'catalogue', + 'chunks', + 'dictionary', + 'infopages', + 'lesmianator', + #'lessons', + 'newtagging', + 'opds', + 'pdcounter', + 'reporting', + 'sponsors', + 'stats', + 'suggest', + 'picture', + 'search', + 'social', + 'waiter', + ] + +INSTALLED_APPS_CONTRIB = [ # external 'django.contrib.auth', 'django.contrib.contenttypes', @@ -75,28 +99,9 @@ INSTALLED_APPS = [ # included 'compress', 'modeltranslation', + ] - # our - 'ajaxable', - 'api', - 'catalogue', - 'chunks', - 'dictionary', - 'infopages', - 'lesmianator', - #'lessons', - 'newtagging', - 'opds', - 'pdcounter', - 'reporting', - 'sponsors', - 'stats', - 'suggest', - 'picture', - 'search', - 'social', - 'waiter', -] +INSTALLED_APPS = INSTALLED_APPS_OUR + INSTALLED_APPS_CONTRIB # Load localsettings, if they exist try: -- 2.20.1