Skip to content

Commit 13e1989

Browse files
committed
test: ast to dict
1 parent 2ac4aa8 commit 13e1989

File tree

7 files changed

+43
-80
lines changed

7 files changed

+43
-80
lines changed

jobs/app.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from flask import g
2+
3+
DATABASE = 'db/jobs.sqlite'
4+
5+
def get_db():
6+
db = getattr(g, '_database', None)
7+
if db is None:
8+
db = g._database = sqlite.connect(DATABASE)

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
beautifulsoup4==4.6.3
12
Flask==1.0.2
23
pytest==3.7.1
34
pytest-json-report==0.7.0
4-
python-dotenv==0.9.1
5+
python-dotenv==0.9.1

tag_names.txt

-62
This file was deleted.

tests/test_module1.py

-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
import os
33
import inspect
44

5-
from pprint import pprint
65
from .utils import *
76
from jobs import app
87

9-
108
@pytest.mark.app_import_flask
119
def test_app_import_flask_module1():
1210
assert 'Flask' in dir(app), 'Have you imported the `Flask` class from `flask`'

tests/test_module2.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
import sys
33

4-
from jinja2 import Environment, PackageLoader, meta
54
from jobs import app
65
from .utils import *
76

tests/test_module3.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from jobs import app
55
from .utils import *
6-
6+
from pprint import pprint
77

88
@pytest.mark.app_import_sqlite
99
def test_app_import_sqlite_module3():
@@ -21,11 +21,13 @@ def test_app_db_path_module3():
2121
@pytest.mark.app_get_db_get_attribute
2222
def test_app_get_db_get_attribute_module3():
2323
assert 'get_db' in dir(app), 'Have you defined a function named `get_db`.'
24-
assert 'getattr:g:_database:None' in get_functions(app.get_db)
24+
assert 'getattr:g:_database:None' in get_functions(app.get_db), 'Have used the `getattr` to get the global `_database`.'
2525

2626
@pytest.mark.app_get_db_connection
2727
def test_app_get_db_connection_module3():
28-
get_statements(app.get_db)
28+
assert 'get_db' in dir(app), 'Have you defined a function named `get_db`.'
29+
assert 'db:true:None' in get_statements(app.get_db), 'Have you created an `if` statement to test if `db` is `None`.'
30+
print(source_dict(app.get_db))
2931
assert False
3032

3133
@pytest.mark.app_get_db_row_factory

tests/utils.py

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import ast
33
import inspect
4-
from pprint import pprint
54

65
from jinja2 import nodes, Environment, PackageLoader, meta, exceptions
76
from bs4 import BeautifulSoup
@@ -10,7 +9,6 @@
109

1110
def get_decorators(source):
1211
decorators = {}
13-
1412
def visit_FunctionDef(node):
1513
decorators[node.name] = []
1614
for n in node.decorator_list:
@@ -26,37 +24,54 @@ def visit_FunctionDef(node):
2624
node_iter = ast.NodeVisitor()
2725
node_iter.visit_FunctionDef = visit_FunctionDef
2826
node_iter.visit(ast.parse(inspect.getsource(source)))
29-
3027
return decorators
3128

3229
def get_functions(source):
3330
functions = []
34-
3531
def visit_Call(node):
3632
name = node.func.attr if isinstance(node.func, ast.Attribute) else node.func.id
3733
if name == 'getattr':
3834
functions.append(name + ':' + node.args[0].id + ':' + node.args[1].s + ':' + str(node.args[2].value))
3935
else:
40-
functions.append(name + ':' + ':'.join([a.s for a in node.args]))
36+
if name is not 'connect':
37+
functions.append(name + ':' + ':'.join([a.s for a in node.args]))
38+
4139

4240
node_iter = ast.NodeVisitor()
4341
node_iter.visit_Call = visit_Call
4442
node_iter.visit(ast.parse(inspect.getsource(source)))
45-
4643
return functions
4744

48-
4945
def get_statements(source):
50-
statment = []
46+
statements = []
5147

5248
def visit_If(node):
53-
print(ast.dump(node))
49+
statement = node.test.left.id
50+
if isinstance(node.test.ops[0], ast.Is) or isinstance(node.test.ops[0], ast.Eq):
51+
statement += ':true'
52+
statement += (':' + str(node.test.comparators[0].value))
53+
statements.append(statement)
5454

5555
node_iter = ast.NodeVisitor()
5656
node_iter.visit_If = visit_If
5757
node_iter.visit(ast.parse(inspect.getsource(source)))
58-
59-
return statment
58+
return statements
59+
60+
def source_dict(source):
61+
return build_dict(ast.parse(inspect.getsource(source)))
62+
63+
def build_dict(node):
64+
result = {}
65+
result['node_type'] = node.__class__.__name__
66+
for attr in dir(node):
67+
if not attr.startswith("_") and attr != 'ctx' and attr != 'lineno' and attr != 'col_offset':
68+
value = getattr(node, attr)
69+
if isinstance(value, ast.AST):
70+
value = build_dict(value)
71+
elif isinstance(value, list):
72+
value = [build_dict(n) for n in value]
73+
result[attr] = value
74+
return result
6075

6176
def list_routes(app):
6277
rules = []
@@ -97,3 +112,5 @@ def parsed_content(name):
97112

98113
def template_extends(name):
99114
return list(meta.find_referenced_templates(parsed_content(name)))
115+
116+

0 commit comments

Comments
 (0)