Skip to content

Commit

Permalink
After module 9: Testing and Deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
codesensei-courses committed Apr 18, 2022
1 parent a3dd928 commit c4de006
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 10 deletions.
17 changes: 7 additions & 10 deletions carsharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
from fastapi import FastAPI
from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from sqlmodel import SQLModel, Session, select
from starlette import status
from sqlmodel import SQLModel
from starlette.responses import JSONResponse
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY

from db import engine, get_session
from db import engine
from routers import cars, web, auth
from routers.cars import BadTripException
from schemas import UserOutput, User

app = FastAPI(title="Car Sharing")
app.include_router(web.router)
Expand Down Expand Up @@ -45,11 +42,11 @@ async def unicorn_exception_handler(request: Request, exc: BadTripException):
)


@app.middleware("http")
async def add_cars_cookie(request: Request, call_next):
response = await call_next(request)
response.set_cookie(key="cars_cookie", value="you_visited_the_carsharing_app")
return response
# @app.middleware("http")
# async def add_cars_cookie(request: Request, call_next):
# response = await call_next(request)
# response.set_cookie(key="cars_cookie", value="you_visited_the_carsharing_app")
# return response


if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
filterwarnings =
ignore::sqlalchemy.exc.SAWarning
44 changes: 44 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
anyio==3.5.0
asgiref==3.5.0
attrs==21.4.0
bcrypt==3.2.0
certifi==2021.10.8
cffi==1.15.0
charset-normalizer==2.0.12
click==8.0.3
dnspython==2.2.0
email-validator==1.1.3
fastapi==0.73.0
h11==0.13.0
httptools==0.2.0
idna==3.3
iniconfig==1.1.1
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
orjson==3.6.7
packaging==21.3
passlib==1.7.4
pluggy==1.0.0
py==1.11.0
pycparser==2.21
pydantic==1.9.0
pyparsing==3.0.8
python-dotenv==0.19.2
python-multipart==0.0.5
PyYAML==5.4.1
requests==2.27.1
six==1.16.0
sniffio==1.2.0
SQLAlchemy==1.4.32
sqlalchemy2-stubs==0.0.2a21
sqlmodel==0.0.6
starlette==0.17.1
tomli==2.0.1
typing_extensions==4.1.1
ujson==4.3.0
urllib3==1.26.8
uvicorn==0.15.0
uvloop==0.16.0
watchgod==0.7
websockets==10.1
Empty file added test/__init__.py
Empty file.
Binary file added test/carsharing.db
Binary file not shown.
35 changes: 35 additions & 0 deletions test/test_add_car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from unittest.mock import Mock
from fastapi.testclient import TestClient

from carsharing import app
from routers.cars import add_car
from schemas import CarInput, User, Car

client = TestClient(app)


def test_add_car():
response = client.post("/api/cars/",
json={
"doors": 7,
"size": "xxl"
}, headers={'Authorization': 'Bearer reindert'}
)
assert response.status_code == 200
car = response.json()
assert car['doors'] == 7
assert car['size'] == 'xxl'


def test_add_car_with_mock_session():
mock_session = Mock()
input = CarInput(doors=2, size="xl")
user = User(username="reindert")
result = add_car(car_input=input, session=mock_session, user=user)

mock_session.add.assert_called_once()
mock_session.commit.assert_called_once()
mock_session.refresh.assert_called_once()
assert isinstance(result, Car)
assert result.doors == 2
assert result.size == "xl"
14 changes: 14 additions & 0 deletions test/test_get_cars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi.testclient import TestClient

from carsharing import app

client = TestClient(app)


def test_get_cars():
response = client.get("/api/cars/")
assert response.status_code == 200
cars = response.json()
assert all(["doors" in c for c in cars])
assert all(["size" in c for c in cars])

11 changes: 11 additions & 0 deletions test/test_home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi.testclient import TestClient

from carsharing import app

client = TestClient(app)


def test_home():
response = client.get("/")
assert response.status_code == 200
assert "Welcome" in response.text

0 comments on commit c4de006

Please sign in to comment.