|
1 | 1 | import pytest
|
2 |
| -import sys |
3 |
| -import pprint |
| 2 | +import sys, os, inspect, re |
| 3 | + |
| 4 | +from bs4 import BeautifulSoup |
4 | 5 |
|
5 | 6 | from jobs import app
|
6 | 7 |
|
| 8 | +def list_routes(app): |
| 9 | + rules = [] |
| 10 | + for rule in app.url_map.iter_rules(): |
| 11 | + methods = ','.join(sorted(rule.methods)) |
| 12 | + if rule.endpoint is not 'static': |
| 13 | + rules.append(rule.endpoint+':'+ methods +':'+ str(rule)) |
| 14 | + return rules |
| 15 | + |
7 | 16 | @pytest.mark.app_import_flask
|
8 | 17 | def test_app_import_flask():
|
9 |
| - pass |
| 18 | + assert 'Flask' in dir(app), 'Have you imported the `Flask` class from `flask`' |
| 19 | + assert inspect.isclass(app.Flask), '`Flask` is not a class.' |
| 20 | + assert 'render_template' in dir(app), '`render_template` has not been imported.' |
| 21 | + assert callable(app.render_template), '`render_template` is not a function.' |
10 | 22 |
|
11 | 23 | @pytest.mark.app_create_flask_app
|
12 | 24 | def test_app_create_flask_app():
|
13 |
| - pass |
| 25 | + assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?' |
| 26 | + assert isinstance(app.app, app.Flask), '`app` is not an instance of the `Flask` class.' |
14 | 27 |
|
15 | 28 | @pytest.mark.templates_folder
|
16 | 29 | def test_templates_folder():
|
17 |
| - pass |
| 30 | + assert os.path.isdir('jobs/templates'), 'The `templates` folder has not been created.' |
18 | 31 |
|
19 | 32 | @pytest.mark.index_template
|
20 | 33 | def test_index_template():
|
21 |
| - pass |
| 34 | + assert os.path.isfile('jobs/templates/index.html'), 'The `index.html` template does not exist in the `templates` folder.' |
| 35 | + index = BeautifulSoup(open(os.getcwd() + '/jobs/templates/index.html'), 'html.parser') |
| 36 | + assert index.find('h1'), 'The `index.html` template does not contain an `<h1>`.' |
| 37 | + assert index.find('h1').text == 'Jobs', "The `<h1>` in the `index.html` template does not contain the contents 'Jobs'." |
22 | 38 |
|
23 | 39 | @pytest.mark.app_index_route_function
|
24 | 40 | def test_app_index_route_function():
|
25 |
| - pass |
| 41 | + assert os.path.isfile('jobs/templates/index.html'), 'The `index.html` template does not exist in the `templates` folder.' |
| 42 | + assert 'jobs' in dir(app) |
| 43 | + with app.app.app_context(): |
| 44 | + assert re.findall(r"return\s*render_template\s*\(\s*(?:'|\")index\.html(?:'|\")\s*\)", inspect.getsource(app.jobs)) |
26 | 45 |
|
27 | 46 | @pytest.mark.app_route_decoractors
|
28 | 47 | def test_app_route_decoractors():
|
29 |
| - pass |
| 48 | + assert os.path.isfile('jobs/templates/index.html'), 'The `index.html` template does not exist in the `templates` folder.' |
| 49 | + assert 'jobs' in dir(app) |
| 50 | + |
| 51 | + rules = list_routes(app.app) |
30 | 52 |
|
| 53 | + assert 'jobs:GET,HEAD,OPTIONS:/jobs' in rules |
| 54 | + assert 'jobs:GET,HEAD,OPTIONS:/' in rules |
0 commit comments