Author: phd
Date: 2006-01-23 08:35:50 -0700 (Mon, 23 Jan 2006)
New Revision: 1534
Added:
SQLObject/branches/0.7-bugfix/sqlobject/tests/test_foreignKey.py
Removed:
SQLObject/branches/0.7-bugfix/sqlobject/tests/test_selectBy_foreignKey.py
Modified:
SQLObject/branches/0.7-bugfix/sqlobject/col.py
SQLObject/branches/0.7-bugfix/sqlobject/postgres/pgconnection.py
Log:
Renamed test_selectBy_foreignKey.py to test_foreignKey.py.
Added a test for addColumn().
Added ForeignKey.postgresCreateColumnSQL(); this is a quick and dirty fix
for ALTER TABLE ADD CONSTRAINT, implemented only for 0.7-branch; 0.8 (trunk)
implements this in a much cleaner way.
Modified: SQLObject/branches/0.7-bugfix/sqlobject/col.py
===================================================================
--- SQLObject/branches/0.7-bugfix/sqlobject/col.py 2006-01-23 15:33:18 UTC (rev 1533)
+++ SQLObject/branches/0.7-bugfix/sqlobject/col.py 2006-01-23 15:35:50 UTC (rev 1534)
@@ -757,6 +757,30 @@
sql = ', '.join([sql, constraint])
return sql
+ def postgresCreateColumnSQL(self):
+ sql = SOKeyCol.postgresCreateSQL(self)
+ other = findClass(self.foreignKey, self.soClass.sqlmeta.registry)
+ tName = other.sqlmeta.table
+ idName = other.sqlmeta.idName
+ if self.cascade is not None:
+ if self.cascade == 'null':
+ action = 'ON DELETE SET NULL'
+ elif self.cascade:
+ action = 'ON DELETE CASCADE'
+ else:
+ action = 'ON DELETE RESTRICT'
+ else:
+ action = ''
+ constraint = ('CONSTRAINT %(colName)s_exists '
+ 'REFERENCES %(tName)s (%(idName)s) '
+ '%(action)s' %
+ {'tName': tName,
+ 'colName': self.dbName,
+ 'idName': idName,
+ 'action': action})
+ sql = ' '.join([sql, constraint])
+ return sql
+
def sybaseCreateSQL(self):
sql = SOKeyCol.sybaseCreateSQL(self)
other = findClass(self.foreignKey)
Modified: SQLObject/branches/0.7-bugfix/sqlobject/postgres/pgconnection.py
===================================================================
--- SQLObject/branches/0.7-bugfix/sqlobject/postgres/pgconnection.py 2006-01-23 15:33:18 UTC (rev 1533)
+++ SQLObject/branches/0.7-bugfix/sqlobject/postgres/pgconnection.py 2006-01-23 15:35:50 UTC (rev 1534)
@@ -162,9 +162,13 @@
return result[0]
def addColumn(self, tableName, column):
+ if hasattr(column, "postgresCreateColumnSQL"):
+ postgresCreateSQL = column.postgresCreateColumnSQL
+ else:
+ postgresCreateSQL = column.postgresCreateSQL
self.query('ALTER TABLE %s ADD COLUMN %s' %
(tableName,
- column.postgresCreateSQL()))
+ postgresCreateSQL()))
def delColumn(self, tableName, column):
self.query('ALTER TABLE %s DROP COLUMN %s' %
Copied: SQLObject/branches/0.7-bugfix/sqlobject/tests/test_foreignKey.py (from rev 1530, SQLObject/branches/0.7-bugfix/sqlobject/tests/test_selectBy_foreignKey.py)
===================================================================
--- SQLObject/branches/0.7-bugfix/sqlobject/tests/test_selectBy_foreignKey.py 2006-01-23 12:56:29 UTC (rev 1530)
+++ SQLObject/branches/0.7-bugfix/sqlobject/tests/test_foreignKey.py 2006-01-23 15:35:50 UTC (rev 1534)
@@ -0,0 +1,40 @@
+from sqlobject import *
+from sqlobject.tests.dbtest import *
+
+class TestComposerKey(SQLObject):
+ name = StringCol()
+
+class TestWorkKey(SQLObject):
+ class sqlmeta:
+ idName = "work_id"
+
+ composer = ForeignKey('TestComposerKey', cascade=True)
+ title = StringCol()
+
+class TestWorkKey2(SQLObject):
+ title = StringCol()
+
+def test1():
+ setupClass([TestComposerKey,
+ TestWorkKey])
+
+ c = TestComposerKey(name='Mahler, Gustav')
+ w1 = TestWorkKey(composer=c, title='Symphony No. 9')
+ w2 = TestWorkKey(composer=None, title=None)
+
+ # Select by usual way
+ s = TestWorkKey.selectBy(composerID=c.id, title='Symphony No. 9')
+ assert s[0]==w1
+ # selectBy object
+ s = TestWorkKey.selectBy(composer=c, title='Symphony No. 9')
+ assert s[0]==w1
+ # selectBy id
+ s = TestWorkKey.selectBy(id=w1.id)
+ assert s[0]==w1
+ # is None handled correctly?
+ s = TestWorkKey.selectBy(composer=None, title=None)
+ assert s[0]==w2
+
+def test2():
+ setupClass([TestComposerKey, TestWorkKey2])
+ TestWorkKey2.sqlmeta.addColumn(ForeignKey('TestComposerKey'), changeSchema=True)
Deleted: SQLObject/branches/0.7-bugfix/sqlobject/tests/test_selectBy_foreignKey.py
===================================================================
--- SQLObject/branches/0.7-bugfix/sqlobject/tests/test_selectBy_foreignKey.py 2006-01-23 15:33:18 UTC (rev 1533)
+++ SQLObject/branches/0.7-bugfix/sqlobject/tests/test_selectBy_foreignKey.py 2006-01-23 15:35:50 UTC (rev 1534)
@@ -1,33 +0,0 @@
-from sqlobject import *
-from sqlobject.tests.dbtest import *
-
-class TestComposerKey(SQLObject):
- name = StringCol()
-
-class TestWorkKey(SQLObject):
- class sqlmeta:
- idName = "work_id"
-
- composer = ForeignKey('TestComposerKey')
- title = StringCol()
-
-def test1():
- setupClass([TestComposerKey,
- TestWorkKey])
-
- c = TestComposerKey(name='Mahler, Gustav')
- w1 = TestWorkKey(composer=c, title='Symphony No. 9')
- w2 = TestWorkKey(composer=None, title=None)
-
- # Select by usual way
- s = TestWorkKey.selectBy(composerID=c.id, title='Symphony No. 9')
- assert s[0]==w1
- # selectBy object
- s = TestWorkKey.selectBy(composer=c, title='Symphony No. 9')
- assert s[0]==w1
- # selectBy id
- s = TestWorkKey.selectBy(id=w1.id)
- assert s[0]==w1
- # is None handled correctly?
- s = TestWorkKey.selectBy(composer=None, title=None)
- assert s[0]==w2
|