Changeset 4162 for waeup/branches


Ignore:
Timestamp:
24 May 2009, 10:13:21 (15 years ago)
Author:
uli
Message:

Update tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/university/facultycontainer.txt

    r4080 r4162  
    6969like. It might be a string of integers, a name or whatever; you cannot
    7070know before.
     71
     72Importing from CSV files
     73------------------------
     74
     75We create a facultycontainer, which we want to populate with some CSV
     76data afterwards:
     77
     78    >>> from waeup.university.facultycontainer import FacultyContainer
     79    >>> mycontainer = FacultyContainer()
     80
     81Next, we create a CSV file we want to import:
     82
     83    >>> open('myfaculties.csv', 'wb').write(
     84    ... """code,review_state,title,title_prefix
     85    ... AGR,static,Agriculture,faculty
     86    ... ART,static,Arts,faculty
     87    ... DEN,static,Dentistry,faculty
     88    ... """)
     89
     90We get a suitable importer by asking for an adapter to
     91IWAeUPCSVExporter (see section above for details):
     92
     93    >>> from waeup.interfaces import IWAeUPCSVImporter
     94    >>> importer = IWAeUPCSVImporter(mycontainer)
     95    >>> importer
     96    <waeup.university.facultycontainer.FacultyCSVImporter object at 0x...>
     97
     98This importer complies with the IWAeUPCSVImporter interface:
     99
     100    >>> from waeup.university.facultycontainer import FacultyCSVImporter
     101    >>> IWAeUPCSVImporter.implementedBy(FacultyCSVImporter)
     102    True
     103
     104    >>> from zope.interface.verify import verifyClass
     105    >>> verifyClass(IWAeUPCSVImporter, FacultyCSVImporter)
     106    True
     107
     108The container is still empty:
     109
     110    >>> len(mycontainer)
     111    0
     112
     113Now we can easily import the data:
     114
     115    >>> importer.doImport('myfaculties.csv')
     116
     117The container is now filled:
     118
     119    >>> len(mycontainer)
     120    3
     121
     122    >>> print [(x.code, x.title) for x in list(mycontainer.values())]
     123    [('AGR', 'Agriculture'), ('ART', 'Arts'), ('DEN', 'Dentistry')]
     124
     125
     126We can validate data of CSV files without actually importing
     127them. This can help for instance while checking uploaded files:
     128
     129    >>> importer.validate('myfaculties.csv')
     130    True
     131
     132Clean up:
     133
     134    >>> import os
     135    >>> os.unlink('myfaculties.csv')
Note: See TracChangeset for help on using the changeset viewer.