Coverage for docs_src/sql_databases/sql_app/tests/test_sql_app.py: 100%
29 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi.testclient import TestClient 1abcde
2from sqlalchemy import create_engine 1abcde
3from sqlalchemy.orm import sessionmaker 1abcde
4from sqlalchemy.pool import StaticPool 1abcde
6from ..database import Base 1abcde
7from ..main import app, get_db 1abcde
9SQLALCHEMY_DATABASE_URL = "sqlite://" 1abcde
11engine = create_engine( 1abcde
12 SQLALCHEMY_DATABASE_URL,
13 connect_args={"check_same_thread": False},
14 poolclass=StaticPool,
15)
16TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) 1abcde
19Base.metadata.create_all(bind=engine) 1abcde
22def override_get_db(): 1abcde
23 try: 1abcde
24 db = TestingSessionLocal() 1abcde
25 yield db 1abcde
26 finally:
27 db.close() 1abcde
30app.dependency_overrides[get_db] = override_get_db 1abcde
32client = TestClient(app) 1abcde
35def test_create_user(): 1abcde
36 response = client.post( 1abcde
37 "/users/",
38 json={"email": "deadpool@example.com", "password": "chimichangas4life"},
39 )
40 assert response.status_code == 200, response.text 1abcde
41 data = response.json() 1abcde
42 assert data["email"] == "deadpool@example.com" 1abcde
43 assert "id" in data 1abcde
44 user_id = data["id"] 1abcde
46 response = client.get(f"/users/{user_id}") 1abcde
47 assert response.status_code == 200, response.text 1abcde
48 data = response.json() 1abcde
49 assert data["email"] == "deadpool@example.com" 1abcde
50 assert data["id"] == user_id 1abcde