-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_module3.py
85 lines (72 loc) · 5.38 KB
/
test_module3.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pytest
import inspect
from jobs import app
from .utils import *
@pytest.mark.test_app_import_sqlite_module3
def test_app_import_sqlite_module3():
assert 'sqlite3' in dir(app), 'Have you imported `sqlite`?'
@pytest.mark.test_app_import_g_module3
def test_app_import_g_module3():
assert 'g' in dir(app), 'Have you imported the `g` class from `flask`?'
@pytest.mark.test_app_db_path_module3
def test_app_db_path_module3():
assert 'PATH' in dir(app), 'Have you created a constant called `PATH`.'
assert app.PATH == 'db/jobs.sqlite', 'Have you created a constant called `PATH`?'
@pytest.mark.test_app_open_connection_get_attribute_module3
def test_app_open_connection_get_attribute_module3():
assert 'open_connection' in dir(app), 'Have you defined a function named `open_connection`.'
assert 'getattr:g:_connection:None' in get_functions(app.open_connection), 'Have you used the `getattr` function to get the global `_connection`?'
@pytest.mark.test_app_open_connection_connection_module3
def test_app_open_connection_connection_module3():
assert 'g' in dir(app), 'Have you imported the `g` class from `flask`?'
assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?'
assert 'open_connection' in dir(app), 'Have you defined a function named `open_connection`.'
with app.app.app_context():
app.open_connection()
assert hasattr(app.g, '_connection'), 'Did you assign the `_connection` attribute to `g`?'
_, _, db_name = app.g._connection.execute('PRAGMA database_list').fetchone()
assert os.path.join(os.getcwd(), 'db', 'jobs.sqlite') == db_name, 'Did you pass the `connect` function the `PATH` constant?'
@pytest.mark.test_app_open_connection_row_factory_module3
def test_app_open_connection_row_factory_module3():
assert 'g' in dir(app), 'Have you imported the `g` class from `flask`?'
assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?'
assert 'open_connection' in dir(app), 'Have you defined a function named `open_connection`.'
with app.app.app_context():
db = app.open_connection()
assert isinstance(db, app.sqlite3.Connection), 'Are you returning the database connection?'
assert id(db.row_factory) == id(app.sqlite3.Row), 'Have you set the database `row_factory` to the sqlite3.Row class?'
@pytest.mark.test_app_execute_sql_module3
def test_app_execute_sql_module3():
assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?'
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
assert 'open_connection' in get_functions(app.execute_sql), 'Have you called the `open_connection` function in `execute_sql`?'
@pytest.mark.test_app_execute_sql_parameters_module3
def test_app_execute_sql_parameters_module3():
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
parameters = inspect.getfullargspec(app.execute_sql)
assert parameters.args[0] == 'sql' and parameters.args[1] == 'values' and parameters.args[2] == 'commit' and parameters.args[3] == 'single', 'Have you added the correct parameters to the `execute_sql` function parameters list?'
assert parameters.defaults[0] == () and parameters.defaults[1] == False and parameters.defaults[2] == False, 'Do the `args` and `one` parameters have the correct defaults in the `execute_sql` function parameters list?'
@pytest.mark.test_app_execute_sql_execute_module3
def test_app_execute_sql_execute_module3():
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
assert 'execute:sql:values' in get_functions(app.execute_sql), 'Have you called the `execute` function in `execute_sql`?'
@pytest.mark.test_app_execute_sql_results_module3
def test_app_execute_sql_results_module3():
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
assert 'fetchall' in get_functions(app.execute_sql), 'Have you called the `fetchall` function in `execute_sql`?'
assert 'fetchone' in get_functions(app.execute_sql), 'Have you called the `fetchone` function in `execute_sql`?'
assert 'commit' in get_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
assert 'close' in get_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
with app.app.app_context():
results = app.execute_sql('SELECT * FROM job', single=True)
assert type(results) != list, 'Have you create an if statement to only return one result in `one` is true?'
@pytest.mark.test_app_close_connection_module3
def test_app_close_connection_module3():
assert 'close_connection' in dir(app), 'Have you defined a function named `close_connection`.'
assert 'getattr:g:_connection:None' in get_functions(app.open_connection), 'Have you used the `getattr` function to get the global `_connection`?'
assert 'close' in get_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
@pytest.mark.test_app_close_connection_decorator_module3
def test_app_close_connection_decorator_module3():
assert 'close_connection' in dir(app), 'Have you defined a function named `close_connection`.'
decorator = get_decorators(app.close_connection)['close_connection'][0][0]
assert decorator == 'teardown_appcontext', 'Does `close_connection` have a `teardown_appcontext` decorator?'