[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests - __init__.py:1.1 testArguments.py:1.1 testDT_SQLGroup.py:1.1 testDT_SQLTest.py:1.1 testDT_SQLVar.py:1.1 testSQLScript.py:1.1

Stephan Richter srichter@cbu.edu
Wed, 10 Jul 2002 20:03:20 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests
In directory cvs.zope.org:/tmp/cvs-serv9815/SQLScript/tests

Added Files:
	__init__.py testArguments.py testDT_SQLGroup.py 
	testDT_SQLTest.py testDT_SQLVar.py testSQLScript.py 
Log Message:
SQL Scripts that function almost exactely like Zope 2 SQL methods. 


=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests/__init__.py ===


=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests/testArguments.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
# 
##############################################################################
"""DT_SQLVar Tests

$Id: testArguments.py,v 1.1 2002/07/11 00:03:19 srichter Exp $
"""

import unittest
from Zope.App.OFS.Content.SQLScript.Arguments import \
     Arguments, parseArguments, InvalidParameter 


class TestDT_SQLVar(unittest.TestCase):

    def _compareArgumentObjects(self, result, args):
        self.assertEqual(args.items(), result.items())


    def testSimpleParseArgument(self):
        args = parseArguments('arg1')
        result = Arguments({'arg1': {}})
        self._compareArgumentObjects(result, args)


    def testParseArgumentWithType(self):
        args = parseArguments('arg1:int')
        result = Arguments({'arg1': {'type': 'int'}})
        self._compareArgumentObjects(result, args)


    def testParseArgumentWithDefault(self):
        args1 = parseArguments('arg1=value')
        result1 = Arguments({'arg1': {'default': 'value'}})
        self._compareArgumentObjects(result1, args1)

        args2 = parseArguments('arg1="value"')
        result2 = Arguments({'arg1': {'default': 'value'}})
        self._compareArgumentObjects(result2, args2)


    def testParseArgumentWithTypeAndDefault(self):
        args1 = parseArguments('arg1:string=value')
        result1 = Arguments({'arg1': {'default': 'value', 'type': 'string'}})
        self._compareArgumentObjects(result1, args1)

        args2 = parseArguments('arg1:string="value"')
        result2 = Arguments({'arg1': {'default': 'value', 'type': 'string'}})
        self._compareArgumentObjects(result2, args2)


    def testParseMultipleArguments(self):
        args1 = parseArguments('arg1:string=value arg2')
        result1 = Arguments({'arg1': {'default': 'value', 'type': 'string'},
                             'arg2': {}})
        self._compareArgumentObjects(result1, args1)

        args2 = parseArguments('arg1:string=value\narg2')
        result2 = Arguments({'arg1': {'default': 'value', 'type': 'string'},
                             'arg2': {}})
        self._compareArgumentObjects(result2, args2)


    def testParseErrors(self):
        self.assertRaises(InvalidParameter, parseArguments, 'arg1:""')  
        self.assertRaises(InvalidParameter, parseArguments, 'arg1 = value')  
        self.assertRaises(InvalidParameter, parseArguments, 'arg1="value\' ')  
        self.assertRaises(InvalidParameter, parseArguments, 'arg1:=value')



def test_suite():
    suite = unittest.TestSuite()
    suite.addTest( unittest.makeSuite(TestDT_SQLVar) )
    return suite

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


=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests/testDT_SQLGroup.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
# 
##############################################################################
"""DT_SQLVar Tests

$Id: testDT_SQLGroup.py,v 1.1 2002/07/11 00:03:19 srichter Exp $
"""

import unittest
from Zope.App.OFS.Content.SQLScript.SQLScript import SQLDTML 


class TestDT_SQLGroup(unittest.TestCase):

    doc_class = SQLDTML


    def testSimpleUse(self):
        html = self.doc_class("""
          <dtml-sqlgroup>
            <dtml-sqlvar column type=nb>
          </dtml-sqlgroup>""")
        result = "'name'"

        self.assertEqual(html(column='name').strip(), result)


    def testComplexUse(self):
        html = self.doc_class("""
          <dtml-sqlgroup required>
            <dtml-sqlgroup>
              <dtml-sqltest name column=nick_name type=nb multiple optional>
            <dtml-or>
              <dtml-sqltest name column=first_name type=nb multiple optional>
            </dtml-sqlgroup>
          <dtml-and>
            <dtml-sqltest home_town type=nb optional>
          <dtml-and>
            <dtml-if minimum_age>
               age >= <dtml-sqlvar minimum_age type=int>
            </dtml-if>
          <dtml-and>
            <dtml-if maximum_age>
               age <= <dtml-sqlvar maximum_age type=int>
            </dtml-if>
          </dtml-sqlgroup>
        """)

        result = """
((nick_name = 'stephan'
 or first_name = 'stephan'
)
 and home_town = 'berlin'
 and age >= 16
 and age <= 21
)"""
        self.assertEqual(html(name="stephan", home_town="berlin",
                              minimum_age=16, maximum_age="21").strip(),
                         result.strip())



def test_suite():
    suite = unittest.TestSuite()
    suite.addTest( unittest.makeSuite(TestDT_SQLGroup) )
    return suite

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


=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests/testDT_SQLTest.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
# 
##############################################################################
"""DT_SQLVar Tests

$Id: testDT_SQLTest.py,v 1.1 2002/07/11 00:03:19 srichter Exp $
"""

import unittest
from Zope.App.OFS.Content.SQLScript.SQLScript import SQLDTML 
from Zope.App.OFS.Content.SQLScript import DT_SQLTest 


class TestDT_SQLTest(unittest.TestCase):

    doc_class = SQLDTML


    def testSimpleUse(self):
        html = self.doc_class("<dtml-sqltest column type=nb>")
        result = "column = 'name'"

        self.assertEqual(html(column='name'), result)


    def testIntType(self):
        html = self.doc_class("<dtml-sqltest column type=int>")
        result = "column = 3"
    
        self.assertEqual(html(column=3), result)
        self.assertEqual(html(column='3'), result)
        self.assertEqual(html(column=3.1), result)


    def testFloatType(self):
        html = self.doc_class("<dtml-sqltest column type=float>")
        result = "column = 3.1"
    
        self.assertEqual(html(column=3), "column = 3.0")
        self.assertEqual(html(column='3'), "column = 3")
        self.assertEqual(html(column='3.1'), result)
        self.assertEqual(html(column=3.1), result)


    def testStringTypeAndEscaping(self):
        html = self.doc_class("<dtml-sqltest column type=nb>")
    
        self.assertEqual(html(column='name'), "column = 'name'")
        self.assertEqual(html(column='Let\'s do it'),
                         "column = 'Let''s do it'")
        # Acid test :)
        self.assertEqual(html(column="\'\'"), "column = ''''''")


    def testOperators(self):
        for item in DT_SQLTest.comparison_operators.items():
            html = self.doc_class(
                "<dtml-sqltest column type=nb op=%s>" %item[0])
            result = "column %s 'name'" %item[1]

            self.assertEqual(html(column='name'), result)


    def testCustomColumnName(self):
            html = self.doc_class(
                "<dtml-sqltest col column=col type=nb optional>")
            result1 = "col = 'name'"
            result2 = ""

            self.assertEqual(html(col='name'), result1)
            self.assertEqual(html(col=''), result2)
            self.assertEqual(html(), result2)


    def testOptional(self):
            html = self.doc_class("<dtml-sqltest column type=nb optional>")
            result1 = "column = 'name'"
            result2 = ""

            self.assertEqual(html(column='name'), result1)
            self.assertEqual(html(column=''), result2)
            self.assertEqual(html(), result2)


    def testMultiple(self):
            html = self.doc_class(
                "<dtml-sqltest column type=nb optional multiple>")
            result1 = "column in ('name1', 'name2')"
            result2 = ""

            self.assertEqual(html(column=('name1', 'name2')), result1)
            self.assertEqual(html(column=()), result2)
            self.assertEqual(html(), result2)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest( unittest.makeSuite(TestDT_SQLTest) )
    return suite

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


=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests/testDT_SQLVar.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
# 
##############################################################################
"""DT_SQLVar Tests

$Id: testDT_SQLVar.py,v 1.1 2002/07/11 00:03:19 srichter Exp $
"""

import unittest
from Zope.App.OFS.Content.SQLScript.SQLScript import SQLDTML 


class TestDT_SQLVar(unittest.TestCase):

    doc_class = SQLDTML


    def testSimpleUse(self):
        html = self.doc_class("<dtml-sqlvar column type=nb>")
        result = "'name'"
    
        self.assertEqual(html(column='name'), result)
       

    def testIntType(self):
        html = self.doc_class("<dtml-sqlvar column type=int>")
        result = "3"
    
        self.assertEqual(html(column=3), result)
        self.assertEqual(html(column='3'), result)
        self.assertEqual(html(column=3.1), result)


    def testFloatType(self):
        html = self.doc_class("<dtml-sqlvar column type=float>")
        result = "3.1"
    
        self.assertEqual(html(column=3), "3.0")
        self.assertEqual(html(column='3'), "3")
        self.assertEqual(html(column='3.1'), result)
        self.assertEqual(html(column=3.1), result)


    def testStringTypeAndEscaping(self):
        html = self.doc_class("<dtml-sqlvar column type=nb>")
    
        self.assertEqual(html(column='name'), "'name'")
        self.assertEqual(html(column='Let\'s do it'), "'Let''s do it'")
        # Acid test :)
        self.assertEqual(html(column="\'\'"), "''''''")


    def testOptional(self):
        html = self.doc_class("""<dtml-sqlvar column type=nb optional>""")
        result = "null"
    
        self.assertEqual(html(column=None), result)
        self.assertEqual(html(column=''), result)
        self.assertEqual(html(), result)
    


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest( unittest.makeSuite(TestDT_SQLVar) )
    return suite

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


=== Added File Zope3/lib/python/Zope/App/OFS/Content/SQLScript/tests/testSQLScript.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
# 
##############################################################################
"""DT_SQLVar Tests

$Id: testSQLScript.py,v 1.1 2002/07/11 00:03:19 srichter Exp $
"""

import unittest

from Zope.App.RDB.IConnectionService import IConnectionService
from Zope.App.RDB.IZopeConnection import IZopeConnection
from Zope.App.RDB.IZopeCursor import IZopeCursor
import Zope.ComponentArchitecture
from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
from Zope.ComponentArchitecture.GlobalServiceManager import \
     serviceManager as sm

from Zope.App.OFS.Content.SQLScript.SQLScript import SQLScript 
from Zope.App.OFS.Content.SQLScript.Arguments import Arguments

# Make spme fixes, so that we overcome some of the natural ZODB properties
def getNextServiceManager(context):
    return sm

class CursorStub:

    __implements__ = IZopeCursor

    description = (('name', 'string'),)

    def execute(self, operation, parameters=None):
        self.result = {"SELECT name FROM Table WHERE id = 1":
                       (('stephan',),)}[operation]

    def fetchall(self):
        return self.result


class ConnectionStub:
    __implements__ = IZopeConnection

    def cursor(self):
        return CursorStub()


class ConnectionServiceStub:
    __implements__ = IConnectionService

    def getConnection(self, name):
        return ConnectionStub()


class SQLScriptTest(unittest.TestCase, PlacelessSetup):

    def setUp(self):
        PlacelessSetup.setUp(self)
        sm.defineService('ConnectionService', IConnectionService)
        sm.provideService('ConnectionService', ConnectionServiceStub())
        self._old_getNextServiceManager = \
                              Zope.ComponentArchitecture.getNextServiceManager
        Zope.ComponentArchitecture.getNextServiceManager = \
                              getNextServiceManager

    def tearDown(self):
        Zope.ComponentArchitecture.getNextServiceManager = \
                              self._old_getNextServiceManager
        

    def _getScript(self):
        return SQLScript("my_connection",
                   "SELECT name FROM Table WHERE <dtml-sqltest id type=int>",
                         'id')


    def testGetArguments(self):
        assert isinstance(arguments, StringTypes), \
               '"arguments" argument of setArguments() must be a string' 
        self._arg_string = arguments
        self.arguments = parseArguments(arguments)


    def testGetArguments(self):
        result = Arguments({'id': {}})
        args = self._getScript().getArguments()
        self.assertEqual(args, result)


    def testGetArgumentsString(self):
        self.assertEqual('id', self._getScript().getArgumentsString())


    def testSetSource(self):
        script = self._getScript()
        script.setSource('SELECT * FROM Table')
        self.assertEqual('SELECT * FROM Table', script.getSource())


    def testGetSource(self):
        self.assertEqual(
            "SELECT name FROM Table WHERE <dtml-sqltest id type=int>",
            self._getScript().getSource())


    def testSetConnectionName(self):
        script = self._getScript()
        script.setConnectionName('test_conn')
        self.assertEqual('test_conn', script.getConnectionName())


    def testGetConnectionName(self):
        self.assertEqual('my_connection',
                         self._getScript().getConnectionName())
        

    def testSQLScript(self):
        result = self._getScript()(id=1)
        self.assertEqual(result.names, ('name',))
        self.assertEqual(result[0].name, 'stephan')


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(SQLScriptTest))
    return suite

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