1 | # Tests for student related reports |
---|
2 | import os |
---|
3 | from zc.async.testing import wait_for_result |
---|
4 | from zope.interface.verify import verifyClass, verifyObject |
---|
5 | from zope.component import getUtility |
---|
6 | from waeup.kofa.interfaces import IJobManager |
---|
7 | from waeup.kofa.students.reports import ( |
---|
8 | get_students_by, StudentsReport, IStudentsReport) |
---|
9 | from waeup.kofa.students.tests.test_catalog import CatalogTestSetup |
---|
10 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
11 | from waeup.kofa.testing import FunctionalLayer |
---|
12 | from waeup.kofa.tests.test_async import FunctionalAsyncTestCase |
---|
13 | |
---|
14 | class StudentsReportTests(CatalogTestSetup): |
---|
15 | |
---|
16 | layer = FunctionalLayer |
---|
17 | |
---|
18 | states = ('created', 'admitted', 'clearance started', |
---|
19 | 'clearance requested', 'cleared', 'school fee paid', |
---|
20 | 'courses registered', 'courses validated', 'returning', 'Total') |
---|
21 | |
---|
22 | def test_iface(self): |
---|
23 | # ensure we fullfill interface contracts |
---|
24 | obj = StudentsReport(2010, 'Undergraduate Full-Time') |
---|
25 | verifyClass(IStudentsReport, StudentsReport) |
---|
26 | verifyObject(IStudentsReport, obj) |
---|
27 | return |
---|
28 | |
---|
29 | def test_get_students_by_session_simple(self): |
---|
30 | # we can get a table with one student |
---|
31 | result1 = get_students_by(2010, 'Undergraduate Full-Time') |
---|
32 | result2 = get_students_by(2009, 'Undergraduate Full-Time') |
---|
33 | self.assertEqual( |
---|
34 | result1, |
---|
35 | ((u'fac1', u'Total',), |
---|
36 | self.states, |
---|
37 | ((1, 0, 0, 0, 0, 0, 0, 0, 0, 1), |
---|
38 | (1, 0, 0, 0, 0, 0, 0, 0, 0, 1),))) |
---|
39 | self.assertEqual( |
---|
40 | result2, |
---|
41 | ((u'fac1', u'Total'), |
---|
42 | self.states, |
---|
43 | ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
44 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0),))) |
---|
45 | return |
---|
46 | |
---|
47 | def test_get_students_by_session_multiple(self): |
---|
48 | # we can get a table with several students |
---|
49 | self.create_cert('fac2', 'dept2', 'CERT2') |
---|
50 | result1 = get_students_by(2010, 'Undergraduate Full-Time') |
---|
51 | result2 = get_students_by(2009, 'Undergraduate Full-Time') |
---|
52 | self.assertEqual( |
---|
53 | result1, |
---|
54 | ((u'fac1', u'fac2', u'Total'), |
---|
55 | self.states, |
---|
56 | ((1, 0, 0, 0, 0, 0, 0, 0, 0, 1), |
---|
57 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
58 | (1, 0, 0, 0, 0, 0, 0, 0, 0, 1),))) |
---|
59 | self.assertEqual( |
---|
60 | result2, |
---|
61 | ((u'fac1', u'fac2', u'Total'), |
---|
62 | self.states, |
---|
63 | ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
64 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
65 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0),))) |
---|
66 | return |
---|
67 | |
---|
68 | def test_create_pdf(self): |
---|
69 | self.create_cert('FAC2', 'dept2', 'CERT2') |
---|
70 | report = StudentsReport(2010, 'Undergraduate Full-Time') |
---|
71 | result = report.create_pdf() |
---|
72 | self.assertTrue(result.startswith('%PDF-')) |
---|
73 | return |
---|
74 | |
---|
75 | class 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 | 'report %s created: Students (session=2004, mode=All)' |
---|
132 | % job_id in logcontent |
---|
133 | ) |
---|
134 | self.assertTrue( |
---|
135 | 'INFO - zope.mgr - students.reports.StudentsReportPDFView - ' |
---|
136 | 'report %s downloaded: StudentsReport_2004_2005_All_' |
---|
137 | % job_id in logcontent |
---|
138 | ) |
---|
139 | self.assertTrue( |
---|
140 | 'INFO - zope.mgr - browser.reports.ReportsContainerPage - ' |
---|
141 | 'report %s discarded' % job_id in logcontent |
---|
142 | ) |
---|
143 | return |
---|