-
Notifications
You must be signed in to change notification settings - Fork 42
/
08_07_exceptions.py
76 lines (61 loc) · 2.03 KB
/
08_07_exceptions.py
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
import decimal
import unittest
class Column:
def __init__(self, name, kind, description=""):
self._name = name
self._kind = kind
self._description = description
@staticmethod
def validate(kind, data):
if kind == 'bigint':
if isinstance(data, int):
return True
return False
elif kind == 'varchar':
if isinstance(data, str):
return True
return False
elif kind == 'numeric':
try:
val = decimal.Decimal(data)
except:
return False
return True
class DataTable:
def __init__(self, name):
self._name = name
self._columns = []
self._references = []
self._referenced = []
self._data = []
def add_column(self, name, kind, description=""):
self._validate_kind(kind)
column = Column(name, kind, description=description)
self._columns.append(column)
return column
def _validate_kind(self, kind):
if not kind in ('bigint', 'numeric', 'varchar'):
raise Exception("Tipo inválido")
class DataTableTest(unittest.TestCase):
def test_add_column(self):
table = DataTable("Test")
self.assertEqual(0, len(table._columns))
table.add_column('BId', 'bigint')
self.assertEqual(1, len(table._columns))
table.add_column('value', 'numeric')
self.assertEqual(2, len(table._columns))
table.add_column('desc', 'varchar')
self.assertEqual(3, len(table._columns))
def test_add_column_invalid_type(self):
a_table = DataTable('A')
self.assertRaises(Exception,
a_table.add_column, ('col', 'invalid'))
def test_add_column_invalid_type_fail(self):
a_table = DataTable('A')
error = False
try:
a_table.add_column('col', 'invalid')
except:
error = True
if not error:
self.fail("Chamada não gerou erro, mas deveria")