GithubHelp home page GithubHelp logo

fastapi-with-postgresql-and-unit-tests's Introduction

Fastapi โšก with postgresql and unit tests ๐Ÿš€๏ธ

In this project we will use fastapi to create an api and connect it to postgresql with sqlmodel to finish performing unit tests with pytest. This is an example project that you can download, modify and use to perform your own tests.
Now to execute this project I have used poetry as a virtual environment, which you can download and install according to its documentation, which you can verify in the following link:
You can also use venv as a virtual environment, you just have to install the dependencies ๐Ÿ“ฅ that I list below:
fastapi
pydantic = {extras = ["email"], version = "^2.7.1"}
psycopg2-binary
sqlmodel
faker
pytest
๐Ÿ’ป Now with poetry installed we will execute the following commands, you must open a console that points to the root folder of the project ๐Ÿ“‚.
# This command creates and activates the virtual environment:
poetry shell

# This command installs all dependencies:
poetry install
๐Ÿ’ป Now that we have all the dependencies installed and we can run our project ๐Ÿ“‚, for which we can use one of the following commands:
Fastapi command:
fastapi dev main.py

Uvicorn command:
uvicorn main:app --reload

๐Ÿ’ก Now that we have our project running, we only need to configure the connection to the database, which you must do in 2 files ๐Ÿ“„, one is the api project and the second is the test file.
DATABASE_URL = "YOUR CONNECTION"

Files:
db->database.py
test->users->users->users_test.py
๐Ÿ’ก This project automatically creates the users table, so there is no need to create it manually ๐Ÿ› .
And we are ready to test our api, feel free to test each endpoint of the api ๐Ÿ‘จ๐Ÿปโ€๐Ÿ”ง.

๐Ÿ’ก Finally we need to do our unit testing test, for which we will use pytest ๐Ÿšง. You just need to run the command in the console:
pytest

โœ… Excellent, you are now running your unit tests, but as you can see the prints ๐Ÿ“ข that we have in the test are not shown, if you want to see these prints ๐Ÿ“ข in the console you must execute the following command:
pytest -s

๐Ÿ“ข As an extra bonus I have left the SQLite database connection commented in case you want to test with this database.
๐Ÿ‘จ๐Ÿป I hope this small project is very useful for you to practice with fastapi โšก, postgresql and unit tests. If it has been useful to you, don't forget to follow me and leave the repo a star ๐ŸŒŸ.
See you in another project ๐Ÿ™‹๐Ÿป

fastapi-with-postgresql-and-unit-tests's People

Contributors

egoan82 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.