array.py :  » Database » PyPgSQL » pyPgSQL-2.5.1 » test » regression » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyPgSQL 
PyPgSQL » pyPgSQL 2.5.1 » test » regression » array.py
#!/usr/local/bin/python
#-*- coding: ISO-8859-1 -*-
#ident "@(#) $Id: array.py,v 1.4 2003/07/12 17:19:06 ghaering Exp $"
# vi:set sw=4 ts=8 showmode ai: 
#-----------------------------------------------------------------------+
# Name:    array.py            |
#                  |
# Description:  array.py contains test cases for using the PostgreSQL  |
#    array type from within the PgSQL module.    |
#                  |
# Note:    These test cases requires that a test database named  |
#    pypgsql exists and that the person running the test has  |
#    full rights to the database.        |
#=======================================================================|
# Copyright 2002 by Gerhard Hring.          |
# All rights reserved.              |
#                  |
# Permission to use, copy, modify, and distribute this software and its  |
# documentation for any purpose and without fee is hereby granted, pro-  |
# vided that the above copyright notice appear in all copies and that  |
# both that copyright notice and this permission notice appear in sup-  |
# porting documentation, and that the copyright owner's name not be  |
# used in advertising or publicity pertaining to distribution of the  |
# software without specific, written prior permission.      |
#                  |
# THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,  |
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN  |
# NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR  |
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS  |
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE  |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE  |
# USE OR PERFORMANCE OF THIS SOFTWARE.          |
#=======================================================================|
# Revision History:              |
#                  |
# Date      Ini Description            |
# --------- --- ------------------------------------------------------- |
# 02NOV2002 bga Modified code to use the PgArray class for PostgreSQL  |
#    arrays.              |
# 21APR2002 gh  Added some more mean testcases, which test for control  |
#    characters and other special characters inside strings,  |
#    and multi-dimensional arrays.        |
# 16APR2002 gh  Initial release by Gerhard Hring.      |
#-----------------------------------------------------------------------+
import unittest, types
import sys
from pyPgSQL import PgSQL
PgArray = PgSQL.PgArray

class ArrayTestCases(unittest.TestCase):
    def setUp(self):
  self.conn = PgSQL.connect(database="pypgsql")
        self.conn.autocommit = 1

    def checkEqual(self, real, planned):
  self.failUnlessEqual(real.value, planned.value, \
      "Received %s, should have been %s" % (repr(real), repr(planned)))

    def CheckForIntegerInsert(self):
  ivalues = PgArray([3,4,5])
  cursor = self.conn.cursor()
        try:
            cursor.execute("drop table test")
        except:
            pass
  cursor.execute("create table test (ia int[])")
  cursor.execute("insert into test(ia) values (%s)", ivalues)
  cursor.execute("select ia from test")
  result = cursor.fetchone()
  self.failUnless(isinstance(result.ia, PgArray), \
      "the integer array isn't returned as a PgArray")
  self.checkEqual(result.ia, ivalues)

    def CheckForNumericInsert(self):
  numlist = PgArray(map(PgSQL.PgNumeric, ['4.7', '-3.2', '23.17']))

  cursor = self.conn.cursor()
        try:
            cursor.execute("drop table test")
        except:
            pass
  cursor.execute("create table test(na numeric[])")
  cursor.execute("insert into test(na) values (%s)", numlist)
  cursor.execute("select na from test")
  result = cursor.fetchone()
  self.failUnless(isinstance(result.na, PgArray), \
      "the numeric array isn't returned as a PgArray")
  self.checkEqual(result.na, numlist)

    def CheckForStringInsert(self):
  cursor = self.conn.cursor()

  stringlists = PgArray([['hey', 'you', 'there'],
             ['fancy', ',', '{chars}', '"'],
             ['what about', 'some \\backslashes'],
             ['some more \\', '\\ and \\ etc'],
             ['aa', '%s\n%s' % (chr(10), chr(29))],
             ['what about backslashes', 'at the end\\'],
             ['interesting', '"', '\\"', '\\"\\'],
             ['{}\\', '"\\}}\\\'"']])

  for stringlist in stringlists:
            try:
                cursor.execute("drop table test")
            except:
                pass
            cursor.execute("create table test(va varchar[])")
      cursor.execute("insert into test(va) values (%s)", stringlist)
      cursor.execute("select va from test")
      result = cursor.fetchone()
      self.failUnless(isinstance(result.va, PgArray), \
    "the varchar array isn't returned as a PgArray")
      self.checkEqual(result.va, stringlist)

    def CheckForStringInsertBruteForce(self):
  cursor = self.conn.cursor()

        chars = "\"'{}\\\n%s%sabc" % (chr(14), chr(5))
        
        try:
            cursor.execute("drop table test")
        except:
            pass
        cursor.execute("create table test(va varchar[])")
        self.conn.commit()
        for i in chars:
            for j in chars:
                for k in chars:
              cursor.execute("insert into test(va) values (%s)", PgArray([i+j+k]))
              cursor.execute("select va from test where oid=%s", cursor.oidValue)
              result = cursor.fetchone()
              self.failUnless(isinstance(result.va, PgArray), \
            "the varchar array isn't returned as a PgArray")
              self.checkEqual(result.va, [i+j+k])

    def CheckForStringInsertMultiDim(self):
  cursor = self.conn.cursor()

  stringlists =  PgArray([[["a", "b"], ["c", "d"]],
        [[["a", "b"], ["c", "d"]],
         [["e", None], ["g", "h"]]],
        [[["{}", "\005{}\\'"], ["'asf'", "[\\{]"]],
         [["e", "f"], ["g", "h"]]],
              [["a", "b"], ["c", "d"]]])

  for stringlist in stringlists:
            try:
                cursor.execute("drop table test")
            except:
                pass
            cursor.execute("create table test(va varchar[])")
      cursor.execute("insert into test(va) values (%s)", stringlist)
      cursor.execute("select va from test")
      result = cursor.fetchone()
      self.failUnless(isinstance(result.va, PgArray), \
    "the varchar array isn't returned as a PgArray")

            expected = stringlist[:]
            try:
                if expected[1][0][1] is None:
                    expected[1][0][1] = ''
            except IndexError, reason:
                pass

      self.checkEqual(result.va, expected)

    def CheckForIntInsertMultiDim(self):
  cursor = self.conn.cursor()

        intlists = PgArray([[[1,2], [3,4]],
          [[-5,3,4], [2, None, 10]]])

        excpected_intlists = PgArray([[[1,2], [3,4]],
              [[-5,3,4], [2, 0, 10]]])
        
  for intlist, expected in zip(intlists, excpected_intlists):
            try:
                cursor.execute("drop table test")
            except:
                pass
            cursor.execute("create table test(ia int[])")
      cursor.execute("insert into test(ia) values (%s)", intlist)
      cursor.execute("select ia from test")
      result = cursor.fetchone()
      self.failUnless(isinstance(result.ia, PgArray), \
    "the int array isn't returned as a PgArray")
      self.checkEqual(result.ia, expected)

if __name__ == "__main__":
    TestSuite = unittest.TestSuite()

    TestSuite.addTest(ArrayTestCases("CheckForIntegerInsert"))
    TestSuite.addTest(ArrayTestCases("CheckForNumericInsert"))
    TestSuite.addTest(ArrayTestCases("CheckForStringInsert"))
    #TestSuite.addTest(ArrayTestCases("CheckForStringInsertBruteForce"))
    TestSuite.addTest(ArrayTestCases("CheckForStringInsertMultiDim"))
    TestSuite.addTest(ArrayTestCases("CheckForIntInsertMultiDim"))

    runner = unittest.TextTestRunner()
    runner.run(TestSuite)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.