You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tasks.md
+38-40Lines changed: 38 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -140,57 +140,55 @@ At this point you have a styled application. Check out the styles:
140
140
141
141
## 3.3 - Database Path
142
142
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`.
144
144
145
145
## 3.4 - Global Database Attribute
146
146
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`.
148
148
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`.
150
150
151
151
## 3.5 - Global Database Connection
152
152
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.
154
154
155
155
## 3.6 - sqlite3 Row Factory
156
156
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`:
158
158
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.
161
161
162
162
## 3.7 - Query Database Function
163
163
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.
165
165
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`.
167
167
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.
169
169
170
170
## 3.8 - Query Database Function Parameters
171
171
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:
173
173
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`.
177
178
178
179
## 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`.
180
181
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`:
183
184
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.
186
190
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
194
192
195
193
@pytest.mark.app_close_connection In order to make sure the database connection is closed when the `app_context` is torn down:
196
194
@@ -199,11 +197,11 @@ In the body of `query_db` create a variable called `db`. Assign this variable th
199
197
200
198
In the function body:
201
199
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`.
205
203
206
-
## 3.13 - Close the Connection Decorator
204
+
## 3.12 - Close the Connection Decorator
207
205
208
206
@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`.
209
207
@@ -279,7 +277,7 @@ In `<p>` tag add the following:
279
277
280
278
@pytest.mark.app_jobs_route_jobs In `app.py` locate the `jobs` function.
281
279
282
-
Above the `render_template` function, call the `query_db` function:
280
+
Above the `render_template` function, call the `execute_sql` function:
283
281
284
282
- 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'`.
285
283
- 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
311
309
312
310
## 5.3 - Job Route Decorator
313
311
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.
315
313
316
314
Still in `app.py`, add a route decorator with the URL path `/job/<job_id>` to the `job` function.
317
315
@@ -321,12 +319,12 @@ Still in `app.py`, add a route decorator with the URL path `/job/<job_id>` to th
321
319
322
320
## 5.5 - Job Route Data
323
321
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`:
326
324
327
325
- 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 = ?'`
328
326
- List Literal: [job_id]
329
-
- True: This will bring back only one result.
327
+
-single=True, This will bring back only one result.
330
328
331
329
## 5.6 - Job Route Pass Data
332
330
@@ -394,9 +392,9 @@ In the body return a call to the `render_template` function passing in the `empl
394
392
395
393
## 6.8 - Employer Route Employer Details
396
394
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`.
398
396
399
-
Pass the following arguments to `query_db`:
397
+
Pass the following arguments to `execute_sql`:
400
398
401
399
- SQL Query: 'SELECT * FROM employer WHERE id=?'
402
400
- List Literal: [employer_id]
@@ -406,7 +404,7 @@ In the `render_template` function, pass a keyword argument of `employer=employer
406
404
407
405
## 6.9 - Employer Route Employer Jobs
408
406
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:
410
408
411
409
- SQL Query: `'SELECT job.id, job.title, job.description, job.salary FROM job JOIN employer ON employer.id = job.employer_id WHERE employer.id = ?'`
412
410
- List Literal: [employer_id]
@@ -415,7 +413,7 @@ In the `render_template` function, add another keyword argument of `jobs=jobs`
415
413
416
414
## 6.10 - Employer Route Employer Review
417
415
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:
419
417
420
418
- SQL Query: 'SELECT review, rating, title, date, status FROM review JOIN employer ON employer.id = review.employer_id WHERE employer.id = ?'
421
419
- List Literal: [employer_id]
@@ -459,7 +457,7 @@ In the body of the function return the `render_template` function passing in the
459
457
460
458
@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:
461
459
462
-
- Assign a `db` variable to a call to `get_db()`
460
+
- Assign a `db` variable to a call to `open_connection()`
463
461
-`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)`
464
462
-`commit` the changes to the database.
465
463
- 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.**
assert'g'indir(app), 'Have you imported the `g` class from `flask`?'
39
39
assert'app'indir(app), 'Have you created an instance of the `Flask` class called `app`?'
40
-
assert'get_db'indir(app), 'Have you defined a function named `get_db`.'
40
+
assert'open_connection'indir(app), 'Have you defined a function named `open_connection`.'
41
41
withapp.app.app_context():
42
-
db=app.get_db()
42
+
db=app.open_connection()
43
43
assertisinstance(db, app.sqlite3.Connection), 'Are you returning the database connection?'
44
44
assertid(db.row_factory) ==id(app.sqlite3.Row), 'Have you set the database `row_factory` to the sqlite3.Row class?'
45
45
46
-
@pytest.mark.app_query_db
47
-
deftest_app_query_db_module3():
46
+
@pytest.mark.app_execute_sql
47
+
deftest_app_execute_sql_module3():
48
48
assert'app'indir(app), 'Have you created an instance of the `Flask` class called `app`?'
49
-
assert'query_db'indir(app), 'Have you defined a function named `query_db`.'
50
-
assert'get_db'inget_functions(app.query_db), 'Have you called the `get_db` function in `query_db`?'
49
+
assert'execute_sql'indir(app), 'Have you defined a function named `execute_sql`.'
50
+
assert'open_connection'inget_functions(app.execute_sql), 'Have you called the `open_connection` function in `execute_sql`?'
51
51
52
-
@pytest.mark.app_query_db_parameters
53
-
deftest_app_query_db_parameters_module3():
54
-
assert'query_db'indir(app), 'Have you defined a function named `query_db`.'
55
-
parameters=inspect.getfullargspec(app.query_db)
56
-
assertparameters.args[0] =='query'andparameters.args[1] =='args'andparameters.args[2] =='one', 'Have you added the correct parameters to the `query_db` function parameters list?'
57
-
assertparameters.defaults[0] == () andparameters.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
+
deftest_app_execute_sql_parameters_module3():
54
+
assert'execute_sql'indir(app), 'Have you defined a function named `execute_sql`.'
assertparameters.args[0] =='sql'andparameters.args[1] =='values'andparameters.args[2] =='commit'andparameters.args[3] =='single', 'Have you added the correct parameters to the `execute_sql` function parameters list?'
57
+
assertparameters.defaults[0] == () andparameters.defaults[1] ==Falseandparameters.defaults[2] ==False, 'Do the `args` and `one` parameters have the correct defaults in the `execute_sql` function parameters list?'
58
58
59
-
@pytest.mark.app_query_db_execute
60
-
deftest_app_query_db_execute_module3():
61
-
assert'query_db'indir(app), 'Have you defined a function named `query_db`.'
62
-
assert'execute:query:args'inget_functions(app.query_db), 'Have you called the `execute` function in `query_db`?'
59
+
@pytest.mark.app_execute_sql_execute
60
+
deftest_app_execute_sql_execute_module3():
61
+
assert'execute_sql'indir(app), 'Have you defined a function named `execute_sql`.'
62
+
assert'execute:sql:values'inget_functions(app.execute_sql), 'Have you called the `execute` function in `execute_sql`?'
63
63
64
-
@pytest.mark.app_query_db_fetchall
65
-
deftest_app_query_db_fetchall_module3():
66
-
assert'query_db'indir(app), 'Have you defined a function named `query_db`.'
67
-
assert'fetchall'inget_functions(app.query_db), 'Have you called the `fetchall` function in `query_db`?'
68
-
assert'close'inget_functions(app.query_db), 'Have you called the `close` function in `query_db`?'
69
-
70
-
@pytest.mark.app_query_db_one
71
-
deftest_app_query_db_one_module3():
72
-
assert'query_db'indir(app), 'Have you defined a function named `query_db`.'
64
+
@pytest.mark.app_execute_sql_fetchall
65
+
deftest_app_execute_sql_fetchall_module3():
66
+
assert'execute_sql'indir(app), 'Have you defined a function named `execute_sql`.'
67
+
assert'fetchall'inget_functions(app.execute_sql), 'Have you called the `fetchall` function in `execute_sql`?'
68
+
assert'fetchone'inget_functions(app.execute_sql), 'Have you called the `fetchone` function in `execute_sql`?'
69
+
assert'commit'inget_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
70
+
assert'close'inget_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
73
71
withapp.app.app_context():
74
-
results=app.query_db('SELECT * FROM job', one=True)
72
+
results=app.execute_sql('SELECT * FROM job', single=True)
75
73
asserttype(results) !=list, 'Have you create an if statement to only return one result in `one` is true?'
76
74
77
75
@pytest.mark.app_close_connection
78
76
deftest_app_close_connection_module3():
79
77
assert'close_connection'indir(app), 'Have you defined a function named `close_connection`.'
80
-
assert'getattr:g:_database:None'inget_functions(app.get_db), 'Have you used the `getattr` function to get the global `_database`?'
81
-
assert'close'inget_functions(app.query_db), 'Have you called the `close` function in `query_db`?'
78
+
assert'getattr:g:_connection:None'inget_functions(app.open_connection), 'Have you used the `getattr` function to get the global `_connection`?'
79
+
assert'close'inget_functions(app.execute_sql), 'Have you called the `close` function in `execute_sql`?'
0 commit comments