[Zope-CVS] CVS: Products/AdaptableStorage/gateway_sql/tests - __init__.py:1.1 testZope2SQL.py:1.1

Shane Hathaway shane@zope.com
Tue, 10 Dec 2002 15:37:53 -0500


Update of /cvs-repository/Products/AdaptableStorage/gateway_sql/tests
In directory cvs.zope.org:/tmp/cvs-serv10311/gateway_sql/tests

Added Files:
	__init__.py testZope2SQL.py 
Log Message:
Checkpoint: storage to a Postgres database.  Not working yet.


=== Added File Products/AdaptableStorage/gateway_sql/tests/__init__.py ===



=== Added File Products/AdaptableStorage/gateway_sql/tests/testZope2SQL.py ===
##############################################################################
#
# Copyright (c) 2002 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.
#
##############################################################################
"""Test of storing folders in a Postgres database via ZODB

$Id: testZope2SQL.py,v 1.1 2002/12/10 20:37:52 shane Exp $
"""

import os
import unittest

from ZODB import Persistent
from Persistence import PersistentMapping
from OFS.Folder import Folder
from OFS.SimpleItem import SimpleItem

from Products.AdaptableStorage.zodb.ASDB import ASDB
from Products.AdaptableStorage.zodb.ASStorage import ASStorage
from Products.AdaptableStorage.zodb.StaticResource import StaticResource
from Products.AdaptableStorage.Zope2SQL import createMapper


class TestFolder(Folder):

    meta_type = 'Zope2SQL Test Folder'

    def __init__(self, title):
        self.title = title


class TestFile(SimpleItem):

    meta_type = 'Zope2SQL Test File'

    def __init__(self, content):
        self.content = content


class Zope2SQLTests (unittest.TestCase):

    def setUp(self):
        dm, conn, gws = createMapper('', 'test_temp')
        self.dm = dm
        self.conn = conn
        self.gws = gws
        resource = StaticResource(dm)
        storage = ASStorage(resource, [conn])
        self.storage = storage
        db = ASDB(storage, resource)
        self.db = db

    def tearDown(self):
        self.db.close()
        # Drop the tables
        for gw in self.gws:
            if hasattr(gw, 'drop'):
                gw.drop()
        conn.commit()

    def testLoad(self):
        conn = self.db.open()
        try:
            app = conn.root()['Application']
            app.getId()
        finally:
            conn.close()

    def testStore(self):
        conn = self.db.open()
        try:
            app = conn.root()['Application']
            f = Folder()
            f.id = 'Holidays'
            app._setObject(f.id, f, set_owner=0)
            get_transaction().commit()

            f2 = Folder()
            f2.id = 'Christmas'
            f._setObject(f2.id, f2, set_owner=0)
            get_transaction().commit()

            f3 = Folder()
            f3.id = 'Eve'
            f2._setObject(f3.id, f3, set_owner=0)
            get_transaction().commit()

            conn2 = self.db.open()
            try:
                app = conn2.root()['Application']
                self.assert_(hasattr(app, 'Holidays'))
                self.assert_(hasattr(app.Holidays, 'Christmas'))
                self.assert_(hasattr(app.Holidays.Christmas, 'Eve'))
            finally:
                conn2.close()

        finally:
            conn.close()


    def testAnyFolderishStorage(self):
        # Try to store a folderish object of an otherwise unknown class
        conn = self.db.open()
        try:
            app = conn.root()['Application']
            f = Folder()
            f.id = 'Holidays'
            app._setObject(f.id, f, set_owner=0)
            get_transaction().commit()

            f2 = TestFolder("New Year's Eve")
            f2.id = 'NewYear'
            f._setObject(f2.id, f2, set_owner=0)
            get_transaction().commit()

            # Verify the object is in its own database record
            self.assertNotEqual(f2._p_oid, None)
            f2._p_changed = None
            self.assert_(f2._p_changed is None)

            # Verify the ability to load it
            conn2 = self.db.open()
            try:
                app2 = conn2.root()['Application']
                ff = app2.Holidays.NewYear
                self.assertEqual(ff.title, "New Year's Eve")
                self.assertEqual(ff.__class__, TestFolder)
            finally:
                conn2.close()
        finally:
            conn.close()


    def testAnyFileishStorage(self):
        # Try to store a fileish object of an otherwise unknown class
        conn = self.db.open()
        try:
            content = 'insert wise expression here'

            app = conn.root()['Application']
            f = TestFile(content)
            f.id = 'testitem'
            app._setObject(f.id, f, set_owner=0)
            get_transaction().commit()

            # Verify the object is in its own database record
            self.assertNotEqual(f._p_oid, None)
            f._p_changed = None
            self.assert_(f._p_changed is None)

            # Verify the ability to load it
            conn2 = self.db.open()
            try:
                app2 = conn2.root()['Application']
                ff = app2.testitem
                self.assertEqual(ff.content, content)
                self.assertEqual(ff.__class__, TestFile)
            finally:
                conn2.close()
        finally:
            conn.close()


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