[Checkins] SVN: megrok.rdb/trunk/src/megrok/rdb/schema. Verify that ordering works correctly in schema translation.

Martijn Faassen faassen at infrae.com
Sat Jul 12 09:35:04 EDT 2008


Log message for revision 88285:
  Verify that ordering works correctly in schema translation.
  

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

-=-
Modified: megrok.rdb/trunk/src/megrok/rdb/schema.py
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/schema.py	2008-07-12 12:54:58 UTC (rev 88284)
+++ megrok.rdb/trunk/src/megrok/rdb/schema.py	2008-07-12 13:35:03 UTC (rev 88285)
@@ -24,9 +24,9 @@
         field.__name__ = str(column.name)
         field.title = unicode(column.name)
         field.required = not column.nullable
+        attrs[column.name] = field
         field.order = i
-        attrs[column.name] = field
-
+       
     return InterfaceClass(name=model.__table__.name,
                           bases=bases,
                           attrs=attrs,
@@ -43,3 +43,4 @@
 def field_from_sa_integer(i):
     return Int(__name__ = u'__dummy__',
                 title = u'__dummy__')
+

Modified: megrok.rdb/trunk/src/megrok/rdb/schema.txt
===================================================================
--- megrok.rdb/trunk/src/megrok/rdb/schema.txt	2008-07-12 12:54:58 UTC (rev 88284)
+++ megrok.rdb/trunk/src/megrok/rdb/schema.txt	2008-07-12 13:35:03 UTC (rev 88285)
@@ -13,18 +13,11 @@
 
   >>> 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::
 
@@ -33,8 +26,6 @@
   >>> grok('megrok.rdb.schema')
   >>> __file__ = 'foo' # hack
   >>> from grok.testing import grok_component
-  >>> grok_component('Courses', Courses)
-  True
   >>> grok_component('Department', Department)
   True
 
@@ -43,7 +34,8 @@
   >>> from megrok.rdb.schema import schema_from_model
   >>> schema = schema_from_model(Department)
 
-The schema will have one field, ``name``::
+The schema will have one field, ``name``. The primary key is not
+included by default::
 
   >>> list(schema)
   ['name']
@@ -54,3 +46,49 @@
   >>> isinstance(schema['name'], TextLine)
   True
 
+Field order
+-----------
+
+The order of the schema fields is the same as the order in the
+SQLAlchemy definition::
+
+  >>> class Person(rdb.Model):
+  ...   rdb.metadata(metadata)
+  ...
+  ...   id = Column('id', Integer, primary_key=True)
+  ...   first_name = Column('first_name', String(50))
+  ...   last_name = Column('last_name', String(50))
+  >>> grok_component('Person', Person)
+  True
+  >>> schema = schema_from_model(Person)
+  >>> from zope.schema import getFieldsInOrder
+
+We define a function which shows the fields in the schema in schema
+order::
+
+  >>> def names(schema):
+  ...    return [name for (name, field) in getFieldsInOrder(schema)]
+
+The fields are in the definition order as they are in the table::
+
+  >>> names(schema)
+  ['first_name', 'last_name']
+
+Let's try a few more fields::
+
+  >>> class Person2(rdb.Model):
+  ...   rdb.metadata(metadata)
+  ...
+  ...   id = Column('id', Integer, primary_key=True)
+  ...   c = Column('c', String(50))
+  ...   b = Column('b', String(50))
+  ...   a = Column('a', String(50))
+  >>> grok_component('Person2', Person2)
+  True
+
+  >>> schema = schema_from_model(Person2)
+
+The fields are in the right order::
+
+  >>> names(schema)
+  ['c', 'b', 'a']



More information about the Checkins mailing list