-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_sql_parameterization.py
More file actions
56 lines (46 loc) · 1.73 KB
/
test_sql_parameterization.py
File metadata and controls
56 lines (46 loc) · 1.73 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
from codemodder.codemods.test import BaseIntegrationTest
from core_codemods.sql_parameterization import (
SQLQueryParameterization,
SQLQueryParameterizationTransformer,
)
class TestSQLQueryParameterization(BaseIntegrationTest):
codemod = SQLQueryParameterization
original_code = """
import sqlite3
connection = sqlite3.connect(":memory:")
connection.cursor().execute("CREATE TABLE Users (name, phone)")
connection.cursor().execute("INSERT INTO Users VALUES ('Jenny','867-5309')")
def foo(cursor: sqlite3.Cursor, name: str, phone: str):
a = "SELECT * FROM Users"
b = " WHERE name ='" + name
c = "' AND phone = '" + phone + "'"
r = cursor.execute(a + b + c)
print(r.fetchone())
foo(connection.cursor(), "Jenny", "867-5309")
"""
replacement_lines = [
(10, """ b = " WHERE name =?"\n"""),
(11, """ c = " AND phone = ?"\n"""),
(12, """ r = cursor.execute(a + b + c, (name, phone, ))\n"""),
]
# fmt: off
expected_diff = (
"""--- \n"""
"""+++ \n"""
"""@@ -7,9 +7,9 @@\n"""
""" \n"""
""" def foo(cursor: sqlite3.Cursor, name: str, phone: str):\n"""
""" a = "SELECT * FROM Users"\n"""
"""- b = " WHERE name ='" + name\n"""
"""- c = "' AND phone = '" + phone + "'"\n"""
"""- r = cursor.execute(a + b + c)\n"""
"""+ b = " WHERE name =?"\n"""
"""+ c = " AND phone = ?"\n"""
"""+ r = cursor.execute(a + b + c, (name, phone, ))\n"""
""" print(r.fetchone())\n"""
""" \n"""
""" \n""")
# fmt: on
expected_line_change = "12"
change_description = SQLQueryParameterizationTransformer.change_description
num_changed_files = 1