[Zope3-checkins] CVS: zopeproducts/sqlexpr/tests - __init__.py:1.1 test_sqlexpr.py:1.1

Stephan Richter srichter@cosmos.phy.tufts.edu
Wed, 11 Jun 2003 00:14:59 -0400


Update of /cvs-repository/zopeproducts/sqlexpr/tests
In directory cvs.zope.org:/tmp/cvs-serv7626/sqlexpr/tests

Added Files:
	__init__.py test_sqlexpr.py 
Log Message:
This is a little product that adds another TALES expression to page 
templates. SQL expressions allow you to write SQL statements in your page
templates directly without the need for creating new SQL Scripts, 
connection services and database adapters. The used database can be either 
specified with a connection object name or by directly specifying the RDB 
DA and the DSM. See README.txt for detailed examples.

Thanks goes to Alan Runyaga, who tickled the idea out of me today. He was
looking for allowing Zope to be more similar to PHP and other inline 
scripting tools, since they are more familiar to the Web scripter and 
therefore easier to learn.

This product will serve as base for the "Developing a new TALES expression"
recipe in the Zope 3 Developer's Cookbook.


=== Added File zopeproducts/sqlexpr/tests/__init__.py ===


=== Added File zopeproducts/sqlexpr/tests/test_sqlexpr.py ===
##############################################################################
#
# Copyright (c) 2001, 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.
#
##############################################################################
"""SQL Expression Type Tests

$Id: test_sqlexpr.py,v 1.1 2003/06/11 04:14:58 srichter Exp $
"""
import unittest

from zope.component.interfaces import IFactory 
from zope.interface import implements
from zope.component.tests.placelesssetup import PlacelessSetup
from zope.component.factory import provideFactory 
from zope.tales.tests.test_expressions import Data
from zope.tales.engine import Engine
from zope.app.rdb.tests.stubs import ConnectionStub
from zopeproducts.sqlexpr.sqlexpr import SQLExpr, NoConnectionSpecified

class ConnectionStub:

    def __init__(self):
        self._called={}

    def cursor(self):
        return CursorStub()

    def answer(self):
        return 42

    def commit(self, *ignored):
        v = self._called.setdefault('commit',0)
        v+=1
        self._called['commit']=v

    def rollback(self, *ignored):
        v = self._called.setdefault('rollback',0)
        v+=1
        self._called['rollback']=v

class CursorStub:

    description = (('id', 0, 0, 0, 0, 0, 0),
                   ('name', 0, 0, 0, 0, 0, 0),
                   ('email', 0, 0, 0, 0, 0, 0))
                   

    def fetchall(self, *args, **kw):
        return ((1, 'Stephan', 'srichter'),
               (2, 'Foo Bar', 'foobar'))

    def execute(self, operation, *args, **kw):
        assert operation == 'SELECT num FROM hitchhike'


class TypeInfoStub:
    paramstyle = 'pyformat'
    threadsafety = 0
    def getConverter(self, type):
        return lambda x: x


class FactoryStub(object):

    implements(IFactory)

    def __call__(self, *args, **kw):
        return ConnectionStub

    def getInterfaces(self):
        return TrueConnectionStub.__implements__


class SQLExprTest(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        super(SQLExprTest, self).setUp()
        self.context = Data(vars = {'rdb': 'rdb_conn',
                                    'dsn': 'dbi://test'})
        self.engine = Engine
        provideFactory('rdb_conn', FactoryStub())

    def test_exprUsingRDBAndDSN(self):
        expr = SQLExpr('name', 'SELECT num FROM hitchhike', self.engine)
        result = expr(self.context)
        self.assertEqual(1, result[0].id)
        self.assertEqual('Stephan', result[0].name)
        self.assertEqual('srichter', result[0].email)
        self.assertEqual('Foo Bar', result[1].name)

    def test_noRDBSpecs(self):
        expr = SQLExpr('name', 'SELECT num FROM hitchhike', self.engine)
        self.assertRaises(NoConnectionSpecified, expr, Data(vars={}))

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(SQLExprTest),
                        ))

if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())