|
| 1 | +import json |
| 2 | + |
| 3 | +from fastapi import FastAPI |
| 4 | +from sqlalchemy import Column, Integer, \ |
| 5 | + ForeignKey, Table, String |
| 6 | +from sqlalchemy.orm import declarative_base, sessionmaker, relationship |
| 7 | + |
| 8 | +from src.fastapi_quickcrud.misc.type import SqlType |
| 9 | +from src.fastapi_quickcrud.crud_router import crud_router_builder |
| 10 | + |
| 11 | +app = FastAPI() |
| 12 | + |
| 13 | +Base = declarative_base() |
| 14 | +metadata = Base.metadata |
| 15 | + |
| 16 | +from sqlalchemy import create_engine |
| 17 | + |
| 18 | +from sqlalchemy.pool import StaticPool |
| 19 | +engine = create_engine('sqlite://', echo=True, |
| 20 | + connect_args={"check_same_thread": False}, pool_recycle=7200, poolclass=StaticPool) |
| 21 | +session = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
| 22 | + |
| 23 | + |
| 24 | +def get_transaction_session(): |
| 25 | + try: |
| 26 | + db = session() |
| 27 | + yield db |
| 28 | + finally: |
| 29 | + db.close() |
| 30 | + |
| 31 | + |
| 32 | +class User(Base): |
| 33 | + __tablename__ = 'test_users' |
| 34 | + id = Column(Integer, primary_key=True, autoincrement=True, unique=True) |
| 35 | + name = Column(String, nullable=False) |
| 36 | + email = Column(String, nullable=False) |
| 37 | + |
| 38 | + |
| 39 | +friend = Table( |
| 40 | + 'test_friend', Base.metadata, |
| 41 | + Column('id', ForeignKey('test_users.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False), |
| 42 | + Column('friend_name', String, nullable=False) |
| 43 | +) |
| 44 | + |
| 45 | +crud_route_1 = crud_router_builder(db_session=get_transaction_session, |
| 46 | + db_model=User, |
| 47 | + prefix="/user", |
| 48 | + tags=["User"] |
| 49 | + ) |
| 50 | +crud_route_2 = crud_router_builder(db_session=get_transaction_session, |
| 51 | + db_model=friend, |
| 52 | + prefix="/friend", |
| 53 | + tags=["friend"] |
| 54 | + ) |
| 55 | + |
| 56 | +from starlette.testclient import TestClient |
| 57 | + |
| 58 | +[app.include_router(i) for i in |
| 59 | + [crud_route_1,crud_route_2]] |
| 60 | + |
| 61 | +client = TestClient(app) |
| 62 | + |
| 63 | + |
| 64 | +def test_(): |
| 65 | + headers = { |
| 66 | + 'accept': '*/*', |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + } |
| 69 | + data = '[{"id": 1,"name": "string","email": "string"}]' |
| 70 | + response = client.post('/user', headers=headers, data=data) |
| 71 | + assert response.status_code == 201 |
| 72 | + assert response.json() == [{"id": 1,"name": "string","email": "string"}] |
| 73 | + |
| 74 | + |
| 75 | + data =' [{"id": 1,"friend_name": "string"}]' |
| 76 | + response = client.post('/friend', headers=headers, data = data) |
| 77 | + assert response.status_code == 201 |
| 78 | + assert response.json() == [ |
| 79 | + { |
| 80 | + "id": 1, |
| 81 | + "friend_name": "string" |
| 82 | + } |
| 83 | +] |
| 84 | + |
| 85 | + response = client.get('/friend?join_foreign_table=test_users', headers=headers) |
| 86 | + assert response.status_code == 200 |
| 87 | + assert response.json() == [ |
| 88 | + { |
| 89 | + "test_users_foreign": [ |
| 90 | + { |
| 91 | + "id": 1, |
| 92 | + "name": "string", |
| 93 | + "email": "string" |
| 94 | + } |
| 95 | + ], |
| 96 | + "id": 1, |
| 97 | + "friend_name": "string" |
| 98 | + } |
| 99 | +] |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +def setup_module(module): |
| 104 | + User.__table__.create(module.engine, checkfirst=True) |
| 105 | + friend.create(module.engine, checkfirst=True) |
| 106 | + |
| 107 | + |
| 108 | +def teardown_module(module): |
| 109 | + friend.drop(engine, checkfirst=True) |
| 110 | + User.__table__.drop(engine, checkfirst=True) |
0 commit comments