Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #17

Merged
merged 24 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add contains sql operator
  • Loading branch information
LuisLuii committed Mar 6, 2022
commit a1ede529b3883a81586baf57bafee96cc3936d15
1 change: 1 addition & 0 deletions src/fastapi_quickcrud/misc/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class MatchingPatternInStringBase(StrEnum):
case_sensitive = auto()
not_case_insensitive = auto()
not_case_sensitive = auto()
contains = auto()


class PGSQLMatchingPattern(StrEnum):
Expand Down
3 changes: 3 additions & 0 deletions src/fastapi_quickcrud/misc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ def sqlalchemy_to_pydantic(
MatchingPatternInStringBase.not_case_sensitive:
lambda field, values: or_(field.not_like(value) for value in values),

MatchingPatternInStringBase.contains:
lambda field, values: or_(field.contains(value) for value in values),

PGSQLMatchingPatternInString.similar_to:
lambda field, values: or_(field.op("SIMILAR TO")(value) for value in values),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import json

from fastapi import FastAPI
from sqlalchemy import Column, Integer, \
ForeignKey, Table, String
from sqlalchemy.orm import declarative_base, sessionmaker, relationship

from src.fastapi_quickcrud.misc.type import SqlType
from src.fastapi_quickcrud.crud_router import crud_router_builder

app = FastAPI()

Base = declarative_base()
metadata = Base.metadata

from sqlalchemy import create_engine

from sqlalchemy.pool import StaticPool
engine = create_engine('sqlite://', echo=True,
connect_args={"check_same_thread": False}, pool_recycle=7200, poolclass=StaticPool)
session = sessionmaker(autocommit=False, autoflush=False, bind=engine)


def get_transaction_session():
try:
db = session()
yield db
finally:
db.close()


class User(Base):
__tablename__ = 'test_users'
id = Column(Integer, primary_key=True, autoincrement=True, unique=True)
name = Column(String, nullable=False)
email = Column(String, nullable=False)


friend = Table(
'test_friend', Base.metadata,
Column('id', ForeignKey('test_users.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False),
Column('friend_name', String, nullable=False)
)

crud_route_1 = crud_router_builder(db_session=get_transaction_session,
db_model=User,
prefix="/user",
tags=["User"]
)
crud_route_2 = crud_router_builder(db_session=get_transaction_session,
db_model=friend,
prefix="/friend",
tags=["friend"]
)

from starlette.testclient import TestClient

[app.include_router(i) for i in
[crud_route_1,crud_route_2]]

client = TestClient(app)


def test_():
headers = {
'accept': '*/*',
'Content-Type': 'application/json',
}
data = '[{"id": 1,"name": "string","email": "string"}]'
response = client.post('/user', headers=headers, data=data)
assert response.status_code == 201
assert response.json() == [{"id": 1,"name": "string","email": "string"}]


data =' [{"id": 1,"friend_name": "string"}]'
response = client.post('/friend', headers=headers, data = data)
assert response.status_code == 201
assert response.json() == [
{
"id": 1,
"friend_name": "string"
}
]

response = client.get('/friend?join_foreign_table=test_users', headers=headers)
assert response.status_code == 200
assert response.json() == [
{
"test_users_foreign": [
{
"id": 1,
"name": "string",
"email": "string"
}
],
"id": 1,
"friend_name": "string"
}
]



def setup_module(module):
User.__table__.create(module.engine, checkfirst=True)
friend.create(module.engine, checkfirst=True)


def teardown_module(module):
friend.drop(engine, checkfirst=True)
User.__table__.drop(engine, checkfirst=True)
Loading