Coverage for docs_src/sql_databases/sql_app_py39/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 1abcd

2from sqlalchemy import create_engine 1abcd

3from sqlalchemy.orm import sessionmaker 1abcd

4 

5from ..database import Base 1abcd

6from ..main import app, get_db 1abcd

7 

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

9 

10engine = create_engine( 1abcd

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

12) 

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

14 

15 

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

17 

18 

19def override_get_db(): 1abcd

20 try: 1abcd

21 db = TestingSessionLocal() 1abcd

22 yield db 1abcd

23 finally: 

24 db.close() 1abcd

25 

26 

27app.dependency_overrides[get_db] = override_get_db 1abcd

28 

29client = TestClient(app) 1abcd

30 

31 

32def test_create_user(): 1abcd

33 response = client.post( 1abcd

34 "/users/", 

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

36 ) 

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

38 data = response.json() 1abcd

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

40 assert "id" in data 1abcd

41 user_id = data["id"] 1abcd

42 

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

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

45 data = response.json() 1abcd

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

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