-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgrt.py
121 lines (96 loc) · 3.71 KB
/
grt.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
from mock import MagicMock
root = MagicMock()
modules = MagicMock()
def get_grt_foreignKey(fk_name, columns=None, referencedColumns=None, deleteRule='NO ACTION', updateRule='SET NULL'):
"""Mock a foreign key
Returns a Mock object representing the basic needs of a foreignKey
Arguments:
fk_name {str} -- The name of the foreign key
Keyword Arguments:
columns {list} -- Local columns this key binds to (default: {None})
referencedColumns {list} -- Remote columns this key binds to (default: {None})
deleteRule {str} -- Delete rule (default: {'NO ACTION'})
updateRule {str} -- Update rule (default: {'SET NULL'})
Returns:
MagicMock -- GRT Compatible Foreign Key
"""
fk = MagicMock(
columns=columns or [],
referencedColumns=referencedColumns or [],
deleteRule=deleteRule,
updateRule=updateRule
)
fk.name = fk_name
return fk
def get_grt_index(name, index_type='PRIMARY', columns=None):
"""Mock an index
Returns a Mock object representing the basic needs of an index
Arguments:
name {str} -- The name of the index
Keyword Arguments:
index_type {str} -- The type of index (default: {'PRIMARY'})
columns {list} -- The local colums this index binds to (default: {None})
Returns:
MagicMock -- GRT Compatible Index
"""
indx = MagicMock(
columns=[MagicMock(referencedColumn=c) for c in (columns or [])],
indexType=index_type
)
indx.name = name
return indx
def get_grt_table(table_name, columns=None, indices=None, foreignKeys=None, tableEngine=None, charset='utf8', comment=None):
"""Mock a table
Returns a Mock object representing the basic needs of a table
Arguments:
table_name {str} -- The name of the database table
Keyword Arguments:
columns {list} -- All the columns of the table (default: {None})
indices {list} -- All the indices of the table (default: {None})
foreignKeys {list} -- All the foreign keys of the table (default: {None})
tableEngine {str} -- Table engine if required (default: {None})
charset {str} -- Charset (default: {'utf8'})
comment {str} -- Comment (default: {None})
Returns:
MagicMock -- GRT Compatible Table
"""
table = MagicMock(
tableEngine=tableEngine,
defaultCharacterSetName=charset,
columns=columns or [],
indices=indices or [],
foreignKeys=foreignKeys or []
)
table.name = table_name
if comment is not None:
table.comment = comment
for c in table.columns:
c.owner = table
return table
def get_grt_column(column_name, table_name, sql_type, defaultValue=None, comment=None, isNotNull=0, autoIncrement=0):
"""Mock a column
Returns a Mock object representing the basic needs of a column
Arguments:
column_name {str} -- The name of the column
table_name {str} -- The name of the table (a table will be accessible at o.owner)
sql_type {str} -- The SQL type of the column
Keyword Arguments:
defaultValue {str} -- Default value (default: {None})
comment {str} -- Comment (default: {None})
isNotNull {number} -- Is not null (default: {0})
autoIncrement {number} -- Auto Increment (default: {0})
Returns:
MagicMock -- GRT Compatible Column
"""
column = MagicMock(
owner=get_grt_table(table_name),
defaultValue=defaultValue,
formattedType=sql_type,
formattedRawType=sql_type,
isNotNull=isNotNull,
autoIncrement=autoIncrement
)
column.name = column_name
if comment is not None:
column.comment = comment
return column