[SQL-CVS] r2885 - in SQLObject/trunk/sqlobject: . inheritance inheritance/tests tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2007-08-21 12:53:12
|
Author: phd Date: 2007-08-21 06:53:06 -0600 (Tue, 21 Aug 2007) New Revision: 2885 Added: SQLObject/trunk/sqlobject/inheritance/tests/test_asdict.py SQLObject/trunk/sqlobject/tests/test_asdict.py Modified: SQLObject/trunk/sqlobject/inheritance/__init__.py SQLObject/trunk/sqlobject/main.py Log: Added sqlmeta.getColumns(). sqlmeta.asDict() now uses getColumns(). Modified: SQLObject/trunk/sqlobject/inheritance/__init__.py =================================================================== --- SQLObject/trunk/sqlobject/inheritance/__init__.py 2007-08-21 08:15:42 UTC (rev 2884) +++ SQLObject/trunk/sqlobject/inheritance/__init__.py 2007-08-21 12:53:06 UTC (rev 2885) @@ -201,12 +201,11 @@ sm = sm.parentClass.sqlmeta return columns - def asDict(sqlmeta): - result = {} - for key in sqlmeta.getAllColumns(): - result[key] = getattr(sqlmeta.instance, key) - result['id'] = sqlmeta.instance.id - return result + def getColumns(sqlmeta): + columns = sqlmeta.getAllColumns() + if columns.has_key('childName'): + del columns['childName'] + return columns class InheritableSQLObject(SQLObject): Added: SQLObject/trunk/sqlobject/inheritance/tests/test_asdict.py =================================================================== --- SQLObject/trunk/sqlobject/inheritance/tests/test_asdict.py (rev 0) +++ SQLObject/trunk/sqlobject/inheritance/tests/test_asdict.py 2007-08-21 12:53:06 UTC (rev 2885) @@ -0,0 +1,31 @@ +from sqlobject import * +from sqlobject.inheritance import * +from sqlobject.tests.dbtest import * + +######################################## +## sqlmeta.asDict +######################################## + +class InheritablePerson(InheritableSQLObject): + first = StringCol() + last = StringCol(alternateID=True, length=255) + +class Boss(InheritablePerson): + department = StringCol() + +class Employee(InheritablePerson): + _inheritable = False + position = StringCol() + +def test_asDict(): + setupClass([InheritablePerson, Boss, Employee]) + InheritablePerson(first='Oneof', last='Authors') + Boss(first='Boss', last='The', department='Dep') + Employee(first='Project', last='Leader', position='Project leader') + + assert InheritablePerson.get(1).sqlmeta.asDict() == \ + dict(first='Oneof', last='Authors', id=1) + assert InheritablePerson.get(2).sqlmeta.asDict() == \ + dict(first='Boss', last='The', department='Dep', id=2) + assert InheritablePerson.get(3).sqlmeta.asDict() == \ + dict(first='Project', last='Leader', position='Project leader', id=3) Modified: SQLObject/trunk/sqlobject/main.py =================================================================== --- SQLObject/trunk/sqlobject/main.py 2007-08-21 08:15:42 UTC (rev 2884) +++ SQLObject/trunk/sqlobject/main.py 2007-08-21 12:53:06 UTC (rev 2885) @@ -609,12 +609,15 @@ ## Utility methods ######################################## + def getColumns(sqlmeta): + return sqlmeta.columns.copy() + def asDict(self): """ Return the object as a dictionary of columns to values. """ result = {} - for key in self.columns: + for key in self.getColumns(): result[key] = getattr(self.instance, key) result['id'] = self.instance.id return result Added: SQLObject/trunk/sqlobject/tests/test_asdict.py =================================================================== --- SQLObject/trunk/sqlobject/tests/test_asdict.py (rev 0) +++ SQLObject/trunk/sqlobject/tests/test_asdict.py 2007-08-21 12:53:06 UTC (rev 2885) @@ -0,0 +1,15 @@ +from sqlobject import * +from sqlobject.tests.dbtest import * + +######################################## +## sqlmeta.asDict() +######################################## + +class TestAsDict(SQLObject): + name = StringCol(length=10) + name2 = StringCol(length=10) + +def test_asDict(): + setupClass(TestAsDict) + t1 = TestAsDict(name='one', name2='1') + assert t1.sqlmeta.asDict() == dict(name='one', name2='1', id=1) |