Skip to content

Commit a1c4326

Browse files
authored
Dev (#3)
* feat: switch venv management to poetry * fix: using asynctest * fix: test_sqlizer
1 parent 5433370 commit a1c4326

File tree

16 files changed

+76
-67
lines changed

16 files changed

+76
-67
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
.vscode
2-
31
__pycache__
42

53
build
64
dist
75
fastapi_efficient_sql.egg-info
86

9-
Pipfile.lock
7+
Pipfile.lock
8+
poetry.lock

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.7.9

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 BryanLee
3+
Copyright (c) 2022-2023 BryanLee
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Pipfile

Lines changed: 0 additions & 22 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
Installed as package by `pip install fastapi-efficient-sql`
44

5-
Install developing requirements by `pipenv install --skip-lock --dev` or `pip install -r requirements-dev.txt`
5+
Install developing requirements by `pyenv local 3.7.9`, `poetry env use 3.7.9`, `poetry shell` and `pip install -r requirements-dev.txt`
6+
7+
Run demo service by `python -m examples.service`
68

79
Run unittest by `pytest -sv`
810

examples/service/models/demo/account.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from fastapi_esql import BaseModel
22
from tortoise import fields
3-
from tortoise.contrib.pydantic import pydantic_model_creator
43

54
from examples.service.constants.enums import GenderEnum, LocaleEnum
65

@@ -13,6 +12,9 @@ class Account(BaseModel):
1312
`gender` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0:unknown, 1:male, 2:female',
1413
`name` varchar(32) NOT NULL DEFAULT '',
1514
`locale` varchar(5) NOT NULL,
15+
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
16+
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
17+
`extend` json NOT NULL,
1618
PRIMARY KEY (`id`),
1719
KEY `created_at-idx` (`created_at`),
1820
KEY `updated_at-idx` (`updated_at`)
@@ -27,6 +29,3 @@ class Account(BaseModel):
2729
class Meta:
2830
app = "demo"
2931
table = "account"
30-
31-
32-
AccountIn = pydantic_model_creator(Account, name="AccountIn", exclude=("active"), exclude_readonly=True)

fastapi_esql/utils/sqlizer.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def resolve_wheres(
5050
model: Optional[Model] = None,
5151
) -> str:
5252
if not model and not isinstance(wheres, str):
53-
raise WrongParamsError("Parameter `wheres` only support str type if no model passed")
53+
raise WrongParamsError("Parameter `wheres` only supports str if no model exists")
5454

5555
if isinstance(wheres, str):
5656
return wheres
@@ -61,10 +61,10 @@ def resolve_wheres(
6161
elif isinstance(wheres, list):
6262
qs = [q for q in wheres if isinstance(q, Q)]
6363
else:
64-
raise WrongParamsError("Parameter `wheres` only supports str, dict and list type")
64+
raise WrongParamsError("Parameter `wheres` only support str, Q, Dict[str, Any] and List[Q]")
6565

6666
if not qs:
67-
raise QsParsingError("Parsing `wheres` for qs failed!")
67+
raise QsParsingError("Parsing `wheres` for QuerySet failed")
6868

6969
modifier = QueryModifier()
7070
for q in qs:
@@ -122,9 +122,9 @@ def select_custom_fields(
122122
model: Optional[Model] = None,
123123
) -> Optional[str]:
124124
if not all([table, fields, wheres]):
125-
raise WrongParamsError("Please check your params")
125+
raise WrongParamsError("Parameters `table`, `fields`, `wheres` are required")
126126
if having and not groups:
127-
raise WrongParamsError("Parameter `groups` shoud be no empty when `having` isn't")
127+
raise WrongParamsError("Parameter `groups` shoud not be empty if `having` exists")
128128

129129
group_by = f" GROUP BY {', '.join(groups)}" if groups else ""
130130
having_ = f" HAVING {having}" if having else ""
@@ -160,9 +160,11 @@ def update_json_field(
160160
model: Optional[Model] = None,
161161
) -> Optional[str]:
162162
if not all([table, json_field, wheres]):
163-
raise WrongParamsError("Please check your params")
163+
raise WrongParamsError("Parameters `table`, `json_field`, `wheres` are required")
164164
if not any([merge_dict, path_value_dict, remove_paths]):
165-
raise WrongParamsError("Please check your params")
165+
raise WrongParamsError(
166+
"At least one no empty parameter is required between `merge_dict`, `path_value_dict` and `remove_paths`"
167+
)
166168

167169
json_obj = f"COALESCE({json_field}, '{json_type()}')"
168170
if remove_paths:
@@ -195,7 +197,7 @@ def upsert_on_duplicate(
195197
using_values: bool = False,
196198
) -> Optional[str]:
197199
if not all([table, dicts, insert_fields, upsert_fields]):
198-
raise WrongParamsError("Please check your params")
200+
raise WrongParamsError("Parameters `table`, `dicts`, `insert_fields`, `upsert_fields` are required")
199201

200202
values = [
201203
f" ({', '.join(cls.sqlize_value(d.get(f)) for f in insert_fields)})"
@@ -236,8 +238,10 @@ def insert_into_select(
236238
to_table: Optional[str] = None,
237239
model: Optional[Model] = None,
238240
) -> Optional[str]:
239-
if not all([table, wheres] or not any([remain_fields, assign_field_dict])):
240-
raise WrongParamsError("Please check your params")
241+
if not all([table, wheres]):
242+
raise WrongParamsError("Parameters `table`, `wheres` are required")
243+
if not any([remain_fields, assign_field_dict]):
244+
raise WrongParamsError("At least one no empty parameter is required between `remain_fields` and `assign_field_dict`")
241245

242246
fields = [*remain_fields]
243247
assign_fields = []
@@ -263,7 +267,7 @@ def build_fly_table(
263267
using_values: bool = True,
264268
) -> Optional[str]:
265269
if not all([dicts, fields]):
266-
raise WrongParamsError("Please check your params")
270+
raise WrongParamsError("Parameters `dicts`, `fields` are required")
267271

268272
if using_values:
269273
rows = [
@@ -297,7 +301,7 @@ def bulk_update_from_dicts(
297301
using_values: bool = True,
298302
) -> Optional[str]:
299303
if not all([table, dicts, join_fields, update_fields]):
300-
raise WrongParamsError("Please check your params")
304+
raise WrongParamsError("Parameters `table`, `dicts`, `join_fields`, `update_fields` are required")
301305

302306
joins = [f"{table}.{jf}=tmp.{jf}" for jf in join_fields]
303307
updates = [f"{table}.{uf}=tmp.{uf}" for uf in update_fields]

pyproject.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[tool.poetry]
2+
name = "fastapi-efficient-sql"
3+
version = "0.0.8"
4+
description = "Generate bulk DML SQL and execute them based on Tortoise ORM and mysql8.0+, and integrated with FastAPI."
5+
authors = ["BryanLee <bryanlee@126.com>"]
6+
license = "MIT"
7+
readme = "README.md"
8+
packages = [{include = "fastapi_esql"}]
9+
10+
[tool.poetry.dependencies]
11+
python = "^3.7"
12+
13+
14+
[build-system]
15+
requires = ["poetry-core"]
16+
build-backend = "poetry.core.masonry.api"

requirements-dev.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
aiomysql
22
asynctest
3-
autopep8
43
cryptography
54
faker
65
fastapi
76
pytest
87
pytest-asyncio
9-
tortoise-orm
8+
tortoise-orm>=0.16.17
109
uvicorn

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
tortoise-orm
1+
tortoise-orm>=0.16.17

0 commit comments

Comments
 (0)