Skip to content

Commit

Permalink
Tweaks to boolean variable improvements patch supplied by Alex Henrie
Browse files Browse the repository at this point in the history
(#435).
  • Loading branch information
anthony-tuininga committed May 16, 2020
1 parent 17fd92f commit 856d0aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
10 changes: 6 additions & 4 deletions doc/src/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ Version 8.0 (TBD)
`421 <https://github.com/oracle/python-cx_Oracle/pull/421>`__,
`422 <https://github.com/oracle/python-cx_Oracle/pull/422>`__ and
`423 <https://github.com/oracle/python-cx_Oracle/pull/423>`__).
#) Python objects bound to boolean variables are now converted to True or
False based on whether they would be considered True or False in a Python
if statement. Previously, only True was treated as True and all other
Python values (including 1, 1.0, and "foo") were treated as False
(pull request
`435 <https://github.com/oracle/python-cx_Oracle/pull/435>`__).
#) Documentation and test suite improvements.
#) Python objects bound to boolean variables are now converted to true or false
based on whether they would be considered true or false in a Python if
statement. (Previously, only True was treated as true and all other Python
values including 1, 1.0, and "foo" were treated as false.)


Version 7.3 (December 2019)
Expand Down
2 changes: 2 additions & 0 deletions src/cxoTransform.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ int cxoTransform_fromPython(cxoTransformNum transformNum,
switch (transformNum) {
case CXO_TRANSFORM_BOOLEAN:
dbValue->asBoolean = PyObject_IsTrue(pyValue);
if (PyErr_Occurred())
return -1;
return 0;
case CXO_TRANSFORM_BINARY:
case CXO_TRANSFORM_FIXED_CHAR:
Expand Down
45 changes: 14 additions & 31 deletions test/BooleanVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

class TestCase(TestEnv.BaseTestCase):

def __testBindValueAsBoolean(self, value):
expectedResult = str(bool(value)).upper()
var = self.cursor.var(bool)
var.setvalue(0, value)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, expectedResult)

def testBindFalse(self):
"test binding in a False value"
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
Expand All @@ -23,29 +31,13 @@ def testBindFalse(self):

def testBindFloatAsBoolean(self):
"test binding in a float as a boolean"
var = self.cursor.var(bool)
var.setvalue(0, 0.0)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "FALSE")
var = self.cursor.var(bool)
var.setvalue(0, 1.0)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "TRUE")
self.__testBindValueAsBoolean(0.0)
self.__testBindValueAsBoolean(1.0)

def testBindIntegerAsBoolean(self):
"test binding in an integer as a boolean"
var = self.cursor.var(bool)
var.setvalue(0, 0)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "FALSE")
var = self.cursor.var(bool)
var.setvalue(0, 1)
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "TRUE")
self.__testBindValueAsBoolean(0)
self.__testBindValueAsBoolean(1)

def testBindNull(self):
"test binding in a null value"
Expand All @@ -68,16 +60,8 @@ def testBindOutTrue(self):

def testBindStringAsBoolean(self):
"test binding in a string as a boolean"
var = self.cursor.var(bool)
var.setvalue(0, "")
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "FALSE")
var = self.cursor.var(bool)
var.setvalue(0, "0")
result = self.cursor.callfunc("pkg_TestBooleans.GetStringRep", str,
(var,))
self.assertEqual(result, "TRUE")
self.__testBindValueAsBoolean("")
self.__testBindValueAsBoolean("0")

def testBindTrue(self):
"test binding in a True value"
Expand All @@ -87,4 +71,3 @@ def testBindTrue(self):

if __name__ == "__main__":
TestEnv.RunTestCases()

0 comments on commit 856d0aa

Please sign in to comment.