python-cx_Oracle/test/test_1400_datetime_var.py
Anthony Tuininga d242bc716d Rework build to use setuptools exclusively and enable use of pyproject.toml;
rework test suite to use tox and simplify test suite (also added a test number
to each test case for easier reference).
2020-11-02 15:21:52 -07:00

253 lines
10 KiB
Python

#------------------------------------------------------------------------------
# Copyright (c) 2016, 2020, 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.
#------------------------------------------------------------------------------
"""
1400 - Module for testing date/time variables
"""
import TestEnv
import cx_Oracle
import datetime
import time
class TestCase(TestEnv.BaseTestCase):
def setUp(self):
TestEnv.BaseTestCase.setUp(self)
self.rawData = []
self.dataByKey = {}
for i in range(1, 11):
timeTuple = (2002, 12, 9, 0, 0, 0, 0, 0, -1)
timeInTicks = time.mktime(timeTuple) + i * 86400 + i * 8640
dateCol = cx_Oracle.TimestampFromTicks(int(timeInTicks))
if i % 2:
timeInTicks = time.mktime(timeTuple) + i * 86400 * 2 + \
i * 12960
nullableCol = cx_Oracle.TimestampFromTicks(int(timeInTicks))
else:
nullableCol = None
tuple = (i, dateCol, nullableCol)
self.rawData.append(tuple)
self.dataByKey[i] = tuple
def test_1400_BindDate(self):
"1400 - test binding in a date"
self.cursor.execute("""
select * from TestDates
where DateCol = :value""",
value = cx_Oracle.Timestamp(2002, 12, 13, 9, 36, 0))
self.assertEqual(self.cursor.fetchall(), [self.dataByKey[4]])
def test_1401_BindDateTime(self):
"1401 - test binding in a datetime.datetime value"
self.cursor.execute("""
select * from TestDates
where DateCol = :value""",
value = datetime.datetime(2002, 12, 13, 9, 36, 0))
self.assertEqual(self.cursor.fetchall(), [self.dataByKey[4]])
def test_1402_BindDateInDateTimeVar(self):
"1402 - test binding date in a datetime variable"
var = self.cursor.var(cx_Oracle.DATETIME)
dateVal = datetime.date.today()
var.setvalue(0, dateVal)
self.assertEqual(var.getvalue().date(), dateVal)
def test_1403_BindDateAfterString(self):
"1403 - test binding in a date after setting input sizes to a string"
self.cursor.setinputsizes(value = 15)
self.cursor.execute("""
select * from TestDates
where DateCol = :value""",
value = cx_Oracle.Timestamp(2002, 12, 14, 12, 0, 0))
self.assertEqual(self.cursor.fetchall(), [self.dataByKey[5]])
def test_1404_BindNull(self):
"1404 - test binding in a null"
self.cursor.setinputsizes(value = cx_Oracle.DATETIME)
self.cursor.execute("""
select * from TestDates
where DateCol = :value""",
value = None)
self.assertEqual(self.cursor.fetchall(), [])
def test_1405_BindDateArrayDirect(self):
"1405 - test binding in a date array"
returnValue = self.cursor.var(cx_Oracle.NUMBER)
array = [r[1] for r in self.rawData]
statement = """
begin
:returnValue := pkg_TestDateArrays.TestInArrays(
:startValue, :baseDate, :array);
end;"""
self.cursor.execute(statement,
returnValue = returnValue,
startValue = 5,
baseDate = cx_Oracle.Date(2002, 12, 12),
array = array)
self.assertEqual(returnValue.getvalue(), 35.5)
array = array + array[:5]
self.cursor.execute(statement,
startValue = 7,
baseDate = cx_Oracle.Date(2002, 12, 13),
array = array)
self.assertEqual(returnValue.getvalue(), 24.0)
def test_1406_BindDateArrayBySizes(self):
"1406 - test binding in a date array (with setinputsizes)"
returnValue = self.cursor.var(cx_Oracle.NUMBER)
self.cursor.setinputsizes(array = [cx_Oracle.DATETIME, 10])
array = [r[1] for r in self.rawData]
self.cursor.execute("""
begin
:returnValue := pkg_TestDateArrays.TestInArrays(
:startValue, :baseDate, :array);
end;""",
returnValue = returnValue,
startValue = 6,
baseDate = cx_Oracle.Date(2002, 12, 13),
array = array)
self.assertEqual(returnValue.getvalue(), 26.5)
def test_1407_BindDateArrayByVar(self):
"1407 - test binding in a date array (with arrayvar)"
returnValue = self.cursor.var(cx_Oracle.NUMBER)
array = self.cursor.arrayvar(cx_Oracle.DATETIME, 10, 20)
array.setvalue(0, [r[1] for r in self.rawData])
self.cursor.execute("""
begin
:returnValue := pkg_TestDateArrays.TestInArrays(
:startValue, :baseDate, :array);
end;""",
returnValue = returnValue,
startValue = 7,
baseDate = cx_Oracle.Date(2002, 12, 14),
array = array)
self.assertEqual(returnValue.getvalue(), 17.5)
def test_1408_BindInOutDateArrayByVar(self):
"1408 - test binding in/out a date array (with arrayvar)"
array = self.cursor.arrayvar(cx_Oracle.DATETIME, 10, 100)
originalData = [r[1] for r in self.rawData]
array.setvalue(0, originalData)
self.cursor.execute("""
begin
pkg_TestDateArrays.TestInOutArrays(:numElems, :array);
end;""",
numElems = 5,
array = array)
self.assertEqual(array.getvalue(),
[ cx_Oracle.Timestamp(2002, 12, 17, 2, 24, 0),
cx_Oracle.Timestamp(2002, 12, 18, 4, 48, 0),
cx_Oracle.Timestamp(2002, 12, 19, 7, 12, 0),
cx_Oracle.Timestamp(2002, 12, 20, 9, 36, 0),
cx_Oracle.Timestamp(2002, 12, 21, 12, 0, 0) ] + \
originalData[5:])
def test_1409_BindOutDateArrayByVar(self):
"1409 - test binding out a date array (with arrayvar)"
array = self.cursor.arrayvar(cx_Oracle.DATETIME, 6, 100)
self.cursor.execute("""
begin
pkg_TestDateArrays.TestOutArrays(:numElems, :array);
end;""",
numElems = 6,
array = array)
self.assertEqual(array.getvalue(),
[ cx_Oracle.Timestamp(2002, 12, 13, 4, 48, 0),
cx_Oracle.Timestamp(2002, 12, 14, 9, 36, 0),
cx_Oracle.Timestamp(2002, 12, 15, 14, 24, 0),
cx_Oracle.Timestamp(2002, 12, 16, 19, 12, 0),
cx_Oracle.Timestamp(2002, 12, 18, 0, 0, 0),
cx_Oracle.Timestamp(2002, 12, 19, 4, 48, 0) ])
def test_1410_BindOutSetInputSizes(self):
"1410 - test binding out with set input sizes defined"
vars = self.cursor.setinputsizes(value = cx_Oracle.DATETIME)
self.cursor.execute("""
begin
:value := to_date(20021209, 'YYYYMMDD');
end;""")
self.assertEqual(vars["value"].getvalue(),
cx_Oracle.Timestamp(2002, 12, 9))
def test_1411_BindInOutSetInputSizes(self):
"1411 - test binding in/out with set input sizes defined"
vars = self.cursor.setinputsizes(value = cx_Oracle.DATETIME)
self.cursor.execute("""
begin
:value := :value + 5.25;
end;""",
value = cx_Oracle.Timestamp(2002, 12, 12, 10, 0, 0))
self.assertEqual(vars["value"].getvalue(),
cx_Oracle.Timestamp(2002, 12, 17, 16, 0, 0))
def test_1412_BindOutVar(self):
"1412 - test binding out with cursor.var() method"
var = self.cursor.var(cx_Oracle.DATETIME)
self.cursor.execute("""
begin
:value := to_date('20021231 12:31:00',
'YYYYMMDD HH24:MI:SS');
end;""",
value = var)
self.assertEqual(var.getvalue(),
cx_Oracle.Timestamp(2002, 12, 31, 12, 31, 0))
def test_1413_BindInOutVarDirectSet(self):
"1413 - test binding in/out with cursor.var() method"
var = self.cursor.var(cx_Oracle.DATETIME)
var.setvalue(0, cx_Oracle.Timestamp(2002, 12, 9, 6, 0, 0))
self.cursor.execute("""
begin
:value := :value + 5.25;
end;""",
value = var)
self.assertEqual(var.getvalue(),
cx_Oracle.Timestamp(2002, 12, 14, 12, 0, 0))
def test_1414_CursorDescription(self):
"1414 - test cursor description is accurate"
self.cursor.execute("select * from TestDates")
self.assertEqual(self.cursor.description,
[ ('INTCOL', cx_Oracle.DB_TYPE_NUMBER, 10, None, 9, 0, 0),
('DATECOL', cx_Oracle.DB_TYPE_DATE, 23, None, None, None, 0),
('NULLABLECOL', cx_Oracle.DB_TYPE_DATE, 23, None, None, None,
1) ])
def test_1415_FetchAll(self):
"1415 - test that fetching all of the data returns the correct results"
self.cursor.execute("select * From TestDates order by IntCol")
self.assertEqual(self.cursor.fetchall(), self.rawData)
self.assertEqual(self.cursor.fetchall(), [])
def test_1416_FetchMany(self):
"1416 - test that fetching data in chunks returns the correct results"
self.cursor.execute("select * From TestDates 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 test_1417_FetchOne(self):
"1417 - test that fetching a single row returns the correct results"
self.cursor.execute("""
select *
from TestDates
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()