Skip to content

Commit

Permalink
Merge pull request #733 from lcorbasson/boolean-to-number
Browse files Browse the repository at this point in the history
Allow the conversion of booleans to numbers
  • Loading branch information
jpmckinney committed Feb 6, 2020
2 parents 7d7c053 + c0ada4f commit 916fbd4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions agate/data_types/number.py
Expand Up @@ -70,6 +70,10 @@ def cast(self, d):
return Decimal(d)
elif t is float:
return Decimal(repr(d))
elif d == False:
return Decimal(0)
elif d == True:
return Decimal(1)
elif not isinstance(d, six.string_types):
raise CastError('Can not parse value "%s" as Decimal.' % d)

Expand Down
7 changes: 6 additions & 1 deletion tests/test_data_types.py
Expand Up @@ -124,7 +124,7 @@ def setUp(self):
def test_test(self):
self.assertEqual(self.type.test(None), True)
self.assertEqual(self.type.test('N/A'), True)
self.assertEqual(self.type.test(True), False)
self.assertEqual(self.type.test(True), True)
self.assertEqual(self.type.test('True'), False)
self.assertEqual(self.type.test(1), True)
self.assertEqual(self.type.test(Decimal('1')), True)
Expand Down Expand Up @@ -156,6 +156,11 @@ def test_cast_long(self):
self.assertEqual(self.type.test(long('141414')), True)
self.assertEqual(self.type.cast(long('141414')), Decimal('141414'))

def test_boolean_cast(self):
values = (True, False)
casted = tuple(self.type.cast(v) for v in values)
self.assertSequenceEqual(casted, (Decimal('1'), Decimal('0')))

def test_currency_cast(self):
values = ('$2.70', '-$0.70', u'€14', u'50¢', u'-75¢', u'-$1,287')
casted = tuple(self.type.cast(v) for v in values)
Expand Down

0 comments on commit 916fbd4

Please sign in to comment.