[CalendarServer-changes] [9613] CalendarServer/branches/users/glyph/q
source_changes at macosforge.org
source_changes at macosforge.org
Sat Aug 11 01:54:52 PDT 2012
Revision: 9613
http://trac.macosforge.org/projects/calendarserver/changeset/9613
Author: glyph at apple.com
Date: 2012-08-11 01:54:52 -0700 (Sat, 11 Aug 2012)
Log Message:
-----------
Test cases for update method and read-only attributes.
Modified Paths:
--------------
CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py
CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py
Property Changed:
----------------
CalendarServer/branches/users/glyph/q/
Modified: CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py
===================================================================
--- CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py 2012-08-11 08:54:51 UTC (rev 9612)
+++ CalendarServer/branches/users/glyph/q/twext/enterprise/dal/record.py 2012-08-11 08:54:52 UTC (rev 9613)
@@ -26,8 +26,19 @@
from twext.enterprise.dal.syntax import Select, Tuple, Constant, ColumnSyntax
from twext.enterprise.dal.syntax import Insert
+class ReadOnly(AttributeError):
+ """
+ A caller attempted to set an attribute on a database-backed record, rather
+ than updating it through L{_RecordBase.update}.
+ """
+ def __init__(self, className, attributeName):
+ self.className = className
+ self.attributeName = attributeName
+ super(ReadOnly, self).__init__()
+
+
class _RecordBase(object):
@classmethod
@inlineCallbacks
@@ -81,3 +92,8 @@
return type(table.model.name, tuple([_RecordBase]),
dict(__tbl__=table))
+
+__all__ = [
+ "ReadOnly",
+ "fromTable",
+]
\ No newline at end of file
Modified: CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py
===================================================================
--- CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py 2012-08-11 08:54:51 UTC (rev 9612)
+++ CalendarServer/branches/users/glyph/q/twext/enterprise/dal/test/test_record.py 2012-08-11 08:54:52 UTC (rev 9613)
@@ -24,7 +24,7 @@
from twisted.trial.unittest import TestCase
-from twext.enterprise.dal.record import fromTable
+from twext.enterprise.dal.record import fromTable, ReadOnly
from twext.enterprise.dal.syntax import SQLITE_DIALECT
from twext.enterprise.dal.test.test_parseschema import SchemaTestHelper
@@ -102,3 +102,41 @@
self.assertEqual(rows, [tuple([3, u'epsilon'])])
+ @inlineCallbacks
+ def test_attributesArentMutableYet(self):
+ """
+ Changing attributes on a database object is not supported yet, because
+ it's not entirely clear when to flush the SQL to the database.
+ Instead, for the time being, use C{.update}. When you attempt to set
+ an attribute, an error will be raised informing you of this fact, so
+ that the error is clear.
+ """
+ txn = self.pool.connection()
+ rec = yield TestRecord.create(txn, beta=7, gamma=u'what')
+ def setit():
+ rec.beta = 12
+ ro = self.assertRaises(ReadOnly, setit)
+ self.assertEqual(rec.beta, 7)
+ self.assertContains(repr(ro),
+ "SQL-backed attribute 'TestRecord.beta' is "
+ "read-only. use '.update(...)' to modify "
+ "attributes.")
+
+
+ @inlineCallbacks
+ def test_simpleUpdate(self):
+ """
+ L{Record.update} will change the values on the record and in te
+ database.
+ """
+ txn = self.pool.connection()
+ rec = yield TestRecord.create(txn, beta=3, gamme=u'epsilon')
+ yield rec.update(gamma=u'otherwise')
+ self.assertEqual(rec.beta, u'otherwise')
+ yield txn.commit()
+ # Make sure that it persists.
+ txn = self.pool.connection()
+ rec = yield TestRecord.load(txn, 3)
+ self.assertEqual(rec.gamma, u'otherwise')
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120811/815a1658/attachment-0001.html>
More information about the calendarserver-changes
mailing list