Backend Test for Insait
This README provides an overview of the Flask app, including how to build, run, and manage the application using Docker, as well as database migrations. Additionally, it provides details on how to use the associated Makefile
for ease of development.
Before running the Flask app in Docker, make sure you have the following installed on your machine:
- Docker
- Docker Compose
If you don't have make
installed, you can still execute the individual Docker commands as described below.
To set up and run the Flask app in your local environment, follow the steps below.
git clone [email protected]:savannahtech/insait-ibrahim.git Insight-Backend-Test
cd Insight-Backend-Test
Create a .env file in the root directory of the project. A sample is provided in the .env.example
with the required keys
The following command are to be executed one after the order.
The original command are being abstracted by using make
. This makes the commands simple and easy to remember
The Makefile simplifies the process of interacting with Docker Compose and Alembic for managing your Flask app. Below are the available commands you can run with make.
Note: If you do not have make installed, you can always copy the Docker commands from the
Makefile
and run them directly.
The first step after cloning the repo and creating .env
file is to build the docker image. Run the command below in the terminal to start the building process.
make build
If you do not have
make
installed, run the command below instead
docker compose -f docker-compose.yml build
Once the docker image has been built with the command above, you can run it with the command below
make run
If you do not have
make
installed, run the command below instead
docker compose -f docker-compose.yml up --no-build --abort-on-container-exit --remove-orphans
Instead of running two command to build and run the image, it can be done with a single command instead. This command will build the image and run it automatically after building
make build-run
If you do not have
make
installed, run the command below instead
docker compose -f docker-compose.yml up --build
Once the docker image is running, you can access the /ask
endpoint by making a POST request to http://localhost:8000/ask
A sample payload is show below
{
"question": "In 250 characters, what is Django in Python?"
}
A sample response is shown below
{
"answer": "Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It promotes the use of the Model-View-Template (MVT) architecture, providing built-in features like an admin panel, ORM, and security tools for developers.",
"created_at": "Fri, 20 Dec 2024 16:00:45 GMT",
"id": 4,
"question": "In 250 characters, what is Django?"
}
If any changes were make to the Question
class inside the insait/models.py
file, and the changes are to reflect in the database, then the command below is what is needed.
Before running the command, a migration message should be added to the
Makefile
(if using themak
approach). The migration message will replace"Initial migration"
that is inside theMakefile
. The migration message should be de detailed
make migrate
If you do not have
make
installed, run the command below instead, while replacing"Initial migration"
with the intended migration message
docker-compose exec flask-app alembic revision --autogenerate -m "Initial migration"
The repo has 3 test cases included. To run these tests ore any other test added to the insait/tes_app.py
, the command below can be used
make test
If you do not have
make
installed, run the command below instead
docker-compose exec flask-app pytest
To stop the container when it's no longer needed, run the command below
make down
If you do not have
make
installed, run the command below instead
docker compose -f docker-compose.yml down --remove-orphans