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', |
---|
21 | 'graduated', 'transcript requested', 'Total') |
---|
22 | |
---|
23 | def test_iface(self): |
---|
24 | # ensure we fullfill interface contracts |
---|
25 | obj = StudentsReport(2010, 'Undergraduate Full-Time') |
---|
26 | verifyClass(IStudentsReport, StudentsReport) |
---|
27 | verifyObject(IStudentsReport, obj) |
---|
28 | return |
---|
29 | |
---|
30 | def test_get_students_by_session_simple(self): |
---|
31 | # we can get a table with one student |
---|
32 | result1 = get_students_by(2010, 'Undergraduate Full-Time') |
---|
33 | result2 = get_students_by(2009, 'Undergraduate Full-Time') |
---|
34 | self.assertEqual( |
---|
35 | result1, |
---|
36 | ((u'fac1', u'Total',), |
---|
37 | self.states, |
---|
38 | ((1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), |
---|
39 | (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),))) |
---|
40 | self.assertEqual( |
---|
41 | result2, |
---|
42 | ((u'fac1', u'Total'), |
---|
43 | self.states, |
---|
44 | ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
45 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),))) |
---|
46 | return |
---|
47 | |
---|
48 | def test_get_students_by_session_multiple(self): |
---|
49 | # we can get a table with several students |
---|
50 | self.create_cert('fac2', 'dept2', 'CERT2') |
---|
51 | result1 = get_students_by(2010, 'Undergraduate Full-Time') |
---|
52 | result2 = get_students_by(2009, 'Undergraduate Full-Time') |
---|
53 | self.assertEqual( |
---|
54 | result1, |
---|
55 | ((u'fac1', u'fac2', u'Total'), |
---|
56 | self.states, |
---|
57 | ((1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), |
---|
58 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
59 | (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),))) |
---|
60 | self.assertEqual( |
---|
61 | result2, |
---|
62 | ((u'fac1', u'fac2', u'Total'), |
---|
63 | self.states, |
---|
64 | ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
65 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), |
---|
66 | (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),))) |
---|
67 | return |
---|
68 | |
---|
69 | def test_create_pdf(self): |
---|
70 | self.create_cert('FAC2', 'dept2', 'CERT2') |
---|
71 | report = StudentsReport(2010, 'Undergraduate Full-Time') |
---|
72 | result = report.create_pdf() |
---|
73 | self.assertTrue(result.startswith('%PDF-')) |
---|
74 | return |
---|
75 | |
---|
76 | class StudentsReportUITests(StudentsFullSetup, FunctionalAsyncTestCase): |
---|
77 | |
---|
78 | layer = FunctionalLayer |
---|
79 | |
---|
80 | def wait_for_report_job_completed(self): |
---|
81 | # helper function waiting until the current export job is completed |
---|
82 | manager = getUtility(IJobManager) |
---|
83 | job_id = self.app['reports'].running_report_jobs[0][0] |
---|
84 | job = manager.get(job_id) |
---|
85 | wait_for_result(job) |
---|
86 | return job_id |
---|
87 | |
---|
88 | def stored_in_reports(self, job_id): |
---|
89 | # tell whether job_id is stored in reports's running jobs list |
---|
90 | for entry in list(self.app['reports'].running_report_jobs): |
---|
91 | if entry[0] == job_id: |
---|
92 | return True |
---|
93 | return False |
---|
94 | |
---|
95 | def trigger_report_creation(self): |
---|
96 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
97 | self.browser.open('http://localhost/app/reports') |
---|
98 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
99 | self.browser.getLink("Create new report").click() |
---|
100 | self.browser.getControl(name="generator").value = ['students_by'] |
---|
101 | self.browser.getControl("Configure").click() |
---|
102 | self.browser.getControl(name="mode").value = ['All'] |
---|
103 | self.browser.getControl(name="session").value = ['2004'] |
---|
104 | self.browser.getControl("Create").click() |
---|
105 | return |
---|
106 | |
---|
107 | def test_report_download(self): |
---|
108 | # We can download a generated report |
---|
109 | self.trigger_report_creation() |
---|
110 | # When the job is finished and we reload the page... |
---|
111 | job_id = self.wait_for_report_job_completed() |
---|
112 | self.browser.open('http://localhost/app/reports') |
---|
113 | # ... the pdf file can be downloaded ... |
---|
114 | self.browser.getControl("Download").click() |
---|
115 | self.assertEqual(self.browser.headers['content-type'], |
---|
116 | 'application/pdf') |
---|
117 | self.assertTrue( |
---|
118 | 'filename="StudentsReport_2004_2005_All_' in |
---|
119 | self.browser.headers['content-disposition']) |
---|
120 | self.assertEqual(len(self.app['reports'].running_report_jobs), 1) |
---|
121 | job_id = self.app['reports'].running_report_jobs[0][0] |
---|
122 | # ... and discarded |
---|
123 | self.browser.open('http://localhost/app/reports') |
---|
124 | self.browser.getControl("Discard").click() |
---|
125 | self.assertEqual(len(self.app['reports'].running_report_jobs), 0) |
---|
126 | # Creation, downloading and discarding is logged |
---|
127 | logfile = os.path.join( |
---|
128 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
129 | logcontent = open(logfile).read() |
---|
130 | self.assertTrue( |
---|
131 | 'INFO - zope.mgr - students.reports.StudentsReportGeneratorPage - ' |
---|
132 | 'report %s created: Students (session=2004, mode=All)' |
---|
133 | % job_id in logcontent |
---|
134 | ) |
---|
135 | self.assertTrue( |
---|
136 | 'INFO - zope.mgr - students.reports.StudentsReportPDFView - ' |
---|
137 | 'report %s downloaded: StudentsReport_2004_2005_All_' |
---|
138 | % job_id in logcontent |
---|
139 | ) |
---|
140 | self.assertTrue( |
---|
141 | 'INFO - zope.mgr - browser.reports.ReportsContainerPage - ' |
---|
142 | 'report %s discarded' % job_id in logcontent |
---|
143 | ) |
---|
144 | return |
---|