Changeset 9678


Ignore:
Timestamp:
18 Nov 2012, 09:19:35 (12 years ago)
Author:
Henrik Bettermann
Message:

Add UI tests for for students reports.py.

Fix render method of StudentsReportPDFView.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/reports.py

    r9673 r9678  
    124124        self.mode = mode
    125125        self.author = author
    126         self._creation_dt_string = self.creation_dt.astimezone(
     126        self.creation_dt_string = self.creation_dt.astimezone(
    127127            getUtility(IKofaUtils).tzinfo).strftime("%Y-%m-%d %H:%M:%S %Z")
    128128        self.data = get_students_by(session, mode)
     
    132132        table_data = tbl_data_to_table(*self.data)
    133133        col_widths = [None,] + [1.6*cm] * len(self.data[1]) + [None,]
    134         pdf_data = [Paragraph('<b>%s</b>' % self._creation_dt_string,
     134        pdf_data = [Paragraph('<b>%s</b>' % self.creation_dt_string,
    135135                              STYLE["Normal"]),
    136136                    Spacer(1, 12),]
     
    218218        filename = 'StudentsReport_%s_%s_%s.pdf' % (
    219219            self.context.session, self.context.mode,
    220             self._creation_dt_string)
     220            self.context.creation_dt_string)
    221221        filename = filename.replace(
    222222            '/', '_').replace(' ','_').replace(':', '-')
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_reports.py

    r9673 r9678  
    11# Tests for student related reports
     2import os
     3from zc.async.testing import wait_for_result
    24from zope.interface.verify import verifyClass, verifyObject
     5from zope.component import getUtility
     6from waeup.kofa.interfaces import IJobManager
    37from waeup.kofa.students.reports import (
    48    get_students_by, StudentsReport, IStudentsReport)
    59from waeup.kofa.students.tests.test_catalog import CatalogTestSetup
     10from waeup.kofa.students.tests.test_browser import StudentsFullSetup
    611from waeup.kofa.testing import FunctionalLayer
     12from waeup.kofa.tests.test_async import FunctionalAsyncTestCase
    713
    814class StudentsReportTests(CatalogTestSetup):
     
    6672        self.assertTrue(result.startswith('%PDF-'))
    6773        return
     74
     75class StudentsReportUITests(StudentsFullSetup, FunctionalAsyncTestCase):
     76
     77    layer = FunctionalLayer
     78
     79    def wait_for_report_job_completed(self):
     80        # helper function waiting until the current export job is completed
     81        manager = getUtility(IJobManager)
     82        job_id = self.app['reports'].running_report_jobs[0][0]
     83        job = manager.get(job_id)
     84        wait_for_result(job)
     85        return job_id
     86
     87    def stored_in_reports(self, job_id):
     88        # tell whether job_id is stored in reports's running jobs list
     89        for entry in list(self.app['reports'].running_report_jobs):
     90            if entry[0] == job_id:
     91                return True
     92        return False
     93
     94    def trigger_report_creation(self):
     95        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     96        self.browser.open('http://localhost/app/reports')
     97        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     98        self.browser.getLink("Create new report").click()
     99        self.browser.getControl(name="generator").value = ['students_by']
     100        self.browser.getControl("Configure").click()
     101        self.browser.getControl(name="mode").value = ['All']
     102        self.browser.getControl(name="session").value = ['2004']
     103        self.browser.getControl("Create").click()
     104        return
     105
     106    def test_report_download(self):
     107        # We can download a generated report
     108        self.trigger_report_creation()
     109        # When the job is finished and we reload the page...
     110        job_id = self.wait_for_report_job_completed()
     111        self.browser.open('http://localhost/app/reports')
     112        # ... the pdf file can be downloaded ...
     113        self.browser.getControl("Download").click()
     114        self.assertEqual(self.browser.headers['content-type'],
     115                         'application/pdf')
     116        self.assertTrue(
     117            'filename="StudentsReport_2004_2005_All_' in
     118            self.browser.headers['content-disposition'])
     119        self.assertEqual(len(self.app['reports'].running_report_jobs), 1)
     120        job_id = self.app['reports'].running_report_jobs[0][0]
     121        # ... and discarded
     122        self.browser.open('http://localhost/app/reports')
     123        self.browser.getControl("Discard").click()
     124        self.assertEqual(len(self.app['reports'].running_report_jobs), 0)
     125        # Creation, downloading and discarding is logged
     126        logfile = os.path.join(
     127            self.app['datacenter'].storage, 'logs', 'main.log')
     128        logcontent = open(logfile).read()
     129        self.assertTrue(
     130            'INFO - zope.mgr - students.reports.StudentsReportGeneratorPage - '
     131            'created: Students (session=2004, mode=All)' in logcontent
     132            )
     133        self.assertTrue(
     134            'INFO - zope.mgr - students.reports.StudentsReportPDFView - '
     135            'downloaded: StudentsReport_2004_2005_All_' in logcontent
     136            )
     137        self.assertTrue(
     138            'INFO - zope.mgr - browser.reports.ReportsContainerPage - '
     139            'discarded: %s' % job_id in logcontent
     140            )
     141        return
Note: See TracChangeset for help on using the changeset viewer.