Skip to content

Commit 2ac4aa8

Browse files
committed
test(modules): module2 complete
1 parent 6e28fda commit 2ac4aa8

File tree

5 files changed

+80
-20
lines changed

5 files changed

+80
-20
lines changed

tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ In the body of the `get_db` function use the built_in `getattr()` function to ge
151151

152152
## 3.5 - Global Database Connection
153153

154-
@pytest.mark.app_get_db_connection Still in the `get_db` function, test if `db` is `None` if it is, set `db` and `g._database` to `sqlite3.connect(DATABASE)` using multiple assignment.
154+
@pytest.mark.app_get_db_connection Still in the `get_db` function, test if `db` is `None` if it is, set `db` and `g._database` to `sqlite3.connect(DATABASE)` using multiple assignment.
155155

156156
## 3.6 - sqlite3 Row Factory
157157

tests/test_module1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def test_templates_folder_module1():
2626
@pytest.mark.index_template
2727
def test_index_template_module1():
2828
assert template_exists('index'), 'The `index.html` template does not exist in the `templates` folder.'
29-
assert tag_contains('index', 'h1', 'Jobs'), "The `<h1>` in the `index.html` template does not contain the contents 'Jobs'."
29+
assert template_find('index', 'h1', limit=1), "The `<h1>` in the `index.html` template does not contain the contents 'Jobs'."
30+
assert template_find('index', 'h1', limit=1).text == 'Jobs', "The `<h1>` in the `index.html` template does not contain the contents 'Jobs'."
3031

3132
@pytest.mark.app_index_route_function
3233
def test_app_index_route_function_module1():

tests/test_module2.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
import pytest
22
import sys
33

4+
from jinja2 import Environment, PackageLoader, meta
45
from jobs import app
6+
from .utils import *
7+
8+
values = template_values('layout', 'url_for')
59

610
@pytest.mark.layout_template
711
def test_layout_template_module2():
8-
pass
12+
assert template_exists('layout'), 'The `layout.html` template does not exist in the `templates` folder.'
913

1014
@pytest.mark.add_bulma_css_framework
1115
def test_add_bulma_css_framework_module2():
12-
pass
16+
assert template_exists('layout'), 'The `layout.html` template does not exist in the `templates` folder.'
17+
assert 'static:filename:css/bulma.min.css' in values, 'Looks like `bulma.min.css` is not linked in `layout.html`.'
1318

1419
@pytest.mark.add_custom_css
1520
def test_add_custom_css_module2():
16-
pass
21+
assert template_exists('layout'), 'The `layout.html` template does not exist in the `templates` folder.'
22+
assert 'static:filename:css/app.css' in values, 'Looks like `app.css` is not linked in `layout.html`.'
1723

1824
@pytest.mark.add_fontawesome
1925
def test_add_fontawesome_module2():
20-
pass
26+
assert template_exists('layout'), 'The `layout.html` template does not exist in the `templates` folder.'
27+
attr = {
28+
'href': 'https://use.fontawesome.com/releases/v5.2.0/css/all.css',
29+
'rel': 'stylesheet'
30+
}
31+
assert template_doc('layout').find('link', attr), 'Looks like FontAwesome is not linked in `layout.html`.'
2132

2233
@pytest.mark.extend_base_template
2334
def test_extend_base_template_module2():
24-
pass
35+
assert template_exists('index'), 'The `index.html` template does not exist in the `templates` folder.'
36+
assert template_exists('layout'), 'The `layout.html` template does not exist in the `templates` folder.'
37+
assert 'layout.html' in template_extends('index'), 'The `index.html` template does not extend `layout.html`.'

tests/test_module3.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22
import sys
33

44
from jobs import app
5+
from .utils import *
6+
57

68
@pytest.mark.app_import_sqlite
79
def test_app_import_sqlite_module3():
8-
pass
10+
assert 'sqlite3' in dir(app), 'Have you imported `sqlite`'
911

1012
@pytest.mark.app_import_g
1113
def test_app_import_g_module3():
12-
pass
14+
assert 'g' in dir(app), 'Have you imported the `g` class from `flask`'
1315

1416
@pytest.mark.app_db_path
1517
def test_app_db_path_module3():
16-
pass
18+
assert 'DATABASE' in dir(app), 'Have you created a constant called `DATABASE`.'
19+
assert app.DATABASE == 'db/jobs.sqlite', 'Have you created a constant called `DATABASE`.'
1720

1821
@pytest.mark.app_get_db_get_attribute
1922
def test_app_get_db_get_attribute_module3():
20-
pass
23+
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)
2125

2226
@pytest.mark.app_get_db_connection
2327
def test_app_get_db_connection_module3():
24-
pass
28+
get_statements(app.get_db)
29+
assert False
2530

2631
@pytest.mark.app_get_db_row_factory
2732
def test_app_get_db_row_factory_module3():

tests/utils.py

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import inspect
44
from pprint import pprint
55

6+
from jinja2 import nodes, Environment, PackageLoader, meta, exceptions
67
from bs4 import BeautifulSoup
78

9+
env = Environment(loader=PackageLoader('jobs', 'templates'))
10+
811
def get_decorators(source):
912
decorators = {}
1013

@@ -23,36 +26,74 @@ def visit_FunctionDef(node):
2326
node_iter = ast.NodeVisitor()
2427
node_iter.visit_FunctionDef = visit_FunctionDef
2528
node_iter.visit(ast.parse(inspect.getsource(source)))
26-
return decorators
2729

30+
return decorators
2831

2932
def get_functions(source):
3033
functions = []
3134

3235
def visit_Call(node):
3336
name = node.func.attr if isinstance(node.func, ast.Attribute) else node.func.id
34-
functions.append(name + ':' + ':'.join([a.s for a in node.args]))
37+
if name == 'getattr':
38+
functions.append(name + ':' + node.args[0].id + ':' + node.args[1].s + ':' + str(node.args[2].value))
39+
else:
40+
functions.append(name + ':' + ':'.join([a.s for a in node.args]))
3541

3642
node_iter = ast.NodeVisitor()
3743
node_iter.visit_Call = visit_Call
3844
node_iter.visit(ast.parse(inspect.getsource(source)))
3945

4046
return functions
4147

48+
49+
def get_statements(source):
50+
statment = []
51+
52+
def visit_If(node):
53+
print(ast.dump(node))
54+
55+
node_iter = ast.NodeVisitor()
56+
node_iter.visit_If = visit_If
57+
node_iter.visit(ast.parse(inspect.getsource(source)))
58+
59+
return statment
60+
4261
def list_routes(app):
4362
rules = []
63+
4464
for rule in app.url_map.iter_rules():
4565
methods = ','.join(sorted(rule.methods))
4666
if rule.endpoint is not 'static':
47-
rules.append(rule.endpoint+':'+ methods +':'+ str(rule))
67+
rules.append(rule.endpoint + ':' + methods + ':' + str(rule))
68+
4869
return rules
4970

71+
def template_values(name, function):
72+
values = []
73+
74+
for node in parsed_content(name).find_all(nodes.Call):
75+
if node.node.name == function:
76+
values.append(node.args[0].value + ':' + node.kwargs[0].key + ':' + node.kwargs[0].value.value)
77+
78+
return values
79+
5080
def template_exists(name):
5181
return os.path.isfile('jobs/templates/' + name + '.html')
5282

53-
def template_contains(name, tag):
54-
doc = BeautifulSoup(open(os.getcwd() + '/jobs/templates/' + name + '.html'), 'html.parser')
55-
return doc.find(tag)
83+
def template_source(name):
84+
try:
85+
return env.loader.get_source(env, name + '.html')[0]
86+
except exceptions.TemplateNotFound:
87+
return None
88+
89+
def template_doc(name):
90+
return BeautifulSoup(template_source(name), 'html.parser')
91+
92+
def template_find(name, tag, limit=None):
93+
return BeautifulSoup(template_source(name), 'html.parser').find_all(tag, limit=limit)
94+
95+
def parsed_content(name):
96+
return env.parse(template_source(name))
5697

57-
def tag_contains(name, tag, text):
58-
return template_contains(name, tag).text if template_contains(name, tag) else False
98+
def template_extends(name):
99+
return list(meta.find_referenced_templates(parsed_content(name)))

0 commit comments

Comments
 (0)