-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
This file was deleted.
This file was deleted.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from fastapi import FastAPI | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get('/') | ||
def read_root(): | ||
return {'message': 'Olá Mundo!'} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
[tool.poetry] | ||
name = "fast-zero" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Your Name <[email protected]>"] | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = "3.12.*" # ou 3.11.* | ||
fastapi = "^0.109.2" | ||
uvicorn = "^0.27.0.post1" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "^8.0.0" | ||
pytest-cov = "^4.1.0" | ||
taskipy = "^1.12.2" | ||
ruff = "^0.2.1" | ||
httpx = "^0.26.0" | ||
|
||
[tool.ruff] | ||
line-length = 79 | ||
extend-exclude = ['migrations'] | ||
|
||
[tool.ruff.lint] | ||
preview = true | ||
select = ['I', 'F', 'E', 'W', 'PL', 'PT'] | ||
|
||
[tool.ruff.format] | ||
preview = true | ||
quote-style = 'single' | ||
|
||
[tool.pytest.ini_options] | ||
pythonpath = "." | ||
addopts = '-p no:warnings' | ||
|
||
[tool.taskipy.tasks] | ||
lint = 'ruff check .; ruff check . --diff' | ||
format = 'ruff check . --fix; ruff format .' | ||
run = 'uvicorn fast_zero.app:app --reload' | ||
pre_test = 'task lint' | ||
test = 'pytest -s -x --cov=fast_zero -vv' | ||
post_test = 'coverage html' | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from fastapi.testclient import TestClient # (1)! | ||
|
||
from fast_zero.app import app # (2)! | ||
|
||
client = TestClient(app) # (3)! | ||
|
||
|
||
from http import HTTPStatus # (6)! | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
from fast_zero.app import app | ||
|
||
|
||
def test_root_deve_retornar_ok_e_ola_mundo(): # (1)! | ||
client = TestClient(app) # (2)! | ||
|
||
response = client.get('/') # (3)! | ||
|
||
assert response.status_code == HTTPStatus.OK # (4)! | ||
assert response.json() == {'message': 'Olá Mundo!'} # (5)! | ||
|
||
|
||
from http import HTTPStatus | ||
|
||
from fastapi.testclient import TestClient | ||
|
||
from fast_zero.app import app | ||
|
||
|
||
def test_root_deve_retornar_ok_e_ola_mundo(): | ||
client = TestClient(app) # Arrange | ||
|
||
response = client.get('/') # Act | ||
|
||
assert response.status_code == HTTPStatus.OK # Assert | ||
assert response.json() == {'message': 'Olá Mundo!'} # Assert |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from fastapi import FastAPI | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.post('/users/') | ||
def create_user(): | ||
... | ||
|
||
|
||
from http import HTTPStatus | ||
|
||
# ... | ||
|
||
@app.post('/users/', status_code=HTTPStatus.CREATED) | ||
def create_user(): | ||
... | ||
|
||
|
||
from fast_zero.schemas import Message, UserSchema | ||
|
||
# ... | ||
|
||
@app.post('/users/', status_code=HTTPStatus.CREATED) | ||
def create_user(user: UserSchema): | ||
... | ||
|
||
|
||
from fast_zero.schemas import Message, UserPublic, UserSchema | ||
|
||
# Código omitido | ||
|
||
@app.post('/users/', status_code=HTTPStatus.CREATED, response_model=UserPublic) | ||
def create_user(user: UserSchema): | ||
... | ||
|
||
|
||
@app.post('/users/', status_code=HTTPStatus.CREATED, response_model=UserPublic) | ||
def create_user(user: UserSchema): | ||
return user | ||
|
||
|
||
from fast_zero.schemas import Message, UserDB, UserPublic, UserSchema | ||
|
||
# código omitido | ||
|
||
database = [] #(1)! | ||
|
||
# código omitido | ||
|
||
@app.post('/users/', status_code=HTTPStatus.CREATED, response_model=UserPublic) | ||
def create_user(user: UserSchema): | ||
user_with_id = UserDB(**user.model_dump(), id=len(database) + 1) #(2)! | ||
|
||
database.append(user_with_id) | ||
|
||
return user_with_id | ||
|
||
|
||
from fast_zero.schemas import Message, UserDB, UserList, UserPublic, UserSchema | ||
|
||
# código omitido | ||
|
||
@app.get('/users/', response_model=UserList) | ||
def read_users(): | ||
return {'users': database} | ||
|
||
|
||
from fastapi import FastAPI, HTTPException | ||
|
||
# ... | ||
|
||
@app.put('/users/{user_id}', response_model=UserPublic) | ||
def update_user(user_id: int, user: UserSchema): | ||
if user_id > len(database) or user_id < 1: #(1)! | ||
raise HTTPException( | ||
status_code=HTTPStatus.NOT_FOUND, detail='User not found' | ||
) #(2)! | ||
|
||
user_with_id = UserDB(**user.model_dump(), id=user_id) | ||
database[user_id - 1] = user_with_id #(3)! | ||
|
||
return user_with_id | ||
|
||
|
||
@app.delete('/users/{user_id}', response_model=Message) | ||
def delete_user(user_id: int): | ||
if user_id > len(database) or user_id < 1: | ||
raise HTTPException( | ||
status_code=HTTPStatus.NOT_FOUND, detail='User not found' | ||
) | ||
|
||
del database[user_id - 1] | ||
|
||
return {'message': 'User deleted'} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from pydantic import BaseModel, EmailStr | ||
|
||
|
||
class Message(BaseModel): | ||
message: str | ||
|
||
|
||
class UserSchema(BaseModel): | ||
username: str | ||
email: EmailStr | ||
password: str | ||
|
||
|
||
class UserPublic(BaseModel): | ||
id: int | ||
username: str | ||
email: EmailStr | ||
|
||
|
||
class UserDB(UserSchema): | ||
id: int | ||
|
||
|
||
class UserList(BaseModel): | ||
users: list[UserPublic] | ||
|
||
|
||
class UserSchema(BaseModel): | ||
username: str | ||
email: str | ||
password: str | ||
|
||
|
||
class UserPublic(BaseModel): | ||
username: str | ||
email: str |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
from fast_zero.app import app | ||
|
||
|
||
@pytest.fixture() | ||
def client(): | ||
return TestClient(app) |