Skip to content

Commit 340dcb7

Browse files
Rework test suite and samples so that they are independent of each other and
so that the SQL scripts used to create/drop schemas are easily adjusted to use different schema names, if desired. Improve documentation for test suite and samples.
1 parent 628d171 commit 340dcb7

30 files changed

+1573
-1023
lines changed

samples/AdvancedQueuing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717

1818
from __future__ import print_function
1919

20-
CONNECT_STRING = "cx_Oracle/dev@localhost/orcl"
2120
BOOK_TYPE_NAME = "UDT_BOOK"
2221
QUEUE_NAME = "BOOKS"
2322
QUEUE_TABLE_NAME = "BOOK_QUEUE"
2423

2524
import cx_Oracle
25+
import SampleEnv
2626
import decimal
2727

2828
# connect to database
29-
connection = cx_Oracle.Connection(CONNECT_STRING)
29+
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING)
3030
cursor = connection.cursor()
3131

3232
# drop queue table, if present

samples/AppContext.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
from __future__ import print_function
2020

2121
import cx_Oracle
22+
import SampleEnv
2223

2324
# define constants used throughout the script; adjust as desired
24-
CONNECT_STRING = "cx_Oracle/dev@localhost/orcl"
2525
APP_CTX_NAMESPACE = "CLIENTCONTEXT"
2626
APP_CTX_ENTRIES = [
2727
( APP_CTX_NAMESPACE, "ATTR1", "VALUE1" ),
2828
( APP_CTX_NAMESPACE, "ATTR2", "VALUE2" ),
2929
( APP_CTX_NAMESPACE, "ATTR3", "VALUE3" )
3030
]
3131

32-
connection = cx_Oracle.Connection(CONNECT_STRING, appcontext = APP_CTX_ENTRIES)
32+
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING,
33+
appcontext = APP_CTX_ENTRIES)
3334
cursor = connection.cursor()
3435
for namespace, name, value in APP_CTX_ENTRIES:
3536
cursor.execute("select sys_context(:1, :2) from dual", (namespace, name))

samples/DMLReturningMultipleRows.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
import cx_Oracle
2121
import datetime
22+
import SampleEnv
2223

2324
# truncate table first so that script can be rerun
24-
connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl")
25+
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING)
2526
cursor = connection.cursor()
2627
print("Truncating table...")
2728
cursor.execute("truncate table TestTempTable")

samples/DRCP.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
from __future__ import print_function
3232

3333
import cx_Oracle
34+
import SampleEnv
3435

35-
conn = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl:pooled",
36-
cclass = "PYCLASS", purity = cx_Oracle.ATTR_PURITY_SELF)
36+
conn = cx_Oracle.Connection(SampleEnv.DRCP_CONNECT_STRING, cclass = "PYCLASS",
37+
purity = cx_Oracle.ATTR_PURITY_SELF)
3738
cursor = conn.cursor()
3839
print("Performing query using DRCP...")
3940
for row in cursor.execute("select * from TestNumbers order by IntCol"):

samples/DatabaseChangeNotification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from __future__ import print_function
2121

2222
import cx_Oracle
23+
import SampleEnv
2324
import threading
2425
import time
2526

@@ -38,8 +39,7 @@ def callback(message):
3839
print("-" * 60)
3940
print("=" * 60)
4041

41-
connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl",
42-
events = True)
42+
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING, events = True)
4343
sub = connection.subscribe(callback = callback, timeout = 1800,
4444
qos = cx_Oracle.SUBSCR_QOS_ROWIDS)
4545
print("Subscription:", sub)

samples/Editioning.py

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,10 @@
2020
from __future__ import print_function
2121

2222
import cx_Oracle
23+
import SampleEnv
2324

24-
# define constants used throughout the script; adjust as desired
25-
USER_NAME = "CX_ORACLE_TESTEDITIONS"
26-
PASSWORD = "dev"
27-
DBA_USER_NAME = "system"
28-
DBA_PASSWORD = ""
29-
DSN = ""
30-
EDITION_NAME = "CX_ORACLE_E1"
31-
32-
# create user dropping it first, if necessary
33-
connection = cx_Oracle.Connection(DBA_USER_NAME, DBA_PASSWORD, DSN)
34-
cursor = connection.cursor()
35-
cursor.execute("""
36-
select username
37-
from dba_users
38-
where username = :name""",
39-
name = USER_NAME)
40-
names = [n for n, in cursor]
41-
for name in names:
42-
print("Dropping user", name)
43-
cursor.execute("drop user %s cascade" % name)
44-
print("Creating user", USER_NAME)
45-
cursor.execute("create user %s identified by %s" % (USER_NAME, PASSWORD))
46-
cursor.execute("grant create session, create procedure to %s" % USER_NAME)
47-
cursor.execute("alter user %s enable editions" % USER_NAME)
48-
49-
# create edition, dropping it first, if necessary
50-
cursor.execute("""
51-
select edition_name
52-
from dba_editions
53-
where edition_name = :name""",
54-
name = EDITION_NAME)
55-
names = [n for n, in cursor]
56-
for name in names:
57-
print("Dropping edition", name)
58-
cursor.execute("drop edition %s" % name)
59-
print("Creating edition", EDITION_NAME)
60-
cursor.execute("create edition %s" % EDITION_NAME)
61-
cursor.execute("grant use on edition %s to %s" % (EDITION_NAME, USER_NAME))
62-
63-
# now connect to the newly created user and create a procedure
64-
connection = cx_Oracle.Connection(USER_NAME, PASSWORD, DSN)
25+
# connect to the editions user and create a procedure
26+
connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING)
6527
print("Edition should be None at this point, actual value is",
6628
connection.edition)
6729
cursor = connection.cursor()
@@ -74,9 +36,9 @@
7436
print("Function call should return Base Edition, actually returns", result)
7537

7638
# next, change the edition and recreate the procedure in the new edition
77-
cursor.execute("alter session set edition = %s" % EDITION_NAME)
78-
print("Edition should be %s at this point, actual value is" % EDITION_NAME,
79-
connection.edition)
39+
cursor.execute("alter session set edition = %s" % SampleEnv.EDITION_NAME)
40+
print("Edition should be %s at this point, actual value is" % \
41+
SampleEnv.EDITION_NAME.upper(), connection.edition)
8042
cursor.execute("""
8143
create or replace function TestEditions return varchar2 as
8244
begin
@@ -92,17 +54,17 @@
9254
print("Function call should return Base Edition, actually returns", result)
9355

9456
# the edition can be set upon connection
95-
connection = cx_Oracle.Connection(USER_NAME, PASSWORD, DSN,
96-
edition = EDITION_NAME)
57+
connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING,
58+
edition = SampleEnv.EDITION_NAME.upper())
9759
cursor = connection.cursor()
9860
result = cursor.callfunc("TestEditions", str)
9961
print("Function call should return Edition 1, actually returns", result)
10062

10163
# it can also be set via the environment variable ORA_EDITION
102-
os.environ["ORA_EDITION"] = EDITION_NAME
103-
connection = cx_Oracle.Connection(USER_NAME, PASSWORD, DSN)
104-
print("Edition should be %s at this point, actual value is" % EDITION_NAME,
105-
connection.edition)
64+
os.environ["ORA_EDITION"] = SampleEnv.EDITION_NAME.upper()
65+
connection = cx_Oracle.Connection(SampleEnv.EDITION_CONNECT_STRING)
66+
print("Edition should be %s at this point, actual value is" % \
67+
SampleEnv.EDITION_NAME.upper(), connection.edition)
10668
cursor = connection.cursor()
10769
result = cursor.callfunc("TestEditions", str)
10870
print("Function call should return Edition 1, actually returns", result)

samples/ImplicitResults.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
from __future__ import print_function
2020

2121
import cx_Oracle
22+
import SampleEnv
2223

23-
con = cx_Oracle.connect("cx_Oracle/dev@localhost/orcl")
24-
cur = con.cursor()
24+
connection = cx_Oracle.connect(SampleEnv.MAIN_CONNECT_STRING)
25+
cursor = connection.cursor()
2526

2627
# use PL/SQL block to return two cursors
27-
cur.execute("""
28+
cursor.execute("""
2829
declare
2930
c1 sys_refcursor;
3031
c2 sys_refcursor;
@@ -43,7 +44,7 @@
4344
end;""")
4445

4546
# display results
46-
for ix, resultSet in enumerate(cur.getimplicitresults()):
47+
for ix, resultSet in enumerate(cursor.getimplicitresults()):
4748
print("Result Set #" + str(ix + 1))
4849
for row in resultSet:
4950
print(row)

samples/InsertGeometry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
from __future__ import print_function
1919

2020
import cx_Oracle
21+
import SampleEnv
2122

2223
# create and populate Oracle objects
23-
connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl")
24+
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING)
2425
typeObj = connection.gettype("MDSYS.SDO_GEOMETRY")
2526
elementInfoTypeObj = connection.gettype("MDSYS.SDO_ELEM_INFO_ARRAY")
2627
ordinateTypeObj = connection.gettype("MDSYS.SDO_ORDINATE_ARRAY")

samples/QueryChangeNotification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from __future__ import print_function
2121

2222
import cx_Oracle
23+
import SampleEnv
2324
import threading
2425
import time
2526

@@ -41,8 +42,7 @@ def callback(message):
4142
print("-" * 60)
4243
print("=" * 60)
4344

44-
connection = cx_Oracle.Connection("cx_Oracle/dev@localhost/orcl",
45-
events = True)
45+
connection = cx_Oracle.Connection(SampleEnv.MAIN_CONNECT_STRING, events = True)
4646
sub = connection.subscribe(callback = callback, timeout = 1800,
4747
qos = cx_Oracle.SUBSCR_QOS_QUERY | cx_Oracle.SUBSCR_QOS_ROWIDS)
4848
print("Subscription:", sub)

samples/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This directory contains samples for cx_Oracle.
2+
3+
The schemas and SQL objects that are referenced in the samples can be created
4+
by running the SQL script sql/SetupSamples.sql. The syntax is:
5+
6+
sqlplus / as sysdba @sql/SetupSamples.sql
7+
8+
The script will create users pythondemo and pythoneditions and will create an
9+
edition called python_e1. If you wish to change the names of the users or the
10+
name of the edition you can edit the file sql/SampleEnv.sql. You will also
11+
need to edit the file SampleEnv.py or set environment variables as documented
12+
in SampleEnv.py.
13+
14+
After running the samples, the schemas and SQL objects can be dropped by
15+
running the SQL script sql/DropSamples.sql. The syntax is
16+
17+
sqlplus / as sysdba @sql/DropSamples.sql
18+

0 commit comments

Comments
 (0)