[Zope-CVS] CVS: Products/Ape/lib/apelib/tests - testzodbtables.py:1.1.2.1 testall.py:1.6.2.2

Shane Hathaway shane at zope.com
Thu Dec 25 16:52:42 EST 2003


Update of /cvs-repository/Products/Ape/lib/apelib/tests
In directory cvs.zope.org:/tmp/cvs-serv11216/lib/apelib/tests

Modified Files:
      Tag: ape-0_8-branch
	testall.py 
Added Files:
      Tag: ape-0_8-branch
	testzodbtables.py 
Log Message:
Switched from minitables to zodbtables.

zodbtables is a more mature version of minitables.  Unlike 
minitables, zodbtables is able to persist the database in ZODB, and the 
results of queries are Record objects rather than dictionaries, so the 
results have attributes in addition to items.



=== Added File Products/Ape/lib/apelib/tests/testzodbtables.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""zodbtables tests.

$Id: testzodbtables.py,v 1.1.2.1 2003/12/25 21:52:12 shane Exp $
"""

import unittest
from time import time

from apelib.zodb3 import zodbtables


TEST_DATA = [
    {'name':    'Jose',
     'sex':     'm',
     'address': '101 Example St.',
     'phone':   '123-4567',
     },
    {'name':    'Maria',
     'sex':     'f',
     'address': '102 Example St.',
     },
    {'name':    'Carlos',
     'sex':     'm',
     'phone':   '987-6543',
     },
    {'name':    'Tiago',
     'sex':     'm',
     'phone':   '123-4567',
     },
    {'name':    'Ana',
     'sex':     'f',
     'phone':   '123-4567',
     },
    ]


class ZODBTableTests(unittest.TestCase):

    table_schema = zodbtables.TableSchema()
    table_schema.addColumn('name', primary=1, indexed=1)
    table_schema.addColumn('sex', indexed=1)
    table_schema.addColumn('address')
    table_schema.addColumn('phone', indexed=1)

    def setUp(self):
        self.table = table = zodbtables.Table(self.table_schema)
        for data in TEST_DATA:
            table.insert(data)

    def tearDown(self):
        get_transaction().abort()

    def testSelectByName(self):
        # Searches by primary key
        records = self.table.select({'name': 'Jose'})
        self.assertEqual(len(records), 1)
        self.assertEqual(records[0]['address'], '101 Example St.')

    def testSelectByUnknownName(self):
        # Searches by primary key
        records = self.table.select({'name': 'Joao'})
        self.assertEqual(len(records), 0)

    def testSelectByPhone(self):
        # Searches by index
        records = self.table.select({'phone': '987-6543'})
        self.assertEqual(len(records), 1)
        self.assertEqual(records[0]['name'], 'Carlos')

    def testSelectByAddress(self):
        # Searches one-by-one
        records = self.table.select({'address': '102 Example St.'})
        self.assertEqual(len(records), 1)
        self.assertEqual(records[0]['name'], 'Maria')

    def testSelectMales(self):
        records = self.table.select({'sex': 'm'})
        self.assertEqual(len(records), 3)

    def testSelectFemales(self):
        records = self.table.select({'sex': 'f'})
        self.assertEqual(len(records), 2)

    def testSelectByNameAndSex(self):
        records = self.table.select({'name': 'Jose', 'sex': 'm'})
        self.assertEqual(len(records), 1)

    def testSelectByNameAndIncorrectSex(self):
        records = self.table.select({'name': 'Jose', 'sex': 'f'})
        self.assertEqual(len(records), 0)

    def testSelectBySexAndPhone(self):
        # Intersects two indexes
        records = self.table.select({'phone': '123-4567', 'sex': 'm'})
        self.assertEqual(len(records), 2)

    def testSelectAll(self):
        records = self.table.select({})
        self.assertEqual(len(records), 5)

    def testInsertMinimal(self):
        self.table.insert({'name': 'Edson'})

    def testInsertDuplicate(self):
        self.assertRaises(zodbtables.DuplicateError,
                          self.table.insert, {'name':'Carlos'})

    def testInsertWithoutPrimaryKey(self):
        self.assertRaises(ValueError, self.table.insert, {})

    def testUpdateNewAddress(self):
        # Test adding a value in a non-indexed column
        self.table.update({'name': 'Carlos'}, {'address': '99 Sohcahtoa Ct.'})
        records = self.table.select({'address': '99 Sohcahtoa Ct.'})
        self.assertEqual(len(records), 1)
        self.assertEqual(records[0]['name'], 'Carlos')

    def testUpdateChangeAddress(self):
        # Test changing a value in a non-indexed column
        self.table.update({'name': 'Jose'}, {'address': '99 Sohcahtoa Ct.'})
        records = self.table.select({'address': '99 Sohcahtoa Ct.'})
        self.assertEqual(len(records), 1)
        self.assertEqual(records[0]['name'], 'Jose')

    def testUpdateFemaleAddresses(self):
        # Test changing and adding simultaneously in a non-indexed column
        self.table.update({'sex': 'f'}, {'address': '99 Sohcahtoa Ct.'})
        records = self.table.select({'address': '99 Sohcahtoa Ct.'})
        self.assertEqual(len(records), 2)


    def testUpdateChangePhone(self):
        # Test changing a value in an indexed column
        records = self.table.select({'phone': '123-4567'})
        self.assertEqual(len(records), 3)  # Precondition

        self.table.update({'name': 'Jose'}, {'phone': '111-5555'})
        records = self.table.select({'phone': '123-4567'})
        self.assertEqual(len(records), 2)
        records = self.table.select({'phone': '111-5555'})
        self.assertEqual(len(records), 1)
        self.assertEqual(records[0]['name'], 'Jose')


    def testUpdateChangeName(self):
        # Test changing a value in a primary key column
        records = self.table.select({'name': 'Jose'})
        self.assertEqual(len(records), 1)  # Precondition

        self.table.update({'name': 'Jose'}, {'name': 'Marco'})
        records = self.table.select({'name': 'Jose'})
        self.assertEqual(len(records), 0)
        records = self.table.select({'name': 'Marco'})
        self.assertEqual(len(records), 1)


    def testUpdateNameConflict(self):
        self.assertRaises(zodbtables.DuplicateError, self.table.update,
                          {'name':'Jose'}, {'name': 'Carlos'})


    def testDeleteNothing(self):
        old_count = len(self.table.select({}))
        self.assertEqual(self.table.delete({'name': 'Edson'}), 0)
        new_count = len(self.table.select({}))
        self.assert_(old_count == new_count)


    def testDeleteAll(self):
        count = len(self.table.select({}))
        self.assert_(count > 0)
        self.assertEqual(self.table.delete({}), count)
        new_count = len(self.table.select({}))
        self.assert_(new_count == 0)


    def testDeleteOne(self):
        # Test deletion of one row
        records = self.table.select({'name': 'Jose'})
        self.assertEqual(len(records), 1)  # Precondition
        records = self.table.select({'phone': '123-4567'})
        self.assertEqual(len(records), 3)  # Precondition

        count = self.table.delete({'name': 'Jose'})
        self.assertEqual(count, 1)
        records = self.table.select({'name': 'Jose'})
        self.assertEqual(len(records), 0)
        records = self.table.select({'phone': '123-4567'})
        self.assertEqual(len(records), 2)


    def testDeleteByPhone(self):
        records = self.table.select({'phone': '123-4567'})
        self.assertEqual(len(records), 3)  # Precondition
        
        count = self.table.delete({'phone': '123-4567'})
        self.assertEqual(count, 3)
        records = self.table.select({'phone': '123-4567'})
        self.assertEqual(len(records), 0)
        records = self.table.select({'name': 'Jose'})
        self.assertEqual(len(records), 0)

        # Make sure it didn't delete other data
        records = self.table.select({'name': 'Maria'})
        self.assertEqual(len(records), 1)

    def testSelectPartialPrimaryKey(self):
        # Select by only one part of a primary key
        schema = zodbtables.TableSchema()
        schema.addColumn('name', primary=1)
        schema.addColumn('id', primary=1)
        table = zodbtables.Table(schema)
        table.insert({'name': 'joe', 'id': 1})
        table.insert({'name': 'john', 'id': 2})
        records = table.select({'name': 'joe'})
        self.assertEqual(len(records), 1)


class ZODBTableTestsWithoutPrimaryKey(ZODBTableTests):
    # Same tests but with no primary key.  The absence of a primary
    # key affects many branches of the code.
    table_schema = zodbtables.TableSchema()
    table_schema.addColumn('name', indexed=1)
    table_schema.addColumn('sex', indexed=1)
    table_schema.addColumn('address')
    table_schema.addColumn('phone', indexed=1)

    # Disabled tests
    def testInsertWithoutPrimaryKey(self):
        pass

    def testInsertDuplicate(self):
        pass

    def testUpdateNameConflict(self):
        pass


if __name__ == '__main__':
    unittest.main()



=== Products/Ape/lib/apelib/tests/testall.py 1.6.2.1 => 1.6.2.2 ===
--- Products/Ape/lib/apelib/tests/testall.py:1.6.2.1	Tue Dec 23 00:52:36 2003
+++ Products/Ape/lib/apelib/tests/testall.py	Thu Dec 25 16:52:11 2003
@@ -38,7 +38,7 @@
 from testzope2fs import Zope2FSTests, Zope2FSUnderscoreTests
 from testparams import ParamsTests
 from testsqlimpl import ApelibSQLImplTests
-from apelib.config.tests.test_minitables import MiniTableTests
+from testzodbtables import ZODBTableTests, ZODBTableTestsWithoutPrimaryKey
 from testscanner import ScanControlTests, ScannerTests
 from testzope2sql import PsycopgTests, MySQLTests
 import testzope2sql
@@ -50,7 +50,8 @@
     suite = unittest.TestSuite()
     for klass in (
         SerializationTests,
-        MiniTableTests,
+        ZODBTableTests,
+        ZODBTableTestsWithoutPrimaryKey,
         ApelibImplTests,
         ApeStorageTests,
         ApeIOTests,




More information about the Zope-CVS mailing list