forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalVar.py
More file actions
150 lines (132 loc) · 6.25 KB
/
Copy pathIntervalVar.py
File metadata and controls
150 lines (132 loc) · 6.25 KB
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
#------------------------------------------------------------------------------
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
#
# Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
#
# Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
# Canada. All rights reserved.
#------------------------------------------------------------------------------
"""Module for testing interval variables."""
import TestEnv
import cx_Oracle
import datetime
class TestCase(TestEnv.BaseTestCase):
def setUp(self):
TestEnv.BaseTestCase.setUp(self)
self.rawData = []
self.dataByKey = {}
for i in range(1, 11):
delta = datetime.timedelta(days = i, hours = i, minutes = i * 2,
seconds = i * 3)
if i % 2 == 0:
nullableDelta = None
else:
nullableDelta = datetime.timedelta(days = i + 5, hours = i + 2,
minutes = i * 2 + 5, seconds = i * 3 + 5)
tuple = (i, delta, nullableDelta)
self.rawData.append(tuple)
self.dataByKey[i] = tuple
def testBindInterval(self):
"test binding in an interval"
self.cursor.setinputsizes(value = cx_Oracle.DB_TYPE_INTERVAL_DS)
self.cursor.execute("""
select * from TestIntervals
where IntervalCol = :value""",
value = datetime.timedelta(days = 5, hours = 5, minutes = 10,
seconds = 15))
self.assertEqual(self.cursor.fetchall(), [self.dataByKey[5]])
def testBindNull(self):
"test binding in a null"
self.cursor.setinputsizes(value = cx_Oracle.DB_TYPE_INTERVAL_DS)
self.cursor.execute("""
select * from TestIntervals
where IntervalCol = :value""",
value = None)
self.assertEqual(self.cursor.fetchall(), [])
def testBindOutSetInputSizes(self):
"test binding out with set input sizes defined"
vars = self.cursor.setinputsizes(value = cx_Oracle.DB_TYPE_INTERVAL_DS)
self.cursor.execute("""
begin
:value := to_dsinterval('8 09:24:18.123789');
end;""")
self.assertEqual(vars["value"].getvalue(),
datetime.timedelta(days = 8, hours = 9, minutes = 24,
seconds = 18, microseconds = 123789))
def testBindInOutSetInputSizes(self):
"test binding in/out with set input sizes defined"
vars = self.cursor.setinputsizes(value = cx_Oracle.DB_TYPE_INTERVAL_DS)
self.cursor.execute("""
begin
:value := :value + to_dsinterval('5 08:30:00');
end;""",
value = datetime.timedelta(days = 5, hours = 2, minutes = 15))
self.assertEqual(vars["value"].getvalue(),
datetime.timedelta(days = 10, hours = 10, minutes = 45))
def testBindInOutFractionalSecond(self):
"test binding in/out with set input sizes defined"
vars = self.cursor.setinputsizes(value = cx_Oracle.DB_TYPE_INTERVAL_DS)
self.cursor.execute("""
begin
:value := :value + to_dsinterval('5 08:30:00');
end;""",
value = datetime.timedelta(days = 5, seconds=12.123789))
self.assertEqual(vars["value"].getvalue(),
datetime.timedelta(days = 10, hours = 8, minutes = 30,
seconds=12, microseconds=123789))
def testBindOutVar(self):
"test binding out with cursor.var() method"
var = self.cursor.var(cx_Oracle.DB_TYPE_INTERVAL_DS)
self.cursor.execute("""
begin
:value := to_dsinterval('15 18:35:45.586');
end;""",
value = var)
self.assertEqual(var.getvalue(),
datetime.timedelta(days = 15, hours = 18, minutes = 35,
seconds = 45, milliseconds = 586))
def testBindInOutVarDirectSet(self):
"test binding in/out with cursor.var() method"
var = self.cursor.var(cx_Oracle.DB_TYPE_INTERVAL_DS)
var.setvalue(0, datetime.timedelta(days = 1, minutes = 50))
self.cursor.execute("""
begin
:value := :value + to_dsinterval('8 05:15:00');
end;""",
value = var)
self.assertEqual(var.getvalue(),
datetime.timedelta(days = 9, hours = 6, minutes = 5))
def testCursorDescription(self):
"test cursor description is accurate"
self.cursor.execute("select * from TestIntervals")
self.assertEqual(self.cursor.description,
[ ('INTCOL', cx_Oracle.DB_TYPE_NUMBER, 10, None, 9, 0, 0),
('INTERVALCOL', cx_Oracle.DB_TYPE_INTERVAL_DS, None, None, 2,
6, 0),
('NULLABLECOL', cx_Oracle.DB_TYPE_INTERVAL_DS, None, None, 2,
6, 1) ])
def testFetchAll(self):
"test that fetching all of the data returns the correct results"
self.cursor.execute("select * From TestIntervals order by IntCol")
self.assertEqual(self.cursor.fetchall(), self.rawData)
self.assertEqual(self.cursor.fetchall(), [])
def testFetchMany(self):
"test that fetching data in chunks returns the correct results"
self.cursor.execute("select * From TestIntervals order by IntCol")
self.assertEqual(self.cursor.fetchmany(3), self.rawData[0:3])
self.assertEqual(self.cursor.fetchmany(2), self.rawData[3:5])
self.assertEqual(self.cursor.fetchmany(4), self.rawData[5:9])
self.assertEqual(self.cursor.fetchmany(3), self.rawData[9:])
self.assertEqual(self.cursor.fetchmany(3), [])
def testFetchOne(self):
"test that fetching a single row returns the correct results"
self.cursor.execute("""
select *
from TestIntervals
where IntCol in (3, 4)
order by IntCol""")
self.assertEqual(self.cursor.fetchone(), self.dataByKey[3])
self.assertEqual(self.cursor.fetchone(), self.dataByKey[4])
self.assertEqual(self.cursor.fetchone(), None)
if __name__ == "__main__":
TestEnv.RunTestCases()