Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
my2to3
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bryton Lacquement
my2to3
Commits
0d19c086
Commit
0d19c086
authored
Jul 13, 2020
by
Bryton Lacquement
🚪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: also test the recorded data
parent
3817aa59
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
51 deletions
+88
-51
my2to3/tests/__init__.py
my2to3/tests/__init__.py
+18
-17
my2to3/tests/testFixDivisionTrace.py
my2to3/tests/testFixDivisionTrace.py
+59
-18
my2to3/tests/testFixNestedExceptTrace.py
my2to3/tests/testFixNestedExceptTrace.py
+6
-12
my2to3/trace.py
my2to3/trace.py
+5
-4
No files found.
my2to3/tests/__init__.py
View file @
0d19c086
from
lib2to3.tests.test_fixers
import
FixerTestCase
as
lib2to3FixerTestCase
import
sqlite3
from
my2to3.trace
import
database
,
get_data
,
tracing_functions
class
FixerTestCase
(
lib2to3FixerTestCase
):
...
...
@@ -6,22 +9,20 @@ class FixerTestCase(lib2to3FixerTestCase):
def
setUp
(
self
,
fix_list
=
None
,
fixer_pkg
=
"my2to3"
,
options
=
None
):
super
(
FixerTestCase
,
self
).
setUp
(
fix_list
,
fixer_pkg
,
options
)
if
self
.
fixer
.
endswith
(
'_trace'
):
fix_name
=
'fix_'
+
self
.
fixer
self
.
fixer_module
=
fixer_module
=
getattr
(
__import__
(
'my2to3.fixes'
,
fromlist
=
[
fix_name
]),
fix_name
)
self
.
traces
=
[]
# Wrap fixer_module.trace, to populate self.traces
self
.
old_insert_trace
=
fixer_module
.
insert_trace
def
decorate
(
func
):
def
call
(
*
args
):
self
.
traces
.
append
(
args
)
return
func
(
*
args
)
return
call
fixer_module
.
insert_trace
=
decorate
(
fixer_module
.
insert_trace
)
# Clear the database
# XXX: a better way probably exists.
# TODO:
# - refactor
# - optimize
conn
=
sqlite3
.
connect
(
database
)
c
=
conn
.
cursor
()
for
table
in
c
.
execute
(
"SELECT name FROM sqlite_master WHERE type='table'"
).
fetchall
():
c
.
execute
(
"DELETE FROM %s"
%
table
)
conn
.
commit
()
conn
.
close
()
def
tearDown
(
self
,
*
args
,
**
kw
):
s
uper
(
FixerTestCase
,
self
).
tearDown
(
*
args
,
**
kw
)
def
assertDataEqual
(
self
,
table
,
data
):
s
elf
.
assertEqual
(
get_data
(
table
),
data
)
if
self
.
fixer
.
endswith
(
'_trace'
):
self
.
fixer_module
.
insert_trace
=
self
.
old_insert_trace
def
exec_code
(
self
,
string
):
exec
(
compile
(
string
,
"<string>"
,
'exec'
),
{
f
.
__name__
:
f
for
f
in
tracing_functions
})
my2to3/tests/testFixDivisionTrace.py
View file @
0d19c086
...
...
@@ -7,49 +7,90 @@ class testFixDivisionTrace(FixerTestCase):
fixer
=
"division_trace"
def
test_simple_division
(
self
):
b
=
"""
x / y
"""
a
=
"""division_traced("<string>",1,0,
x , y
)"""
b
=
"""
10 / 20
"""
a
=
"""division_traced("<string>",1,0,
10 , 20
)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
)])
def
test_nested_divisions
(
self
):
b
=
"""10 / 20 / 30"""
a
=
"""division_traced("<string>",1,1,division_traced("<string>",1,0,10 , 20) , 30)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
),
(
u'<string>'
,
1
,
1
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
),
(
u'<string>'
,
1
,
1
,
u'int'
,
u'int'
)])
def
test_nested_divisions_with_parentheses_1
(
self
):
b
=
"""(
x / y) / z
"""
a
=
"""division_traced("<string>",1,1,(division_traced("<string>",1,0,
x , y)) , z
)"""
b
=
"""(
10 / 20) / 30
"""
a
=
"""division_traced("<string>",1,1,(division_traced("<string>",1,0,
10 , 20)) , 30
)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
),
(
u'<string>'
,
1
,
1
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
),
(
u'<string>'
,
1
,
1
,
u'int'
,
u'int'
)])
def
test_nested_divisions_with_parentheses_2
(
self
):
b
=
"""
x / (y / z
)"""
a
=
"""division_traced("<string>",1,1,
x , (division_traced("<string>",1,0,y , z
)))"""
b
=
"""
30 / (20 / 10
)"""
a
=
"""division_traced("<string>",1,1,
30 , (division_traced("<string>",1,0,20 , 10
)))"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
),
(
u'<string>'
,
1
,
1
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
),
(
u'<string>'
,
1
,
1
,
u'int'
,
u'int'
)])
def
test_inline_division_1
(
self
):
b
=
"""1
/ 2 / 3
"""
a
=
"""division_traced("<string>",1,1,division_traced("<string>",1,0,1
, 2) , 3
)"""
b
=
"""1
0 / 20 / 30
"""
a
=
"""division_traced("<string>",1,1,division_traced("<string>",1,0,1
0 , 20) , 30
)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
),
(
u'<string>'
,
1
,
1
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
),
(
u'<string>'
,
1
,
1
,
u'int'
,
u'int'
)])
def
test_inline_division_2
(
self
):
b
=
"""1
/ 2 * 3
"""
a
=
"""division_traced("<string>",1,0,1
, 2) * 3
"""
b
=
"""1
0 / 20 * 30
"""
a
=
"""division_traced("<string>",1,0,1
0 , 20) * 30
"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
)])
def
test_inline_division_3
(
self
):
b
=
"""1
* 2 / 3
"""
a
=
"""division_traced("<string>",1,0,1
* 2 , 3
)"""
b
=
"""1
0 * 20 / 30
"""
a
=
"""division_traced("<string>",1,0,1
0 * 20 , 30
)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
)])
def
test_division_on_line_continuation
(
self
):
b
=
"""
x
\
/
y
"""
a
=
"""
division_traced("<string>",1,0,x
\
,
y
)"""
b
=
"""
10
\
/
20
"""
a
=
"""
division_traced("<string>",1,0,10
\
,
20
)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
)])
def
test_multiline_division
(
self
):
b
=
"""foo =
\
x / y
"""
10 / 20
"""
a
=
"""foo =
\
division_traced("<string>",1,0,
x , y
)"""
division_traced("<string>",1,0,
10 , 20
)"""
self
.
check
(
b
,
a
)
self
.
exec_code
(
a
)
self
.
assertDataEqual
(
"division_modified"
,
[(
u'<string>'
,
1
,
0
)])
self
.
assertDataEqual
(
"division_trace"
,
[(
u'<string>'
,
1
,
0
,
u'int'
,
u'int'
)])
if
__name__
==
'__main__'
:
unittest
.
main
()
my2to3/tests/testFixNestedExceptTrace.py
View file @
0d19c086
...
...
@@ -16,9 +16,8 @@ class testFixNestedExceptTrace(FixerTestCase):
except Exception as e:
3
"""
self
.
assertEqual
(
self
.
traces
,
[])
self
.
unchanged
(
a
)
self
.
assert
Equal
(
self
.
traces
,
[])
self
.
assert
DataEqual
(
"nested_except_trace"
,
[])
def
test_except
(
self
):
a
=
"""
...
...
@@ -30,9 +29,8 @@ class testFixNestedExceptTrace(FixerTestCase):
except Exception as e:
3
"""
self
.
assertEqual
(
self
.
traces
,
[])
self
.
unchanged
(
a
)
self
.
assert
Equal
(
self
.
traces
,
[(
u'<string>'
,
4
,
7
)])
self
.
assert
DataEqual
(
"nested_except_trace"
,
[(
u'<string>'
,
4
,
7
)])
def
test_except_2
(
self
):
a
=
"""
...
...
@@ -44,9 +42,8 @@ class testFixNestedExceptTrace(FixerTestCase):
except Exception as f:
3
"""
self
.
assertEqual
(
self
.
traces
,
[])
self
.
unchanged
(
a
)
self
.
assert
Equal
(
self
.
traces
,
[])
self
.
assert
DataEqual
(
"nested_except_trace"
,
[])
def
test_multiple_except
(
self
):
a
=
"""
...
...
@@ -61,9 +58,8 @@ class testFixNestedExceptTrace(FixerTestCase):
except Exception as e:
4
"""
self
.
assertEqual
(
self
.
traces
,
[])
self
.
unchanged
(
a
)
self
.
assert
Equal
(
self
.
traces
,
[(
u'<string>'
,
7
,
10
),
(
u'<string>'
,
4
,
7
),
(
u'<string>'
,
4
,
10
)])
self
.
assert
DataEqual
(
"nested_except_trace"
,
[(
u'<string>'
,
7
,
10
),
(
u'<string>'
,
4
,
7
),
(
u'<string>'
,
4
,
10
)])
def
test_else
(
self
):
a
=
"""
...
...
@@ -77,9 +73,8 @@ class testFixNestedExceptTrace(FixerTestCase):
except Exception as e:
4
"""
self
.
assertEqual
(
self
.
traces
,
[])
self
.
unchanged
(
a
)
self
.
assert
Equal
(
self
.
traces
,
[])
self
.
assert
DataEqual
(
"nested_except_trace"
,
[])
def
test_finally
(
self
):
a
=
"""
...
...
@@ -93,9 +88,8 @@ class testFixNestedExceptTrace(FixerTestCase):
except Exception as e:
4
"""
self
.
assertEqual
(
self
.
traces
,
[])
self
.
unchanged
(
a
)
self
.
assert
Equal
(
self
.
traces
,
[])
self
.
assert
DataEqual
(
"nested_except_trace"
,
[])
if
__name__
==
'__main__'
:
...
...
my2to3/trace.py
View file @
0d19c086
...
...
@@ -44,19 +44,20 @@ def get_fixers():
]
def
get_data
(
table
,
columns_to_select
,
conditions
):
def
get_data
(
table
,
columns_to_select
=
'*'
,
conditions
=
{}
):
# TODO:
# - refactor
# - optimize
conn
=
sqlite3
.
connect
(
database
)
c
=
conn
.
cursor
()
data
=
c
.
execute
(
"SELECT %s FROM %s WHERE
%s"
%
(
query
=
"SELECT %s FROM
%s"
%
(
', '
.
join
(
columns_to_select
),
table
,
' AND '
.
join
(
k
+
" = "
+
v
for
k
,
v
in
conditions
.
items
()))
)
if
conditions
:
query
+=
"WHERE "
+
' AND '
.
join
(
k
+
" = "
+
v
for
k
,
v
in
conditions
.
items
())
try
:
return
data
.
fetchall
()
return
c
.
execute
(
query
)
.
fetchall
()
finally
:
conn
.
close
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment