Ignore:
Timestamp:
22 Jun 2009, 12:57:10 (16 years ago)
Author:
uli
Message:

Update certificate tests.

File:
1 edited

Legend:

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

    r4325 r4332  
    230230     <waeup.university.certificate.Certificate object at 0x...>
    231231
     232Event Subscribers
     233=================
     234
     235.. function:: removedCourseHandler(course, event)
     236
     237   An event subscriber triggered for
     238   :class:`grok.IObjectRemovedEvent`s, when an :class:`ICourse`
     239   instance is removed from a container.
     240
     241   Tries to remove all referencing :class:`CertificateCourse`
     242   instances that reference the removed course.
     243
     244   To accomplish that, the parents of the removed course are looked up
     245   for a certifcate container which contains a certificate-course that
     246   contains a reference to the deleted course.
     247
     248   .. seealso:: :ref:`removecertificatecourses`
     249
     250   **handles:**
     251     :class:`ICourse`
     252
     253   **event type:**
     254     :class:`grok.IObjectRemovedEvent`
     255
    232256Examples
    233257========
     
    268292    True
    269293
     294Also instances of :class:`CertificateCourse` can be created by asking
     295the component architechture:
     296
     297    >>> from zope.component import createObject
     298    >>> mycertcourse = createObject(u'waeup.CertificateCourse')
     299    >>> mycertcourse
     300    <waeup.university.certificate.CertificateCourse object at 0x...>
     301
     302.. _removecertificatecourses:
     303
     304Persistence of certificate courses
     305----------------------------------
     306
     307If a certificate course requires a certain course and this is course
     308is deleted, also the referencing certificate course is deleted.
     309
     310We setup a data structure that reflects typical usage. It looks like
     311this::
     312
     313    Department-Instance
     314    |
     315    +---> courses
     316    |        |
     317    |        +--------------------> Course-Instance
     318    |                                        ^
     319    +---> certificates                       |
     320             |                               |
     321             +-----> Certificate-Instance    |
     322                        |                    |
     323                        +------> Certificate-Course
     324
     325The certifcate-Course here refers to a Course-Instance.
     326
     327In Python we build such a structure like this (from top to bottom):
     328
     329    >>> from zope.component import createObject
     330    >>> mydept = createObject('waeup.Department')
     331
     332In real world use this data will be stored in a ZODB. We setup our own
     333litte ZODB backend (which is easy!):
     334
     335    >>> from ZODB import FileStorage, DB
     336    >>> dbpath = 'tinyData.fs'
     337    >>> class TinyZODB(object):
     338    ...   def __init__(self, path=dbpath):
     339    ...     self.storage = FileStorage.FileStorage(path)
     340    ...     self.db = DB(self.storage)
     341    ...     self.connection = self.db.open()
     342    ...     self.dbroot = self.connection.root()
     343    ...   def close(self):
     344    ...     self.connection.close()
     345    ...     self.db.close()
     346    ...     self.storage.close()
     347
     348Now we can use this ZODB as backend database and store our data
     349structure:
     350
     351    >>> import transaction
     352    >>> db = TinyZODB()
     353    >>> dbroot = db.dbroot
     354    >>> dbroot['mydept'] = mydept
     355    >>> mycourse = createObject('waeup.Course')
     356    >>> mycourse.code = 'MYCOURSE'
     357    >>> mydept.courses.addCourse(mycourse)
     358    >>> mycert = createObject('waeup.Certificate')
     359    >>> mycert.code = 'MYCERT'
     360    >>> mydept.certificates.addCertificate(mycert)
     361    >>> mycert.addCourseRef(mycourse)
     362
     363    >>> transaction.commit()
     364
     365The data is now stored in the ZODB. We can close the DB, reopen it
     366later and the data will still be there:
     367
     368    >>> db.close()
     369    >>> newdb = TinyZODB()
     370    >>> newdbroot = newdb.dbroot
     371    >>> list(newdbroot)
     372    ['mydept']
     373
     374The certificate-course we stored in the certificate is indeed a
     375reference to the course, not a copy of it:
     376
     377    >>> course = newdbroot['mydept'].courses['MYCOURSE']
     378    >>> certcourse = newdbroot['mydept'].certificates['MYCERT']['MYCOURSE']
     379    >>> certcourse.course is course
     380    True
     381
     382So, we can be sure that modifications to the course are immediately
     383reflected in the certcourse.
     384
     385The interesting question now is: what happens to ``mycertcourse`` when
     386the referenced course ``mycourse`` is deleted?
     387
     388Let's have a look:
     389
     390    >>> list(newdbroot['mydept'].certificates['MYCERT'].keys())
     391    [u'MYCOURSE']
     392
     393    >>> del newdbroot['mydept'].courses['MYCOURSE']
     394    >>> newdbroot['mydept']._p_changed = True
     395    >>> transaction.commit()
     396    >>> list(newdbroot['mydept'].certificates['MYCERT'].keys())
     397    []
     398
     399As we can see, the course was also removed from the certificate.
     400
     401Clean up:
     402
     403    >>> import os
     404    >>> os.unlink(dbpath)
Note: See TracChangeset for help on using the changeset viewer.