File: sptest.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (224 lines) | stat: -rw-r--r-- 7,877 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Jython Database Specification API 2.0
#
# Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>

from zxtest import zxCoreTestCase

class OracleSPTest(zxCoreTestCase):

    def setUp(self):
        zxCoreTestCase.setUp(self)

        c = self.cursor()

        try:
            try:
                c.execute("drop table sptest")
            except:
                self.db.rollback()
            try:
                c.execute("create table sptest (x varchar2(20))")
                c.execute("create or replace procedure procnone is begin insert into sptest values ('testing'); end;")
                c.execute("create or replace procedure procin (y in varchar2) is begin insert into sptest values (y); end;")
                c.execute("create or replace procedure procout (y out varchar2) is begin y := 'tested'; end;")
                c.execute("create or replace procedure procinout (y out varchar2, z in varchar2) is begin insert into sptest values (z); y := 'tested'; end;")
                c.execute("create or replace function funcnone return varchar2 is begin return 'tested'; end;")
                c.execute("create or replace function funcin (y varchar2) return varchar2 is begin return y || y; end;")
                c.execute("create or replace function funcout (y out varchar2) return varchar2 is begin y := 'tested'; return 'returned'; end;")
                self.db.commit()
            except:
                self.db.rollback()
                self.fail("procedure creation failed")

            self.proc_errors("PROC")
            self.proc_errors("FUNC")

        finally:
            c.close()

    def tearDown(self):
        zxCoreTestCase.tearDown(self)

    def proc_errors(self, name):
        c = self.cursor()
        try:
            c.execute("select * from user_errors where name like '%s%%'" % (name.upper()))
            errors = c.fetchall()
            try:
                assert not errors, "found errors"
            except AssertionError, e:
                print "printing errors:"
                for a in errors:
                    print a
                raise e
        finally:
            c.close()

    def testCursor(self):
        c = self.cursor()
        try:

            c.execute("insert into sptest values ('a')")
            c.execute("insert into sptest values ('b')")
            c.execute("insert into sptest values ('c')")
            c.execute("insert into sptest values ('d')")
            c.execute("insert into sptest values ('e')")

            c.execute("""
                    CREATE OR REPLACE PACKAGE types
                    AS
                            TYPE ref_cursor IS REF CURSOR;
                    END;
            """)

            c.execute("""
                    CREATE OR REPLACE FUNCTION funccur(v_x IN VARCHAR)
                            RETURN types.ref_cursor
                    AS
                            funccur_cursor types.ref_cursor;
                    BEGIN
                            OPEN funccur_cursor FOR
                                    SELECT x FROM sptest WHERE x < v_x;
                            RETURN funccur_cursor;
                    END;
            """)

            self.proc_errors("funccur")

            c.callproc("funccur", ("z",))
            data = c.fetchall()
            self.assertEquals(5, len(data))
            c.callproc("funccur", ("c",))
            data = c.fetchall()
            self.assertEquals(2, len(data))

        finally:
            c.close()

    def testProcin(self):
        c = self.cursor()
        try:
            params = ["testProcin"]
            c.callproc("procin", params)
            self.assertEquals([], c.fetchall())
            c.execute("select * from sptest")
            self.assertEquals(1, len(c.fetchall()))
        finally:
            c.close()

    def testProcinout(self):
        c = self.cursor()
        try:
            params = [None, "testing"]
            c.callproc("procinout", params)
            data = c.fetchone()
            assert data is None, "data was not None"
            c.execute("select * from sptest")
            data = c.fetchone()
            self.assertEquals("testing", data[0])
            self.assertEquals("tested", params[0])
        finally:
            c.close()

    def testFuncnone(self):
        c = self.cursor()
        try:
            c.callproc("funcnone")
            data = c.fetchone()
            assert data is not None, "data was None"
            self.assertEquals(1, len(data))
            self.assertEquals("tested", data[0])
        finally:
            c.close()

    def testFuncin(self):
        c = self.cursor()
        try:
            params = ["testing"]
            c.callproc("funcin", params)
            self.assertEquals(1, c.rowcount)
            data = c.fetchone()
            assert data is not None, "data was None"
            self.assertEquals(1, len(data))
            self.assertEquals("testingtesting", data[0])
        finally:
            c.close()

    def testCallingWithKws(self):
        c = self.cursor()
        try:
            params = ["testing"]
            c.callproc("funcin", params=params)
            self.assertEquals(1, c.rowcount)
            data = c.fetchone()
            assert data is not None, "data was None"
            self.assertEquals(1, len(data))
            self.assertEquals("testingtesting", data[0])
        finally:
            c.close()

    def testFuncout(self):
        c = self.cursor()
        try:
            params = [None]
            c.callproc("funcout", params)
            data = c.fetchone()
            assert data is not None, "data was None"
            self.assertEquals(1, len(data))
            self.assertEquals("returned", data[0])
            self.assertEquals("tested", params[0].strip())
        finally:
            c.close()

    def testMultipleFetch(self):
        """testing the second fetch call to a callproc() is None"""
        c = self.cursor()
        try:
            c.callproc("funcnone")
            data = c.fetchone()
            assert data is not None, "data was None"
            data = c.fetchone()
            assert data is None, "data was not None"
        finally:
            c.close()

class SQLServerSPTest(zxCoreTestCase):

    def testProcWithResultSet(self):
        c = self.cursor()
        try:
            for a in (("table", "sptest"), ("procedure", "sp_proctest")):
                try:
                    c.execute("drop %s %s" % (a))
                except:
                    pass

            c.execute("create table sptest (a int, b varchar(32))")
            c.execute("insert into sptest values (1, 'hello')")
            c.execute("insert into sptest values (2, 'there')")
            c.execute("insert into sptest values (3, 'goodbye')")

            c.execute(""" create procedure sp_proctest (@A int) as select a, b from sptest where a <= @A """)
            self.db.commit()

            c.callproc("sp_proctest", (2,))
            data = c.fetchall()
            self.assertEquals(2, len(data))
            self.assertEquals(2, len(c.description))
            assert c.nextset() is not None, "expected an additional result set"
            data = c.fetchall()
            self.assertEquals(1, len(data))
            self.assertEquals(1, len(c.description))
        finally:
            c.close()

#       def testSalesByCategory(self):
#               c = self.cursor()
#               try:
#                       c.execute("use northwind")
#                       c.callproc(("northwind", "dbo", "SalesByCategory"), ["Seafood", "1998"])
#                       data = c.fetchall()
#                       assert data is not None, "no results from SalesByCategory"
#                       assert len(data) > 0, "expected numerous results"
#               finally:
#                       c.close()