forked from deadbok/py-puml-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_SQL2PUML.py
161 lines (131 loc) · 3.88 KB
/
test_SQL2PUML.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from unittest import TestCase
from collections import OrderedDict
from sql2puml import SQL2PUML
from sql2puml import NoTableException
class TestSQL2PUML(TestCase, SQL2PUML):
def test_add_table(self):
self.clear()
self.add_table('Test')
self.assertIn('Test', self.puml_tables)
def test_add_column(self):
self.clear()
with self.assertRaises(NoTableException):
self.add_column('col', 'INTVAR')
self.add_table('Test')
self.add_column('col', 'INTVAR')
self.assertIn('col', self.puml_tables['Test']['default'])
self.assertEqual('INTVAR', self.puml_tables['Test']['default']['col'])
def test_add_column_primary(self):
self.clear()
with self.assertRaises(NoTableException):
self.add_column_primary('col', 'INTVAR')
self.add_table('Test')
self.add_column_primary('col', 'INTVAR')
self.assertIn('col', self.puml_tables['Test']['primary'])
self.assertEqual('INTVAR', self.puml_tables['Test']['primary']['col'])
def test_add_column_foreign(self):
self.clear()
with self.assertRaises(NoTableException):
self.add_column_foreign('col', 'INTVAR', 'other.col')
self.add_table('Test')
self.add_column_foreign('col', 'INTVAR', 'other.col')
self.assertIn('col', self.puml_tables['Test']['foreign'])
self.assertEqual(('INTVAR', 'other.col'), self.puml_tables['Test']['foreign']['col'])
def test_clear(self):
self.clear()
self.assertEqual(self.puml_tables, OrderedDict())
self.assertIsNone(self.current_table)
def test_transform(self):
self.maxDiff = None
sql = """
CREATE TABLE productTable(
product TEXT,
idProd INTEGER PRIMARY KEY
);
CREATE TABLE countryTable(
idCountry INTEGER PRIMARY KEY,
country TEXT
);
CREATE TABLE cityTable(
country TEXT,
city TEXT,
idCity INTEGER PRIMARY KEY,
FOREIGN KEY(country) REFERENCES countryTable(idCountry)
);
CREATE TABLE customerTable(
address TEXT,
email TEXT,
idCust INTEGER PRIMARY KEY,
name TEXT,
city TEXT,
FOREIGN KEY(city) REFERENCES cityTable(idCity)
);
CREATE TABLE orderTable(
idOrder INTEGER PRIMARY KEY,
date DATE,
custId INTEGER,
FOREIGN KEY(custId) REFERENCES customerTable(idCust)
);
CREATE TABLE orderProductTable(
orderId INTEGER PRIMARY KEY,
productId INTEGER,
FOREIGN KEY(orderId) REFERENCES orderTable(idOrder),
FOREIGN KEY(productId) REFERENCES productTable(idProd)
);
"""
result = """
@startuml
skinparam monochrome true
skinparam linetype ortho
scale 2
!define table(x) class x << (T,#FFAAAA) >>
!define view(x) class x << (V,#FFAAAA) >>
!define ent(x) class x << (E,#FFAAAA) >>
!define primary_key(x) <b>PK: x</b>
!define foreign_key(x,reference) <b>FK: </b>x
hide methods
hide stereotypes
table(productTable) {
\tprimary_key(idProd) INTEGER
\t---
\tproduct TEXT
}
table(countryTable) {
\tprimary_key(idCountry) INTEGER
\t---
\tcountry TEXT
}
table(cityTable) {
\tprimary_key(idCity) INTEGER
\tforeign_key(country,countryTable.idCountry) TEXT
\t---
\tcity TEXT
}
table(customerTable) {
\tprimary_key(idCust) INTEGER
\tforeign_key(city,cityTable.idCity) TEXT
\t---
\taddress TEXT
\temail TEXT
\tname TEXT
}
table(orderTable) {
\tprimary_key(idOrder) INTEGER
\tforeign_key(custId,customerTable.idCust) INTEGER
\t---
\tdate DATE
}
table(orderProductTable) {
\tprimary_key(orderId) INTEGER
\tforeign_key(orderId,orderTable.idOrder) INTEGER
\tforeign_key(productId,productTable.idProd) INTEGER
}
cityTable "0..n" -- "1..1" countryTable
customerTable "0..n" -- "1..1" cityTable
orderTable "0..n" -- "1..1" customerTable
orderProductTable "0..n" -- "1..1" orderTable
orderProductTable "0..n" -- "1..1" productTable
@enduml
""".strip()
self.assertEqual(result, self.transform(sql).strip(),
'PUML document code is incorrect')