-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_sql_output_adapter.py
More file actions
159 lines (143 loc) · 5.15 KB
/
test_sql_output_adapter.py
File metadata and controls
159 lines (143 loc) · 5.15 KB
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
# -*- coding: utf-8 -*-
from collections import namedtuple
from cli_helpers.tabular_output import TabularOutputFormatter
from cli_helpers.tabular_output.sql_output_adapter import escape_for_sql_statement, adapter, register_new_formatter
TableReference = namedtuple(
"TableReference", ["schema", "name", "alias", "is_function"]
)
TableReference.ref = property(
lambda self: self.alias
or (
self.name
if self.name.islower() or self.name[0] == '"'
else '"' + self.name + '"'
)
)
def test_escape_for_sql_statement_bytes():
bts = b"837124ab3e8dc0f"
escaped_bytes = escape_for_sql_statement(bts)
assert escaped_bytes == "X'383337313234616233653864633066'"
def __mock_extract_tables(sql):
"""
mock function for extract tables
in mycli, pass `mycli.packages.parseutils.extract_tables`
in pgcli, pass `pgcli.packages.parseutils.extract_tables`
:param sql: sql query
:return:
"""
table_refs = (TableReference(schema=None, name='user', alias='"user"', is_function=False),)
return table_refs
def test_output_sql_insert():
global formatter
formatter = TabularOutputFormatter
register_new_formatter(formatter)
data = [
[
1,
"Jackson",
"jackson_test@gmail.com",
"132454789",
"",
"2022-09-09 19:44:32.712343+08",
"2022-09-09 19:44:32.712343+08",
]
]
header = ["id", "name", "email", "phone", "description", "created_at", "updated_at"]
table_format = "sql-insert"
kwargs = {
"column_types": [int, str, str, str, str, str, str],
"sep_title": "RECORD {n}",
"sep_character": "-",
"sep_length": (1, 25),
"missing_value": "<null>",
"integer_format": "",
"float_format": "",
"disable_numparse": True,
"preserve_whitespace": True,
"max_field_width": 500,
"extract_tables": __mock_extract_tables,
}
formatter.query = 'SELECT * FROM "user";'
# For postgresql
kwargs["delimiter"] = '"'
output = adapter(data, header, table_format=table_format, **kwargs)
output_list = [l for l in output]
expected = [
'INSERT INTO "user" ("id", "name", "email", "phone", "description", "created_at", "updated_at") VALUES',
" ('1', 'Jackson', 'jackson_test@gmail.com', '132454789', '', "
+ "'2022-09-09 19:44:32.712343+08', '2022-09-09 19:44:32.712343+08')",
";",
]
assert expected == output_list
# For mysql
kwargs["delimiter"] = "`"
output = adapter(data, header, table_format=table_format, **kwargs)
output_list = [l for l in output]
expected = [
'INSERT INTO `user` (`id`, `name`, `email`, `phone`, `description`, `created_at`, `updated_at`) VALUES',
" ('1', 'Jackson', 'jackson_test@gmail.com', '132454789', '', "
+ "'2022-09-09 19:44:32.712343+08', '2022-09-09 19:44:32.712343+08')",
";",
]
assert expected == output_list
def test_output_sql_update_pg():
global formatter
formatter = TabularOutputFormatter
register_new_formatter(formatter)
data = [
[
1,
"Jackson",
"jackson_test@gmail.com",
"132454789",
"",
"2022-09-09 19:44:32.712343+08",
"2022-09-09 19:44:32.712343+08",
]
]
header = ["id", "name", "email", "phone", "description", "created_at", "updated_at"]
table_format = "sql-update"
table_refs = (TableReference(schema=None, name='user', alias='"user"', is_function=False),)
kwargs = {
"column_types": [int, str, str, str, str, str, str],
"sep_title": "RECORD {n}",
"sep_character": "-",
"sep_length": (1, 25),
"missing_value": "<null>",
"integer_format": "",
"float_format": "",
"disable_numparse": True,
"preserve_whitespace": True,
"max_field_width": 500,
"extract_tables": __mock_extract_tables,
}
formatter.query = 'SELECT * FROM "user";'
# For postgresql
kwargs["delimiter"] = '"'
output = adapter(data, header, table_format=table_format, **kwargs)
output_list = [l for l in output]
expected = [
'UPDATE "user" SET',
' "name" = \'Jackson\'',
', "email" = \'jackson_test@gmail.com\'',
', "phone" = \'132454789\'',
', "description" = \'\'',
', "created_at" = \'2022-09-09 19:44:32.712343+08\'',
', "updated_at" = \'2022-09-09 19:44:32.712343+08\'',
'WHERE "id" = \'1\';']
assert expected == output_list
# For mysql
kwargs["delimiter"] = "`"
output = adapter(data, header, table_format=table_format, **kwargs)
output_list = [l for l in output]
print(output_list)
expected = [
'UPDATE `user` SET',
" `name` = 'Jackson'",
", `email` = 'jackson_test@gmail.com'",
", `phone` = '132454789'",
", `description` = ''",
", `created_at` = '2022-09-09 19:44:32.712343+08'",
", `updated_at` = '2022-09-09 19:44:32.712343+08'",
"WHERE `id` = '1';"]
assert expected == output_list