Coverage for docs_src/sql_databases/sql_app_py310/tests/test_sql_app.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi.testclient import TestClient 1abc

2from sqlalchemy import create_engine 1abc

3from sqlalchemy.orm import sessionmaker 1abc

4 

5from ..database import Base 1abc

6from ..main import app, get_db 1abc

7 

8SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" 1abc

9 

10engine = create_engine( 1abc

11 SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} 

12) 

13TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) 1abc

14 

15 

16Base.metadata.create_all(bind=engine) 1abc

17 

18 

19def override_get_db(): 1abc

20 try: 1abc

21 db = TestingSessionLocal() 1abc

22 yield db 1abc

23 finally: 

24 db.close() 1abc

25 

26 

27app.dependency_overrides[get_db] = override_get_db 1abc

28 

29client = TestClient(app) 1abc

30 

31 

32def test_create_user(): 1abc

33 response = client.post( 1abc

34 "/users/", 

35 json={"email": "deadpool@example.com", "password": "chimichangas4life"}, 

36 ) 

37 assert response.status_code == 200, response.text 1abc

38 data = response.json() 1abc

39 assert data["email"] == "deadpool@example.com" 1abc

40 assert "id" in data 1abc

41 user_id = data["id"] 1abc

42 

43 response = client.get(f"/users/{user_id}") 1abc

44 assert response.status_code == 200, response.text 1abc

45 data = response.json() 1abc

46 assert data["email"] == "deadpool@example.com" 1abc

47 assert data["id"] == user_id 1abc