[Checkins] SVN: megrok.rdb/trunk/src/megrok/rdb/ Start adding some tests for SQLAlchemy to interface conversion.

Martijn Faassen faassen at infrae.com
Sat Jul 12 08:54:59 EDT 2008


Log message for revision 88284:
  Start adding some tests for SQLAlchemy to interface conversion.
  

Changed:
  A   megrok.rdb/trunk/src/megrok/rdb/schema.txt
  U   megrok.rdb/trunk/src/megrok/rdb/tests.py

-=-
Added: megrok.rdb/trunk/src/megrok/rdb/schema.txt
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/schema.txt	                        (rev 0)
+++ megrok.rdb/trunk/src/megrok/rdb/schema.txt	2008-07-12 12:54:58 UTC (rev 88284)
@@ -0,0 +1,56 @@
+==================================
+SQLAlchemy schema to Zope 3 schema
+==================================
+
+We can convert SQLAlchemy schemas to Zope 3 schemas, which can then
+for instance be used to generate forms and the like.
+
+First we define a model with a SQLAlchemy schema::
+
+  >>> from sqlalchemy import Column, ForeignKey
+  >>> from sqlalchemy.types import Integer, String
+  >>> from sqlalchemy.orm import relation
+
+  >>> from megrok import rdb
+  >>> metadata = rdb.MetaData()
+  >>> class Courses(rdb.Container):
+  ...   pass
+
+  >>> class Department(rdb.Model):
+  ...   rdb.metadata(metadata)
+  ...
+  ...   id = Column('id', Integer, primary_key=True)
+  ...   name = Column('name', String(50))
+  ... 
+  ...   courses = relation('Course', 
+  ...                       backref='department',
+  ...                       collection_class=Courses)
+  
+Let's grok the ``Department`` class now::
+
+  >>> from grok.testing import grok
+  >>> grok('megrok.rdb.meta')
+  >>> grok('megrok.rdb.schema')
+  >>> __file__ = 'foo' # hack
+  >>> from grok.testing import grok_component
+  >>> grok_component('Courses', Courses)
+  True
+  >>> grok_component('Department', Department)
+  True
+
+We then convert the schema to a Zope 3 schema::
+
+  >>> from megrok.rdb.schema import schema_from_model
+  >>> schema = schema_from_model(Department)
+
+The schema will have one field, ``name``::
+
+  >>> list(schema)
+  ['name']
+
+The name field will be a TextLine::
+
+  >>> from zope.schema import TextLine
+  >>> isinstance(schema['name'], TextLine)
+  True
+

Modified: megrok.rdb/trunk/src/megrok/rdb/tests.py
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/tests.py	2008-07-12 12:13:49 UTC (rev 88283)
+++ megrok.rdb/trunk/src/megrok/rdb/tests.py	2008-07-12 12:54:58 UTC (rev 88284)
@@ -21,9 +21,13 @@
     suite = unittest.TestSuite()
     
     suite.addTest(doctest.DocFileSuite(
-        'README.txt',
-        optionflags=optionflags,
-        setUp=setUp,
-        tearDown=tearDown,
-        globs=globs))
+            'README.txt',
+            optionflags=optionflags,
+            setUp=setUp,
+            tearDown=tearDown,
+            globs=globs))
+    suite.addTest(doctest.DocFileSuite(
+            'schema.txt',
+            optionflags=optionflags,
+            ))
     return suite



More information about the Checkins mailing list