Skip to content

Commit 586dbc1

Browse files
committed
docs: refactor module 3 tasks
1 parent 607a36d commit 586dbc1

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

tasks.md

+36-7
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,48 @@ In the body of the `get_db` function use the built-in `getattr()` function to ge
131131

132132
## 3.6 - sqlite3 Row Factory
133133

134-
@pytest.mark.app-sqlite3-row-factory To make accessing data easier, after the if statement in `get_db`, set the row_factory of `db` to `sqlite3.Row`. All rows returned from the database will be named tuples.
134+
@pytest.mark.app-sqlite3-row-factory To make accessing data easier, after the if statement in `get_db`:
135+
- Set the row_factory of `db` to `sqlite3.Row`. **Note: All rows returned from the database will be named tuples.**
136+
- Return the `db` variable.
135137

136-
Return the `db` variable.
138+
## 3.7 - Query Database Function
137139

138-
## 3.7 - Querying the Database
140+
@pytest.mark.app-query-database-function Let’s create a function to make it easier to query the database.
139141

140-
@pytest.mark.app-querying-the-database Let’s create a function below the `get_db` function to make it easier to query the database.
142+
Below the `get_db` function in `app.py` create a function called `query_db`.
141143

142-
Call the function `query_db`, have the function accept three parameters: `query`, `args`, and `one`. Set the default of `args` to an empty tuple `()`. Set the default of `one` to `False`. Call the newly created `get_db` function and assign the return value to a `db` variable. Call the `execute` function, passing in the `query` and `args` variables, on the `db` and assign the return value to a variable called `cursor`. `fetchall()` data from the `cursor` and assign it to a variable called `results`. Close the `cursor` with the `close` function. Next test if there are `results` else return `None`. If there are results test if `one` is `True` and return `results[0]` else return `results`.
144+
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.
143145

144-
## 3.5 - Close the Connection
146+
## 3.8 - Query Database Function Parameters
145147

146-
@pytest.mark.app-close-the-connection In order to make sure the database connection is closed when the `app_context` is torn down create a function in `app.py` called `close_connection`. Add a parameter called `exception`. In the body of the function use the `getattr` function to get the `_database` attribute from the `g` object. If `_database` doesn’t exist set the default to `None`. Assign the return value of the `getattr` function to`db`. If `db` is not `None` `close` the `db`. To ensure this function is called when the `app_context` is destroyed use the ``@app.teardown_appcontext` decorator.
148+
@pytest.mark.app-query-database-function-parameters Still working with the `query_db` function:
149+
- Add three parameters: `query`, `args`, and `one`.
150+
- Set the default of `args` to an empty tuple `()`.
151+
- Set the default of `one` to `False`.
152+
153+
## 3.9 - Query Database Function Execute
154+
@pytest.mark.app-query-database-function-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`.
155+
156+
## 3.10 - Query Database Function Execute
157+
@pytest.mark.app-query-database-function-fetchall In the body of `query_db`:
158+
- `fetchall` data from the `cursor` and assign it to a variable called `results`.
159+
- Close the `cursor` with the `close` function.
160+
161+
## 3.11 - Query Database Function Execute
162+
@pytest.mark.app-query-database-function-single Next, in the function body add a test if `one` is `True`:
163+
- if true return a ternary if, `results[0] if results else None`.
164+
- else return all `results`.
165+
166+
## 3.12 - Close the Connection
167+
168+
@pytest.mark.app-close-the-connection In order to make sure the database connection is closed when the `app_context` is torn down create a function in `app.py` called `close_connection`.
169+
Add a parameter called `exception` to the parameter list.
170+
171+
In the function body:
172+
- Call `getattr` with three arguments `g`, `'_database'`, and `None` and assign the return value to a `db` variable.
173+
- If `db` is not `None` `close` the `db`.
174+
175+
To ensure this function is called when the `app_context` is destroyed use the `@app.teardown_appcontext` decorator.
147176

148177
# Module 04 - Display Jobs and Employers
149178

0 commit comments

Comments
 (0)