[CalendarServer-changes] [6763] CalendarServer/branches/users/glyph/dal/txdav/base/datastore

source_changes at macosforge.org source_changes at macosforge.org
Wed Jan 19 12:56:59 PST 2011


Revision: 6763
          http://trac.macosforge.org/projects/calendarserver/changeset/6763
Author:   glyph at apple.com
Date:     2011-01-19 12:56:59 -0800 (Wed, 19 Jan 2011)
Log Message:
-----------
a few unit tests for schema parsing, and the beginning of sequence support.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py

Added Paths:
-----------
    CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py	2011-01-19 20:56:48 UTC (rev 6762)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlmodel.py	2011-01-19 20:56:59 UTC (rev 6763)
@@ -61,17 +61,20 @@
     """
     An invocation of a stored procedure or built-in function.
     """
+
     def __init__(self, name, args):
         self.name = name
         self.args = args
 
 
+
 class NO_DEFAULT(object):
     """
     Placeholder value for not having a default.
     """
 
 
+
 class Column(object):
 
     def __init__(self, table, name, type):
@@ -146,15 +149,25 @@
         self.descriptiveComment = comment
 
 
+class Sequence(object):
+    """
+    A sequence object.
+    """
 
+    def __init__(self, name):
+        self.name = name
+        self.referringColumns = []
+
+
 class Schema(object):
     """
-    A schema containing tables, indexes, sequences.
+    A schema containing tables, indexes, and sequences.
     """
 
     def __init__(self, filename='<string>'):
         self.filename = filename
         self.tables = []
+        self.sequences = []
 
 
     def __repr__(self):

Modified: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py	2011-01-19 20:56:48 UTC (rev 6762)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/sqlparser.py	2011-01-19 20:56:59 UTC (rev 6763)
@@ -1,3 +1,4 @@
+# -*- test-case-name: txdav.base.datastore.test.test_parseschema -*-
 ##
 # Copyright (c) 2010 Apple Inc. All rights reserved.
 #
@@ -21,6 +22,7 @@
                           Function)
 
 from txdav.base.datastore.sqlmodel import Schema, Table, SQLType, ProcedureCall
+from txdav.base.datastore.sqlmodel import Sequence
 
 def _fixKeywords():
     # In Postgres, 'SEQUENCE' is a keyword, and it behaves like one.
@@ -47,8 +49,13 @@
 
 
 def schemaFromPath(path):
-    self = Schema(path.basename())
+    schema = Schema(path.basename())
     schemaData = path.getContent()
+    addSQLToSchema(schema, schemaData)
+    return schema
+
+
+def addSQLToSchema(schema, schemaData):
     parsed = parse(schemaData)
     for stmt in parsed:
         preface = ''
@@ -57,10 +64,12 @@
         if not stmt.tokens:
             continue
         if stmt.get_type() == 'CREATE':
-            createType = stmt.token_next(1, True)
-            if createType.value.upper() == u'TABLE':
-                t = tableFromCreateStatement(self, stmt)
+            createType = stmt.token_next(1, True).value.upper()
+            if createType == u'TABLE':
+                t = tableFromCreateStatement(schema, stmt)
                 t.addComment(preface)
+            elif createType == u'SEQUENCE':
+                schema.sequences.append(Sequence(stmt.token_next(2, True).get_name()))
         elif stmt.get_type() == 'INSERT':
             insertTokens = iterSignificant(stmt)
             expect(insertTokens, ttype=Keyword.DML, value='INSERT')
@@ -80,10 +89,10 @@
                     [ident.ttype](ident.value)
                 )
 
-            self.tableNamed(tableName).insertSchemaRow(rowData)
+            schema.tableNamed(tableName).insertSchemaRow(rowData)
         else:
             print 'unknown type:', stmt.get_type()
-    return self
+    return schema
 
 
 class _ColumnParser(object):

Added: CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py
===================================================================
--- CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py	                        (rev 0)
+++ CalendarServer/branches/users/glyph/dal/txdav/base/datastore/test/test_parseschema.py	2011-01-19 20:56:59 UTC (rev 6763)
@@ -0,0 +1,52 @@
+##
+# Copyright (c) 2010 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+from txdav.base.datastore.sqlmodel import Schema
+
+from txdav.base.datastore.sqlparser import addSQLToSchema
+from twisted.trial.unittest import TestCase
+
+
+class ParsingExampleTests(TestCase):
+    """
+    Tests for parsing some sample schemas.
+    """
+
+    def test_simplest(self):
+        """
+        Parse an extremely simple schema with one table in it.
+        """
+        s = Schema()
+        addSQLToSchema(s, "create table foo (bar integer);")
+        self.assertEquals(len(s.tables), 1)
+        foo = s.tableNamed('foo')
+        self.assertEquals(len(foo.columns), 1)
+        bar = foo.columns[0]
+        self.assertEquals(bar.name, "bar")
+        self.assertEquals(bar.type.name, "integer")
+
+
+    def test_sequence(self):
+        """
+        Parsing a 'create sequence' statement adds a L{Sequence} to the
+        L{Schema}.
+        """
+        s = Schema()
+        addSQLToSchema(s, "create sequence myseq;")
+        self.assertEquals(len(s.sequences), 1)
+        self.assertEquals(s.sequences[0].name, "myseq")
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110119/d8de8ac3/attachment-0001.html>


More information about the calendarserver-changes mailing list