forked from felipecruz/exemplos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_08_setup.py
65 lines (52 loc) · 1.77 KB
/
08_08_setup.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
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 setUp(self):
self.table = DataTable('A')
def test_add_column(self):
self.assertEqual(0, len(self.table._columns))
self.table.add_column('BId', 'bigint')
self.assertEqual(1, len(self.table._columns))
self.table.add_column('value', 'numeric')
self.assertEqual(2, len(self.table._columns))
self.table.add_column('desc', 'varchar')
self.assertEqual(3, len(self.table._columns))
def test_add_column_invalid_type(self):
self.assertRaises(Exception,
self.table.add_column, ('col', 'invalid'))