Skip to content

Commit ace2403

Browse files
committed
test: db methods refactor
1 parent ccf6337 commit ace2403

File tree

6 files changed

+92
-98
lines changed

6 files changed

+92
-98
lines changed

tasks.md

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -140,57 +140,55 @@ At this point you have a styled application. Check out the styles:
140140

141141
## 3.3 - Database Path
142142

143-
@pytest.mark.app_db_path below all of the import statements, create a constant called `DATABASE`, that contains the path to the already created database stored in `db/jobs.sqlite`.
143+
@pytest.mark.app_db_path below all of the import statements, create a constant called `PATH`, that contains the path to the already created database stored in `db/jobs.sqlite`.
144144

145145
## 3.4 - Global Database Attribute
146146

147-
@pytest.mark.app_get_db_get_attribute At the top of `app.py` create a function called `get_db`.
147+
@pytest.mark.app_open_connection_get_attribute At the top of `app.py` create a function called `open_connection`.
148148

149-
In the body of the `get_db` function use the built_in `getattr()` function to get the `'_database'` attribute from the `g` object, and set the default to `None`. Assign the return value of the `getattr` function to `db`.
149+
In the body of the `open_connection` function use the built_in `getattr()` function to get the `'_connection'` attribute from the `g` object, and set the default to `None`. Assign the return value of the `getattr` function to `db`.
150150

151151
## 3.5 - Global Database Connection
152152

153-
@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.
153+
@pytest.mark.app_open_connection_connection Still in the `open_connection` function, test if `connection` is `None` if it is, set `connection` and `g._connection` to `sqlite3.connect(PATH)` using multiple assignment.
154154

155155
## 3.6 - sqlite3 Row Factory
156156

157-
@pytest.mark.app_get_db_row_factory To make accessing data easier, after the if statement in `get_db`:
157+
@pytest.mark.app_open_connection_row_factory To make accessing data easier, after the if statement in `open_connection`:
158158

159-
- Set the row_factory of `db` to `sqlite3.Row`. **Note: All rows returned from the database will be named tuples.**
160-
- Return the `db` variable.
159+
- Set the row_factory of `connection` to `sqlite3.Row`. **Note: All rows returned from the database will be named tuples.**
160+
- Return the `connection` variable.
161161

162162
## 3.7 - Query Database Function
163163

164-
@pytest.mark.app_query_db Let’s create a function to make it easier to query the database.
164+
@pytest.mark.app_execute_sql Let’s create a function to make it easier to query the database.
165165

166-
Below the `get_db` function in `app.py` create a function called `query_db`.
166+
Below the `open_connection` function in `app.py` create a function called `execute_sql`.
167167

168-
In the body of `query_db` create a variable called `db`. Assign this variable the return value of a call to the newly created `get_db` function.
168+
In the body of `execute_sql` create a variable called `db`. Assign this variable the return value of a call to the newly created `open_connection` function.
169169

170170
## 3.8 - Query Database Function Parameters
171171

172-
@pytest.mark.app_query_db_parameters Still working with the `query_db` function:
172+
@pytest.mark.app_execute_sql_parameters Still working with the `execute_sql` function:
173173

174-
- Add three parameters: `query`, `args`, and `one`.
175-
- Set the default of `args` to an empty tuple `()`.
176-
- Set the default of `one` to `False`.
174+
- Add four parameters: `sql`, `values`, `commit`, and `single`.
175+
- Set the default of `values` to an empty tuple `()`.
176+
- Set the default of `commit` to `False`.
177+
- Set the default of `single` to `False`.
177178

178179
## 3.9 - Query Database Function Execute
179-
@pytest.mark.app_query_db_execute In the body of `query_db` call the `execute` function on `db`, pass in the `query` and `args` variables. Assign the return value to a variable called `cursor`.
180+
@pytest.mark.app_execute_sql_execute In the body of `execute_sql` call the `execute` function on `connection`, pass in the `sql` and `values` variables. Assign the return value to a variable called `cursor`.
180181

181-
## 3.10 - Query Database Function Fetchall
182-
@pytest.mark.app_query_db_fetchall In the body of `query_db`:
182+
## 3.10 - Query Database Function Commit
183+
@pytest.mark.app_execute_sql_commit In the body of `execute_sql`:
183184

184-
- `fetchall` data from the `cursor` and assign it to a variable called `results`.
185-
- Close the `cursor` with the `close` function.
185+
- Create an `if` statement to test if `commit` is `True`.
186+
- If `commit` is `True`, assign the variable `results` the the return of the function `connection.commit()`.
187+
- Else set `results` to the ternary `if`: `cursor.fetchone() if single else cursor.fetchall()`.
188+
- Close the cursor.
189+
- Return `results` variable.
186190

187-
## 3.11 - Query Database Function One Record
188-
@pytest.mark.app_query_db_one Next, in the function body of `query_db` add a test if `one` is `True`:
189-
190-
- if true return a ternary if, `results[0] if results else None`.
191-
- else return all `results`.
192-
193-
## 3.12 - Close the Connection
191+
## 3.11 - Close the Connection
194192

195193
@pytest.mark.app_close_connection In order to make sure the database connection is closed when the `app_context` is torn down:
196194

@@ -199,11 +197,11 @@ In the body of `query_db` create a variable called `db`. Assign this variable th
199197

200198
In the function body:
201199

202-
- Call `getattr` with three arguments `g`, `'_database'`, and `None`
203-
- Assign the return value to a `db` variable.
204-
- If `db` is not `None` `close` the `db`.
200+
- Call `getattr` with three arguments `g`, `'_connection'`, and `None`
201+
- Assign the return value to a `connection` variable.
202+
- If `connection` is not `None` `close` the `connection`.
205203

206-
## 3.13 - Close the Connection Decorator
204+
## 3.12 - Close the Connection Decorator
207205

208206
@pytest.mark.app_close_connection_decorator To ensure the `close_connection` function is called when the `app_context` is destroyed decorate it with `@app.teardown_appcontext`.
209207

@@ -279,7 +277,7 @@ In `<p>` tag add the following:
279277

280278
@pytest.mark.app_jobs_route_jobs In `app.py` locate the `jobs` function.
281279

282-
Above the `render_template` function, call the `query_db` function:
280+
Above the `render_template` function, call the `execute_sql` function:
283281

284282
- Pass in the SQL statement: `'SELECT job.id, job.title, job.description, job.salary, employer.id as employer_id, employer.name as employer_name FROM job JOIN employer ON employer.id = job.employer_id'`.
285283
- Assign the results of the call to a variable called `jobs`.
@@ -311,7 +309,7 @@ After the `extends` tag add a template `block` called `content`. In the block ca
311309

312310
## 5.3 - Job Route Decorator
313311

314-
@pytest.mark.app_job_route_decorator We only need one job from the database, we will use the `query_db` function passing in a query with a where clause. In the where clause we will need a `job_id`. We are going to get this from the URL.
312+
@pytest.mark.app_job_route_decorator We only need one job from the database, we will use the `execute_sql` function passing in a query with a where clause. In the where clause we will need a `job_id`. We are going to get this from the URL.
315313

316314
Still in `app.py`, add a route decorator with the URL path `/job/<job_id>` to the `job` function.
317315

@@ -321,12 +319,12 @@ Still in `app.py`, add a route decorator with the URL path `/job/<job_id>` to th
321319

322320
## 5.5 - Job Route Data
323321

324-
@pytest.mark.app_job_route_data In the `job` function, above the `render_template` function, call the `query_db` function and assign the results of the call to a `job` variable.
325-
Pass these three arguments to `query_db`:
322+
@pytest.mark.app_job_route_data In the `job` function, above the `render_template` function, call the `execute_sql` function and assign the results of the call to a `job` variable.
323+
Pass these three arguments to `execute_sql`:
326324

327325
- SQL Query: `'SELECT job.id, job.title, job.description, job.salary, employer.id as employer_id, employer.name as employer_name FROM job JOIN employer ON employer.id = job.employer_id WHERE job.id = ?'`
328326
- List Literal: [job_id]
329-
- True: This will bring back only one result.
327+
- single=True, This will bring back only one result.
330328

331329
## 5.6 - Job Route Pass Data
332330

@@ -394,9 +392,9 @@ In the body return a call to the `render_template` function passing in the `empl
394392

395393
## 6.8 - Employer Route Employer Details
396394

397-
@pytest.mark.app_employer_route_employers Still working with the `employer` function add `employer_id` to the parameter list so that we have access to this value. Above the `render_template` function make a call to `query_db` and assign the return value to `employer`.
395+
@pytest.mark.app_employer_route_employers Still working with the `employer` function add `employer_id` to the parameter list so that we have access to this value. Above the `render_template` function make a call to `execute_sql` and assign the return value to `employer`.
398396

399-
Pass the following arguments to `query_db`:
397+
Pass the following arguments to `execute_sql`:
400398

401399
- SQL Query: 'SELECT * FROM employer WHERE id=?'
402400
- List Literal: [employer_id]
@@ -406,7 +404,7 @@ In the `render_template` function, pass a keyword argument of `employer=employer
406404

407405
## 6.9 - Employer Route Employer Jobs
408406

409-
@pytest.mark.app_employer_route_jobs On the employer details page, we want to display all of the employers' jobs. In the `employer` function in `app.py` below the `employer` variable, add a call to the `query_db` function and assign the results to a variable called `jobs`. Pass the function two arguments:
407+
@pytest.mark.app_employer_route_jobs On the employer details page, we want to display all of the employers' jobs. In the `employer` function in `app.py` below the `employer` variable, add a call to the `execute_sql` function and assign the results to a variable called `jobs`. Pass the function two arguments:
410408

411409
- SQL Query: `'SELECT job.id, job.title, job.description, job.salary FROM job JOIN employer ON employer.id = job.employer_id WHERE employer.id = ?'`
412410
- List Literal: [employer_id]
@@ -415,7 +413,7 @@ In the `render_template` function, add another keyword argument of `jobs=jobs`
415413

416414
## 6.10 - Employer Route Employer Review
417415

418-
@pytest.mark.app_employer_route_reviews Still in the `employer` function in `app.py` below the jobs query add a new query to get all review for the employer. Make a call to `query_db` and assign the return value to `reviews`. Pass in the arguments:
416+
@pytest.mark.app_employer_route_reviews Still in the `employer` function in `app.py` below the jobs query add a new query to get all review for the employer. Make a call to `execute_sql` and assign the return value to `reviews`. Pass in the arguments:
419417

420418
- SQL Query: 'SELECT review, rating, title, date, status FROM review JOIN employer ON employer.id = review.employer_id WHERE employer.id = ?'
421419
- List Literal: [employer_id]
@@ -459,7 +457,7 @@ In the body of the function return the `render_template` function passing in the
459457

460458
@pytest.mark.app_review_insert_review Still in the `review` function below the variables in the `if` statement, connect to the database, insert the form values, and commit the changes. Follow these steps:
461459

462-
- Assign a `db` variable to a call to `get_db()`
460+
- Assign a `db` variable to a call to `open_connection()`
463461
- `execute` the following SQL statement on `db`: `'INSERT INTO review (review, rating, title, date, status, employer_id) VALUES (?, ?, ?, ?, ?, ?)'` passing the values: `(review, rating, title, date, status, employer_id)`
464462
- `commit` the changes to the database.
465463
- Return a redirect taking the user back to the employer page. **Hint: use `redirect()` and `url_for()` (pass a keyword argument of `employer_id=employer_id`) both of which need to be imported from flask.**

tests/test_module3.py

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,69 @@ def test_app_import_g_module3():
1414

1515
@pytest.mark.app_db_path
1616
def test_app_db_path_module3():
17-
assert 'DATABASE' in dir(app), 'Have you created a constant called `DATABASE`.'
18-
assert app.DATABASE == 'db/jobs.sqlite', 'Have you created a constant called `DATABASE`?'
17+
assert 'PATH' in dir(app), 'Have you created a constant called `PATH`.'
18+
assert app.PATH == 'db/jobs.sqlite', 'Have you created a constant called `PATH`?'
1919

20-
@pytest.mark.app_get_db_get_attribute
21-
def test_app_get_db_get_attribute_module3():
22-
assert 'get_db' in dir(app), 'Have you defined a function named `get_db`.'
23-
assert 'getattr:g:_database:None' in get_functions(app.get_db), 'Have you used the `getattr` function to get the global `_database`?'
20+
@pytest.mark.app_open_connection_get_attribute
21+
def test_app_open_connection_get_attribute_module3():
22+
assert 'open_connection' in dir(app), 'Have you defined a function named `open_connection`.'
23+
assert 'getattr:g:_connection:None' in get_functions(app.open_connection), 'Have you used the `getattr` function to get the global `_connection`?'
2424

25-
@pytest.mark.app_get_db_connection
26-
def test_app_get_db_connection_module3():
25+
@pytest.mark.app_open_connection_connection
26+
def test_app_open_connection_connection_module3():
2727
assert 'g' in dir(app), 'Have you imported the `g` class from `flask`?'
2828
assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?'
29-
assert 'get_db' in dir(app), 'Have you defined a function named `get_db`.'
29+
assert 'open_connection' in dir(app), 'Have you defined a function named `open_connection`.'
3030
with app.app.app_context():
31-
app.get_db()
32-
assert hasattr(app.g, '_database'), 'Did you assign the `_database` attribute to `g`?'
33-
_, _, db_name = app.g._database.execute('PRAGMA database_list').fetchone()
34-
assert os.path.join(os.getcwd(), 'db', 'jobs.sqlite') == db_name, 'Did you pass the `connect` function the `DATABASE` constant?'
31+
app.open_connection()
32+
assert hasattr(app.g, '_connection'), 'Did you assign the `_connection` attribute to `g`?'
33+
_, _, db_name = app.g._connection.execute('PRAGMA database_list').fetchone()
34+
assert os.path.join(os.getcwd(), 'db', 'jobs.sqlite') == db_name, 'Did you pass the `connect` function the `PATH` constant?'
3535

36-
@pytest.mark.app_get_db_row_factory
37-
def test_app_get_db_row_factory_module3():
36+
@pytest.mark.app_open_connection_row_factory
37+
def test_app_open_connection_row_factory_module3():
3838
assert 'g' in dir(app), 'Have you imported the `g` class from `flask`?'
3939
assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?'
40-
assert 'get_db' in dir(app), 'Have you defined a function named `get_db`.'
40+
assert 'open_connection' in dir(app), 'Have you defined a function named `open_connection`.'
4141
with app.app.app_context():
42-
db = app.get_db()
42+
db = app.open_connection()
4343
assert isinstance(db, app.sqlite3.Connection), 'Are you returning the database connection?'
4444
assert id(db.row_factory) == id(app.sqlite3.Row), 'Have you set the database `row_factory` to the sqlite3.Row class?'
4545

46-
@pytest.mark.app_query_db
47-
def test_app_query_db_module3():
46+
@pytest.mark.app_execute_sql
47+
def test_app_execute_sql_module3():
4848
assert 'app' in dir(app), 'Have you created an instance of the `Flask` class called `app`?'
49-
assert 'query_db' in dir(app), 'Have you defined a function named `query_db`.'
50-
assert 'get_db' in get_functions(app.query_db), 'Have you called the `get_db` function in `query_db`?'
49+
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
50+
assert 'open_connection' in get_functions(app.execute_sql), 'Have you called the `open_connection` function in `execute_sql`?'
5151

52-
@pytest.mark.app_query_db_parameters
53-
def test_app_query_db_parameters_module3():
54-
assert 'query_db' in dir(app), 'Have you defined a function named `query_db`.'
55-
parameters = inspect.getfullargspec(app.query_db)
56-
assert parameters.args[0] == 'query' and parameters.args[1] == 'args' and parameters.args[2] == 'one', 'Have you added the correct parameters to the `query_db` function parameters list?'
57-
assert parameters.defaults[0] == () and parameters.defaults[1] == False, 'Do the `args` and `one` parameters have the correct defaults in the `query_db` function parameters list?'
52+
@pytest.mark.app_execute_sql_parameters
53+
def test_app_execute_sql_parameters_module3():
54+
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
55+
parameters = inspect.getfullargspec(app.execute_sql)
56+
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?'
57+
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?'
5858

59-
@pytest.mark.app_query_db_execute
60-
def test_app_query_db_execute_module3():
61-
assert 'query_db' in dir(app), 'Have you defined a function named `query_db`.'
62-
assert 'execute:query:args' in get_functions(app.query_db), 'Have you called the `execute` function in `query_db`?'
59+
@pytest.mark.app_execute_sql_execute
60+
def test_app_execute_sql_execute_module3():
61+
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
62+
assert 'execute:sql:values' in get_functions(app.execute_sql), 'Have you called the `execute` function in `execute_sql`?'
6363

64-
@pytest.mark.app_query_db_fetchall
65-
def test_app_query_db_fetchall_module3():
66-
assert 'query_db' in dir(app), 'Have you defined a function named `query_db`.'
67-
assert 'fetchall' in get_functions(app.query_db), 'Have you called the `fetchall` function in `query_db`?'
68-
assert 'close' in get_functions(app.query_db), 'Have you called the `close` function in `query_db`?'
69-
70-
@pytest.mark.app_query_db_one
71-
def test_app_query_db_one_module3():
72-
assert 'query_db' in dir(app), 'Have you defined a function named `query_db`.'
64+
@pytest.mark.app_execute_sql_fetchall
65+
def test_app_execute_sql_fetchall_module3():
66+
assert 'execute_sql' in dir(app), 'Have you defined a function named `execute_sql`.'
67+
assert 'fetchall' in get_functions(app.execute_sql), 'Have you called the `fetchall` function in `execute_sql`?'
68+
assert 'fetchone' in get_functions(app.execute_sql), 'Have you called the `fetchone` function in `execute_sql`?'
69+
assert 'commit' in get_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
70+
assert 'close' in get_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
7371
with app.app.app_context():
74-
results = app.query_db('SELECT * FROM job', one=True)
72+
results = app.execute_sql('SELECT * FROM job', single=True)
7573
assert type(results) != list, 'Have you create an if statement to only return one result in `one` is true?'
7674

7775
@pytest.mark.app_close_connection
7876
def test_app_close_connection_module3():
7977
assert 'close_connection' in dir(app), 'Have you defined a function named `close_connection`.'
80-
assert 'getattr:g:_database:None' in get_functions(app.get_db), 'Have you used the `getattr` function to get the global `_database`?'
81-
assert 'close' in get_functions(app.query_db), 'Have you called the `close` function in `query_db`?'
78+
assert 'getattr:g:_connection:None' in get_functions(app.open_connection), 'Have you used the `getattr` function to get the global `_connection`?'
79+
assert 'close' in get_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
8280

8381
@pytest.mark.app_close_connection_decorator
8482
def test_app_close_connection_decorator_module3():

0 commit comments

Comments
 (0)