"""
Binder model tests.
"""
import glob
import os
import unittest

from django.conf import settings
from rldjango.apps.binder.models import Pdf
from utils import check_test_mode

settings.FLIPBOOK_IMAGES = (
    ('normal', 0.71),
    ('large', 1.0),
    )


class PdfTests(unittest.TestCase):
    """
    Pdf processing related tests.
    """

    def setUp(self): # pylint: disable-msg=C0103
        """
        Prepare variables.
        """
        check_test_mode()
        self.pdf = Pdf.objects.get(pk=1)
        
    def test_pdf_save(self):
        """
        Remove height and width from pdf then resave and check values.
        """
        orig_width = self.pdf.width
        orig_height = self.pdf.height

        self.pdf.width = 0
        self.pdf.height = 0

        self.pdf.save()

        self.assertEquals(orig_width, self.pdf.width)
        self.assertEquals(orig_height, self.pdf.height)

    def test_generate_pdf_data(self):
        """
        First check if section folder for pdfs then try to generate
        data file of parsed PDF and then check again for js info.
        """
        self.assertFalse(
            glob.glob(os.path.join(settings.SECTIONS_ROOT, '*.pdf')),
            'Test sections folder is not empty.')

        self.pdf.generate_pdf_data()

        files = map(os.path.basename, glob.glob(os.path.join(settings.SECTIONS_ROOT, '*.data')))
        
        self.assertTrue(
            'binder_test_pdf.data' in files,
            'PageCatcher parsing generation seems to be broken.')

        files = map(os.path.basename, glob.glob(os.path.join(settings.SECTIONS_ROOT, '*.js')))

        self.assertTrue(
            'binder_test_pdf.js' in files,
            'Pdf info generation seems to be broken.')


    def test_generate_images(self):
        """
        First check section folder for images then try to generate
        sections images and check again for images and large folder..
        """
        self.assertFalse(
            glob.glob(os.path.join(settings.SECTIONS_ROOT, '*.jpg')),
            'Test sections folder is not empty.')


        self.pdf.generate_images()
        
        files = map(os.path.basename, glob.glob(os.path.join(settings.SECTIONS_ROOT, '*.jpg')))
        
        self.assertTrue(
            'binder_test_pdf_page_0006.jpg' in files,
            'PDF image page generation seems to be broken.')

        files = map(os.path.basename, glob.glob(os.path.join(
            settings.SECTIONS_ROOT, 'large', '*.jpg')))

        self.assertTrue(
            'binder_test_pdf_page_0006.jpg' in files,
            'Sections large images generation seems to be broken.')
