Assignments > HW2: Build Your Second Feature
Due on Fri, 12/08 @ 11:59PM. 20 Points.
I. Introduction
For your second homework assignment, you will be creating one more feature for the class app. Please take a look at the Issue Tracker and assign yourself a task.
By the end of HW2, we should have the foundation of a functioning app that allows people to browse and join groups!
II. Background Information
1. Docker Setup
Pull the latest changes to your local repo.
1
2
git checkout main
git pull
Make sure that…
- Docker is running and
- Any old containers that use port 8000 are stopped
Then, build the images / containers:
1
docker compose up
This command tells Docker to read the instructions in the compose.yaml
file and build 4 containers:
Container | Description |
---|---|
app-ui-1 |
UI container for Node / React stuff |
app-server-1 |
Server container for Python / Fast API |
app-db-1 |
A DB container for PostgreSQL |
app-db_setup-1 |
A temporary DB setup container that’s just used to initialize the DB |
You should only need to run docker compose up
once. Subsequently, you can just start and stop your containers from the CLI or via the Docker dashboard.
If your containers built successfully, visit http://localhost:8000 and behold! The app running!
The easiest way to stop and start your container is by using the Docker Dashboard.
2. Fast API (Python) Development Notes
Server Command Line Cheatsheet
Task | Command |
---|---|
Access the server’s shell | docker exec -it app-server-1 bash poetry shell |
Rebuild the database | docker exec -it app-server-1 poetry run python db/populate.py |
Run the tests | docker exec -it app-server-1 bash scripts/test-server.sh |
Reformat your python code | bash scripts/fix-python.sh (from local machine within src directory) |
Add a new poetry dependency | docker exec -it app-server-1 poetry add <name-of-package> |
SQLAlchemy Cheat Sheet
See Lab 8
Viewing the Log Files (for Uvicorn Web App)
If you run into server errors, navigate to the Docker Dashboard, navigate to the app-server-1
container, and click on it to see the server log file.
Where to put stuff
Instructions / Tips | |
---|---|
Models | Create new models inside of /src/models . One you have built the models, you will need to run the db/populate.py script to actually build the database tables (see cheatsheet). If you want to create some fake data for your new model, feel free to modify the populate.py script. |
Views | Create new views inside of /src/routers . Check out src/routers/tasks.py to see an example. After making a new view, make sure to register it in /src/server.py |
Tests | All automated tests that interact with the database should be created in the src/db/tests directory with a "test_" prefix. See test_tasks.py as an example. |
Test Fixtures | “Fixtures” are functionality that run before and after tests to “set up” the test with relevant data and objects. All fixtures are stored in conftest.py . Several fixtures (designated with the “auto” flag in the decorator) are automatically run before and after each test (more detail on this below). |
Testing Your FastAPI Endpoints
Web Tester
To test your endpoints manually, navigate to http://localhost:8000/docs and tinker with your tests.
Automated Tests
All automated tests that interact with the database should be created in the src/db/tests
directory. Because there is some “magic” involved with the pytest
testing framework, we wanted to share a few notes:
conftest.py
is run before every test. In this folder, a bunch of fixtures are defined, primarily to populate a test database with fake data. These fixtures include:testing_setup_teardown()
, which is run on every test setup and teardown the test database and to create a async database session.users()
which creates a bunch of fake users.tasks()
which creates a bunch of fake tasks.
conftest.py
also has a global object called tester
which has a reference to:
- current_user: the fake, logged in user
- client: the test client
- db_async_session: an asynchronous database session
To see an example of how to use the tester
object, take a look at src/tests/db/test_tasks.py
Feel free to create additional fixtures and / or helper functions in the conftest.py
file.
Running the tests
To run all of the tests, execute the following command:
1
docker exec -it app-server-1 poetry run pytest
Or, you can test specific modules and specific functions, by including the python file at the end.
- The “-v” flag will give you more specific feedback in your test results
- the “-s” flag will output all print statments
- the “-W ignore::DeprecationWarning” statement will suppress deprecation warnings
Here’s an example that tests all of the functions in one file:
1
docker exec -it app-server-1 poetry run pytest tests/db/test_tasks.py -vs -W ignore::DeprecationWarning
3. Node / React Development Notes
Node Command Line Cheatsheet
Task | Command |
---|---|
Access the UI shell | docker exec -it app-ui-1 bash |
Add a new node dependency | docker exec -it app-ui-1 npm install <name-of-package> |
Run Node tests | scripts test-ui.sh (from within the src directory) |
Run formatter and linter | npm run lint npm run format (from within the ui directory) |
Debugging while using Parcel
Recall that Parcel’s job is to compile a “production version” of your react application everytime you save. Give this:
- Turn off your autosave (so that you don’t confuse parcel).
- If changes to your react app aren’t being reflected in the UI, try deleting the
build
directory and then making a small change to one of your React files to trigger a rebuild.
4. Database Development Notes
To access your database and issue queries to PostgreSQL, do the following from the command line:
1
docker exec -it app-db-1 bash
Then, once you’re on the database server’s bash shell, connect to the database as follows:
1
psql -U app_user -d app_db
Once you’re on the PostgreSQL shell and connected to the app_db database, you can issue SQL commands. For instance:
1
select * from users;
When you want to exit the psql shell, type: \q
SQL Cheat Sheet
See Lab 8
Rebuilding the Database with Fake Data
To rebuild the database with fake data, issue the following command:
1
docker exec -it app-server-1 poetry run python db/populate.py
III. Submitting Homework 2
You will be submitting Homework 2 by making a pull request that adheres the guidelines below:
- Ensures all of the code quality checks pass
- Ensures all of the tests pass
- Ensures you’re using descriptive commit messages
- Ensures that your pull request summary text is descriptive
A note on GitHub workflows
Every time you make a pull request, a GitHub Workflow is triggered (see the .github/workflows/pr.yml
file). This is a first round check to ensure that code formatting standards have been followed and that all of the tests pass.
This workflow file checks the following:
- Code quality (by invoking
src/scripts/check.sh
) - Tests (by invoking
src/scripts/test-ui.sh
andscripts/test-server.sh
Given this, please ensure that you have already formatted your code and run all of the tests before creating a pull request.
If you made changes to a python file, please run (from src directory):
1
2
bash scripts/fix-python.sh
docker exec -it app-server-1 bash scripts/test-server.sh
If you made changes to an HTML, CSS, or JavaScript file, please run (from the ui directory):
1
2
3
npm run lint
npm run format
npm run check
All checks must pass before your pull request is reviewed.