55"""Module for testing AQ objects."""
66
77import cx_Oracle
8+ import decimal
9+ import threading
810
911class TestAQ (BaseTestCase ):
12+ bookData = [
13+ ("Wings of Fire" , "A.P.J. Abdul Kalam" ,
14+ decimal .Decimal ("15.75" )),
15+ ("The Story of My Life" , "Hellen Keller" ,
16+ decimal .Decimal ("10.50" )),
17+ ("The Chronicles of Narnia" , "C.S. Lewis" ,
18+ decimal .Decimal ("25.25" ))
19+ ]
20+
21+ def __deqInThread (self , results ):
22+ connection = cx_Oracle .connect (USERNAME , PASSWORD , TNSENTRY )
23+ booksType = connection .gettype ("UDT_BOOK" )
24+ book = booksType .newobject ()
25+ options = connection .deqoptions ()
26+ options .wait = 10
27+ props = connection .msgproperties ()
28+ if connection .deq ("BOOKS" , options , props , book ):
29+ results .append ((book .TITLE , book .AUTHORS , book .PRICE ))
30+ connection .commit ()
1031
1132 def __verifyAttribute (self , obj , attrName , value ):
1233 setattr (obj , attrName , value )
1334 self .assertEqual (getattr (obj , attrName ), value )
1435
36+ def testDeqEmpty (self ):
37+ "test dequeuing an empty queue"
38+ booksType = self .connection .gettype ("UDT_BOOK" )
39+ book = booksType .newobject ()
40+ options = self .connection .deqoptions ()
41+ options .wait = cx_Oracle .DEQ_NO_WAIT
42+ props = self .connection .msgproperties ()
43+ messageId = self .connection .deq ("BOOKS" , options , props , book )
44+ self .assertEqual (messageId , None )
45+
46+ def testDeqEnq (self ):
47+ "test enqueuing and dequeuing multiple messages"
48+ booksType = self .connection .gettype ("UDT_BOOK" )
49+ options = self .connection .enqoptions ()
50+ props = self .connection .msgproperties ()
51+ for title , authors , price in self .bookData :
52+ book = booksType .newobject ()
53+ book .TITLE = title
54+ book .AUTHORS = authors
55+ book .PRICE = price
56+ self .connection .enq ("BOOKS" , options , props , book )
57+ options = self .connection .deqoptions ()
58+ options .navigation = cx_Oracle .DEQ_FIRST_MSG
59+ options .wait = cx_Oracle .DEQ_NO_WAIT
60+ results = []
61+ while self .connection .deq ("BOOKS" , options , props , book ):
62+ row = (book .TITLE , book .AUTHORS , book .PRICE )
63+ results .append (row )
64+ self .connection .commit ()
65+ self .assertEqual (results , self .bookData )
66+
67+ def testDeqModeRemoveNoData (self ):
68+ "test dequeuing with DEQ_REMOVE_NODATA option"
69+ booksType = self .connection .gettype ("UDT_BOOK" )
70+ book = booksType .newobject ()
71+ title , authors , price = self .bookData [1 ]
72+ book .TITLE = title
73+ book .AUTHORS = authors
74+ book .PRICE = price
75+ options = self .connection .enqoptions ()
76+ props = self .connection .msgproperties ()
77+ self .connection .enq ("BOOKS" , options , props , book )
78+ options = self .connection .deqoptions ()
79+ options .navigation = cx_Oracle .DEQ_FIRST_MSG
80+ options .wait = cx_Oracle .DEQ_NO_WAIT
81+ options .mode = cx_Oracle .DEQ_REMOVE_NODATA
82+ book = booksType .newobject ()
83+ messageId = self .connection .deq ("BOOKS" , options , props , book )
84+ self .connection .commit ()
85+ self .assertTrue (messageId is not None )
86+ self .assertEqual (book .TITLE , "" )
87+
1588 def testDeqOptions (self ):
1689 "test getting/setting dequeue options attributes"
1790 options = self .connection .deqoptions ()
@@ -26,11 +99,42 @@ def testDeqOptions(self):
2699 self .__verifyAttribute (options , "visibility" , cx_Oracle .ENQ_IMMEDIATE )
27100 self .__verifyAttribute (options , "wait" , 1287 )
28101
102+ def testDeqWithWait (self ):
103+ "test waiting for dequeue"
104+ results = []
105+ thread = threading .Thread (target = self .__deqInThread ,
106+ args = (results ,))
107+ thread .start ()
108+ booksType = self .connection .gettype ("UDT_BOOK" )
109+ book = booksType .newobject ()
110+ title , authors , price = self .bookData [0 ]
111+ book .TITLE = title
112+ book .AUTHORS = authors
113+ book .PRICE = price
114+ options = self .connection .enqoptions ()
115+ props = self .connection .msgproperties ()
116+ self .connection .enq ("BOOKS" , options , props , book )
117+ self .connection .commit ()
118+ thread .join ()
119+ self .assertEqual (results , [(title , authors , price )])
120+
29121 def testEnqOptions (self ):
30122 "test getting/setting enqueue options attributes"
31123 options = self .connection .enqoptions ()
32124 self .__verifyAttribute (options , "visibility" , cx_Oracle .ENQ_IMMEDIATE )
33125
126+ def testErrorsForInvalidValues (self ):
127+ "test errors for invalid values for options"
128+ booksType = self .connection .gettype ("UDT_BOOK" )
129+ book = booksType .newobject ()
130+ options = self .connection .enqoptions ()
131+ props = self .connection .msgproperties ()
132+ self .assertRaises (TypeError , self .connection .deq , "BOOKS" , options ,
133+ props , book )
134+ options = self .connection .deqoptions ()
135+ self .assertRaises (TypeError , self .connection .enq , "BOOKS" , options ,
136+ props , book )
137+
34138 def testMsgProps (self ):
35139 "test getting/setting message properties attributes"
36140 props = self .connection .msgproperties ()
0 commit comments