source: main/waeup.sirp/trunk/src/waeup/sirp/browser/batchprocessing.txt @ 6271

Last change on this file since 6271 was 6271, checked in by uli, 13 years ago

Fix test.

File size: 16.1 KB
Line 
1Batch processing via browser
2****************************
3
4:test-layer: functional
5
6Preliminaries:
7
8We define a function that looks up a form with several submit buttons
9for the one with a given value (this functionality is missing in
10zope.testbrowser):
11
12    >>> def lookup_submit_value(name, value, browser):
13    ...   """Find a button with a certain value."""
14    ...   for num in range(0, 100):
15    ...     try:
16    ...       button = browser.getControl(name=name, index=num)
17    ...       if button.value.endswith(value):
18    ...         return button
19    ...     except IndexError:
20    ...       break
21    ...   return None
22
23Create a site:
24
25    >>> from waeup.sirp.app import University
26    >>> getRootFolder()['app'] = University()
27
28Create a datacenter storage path:
29
30    >>> import os
31    >>> import tempfile
32    >>> dc_path = tempfile.mkdtemp()
33
34Log in:
35
36    >>> from zope.testbrowser.testing import Browser
37    >>> browser = Browser()
38    >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
39    >>> browser.handleErrors = False
40
41Set datacenter path and deselect moving old data:
42
43    >>> browser.open('http://localhost/app')
44    >>> browser.getLink('Data Center').click()
45    >>> browser.getLink('Edit settings').click()
46    >>> browser.getControl(name='newpath').value = dc_path
47    >>> browser.getControl(name='move').value = False
48    >>> browser.getControl(name='save').click()
49
50Batch processing faculties
51==========================
52
53Go to datacenter page:
54
55    >>> browser.open('http://localhost/app/datacenter')
56
57Prepare a CSV file for faculties:
58
59    >>> open('faculties.csv', 'wb').write(
60    ... """code,review_state,title,title_prefix
61    ... FAC1,static,Faculty 1,faculty
62    ... FAC2,static,Faculty 2,institute
63    ... FAC3,static,Faculty 3,school_of
64    ... """)
65
66Upload the file:
67
68    >>> import cStringIO
69    >>> browser.getLink('Upload CSV file').click()
70    >>> filecontents = cStringIO.StringIO(
71    ...   open('faculties.csv', 'rb').read())
72    >>> filewidget = browser.getControl(name='uploadfile:file')
73    >>> filewidget.add_file(filecontents, 'text/plain', 'faculties.csv')
74    >>> browser.getControl(name='SUBMIT').click()
75
76Step 1: start batch processing:
77
78    >>> browser.getLink('Batch processing').click()
79    >>> button = lookup_submit_value(
80    ...   'select', 'faculties_zope.mgr.csv', browser)
81    >>> button.click()
82
83Step 2: select a processor and mode:
84
85    >>> importerselect = browser.getControl(name='importer')
86    >>> importerselect.displayOptions
87    ['Applicants Container Importer',
88     'JAMB Data Importer',
89     'CertificateCourse importer',
90     'Certificate importer',
91     'Course importer',
92     'Department importer',
93     'Faculty importer']
94
95    >>> importerselect.getControl('Faculty importer').selected = True
96
97    >>> modeselect = browser.getControl(name='mode')
98    >>> modeselect.options
99    ['create', 'update', 'remove']
100
101    >>> modeselect.getControl(value='create').selected = True
102    >>> browser.getControl('Proceed to step 3...').click()
103
104Step 3: Fix headerlines
105
106We get informed that there are no problems with the current header:
107
108    >>> print browser.contents
109    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
110    ...
111    Header fields OK
112    ...
113
114The submit button is enabled:
115
116    >>> browser.getControl('Perform import...').disabled
117    False
118
119    >>> browser.getControl('Perform import...').click()
120
121Step 4: See import results
122
123The import was successful:
124
125    >>> print browser.contents
126    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
127    ...Successfully processed 3 rows...
128    ...Batch processing finished...
129    ...File:...faculties_zope.mgr.csv...
130
131We can watch the entries generated in logfile:
132
133    >>> browser.getControl('View processing log').click()
134    >>> print browser.contents
135    <!DOCTYPE ...
136    <pre>...
137 
138    ... - INFO - --------------------
139    ... - INFO - zope.mgr: Batch processing finished: OK
140    ... - INFO - zope.mgr: Source: /.../faculties_zope.mgr.csv
141    ... - INFO - zope.mgr: Mode: create
142    ... - INFO - zope.mgr: User: zope.mgr
143    ... - INFO - zope.mgr: Processing time: ... s (... s/item)
144    ... - INFO - zope.mgr: Processed: 3 lines (3 successful/ 0 failed)
145    ... - INFO - --------------------
146    </pre>
147    ....
148
149
150Batch processing departments
151============================
152
153    >>> browser.open('http://localhost/app/datacenter')
154
155Prepare a CSV file for departments:
156
157    >>> open('departments.csv', 'wb').write(
158    ... """code,faculty_code,review_state,title,title_prefix
159    ... DEP1,FAC1,static,Department 1,department
160    ... DEP2,FAC2,static,Department 2,centre
161    ... """)
162
163Upload the file:
164
165    >>> import cStringIO
166    >>> browser.getLink('Upload CSV file').click()
167    >>> filecontents = cStringIO.StringIO(
168    ...   open('departments.csv', 'rb').read())
169    >>> filewidget = browser.getControl(name='uploadfile:file')
170    >>> filewidget.add_file(filecontents, 'text/plain', 'departments.csv')
171    >>> browser.getControl(name='SUBMIT').click()
172
173Step 1: start batch processing:
174
175    >>> browser.getLink('Batch processing').click()
176    >>> button = lookup_submit_value(
177    ...   'select', 'departments_zope.mgr.csv', browser)
178    >>> button.click()
179
180Step 2: select a processor and mode:
181
182    >>> importerselect = browser.getControl(name='importer')
183    >>> importerselect.getControl('Department importer').selected = True
184    >>> modeselect = browser.getControl(name='mode')
185    >>> modeselect.getControl(value='create').selected = True
186    >>> browser.getControl('Proceed to step 3...').click()
187
188Step 3: Fix headerlines
189
190We get informed that there are no problems with the current header:
191
192    >>> print browser.contents
193    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
194    ...
195    Header fields OK
196    ...
197
198The submit button is enabled:
199
200    >>> browser.getControl('Perform import...').disabled
201    False
202
203    >>> browser.getControl('Perform import...').click()
204
205Step 4: See import results
206
207The import was successful:
208
209    >>> print browser.contents
210    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
211    ...Successfully processed 2 rows...
212    ...Batch processing finished...
213    ...File:...departments_zope.mgr.csv...
214
215Batch processing courses
216========================
217
218    >>> browser.open('http://localhost/app/datacenter')
219
220Prepare a CSV file for courses:
221
222    >>> open('courses.csv', 'wb').write(
223    ... """code,faculty_code,department_code,title,level,passmark,credits,semester
224    ... CRS1,FAC1,DEP1,Course 1,100,40,2,1
225    ... CRS2,FAC1,DEP1,Course 2,100,40,2,2
226    ... """)
227
228Upload the file:
229
230    >>> import cStringIO
231    >>> browser.getLink('Upload CSV file').click()
232    >>> filecontents = cStringIO.StringIO(
233    ...   open('courses.csv', 'rb').read())
234    >>> filewidget = browser.getControl(name='uploadfile:file')
235    >>> filewidget.add_file(filecontents, 'text/plain', 'courses.csv')
236    >>> browser.getControl(name='SUBMIT').click()
237
238Step 1: start batch processing:
239
240    >>> browser.getLink('Batch processing').click()
241    >>> button = lookup_submit_value(
242    ...   'select', 'courses_zope.mgr.csv', browser)
243    >>> button.click()
244
245Step 2: select a processor and mode:
246
247    >>> importerselect = browser.getControl(name='importer')
248    >>> importerselect.getControl('Course importer', index=1).selected = True
249    >>> modeselect = browser.getControl(name='mode')
250    >>> modeselect.getControl(value='create').selected = True
251    >>> browser.getControl('Proceed to step 3...').click()
252
253Step 3: Fix headerlines
254
255We get informed that there are no problems with the current header:
256
257    >>> print browser.contents
258    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
259    ...
260    Header fields OK
261    ...
262
263The submit button is enabled:
264
265    >>> browser.getControl('Perform import...').disabled
266    False
267
268    >>> browser.getControl('Perform import...').click()
269
270Step 4: See import results
271
272The import was successful:
273
274    >>> print browser.contents
275    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
276    ...Successfully processed 2 rows...
277    ...Batch processing finished...
278    ...File:...courses_zope.mgr.csv...
279
280Batch processing certificates
281=============================
282
283    >>> browser.open('http://localhost/app/datacenter')
284
285Prepare a CSV file for certificates:
286
287    >>> open('certificates.csv', 'wb').write(
288    ... """code,faculty_code,department_code,title,study_mode,start_level,end_level,application_category
289    ... CERT1,FAC1,DEP1,Certificate 1,ug_ft,100,300,basic
290    ... CERT2,FAC1,DEP1,Certificate 2,ug_ft,200,300,cest
291    ... """)
292
293Upload the file:
294
295    >>> import cStringIO
296    >>> browser.getLink('Upload CSV file').click()
297    >>> filecontents = cStringIO.StringIO(
298    ...   open('certificates.csv', 'rb').read())
299    >>> filewidget = browser.getControl(name='uploadfile:file')
300    >>> filewidget.add_file(filecontents, 'text/plain', 'certificates.csv')
301    >>> browser.getControl(name='SUBMIT').click()
302
303Step 1: start batch processing:
304
305    >>> browser.getLink('Batch processing').click()
306    >>> button = lookup_submit_value(
307    ...   'select', 'certificates_zope.mgr.csv', browser)
308    >>> button.click()
309
310
311Step 2: select a processor and mode:
312
313    >>> importerselect = browser.getControl(name='importer')
314    >>> importerselect.getControl('Certificate importer').selected = True
315    >>> modeselect = browser.getControl(name='mode')
316    >>> modeselect.getControl(value='create').selected = True
317    >>> browser.getControl('Proceed to step 3...').click()
318
319Step 3: Fix headerlines
320
321We get informed that there are no problems with the current header:
322
323    >>> print browser.contents
324    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
325    ...
326    Header fields OK
327    ...
328
329The submit button is enabled:
330
331    >>> browser.getControl('Perform import...').disabled
332    False
333
334    >>> browser.getControl('Perform import...').click()
335
336Step 4: See import results
337
338The import was successful:
339
340    >>> print browser.contents
341    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
342    ...Successfully processed 2 rows...
343    ...Batch processing finished...
344    ...File:...certificates_zope.mgr.csv...
345
346Batch processing certificate courses
347====================================
348
349    >>> browser.open('http://localhost/app/datacenter')
350
351Prepare a CSV file for certificate courses:
352
353    >>> open('mycertcourses.csv', 'wb').write(
354    ... """course,faculty_code,department_code,certificate_code,level,core_or_elective
355    ... CRS1,FAC1,DEP1,CERT1,100,True
356    ... CRS2,FAC1,DEP1,CERT1,100,True
357    ... """)
358
359Upload the file:
360
361    >>> import cStringIO
362    >>> browser.getLink('Upload CSV file').click()
363    >>> filecontents = cStringIO.StringIO(
364    ...   open('mycertcourses.csv', 'rb').read())
365    >>> filewidget = browser.getControl(name='uploadfile:file')
366    >>> filewidget.add_file(filecontents, 'text/plain', 'mycertcourses.csv')
367    >>> browser.getControl(name='SUBMIT').click()
368
369Step 1: start batch processing:
370
371    >>> browser.getLink('Batch processing').click()
372    >>> button = lookup_submit_value(
373    ...   'select', 'mycertcourses_zope.mgr.csv', browser)
374    >>> button.click()
375
376Step 2: select a processor and mode:
377
378    >>> importerselect = browser.getControl(name='importer')
379    >>> importerselect.getControl('CertificateCourse importer').selected = True
380    >>> modeselect = browser.getControl(name='mode')
381    >>> modeselect.getControl(value='create').selected = True
382    >>> browser.getControl('Proceed to step 3...').click()
383
384Step 3: Fix headerlines
385
386We get informed that there are no problems with the current header:
387
388    >>> print browser.contents
389    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
390    ...
391    Header fields OK
392    ...
393
394The submit button is enabled:
395
396    >>> browser.getControl('Perform import...').disabled
397    False
398
399    >>> browser.getControl('Perform import...').click()
400
401Step 4: See import results
402
403The import was successful:
404
405    >>> print browser.contents
406    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
407    ...Successfully processed 2 rows...
408    ...Batch processing finished...
409    ...File:...mycertcourses_zope.mgr.csv...
410
411
412Pending files
413=============
414
415When an error occurs during an import, two files are generated: a CSV
416file with finished files and a CSV file with pending data. Both are
417stored in the appropriate subdirectories in datacenter. We try to
418create faculties, from which one already exists.
419
420Go to datacenter page:
421
422    >>> browser.open('http://localhost/app/datacenter')
423
424Prepare a CSV file for faculties:
425
426    >>> open('newfaculties.csv', 'wb').write(
427    ... """code,review_state,title,title_prefix
428    ... FAC1,static,Faculty 1,faculty
429    ... FAC4,static,Faculty 4,school_of
430    ... """)
431
432Upload the file:
433
434    >>> import cStringIO
435    >>> browser.getLink('Upload CSV file').click()
436    >>> filecontents = cStringIO.StringIO(
437    ...   open('newfaculties.csv', 'rb').read())
438    >>> filewidget = browser.getControl(name='uploadfile:file')
439    >>> filewidget.add_file(filecontents, 'text/plain', 'newfaculties.csv')
440    >>> browser.getControl(name='SUBMIT').click()
441
442Step 1: start batch processing:
443
444    >>> browser.getLink('Batch processing').click()
445    >>> button = lookup_submit_value(
446    ...   'select', 'newfaculties_zope.mgr.csv', browser)
447    >>> button.click()
448
449Step 2: select a processor and mode:
450
451    >>> importerselect = browser.getControl(name='importer')
452    >>> importerselect.getControl('Faculty importer').selected = True
453    >>> modeselect = browser.getControl(name='mode')
454    >>> modeselect.getControl(value='create').selected = True
455    >>> browser.getControl('Proceed to step 3...').click()
456
457Step 3: Fix headerlines
458
459As there should be no problem with the headers, we can immediately
460perfom the import:
461
462    >>> browser.getControl('Perform import...').click()
463
464One line could not be imported:
465
466    >>> print browser.contents
467    <!DOCTYPE html PUBLIC...
468    ...
469    ...Processing of 1 rows failed...
470    ...Successfully processed 1 rows...
471    ...
472
473Now there are two files as a result in datacenter storage's root and
474``finished`` dirs:
475
476    >>> pending_file = dc_path + '/newfaculties_zope.mgr.create.pending.csv'
477    >>> print open(pending_file).read()
478    title_prefix,--IGNORE--,code,title,--ERRORS--
479    faculty,static,FAC1,Faculty 1,This object already exists in the same container. Skipping.
480
481    >>> finished_file = dc_path + '/finished/newfaculties_zope.mgr.create.finished.csv'
482    >>> print open(finished_file).read()
483    title_prefix,--IGNORE--,code,title
484    school_of,static,FAC4,Faculty 4
485
486The finished-file contains the dataset we could import, while the
487pending file contains the dataset that failed, appended by an error
488message.
489
490
491Fixing the pending file
492-----------------------
493
494We 'edit' the pending file (setting code to ``FAC5`` and title
495appropriately, and removing the --ERROR-- column) and finish the
496import this way:
497
498    >>> open(dc_path + '/newfaculties_zope.mgr.create.pending.csv', 'wb').write(
499    ... """title_prefix,--IGNORE--,code,title
500    ... faculty,static,FAC5,Faculty 5
501    ... """)
502
503Step 1: start batch processing:
504
505    >>> browser.open('http://localhost/app/datacenter')
506    >>> browser.getLink('Batch processing').click()
507    >>> button = lookup_submit_value(
508    ...   'select', 'newfaculties_zope.mgr.create.pending.csv', browser)
509    >>> button.click()
510
511Step 2: select a processor and mode:
512
513    >>> importerselect = browser.getControl(name='importer')
514    >>> importerselect.getControl('Faculty importer').selected = True
515    >>> modeselect = browser.getControl(name='mode')
516    >>> modeselect.getControl(value='create').selected = True
517    >>> browser.getControl('Proceed to step 3...').click()
518
519Step 3/4: Fix headerlines and import:
520
521As there should be no problem with the headers, we can immediately
522perfom the import:
523
524    >>> browser.getControl('Perform import...').click()
525
526This time everything should work:
527
528    >>> print browser.contents
529    <!DOCTYPE html PUBLIC...
530    ...
531    ...Successfully processed 1 rows...
532    ...
533
534    >>> sorted(os.listdir(dc_path))
535    ['finished', 'logs', 'unfinished']
536
537    >>> os.listdir(dc_path + '/unfinished')
538    []
539
540    >>> sorted(os.listdir(dc_path + '/finished'))
541    ['certificates_zope.mgr.create.finished.csv', ...,
542     'newfaculties_zope.mgr.create.finished.csv',
543     'newfaculties_zope.mgr.csv']
544
545
546Clean up:
547
548    >>> import shutil
549    >>> shutil.rmtree(dc_path)
Note: See TracBrowser for help on using the repository browser.