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

1from fastapi.testclient import TestClient 1abcde

2from sqlalchemy import create_engine 1abcde

3from sqlalchemy.orm import sessionmaker 1abcde

4from sqlalchemy.pool import StaticPool 1abcde

5 

6from ..database import Base 1abcde

7from ..main import app, get_db 1abcde

8 

9SQLALCHEMY_DATABASE_URL = "sqlite://" 1abcde

10 

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

17 

18 

19Base.metadata.create_all(bind=engine) 1abcde

20 

21 

22def override_get_db(): 1abcde

23 try: 1abcde

24 db = TestingSessionLocal() 1abcde

25 yield db 1abcde

26 finally: 

27 db.close() 1abcde

28 

29 

30app.dependency_overrides[get_db] = override_get_db 1abcde

31 

32client = TestClient(app) 1abcde

33 

34 

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

45 

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