-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cert_gen.py
179 lines (145 loc) · 5.44 KB
/
test_cert_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
import os.path
import unittest
import shutil
from mock import create_autospec
from cert_gen import generador_csv
from cert_gen import generate_filename, certificates_generator
from cert_gen import all_students_certificates
from cert_gen import Certificate
class TestGeneradorCertificaciones(unittest.TestCase):
def setUp(self):
self.un_asistente = {
"Apellido": "Perez",
"Nombre": "Juan",
"Curso": "Un curso"
}
def test_generador_csv(self):
a, b = '1,2,3', '4,5,6'
for r, e in zip(
generador_csv(['a,b,c', a, b]),
({'a': '1', 'b': '2', 'c': '3'}, {'a': '4', 'b': '5', 'c': '6'})
):
self.assertEqual(r, e)
def test_genera_nombres(self):
filename = generate_filename("Asistencia", '', self.un_asistente)
self.assertEqual(
filename,
"Kleer - Certificado Asistencia Un curso" +
" - Juan Perez.pdf"
)
def test_genera_nombres(self):
filename = generate_filename("Asistencia", 'pepe', self.un_asistente)
self.assertEqual(
filename,
"pepe/Kleer - Certificado Asistencia Un curso - Juan Perez.pdf"
)
def test_no_tiene_columna_examen(self):
students = [{'Curso': '', 'Nombre': '', 'Apellido': ''}]
attended_cert = create_autospec(Certificate)
all_students_certificates(students, attended_cert, None)
self.assertTrue(True)
def test_one_attended(self):
students = [{'Curso': '', 'Nombre': '', 'Apellido': ''}]
attended_cert = create_autospec(Certificate)
certified_cert = create_autospec(Certificate)
all_students_certificates(students, attended_cert, certified_cert)
self.assertTrue(attended_cert.generate.called)
self.assertFalse(certified_cert.generate.called)
def test_encabezamiento_con_espacio(self):
students = [{
' Curso ': 'lindo',
' Nombre ': 'pepe',
' Apellido ': 'garcia'
}]
attended_cert = Certificate()
all_students_certificates(students, attended_cert, None)
self.assertTrue(True)
### Unit Tests ####
class TestCertificate(unittest.TestCase):
def setUp(self):
self.certificate = Certificate()
self.student = {
"Apellido": "Perez",
"Nombre": "Juan",
"Curso": "Introduccion a Scrum",
"Email": "[email protected]"
}
self.HTMLTEST = """
<html><body>
<p>Hello <strong style="color: #f00;">World</strong>
<hr>
<table border="1" style="background: #eee; padding: 0.5em;">
<tr>
<td>$Apellido</td>
<td>$Nombre</td>
<td>$Curso</td>
</tr>
</table>
</body></html>
"""
def test_replace_one_variable(self):
certificate = Certificate()
certificate.template = "Are you $nombre?"
modified = certificate.replace_variables(nombre="Juan")
self.assertEqual("Are you Juan?", modified)
def test_unknow_variable_raise_exception(self):
certificate = Certificate()
certificate.template = "Hola $nombre"
with self.assertRaises(KeyError):
certificate.replace_variables(pepe="Juan")
def test_replace_2_variables(self):
certificate = Certificate()
certificate.template = "$que $nombre"
modified = certificate.replace_variables(nombre="Juan", que="Hi")
self.assertEqual("Hi Juan", modified)
def test_generate_create_a_pdf(self):
self.certificate.template = self.HTMLTEST
out = generate_filename(self.certificate.type, '', self.student)
self.certificate.output_file = out
if os.path.isfile(out):
os.remove(out)
ok = self.certificate.generate(**self.student)
self.assertTrue(ok)
self.assertTrue(os.path.isfile(out), "Not found {}".format(out))
def test_make_output_dir(self):
folder = "here"
if os.path.exists(folder):
shutil.rmtree(folder)
Certificate(output_path=folder)
self.assertTrue(os.path.isdir(folder))
def test_read_templates_file(self):
attended = "attended_tmpl.html"
certificate = Certificate(template=attended)
self.assertIn('<html>', certificate.template)
def test_type_in_filename(self):
certificate = Certificate(
template="attended_tmpl.html",
type="Asistencia"
)
output_file = certificate.generate(
Apellido="Perez",
Nombre="Juan",
Curso="Introduccion a Scrum",
Email="[email protected]"
)
self.assertEqual(
output_file,
"Kleer - Certificado Asistencia Introduccion a Scrum" +
" - Juan Perez.pdf"
)
### Integration Tests ####
class IntegrationTest(unittest.TestCase):
def test_integracion_basica(self):
input = "input.csv"
out = ("Kleer - Certificado Asistencia " +
"Introduccion a Scrum - Juan Perez.pdf")
attended = "attended_tmpl.html"
certified = "certified_tmpl.html"
output_path = ''
if os.path.isfile(out):
os.remove(out)
certificates_generator(input, attended, certified, output_path)
self.assertTrue(os.path.isfile(out), out)
if __name__ == '__main__':
unittest.main()