"""This module holds the report tests of the ``pdf_reporting`` app."""

import os
import shutil

from django.conf import settings

from mtp.apps.common.tests.test_cases import MTPTestCase
from ..pdf_report import PDFReport


class PDFReportTestCase(MTPTestCase):
    """Test case that tests the ``PDFReport`` class."""

    @classmethod
    def setUpClass(cls):
        """Set up needed attributes and create file path."""
        super().setUpClass()

        cls.filename = 'file.pdf'
        cls.path = os.path.join(settings.PROJECT_ROOT, 'fake/path/')
        cls.file_path = os.path.join(cls.path, cls.filename)

        # We create path to save the file
        if not os.path.exists(cls.path):
            os.makedirs(cls.path)

        # We create the pdf report instance
        template_name = 'pdf_reporting_testapp/rml_test.rml'
        cls.pdf_report = PDFReport(template_name=template_name, context_data={'foo': 'bar'})

    @classmethod
    def tearDownClass(cls):
        """Remove file path and its content."""
        super().tearDownClass()

        # We remove file and its path
        shutil.rmtree(cls.path, ignore_errors=True)

    def test_get_pdf_context_data(self):
        """Test that the ``get_pdf_context_data`` method works."""

        context_data = self.pdf_report._get_pdf_context_data()
        self.assertEqual(context_data['foo'], 'bar')
        self.assertEqual(context_data['theme'], self.theme)

    def test_get_rml_content(self):
        """Test that the ``get_rml_content`` method works."""

        rml_content = self.pdf_report._get_rml_content()
        self.assertIsNotNone(rml_content)

    def test_get_file_data(self):
        """Test that the ``get_file_data`` method works."""

        file_data = self.pdf_report.get_file_content()
        self.assertIsNotNone(file_data)

    def test_generate_file(self):
        """Test that the ``generate_file`` method works."""

        self.pdf_report.generate_file(self.file_path)
        self.assertTrue(os.path.exists(self.file_path))
        self.assertNotEqual(os.stat(self.file_path).st_size, 0)
