Skip to content

Commit

Permalink
tests _fast_crud_column,_antd_crud_column
Browse files Browse the repository at this point in the history
  • Loading branch information
zy7y committed Jul 14, 2024
1 parent 741606a commit 6ac7920
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
35 changes: 18 additions & 17 deletions dfs_generate/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ def _fast_crud_column(column):
fmt = f"{name}: {{ title: '{title}', type: 'text', search: {{ show: true }}}}"
return fmt


def _antd_crud_column(column):
name = to_camel(column["COLUMN_NAME"])
title = column["COLUMN_COMMENT"] or name
fmt = f"{{ title: '{title}', dataIndex: '{name}', key: '{name}', supportSearch: true}}"
return fmt


class Conversion:
def __init__(self, table_name, columns, uri):
self.table_name = table_name
Expand Down Expand Up @@ -83,14 +85,14 @@ def schema(self):
+ 'model_config = {"alias_generator": to_camel, "populate_by_name": True}'
)
return (
"\n".join(imports)
+ "\n\n"
+ RESPONSE_SCHEMA
+ "\n\n"
+ head
+ "\n"
+ "\n".join(fields)
+ "\n"
"\n".join(imports)
+ "\n\n"
+ RESPONSE_SCHEMA
+ "\n\n"
+ head
+ "\n"
+ "\n".join(fields)
+ "\n"
)

def router(self):
Expand All @@ -104,7 +106,7 @@ def vue_api_ts(self):

def vue_crud_ts(self):
columns = (
"{" + ",".join(_fast_crud_column(column) for column in self.columns) + "}"
"{" + ",".join(_fast_crud_column(column) for column in self.columns) + "}"
)
return VUE_CRUD_TS % columns

Expand All @@ -118,7 +120,6 @@ def react_crud_tsx(self):
return REACT_CRUD_TSX % (self.table, columns)

def gencode(self):
self.react_crud_tsx()
return {
"model.py": self.model(),
"dao.py": self.dao(),
Expand Down Expand Up @@ -189,7 +190,7 @@ def _sqlmodel_field_repr(column, imports):

name = column["COLUMN_NAME"]
if kwargs.get("default", "") is None and "func.now" not in kwargs.get(
"sa_column", ""
"sa_column", ""
):
imports.add("from typing import Optional")
field_type = f"Optional[{_type}]"
Expand Down Expand Up @@ -295,12 +296,12 @@ def model(self):
if " " + field not in fields:
fields.append(" " + field)
return (
"\n".join(imports)
+ "\n\n"
+ head
+ "\n"
+ "\n".join(fields)
+ f"\n{' ' * 4}class Meta:\n{' ' * 8}table='{self.table_name}'"
"\n".join(imports)
+ "\n\n"
+ head
+ "\n"
+ "\n".join(fields)
+ f"\n{' ' * 4}class Meta:\n{' ' * 8}table='{self.table_name}'"
)

def dao(self):
Expand Down
40 changes: 33 additions & 7 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
_pydantic_field,
_sqlmodel_field_repr,
_tortoise_field_repr,
_fast_crud_column,
_antd_crud_column
)

# 假设的列数据,用于模拟从数据库获取的信息
Expand Down Expand Up @@ -77,8 +79,8 @@ def test_tortoise_conversion_model(tortoise_conversion_fixture):
assert "class Users(Model):" in model_code
assert 'id = fields.IntField(description="主键ID", pk=True)' in model_code
assert (
'name = fields.CharField(null=True, max_length=100, description="姓名")'
in model_code
'name = fields.CharField(null=True, max_length=100, description="姓名")'
in model_code
)


Expand All @@ -94,11 +96,11 @@ def test_sqlmodel_field_repr():
column = MOCK_COLUMNS[0] # 使用id字段作为测试
imports, field_code = set(), _sqlmodel_field_repr(column, set())
assert (
'id: Optional[int] = Field(default=None,primary_key=True,description="主键ID")'
== field_code
'id: Optional[int] = Field(default=None,primary_key=True,description="主键ID")'
== field_code
)
assert (
"from datetime import datetime" not in imports
"from datetime import datetime" not in imports
) # id字段不应触发默认时间戳逻辑


Expand All @@ -107,6 +109,30 @@ def test_tortoise_field_repr():
column = MOCK_COLUMNS[1]
field_code = _tortoise_field_repr(column)
assert (
'name = fields.CharField(null=True, max_length=100, description="姓名")'
== field_code
'name = fields.CharField(null=True, max_length=100, description="姓名")'
== field_code
)


@pytest.mark.parametrize("column_data,expected", [
({"COLUMN_NAME": "user_name", "COLUMN_COMMENT": "User's Name"},
"userName: { title: 'User's Name', type: 'text', search: { show: true }}"),
({"COLUMN_NAME": "is_active", "COLUMN_COMMENT": ""},
"isActive: { title: 'isActive', type: 'text', search: { show: true }}"),
({"COLUMN_NAME": "last_login_dt", "COLUMN_COMMENT": "Last Login Date & Time"},
"lastLoginDt: { title: 'Last Login Date & Time', type: 'text', search: { show: true }}"),
])
def test_fast_crud_column(column_data, expected):
assert _fast_crud_column(column_data) == expected


@pytest.mark.parametrize("column_data,expected", [
({"COLUMN_NAME": "user_name", "COLUMN_COMMENT": "User's Name"},
"{ title: 'User's Name', dataIndex: 'userName', key: 'userName', supportSearch: true}"),
({"COLUMN_NAME": "is_active", "COLUMN_COMMENT": ""},
"{ title: 'isActive', dataIndex: 'isActive', key: 'isActive', supportSearch: true}"),
({"COLUMN_NAME": "last_login_dt", "COLUMN_COMMENT": "Last Login Date & Time"},
"{ title: 'Last Login Date & Time', dataIndex: 'lastLoginDt', key: 'lastLoginDt', supportSearch: true}"),
])
def test_antd_crud_column(column_data, expected):
assert _antd_crud_column(column_data) == expected

0 comments on commit 6ac7920

Please sign in to comment.