Skip to content

Commit d5144aa

Browse files
Added test cases for cursor.lastrowid and SODA collection truncation (and added
code to check for situation where SODA support is lacking and stop running the test suite for SODA in that case).
1 parent 20686a1 commit d5144aa

File tree

4 files changed

+102
-20
lines changed

4 files changed

+102
-20
lines changed

test/Cursor.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,5 +698,57 @@ def testFetchXMLType(self):
698698
result, = self.cursor.fetchone()
699699
self.assertEqual(result, expectedResult)
700700

701+
def testLastRowid(self):
702+
"test last rowid"
703+
704+
# no statement executed: no rowid
705+
self.assertEqual(None, self.cursor.lastrowid)
706+
707+
# DDL statement executed: no rowid
708+
self.cursor.execute("truncate table TestTempTable")
709+
self.assertEqual(None, self.cursor.lastrowid)
710+
711+
# statement prepared: no rowid
712+
self.cursor.prepare("insert into TestTempTable (IntCol) values (:1)")
713+
self.assertEqual(None, self.cursor.lastrowid)
714+
715+
# multiple rows inserted: rowid of last row inserted
716+
rows = [(n,) for n in range(225)]
717+
self.cursor.executemany(None, rows)
718+
rowid = self.cursor.lastrowid
719+
self.cursor.execute("""
720+
select rowid
721+
from TestTempTable
722+
where IntCol = :1""", rows[-1])
723+
self.assertEqual(rowid, self.cursor.fetchone()[0])
724+
725+
# statement executed but no rows updated: no rowid
726+
self.cursor.execute("delete from TestTempTable where 1 = 0")
727+
self.assertEqual(None, self.cursor.lastrowid)
728+
729+
# stetement executed with one row updated: rowid of updated row
730+
self.cursor.execute("""
731+
update TestTempTable set
732+
StringCol = 'Modified'
733+
where IntCol = :1""", rows[-2])
734+
rowid = self.cursor.lastrowid
735+
self.cursor.execute("""
736+
select rowid
737+
from TestTempTable
738+
where IntCol = :1""", rows[-2])
739+
self.assertEqual(rowid, self.cursor.fetchone()[0])
740+
741+
# statement executed with many rows updated: rowid of last updated row
742+
self.cursor.execute("""
743+
update TestTempTable set
744+
StringCol = 'Row ' || to_char(IntCol)
745+
where IntCol = :1""", rows[-3])
746+
rowid = self.cursor.lastrowid
747+
self.cursor.execute("""
748+
select StringCol
749+
from TestTempTable
750+
where rowid = :1""", [rowid])
751+
self.assertEqual("Row %s" % rows[-3], self.cursor.fetchone()[0])
752+
701753
if __name__ == "__main__":
702754
TestEnv.RunTestCases()

test/SodaCollection.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ def __testSkip(self, coll, numToSkip, expectedContent):
1919
def testInvalidJson(self):
2020
"test inserting invalid JSON value into SODA collection"
2121
invalidJson = "{testKey:testValue}"
22-
sodaDatabase = self.connection.getSodaDatabase()
22+
sodaDatabase = self.getSodaDatabase()
2323
coll = sodaDatabase.createCollection("cxoInvalidJSON")
2424
doc = sodaDatabase.createDocument(invalidJson)
2525
self.assertRaises(cx_Oracle.IntegrityError, coll.insertOne, doc)
2626
coll.drop()
2727

2828
def testInsertDocuments(self):
2929
"test inserting documents into a SODA collection"
30-
sodaDatabase = self.connection.getSodaDatabase()
30+
sodaDatabase = self.getSodaDatabase()
3131
coll = sodaDatabase.createCollection("cxoInsertDocs")
3232
coll.find().remove()
3333
valuesToInsert = [
@@ -49,7 +49,7 @@ def testInsertDocuments(self):
4949

5050
def testSkipDocuments(self):
5151
"test skipping documents in a SODA collection"
52-
sodaDatabase = self.connection.getSodaDatabase()
52+
sodaDatabase = self.getSodaDatabase()
5353
coll = sodaDatabase.createCollection("cxoSkipDocs")
5454
coll.find().remove()
5555
valuesToInsert = [
@@ -69,7 +69,7 @@ def testSkipDocuments(self):
6969

7070
def testReplaceDocument(self):
7171
"test replace documents in SODA collection"
72-
sodaDatabase = self.connection.getSodaDatabase()
72+
sodaDatabase = self.getSodaDatabase()
7373
coll = sodaDatabase.createCollection("cxoReplaceDoc")
7474
coll.find().remove()
7575
content = {'name': 'John', 'address': {'city': 'Sydney'}}
@@ -83,7 +83,7 @@ def testReplaceDocument(self):
8383

8484
def testSearchDocumentsWithContent(self):
8585
"test search documents with content using $like and $regex"
86-
sodaDatabase = self.connection.getSodaDatabase()
86+
sodaDatabase = self.getSodaDatabase()
8787
coll = sodaDatabase.createCollection("cxoSearchDocContent")
8888
coll.find().remove()
8989
data = [
@@ -120,7 +120,7 @@ def testSearchDocumentsWithContent(self):
120120

121121
def testDocumentRemove(self):
122122
"test removing documents"
123-
sodaDatabase = self.connection.getSodaDatabase()
123+
sodaDatabase = self.getSodaDatabase()
124124
coll = sodaDatabase.createCollection("cxoRemoveDocs")
125125
coll.find().remove()
126126
data = [
@@ -156,7 +156,7 @@ def testCreateAndDropIndex(self):
156156
}
157157
]
158158
}
159-
sodaDatabase = self.connection.getSodaDatabase()
159+
sodaDatabase = self.getSodaDatabase()
160160
coll = sodaDatabase.createCollection("cxoTestIndexes")
161161
coll.find().remove()
162162
self.connection.commit()
@@ -170,7 +170,7 @@ def testCreateAndDropIndex(self):
170170
def testGetDocuments(self):
171171
"test getting documents from Collection"
172172
self.connection.autocommit = True
173-
sodaDatabase = self.connection.getSodaDatabase()
173+
sodaDatabase = self.getSodaDatabase()
174174
coll = sodaDatabase.createCollection("cxoTestGetDocs")
175175
coll.find().remove()
176176
data = [
@@ -188,7 +188,7 @@ def testGetDocuments(self):
188188
def testCursor(self):
189189
"test fetching documents from a cursor"
190190
self.connection.autocommit = True
191-
sodaDatabase = self.connection.getSodaDatabase()
191+
sodaDatabase = self.getSodaDatabase()
192192
coll = sodaDatabase.createCollection("cxoFindViaCursor")
193193
coll.find().remove()
194194
data = [
@@ -203,7 +203,7 @@ def testCursor(self):
203203

204204
def testMultipleDocumentRemove(self):
205205
"test removing multiple documents using multiple keys"
206-
sodaDatabase = self.connection.getSodaDatabase()
206+
sodaDatabase = self.getSodaDatabase()
207207
coll = sodaDatabase.createCollection("cxoRemoveMultipleDocs")
208208
coll.find().remove()
209209
data = [
@@ -224,7 +224,7 @@ def testMultipleDocumentRemove(self):
224224

225225
def testDocumentVersion(self):
226226
"test using version to get documents and remove them"
227-
sodaDatabase = self.connection.getSodaDatabase()
227+
sodaDatabase = self.getSodaDatabase()
228228
coll = sodaDatabase.createCollection("cxoDocumentVersion")
229229
coll.find().remove()
230230
content = {'name': 'John', 'address': {'city': 'Bangalore'}}
@@ -248,7 +248,7 @@ def testDocumentVersion(self):
248248

249249
def testGetCursor(self):
250250
"test keys with GetCursor"
251-
sodaDatabase = self.connection.getSodaDatabase()
251+
sodaDatabase = self.getSodaDatabase()
252252
coll = sodaDatabase.createCollection("cxoKeysWithGetCursor")
253253
coll.find().remove()
254254
data = [
@@ -268,13 +268,32 @@ def testGetCursor(self):
268268

269269
def testCreatedOn(self):
270270
"test createdOn attribute of Document"
271-
sodaDatabase = self.connection.getSodaDatabase()
271+
sodaDatabase = self.getSodaDatabase()
272272
coll = sodaDatabase.createCollection("cxoCreatedOn")
273273
coll.find().remove()
274274
data = {'name': 'John', 'address': {'city': 'Bangalore'}}
275275
doc = coll.insertOneAndGet(data)
276276
self.assertEqual(doc.createdOn, doc.lastModified)
277277

278+
def testSodaTruncate(self):
279+
"test Soda truncate"
280+
sodaDatabase = self.getSodaDatabase(minclient=(20,1))
281+
coll = sodaDatabase.createCollection("cxoTruncateDocs")
282+
coll.find().remove()
283+
valuesToInsert = [
284+
{ "name" : "George", "age" : 47 },
285+
{ "name" : "Susan", "age" : 39 },
286+
{ "name" : "John", "age" : 50 },
287+
{ "name" : "Jill", "age" : 54 }
288+
]
289+
for value in valuesToInsert:
290+
coll.insertOne(value)
291+
self.connection.commit()
292+
self.assertEqual(coll.find().count(), len(valuesToInsert))
293+
coll.truncate()
294+
self.assertEqual(coll.find().count(), 0)
295+
coll.drop()
296+
278297
if __name__ == "__main__":
279298
TestEnv.RunTestCases()
280299

test/SodaDatabase.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __verifyDocument(self, doc, rawContent, strContent=None, content=None,
2727

2828
def testCreateDocumentWithJson(self):
2929
"test creating documents with JSON data"
30-
sodaDatabase = self.connection.getSodaDatabase()
30+
sodaDatabase = self.getSodaDatabase()
3131
val = {"testKey1" : "testValue1", "testKey2" : "testValue2" }
3232
strVal = json.dumps(val)
3333
bytesVal = strVal.encode("UTF-8")
@@ -42,7 +42,7 @@ def testCreateDocumentWithJson(self):
4242

4343
def testCreateDocumentWithRaw(self):
4444
"test creating documents with raw data"
45-
sodaDatabase = self.connection.getSodaDatabase()
45+
sodaDatabase = self.getSodaDatabase()
4646
val = b"<html/>"
4747
key = "MyRawKey"
4848
mediaType = "text/html"
@@ -55,7 +55,7 @@ def testCreateDocumentWithRaw(self):
5555

5656
def testGetCollectionNames(self):
5757
"test getting collection names from the database"
58-
sodaDatabase = self.connection.getSodaDatabase()
58+
sodaDatabase = self.getSodaDatabase()
5959
self.__dropExistingCollections(sodaDatabase)
6060
self.assertEqual(sodaDatabase.getCollectionNames(), [])
6161
names = ["zCol", "dCol", "sCol", "aCol", "gCol"]
@@ -74,7 +74,7 @@ def testGetCollectionNames(self):
7474

7575
def testOpenCollection(self):
7676
"test opening a collection"
77-
sodaDatabase = self.connection.getSodaDatabase()
77+
sodaDatabase = self.getSodaDatabase()
7878
self.__dropExistingCollections(sodaDatabase)
7979
coll = sodaDatabase.openCollection("CollectionThatDoesNotExist")
8080
self.assertEqual(coll, None)
@@ -87,15 +87,15 @@ def testRepr(self):
8787
"test SodaDatabase representation"
8888
con1 = self.connection
8989
con2 = TestEnv.GetConnection()
90-
sodaDatabase1 = con1.getSodaDatabase()
90+
sodaDatabase1 = self.getSodaDatabase()
9191
sodaDatabase2 = con1.getSodaDatabase()
9292
sodaDatabase3 = con2.getSodaDatabase()
9393
self.assertEqual(str(sodaDatabase1), str(sodaDatabase2))
9494
self.assertEqual(str(sodaDatabase2), str(sodaDatabase3))
9595

9696
def testNegative(self):
9797
"test negative cases for SODA database methods"
98-
sodaDatabase = self.connection.getSodaDatabase()
98+
sodaDatabase = self.getSodaDatabase()
9999
self.assertRaises(TypeError, sodaDatabase.createCollection)
100100
self.assertRaises(TypeError, sodaDatabase.createCollection, 1)
101101
self.assertRaises(cx_Oracle.DatabaseError,
@@ -104,4 +104,3 @@ def testNegative(self):
104104

105105
if __name__ == "__main__":
106106
TestEnv.RunTestCases()
107-

test/TestEnv.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ def GetClientVersion():
165165

166166
class BaseTestCase(unittest.TestCase):
167167

168+
def getSodaDatabase(self, minclient=(18, 3), minserver=(18, 0),
169+
message="not supported with this client/server combination"):
170+
client = cx_Oracle.clientversion()[:2]
171+
if client < minclient:
172+
self.skipTest(message)
173+
server = tuple(int(s) for s in self.connection.version.split("."))[:2]
174+
if server < minserver:
175+
self.skipTest(message)
176+
if server > (20, 1) and client < (20, 1):
177+
self.skipTest(message)
178+
return self.connection.getSodaDatabase()
179+
168180
def isOnOracleCloud(self, connection=None):
169181
if connection is None:
170182
connection = self.connection

0 commit comments

Comments
 (0)