flowchart LR
A[Write a Test]-->B[Fail Test]-->C[Write Code]-->D[Pass Test]-->E[Refactor]-->A
A representation of the complete User model that we will have after refactoring the code
classDiagram
class User
User : +id Integer
User : +name String
User : +username String
User : -password String
User : +insert()
User : +update()
User : +delete()
User : serialize
User : __repr__()
We are going to start with a simple User Model so that we can fully grasp and appreciate the TDD workflow
classDiagram
class User
User: +id Integer
User : +username String
User : +insert()
User : +update()
User : +delete()
User : serialize
User : __repr__()
graph LR
A[GET Users] -->B[(Are there users in the Database?)]
B -->|No|C[Return 200 OK and Empty list]
B -->|Yes|D[Return 200 OK and list of Users]
graph LR
A[GET User] -->B{Is user id valid?}
B -->|No|C[Return 404 Not Found and Error message]
B -->|Yes|D[Return 200 OK and User object for specified id]
graph TD
A[Post User]-->B
B[Get User Data]-->C{Is Data Valid}
C -->|No|D[Return 400 Bad Request and Message]
C -->|Yes|E{Does User Already Exist }
E -->|Yes|F[Return 409 Conflict and Message]
E -->|No|G[Create User]
G -->|No|H[(Has the User Been Created?)]
H -->|Success|I[Return 201 Created, Message, and Created User]
H -->|Failure|J[Return 500 Internal Server Error and Message]
graph TD
A[PUT User] --> B{Is user id valid}
B -->|No|C[Return 404 Not Found and Error message]
B -->|Yes|D{Is Data Valid}
D -->|No|E[Return 400 Bad Request and Error Message]
D -->|Yes|F{Is Username Unique}
F -->|No|G[Return 409 Conflict and Error Message]
F -->|Yes|H[(Has the User data been updated)]
H -->|Sucess - Modified|I[Return 200 Ok and Message]
H -->|Sucess - Not modified|J[Return 204 No Content]
H -->|Failure - Error occured |K[Return 500 Internal Server Error]
graph TD
A[DELETE User] --> B{Is user id valid?}
B -->|No| C[Return 404 Not Found and Error message]
B -->E[(Delete the user!)]
E -->|Failure|F[Return 500 Internal Server Error and Error Message]
E -->|success| G{Does Response Contain body?}
G-->|Yes|H[Return 200 OK and Message]
G -->|No|J[Return 204 No Content]
-
Create and Activate a Python virtual environment
-
Install requirements
pip install -r requirements.txt
-
Set environment variables specified in the env_sample.txt
- DATABASE_URL for Production
- DEV_DATABASE_URL for Development
- TEST_DATABASE_URL for Testing
Remember to create separate databases for testing and running the code
-
Update the migrations
flask db upgrade
-
Run the project
python -m run
-
Open your terminal at the root of the project
-
create a virtual environment
python -m venv env
-
Activate the virtual environment
- for windows
env\Scripts\activate
- for linux\macOS
source env/bin/activate
- for windows
-
Deactivate the virtual environment
deactivate
- Activate the virtual environment
- Create you test database
- SET the
TEST_DATABASE_URL
environment variable - Run the tests using pytest
pytest tests
{
"username": "Arthur"
}
-
json returns a
python dictionary
withkey-value
pairs-
The parsed JSON data if
mimetype
indicates JSON (*application/json
)is_json
) . -
Calls
get_json()
with default arguments. -
If the request content type is not
application/json
, this will raise a 400 Bad Request error.data = request.json username = data["username"]
-
-
get_json(force=False, silent=False, _ cache=True_) returns a
dict
withkey-value
pairs-
Raises a 400 error if the content type is incorrect.
-
force (bool) – Ignore the mimetype and always try to parse JSON.
data = request.get_json(force=True)
-
-
flask.json.loads(s, app=None, ** kwargs)
-
Serialize an object to a string of JSON.
data = json.loads(request.data.decode())
-
-
-
Serialize data to JSON and wrap it in a Response with the
application/json
mimetypejsonify({"username": "Arthur"})
-
-
flask.json.dumps(obj, app=None, ** kwargs)
-
Serialize an object to a string of JSON.
user_dict = { "username": "Arthur" } json_data = json.dumps(user_dict)
-