Skip to content

Commit

Permalink
fix _get_dialects - properties (#1859)
Browse files Browse the repository at this point in the history
* fix `_get_dialects`

---------

Co-authored-by: Waket Zheng <[email protected]>
  • Loading branch information
markus-96 and waketzheng authored Jan 23, 2025
1 parent 948ccdb commit 415d1ae
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed
^^^^^
- Rename pypika to pypika_tortoise for fixing package name conflict (#1829)
- Concurrent connection pool initialization (#1825)
- `_get_dialects`: support properties (#1859)

Changed
^^^^^^^
Expand Down
10 changes: 8 additions & 2 deletions tests/test_early_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ async def test_early_init(self):
"description": "The Event NAME",
"docstring": "The Event NAME\nIt's pretty important",
"constraints": {"max_length": 255},
"db_field_types": {"": "VARCHAR(255)"},
"db_field_types": {
"": "VARCHAR(255)",
"oracle": "NVARCHAR2(255)",
},
},
{
"name": "created_at",
Expand Down Expand Up @@ -252,7 +255,10 @@ async def test_early_init(self):
"name": "name",
"field_type": "CharField",
"db_column": "name",
"db_field_types": {"": "VARCHAR(255)"},
"db_field_types": {
"": "VARCHAR(255)",
"oracle": "NVARCHAR2(255)",
},
"python_type": "str",
"generated": False,
"nullable": False,
Expand Down
65 changes: 52 additions & 13 deletions tests/utils/test_describe_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ def test_describe_model_straight(self):
"name": "chars",
"field_type": "CharField",
"db_column": "chars",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": "str",
"generated": False,
"nullable": False,
Expand All @@ -186,7 +189,10 @@ def test_describe_model_straight(self):
"name": "blip",
"field_type": "CharField",
"db_column": "blip",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": "str",
"generated": False,
"nullable": False,
Expand All @@ -201,7 +207,10 @@ def test_describe_model_straight(self):
"name": "nullable",
"field_type": "CharField",
"db_column": "nullable",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": "str",
"generated": False,
"nullable": True,
Expand Down Expand Up @@ -391,7 +400,10 @@ def test_describe_model_straight_native(self):
"name": "chars",
"field_type": fields.CharField,
"db_column": "chars",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": str,
"generated": False,
"nullable": False,
Expand All @@ -406,7 +418,10 @@ def test_describe_model_straight_native(self):
"name": "blip",
"field_type": fields.CharField,
"db_column": "blip",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": str,
"generated": False,
"nullable": False,
Expand All @@ -421,7 +436,10 @@ def test_describe_model_straight_native(self):
"name": "nullable",
"field_type": fields.CharField,
"db_column": "nullable",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": str,
"generated": False,
"nullable": True,
Expand Down Expand Up @@ -611,7 +629,10 @@ def test_describe_model_source(self):
"name": "chars",
"field_type": "CharField",
"db_column": "some_chars_table",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": "str",
"generated": False,
"nullable": False,
Expand All @@ -626,7 +647,10 @@ def test_describe_model_source(self):
"name": "blip",
"field_type": "CharField",
"db_column": "da_blip",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": "str",
"generated": False,
"nullable": False,
Expand All @@ -641,7 +665,10 @@ def test_describe_model_source(self):
"name": "nullable",
"field_type": "CharField",
"db_column": "some_nullable",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": "str",
"generated": False,
"nullable": True,
Expand Down Expand Up @@ -831,7 +858,10 @@ def test_describe_model_source_native(self):
"name": "chars",
"field_type": fields.CharField,
"db_column": "some_chars_table",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": str,
"generated": False,
"nullable": False,
Expand All @@ -846,7 +876,10 @@ def test_describe_model_source_native(self):
"name": "blip",
"field_type": fields.CharField,
"db_column": "da_blip",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": str,
"generated": False,
"nullable": False,
Expand All @@ -861,7 +894,10 @@ def test_describe_model_source_native(self):
"name": "nullable",
"field_type": fields.CharField,
"db_column": "some_nullable",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"python_type": str,
"generated": False,
"nullable": True,
Expand Down Expand Up @@ -1180,7 +1216,10 @@ def test_describe_model_uuidpk_relatednull(self):
"data_fields": [
{
"db_column": "name",
"db_field_types": {"": "VARCHAR(50)"},
"db_field_types": {
"": "VARCHAR(50)",
"oracle": "NVARCHAR2(50)",
},
"default": None,
"description": None,
"docstring": None,
Expand Down
22 changes: 15 additions & 7 deletions tortoise/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,25 @@ def constraints(self) -> dict:

def _get_dialects(self) -> dict[str, dict]:
ret = {}
for dialect in [key for key in dir(self) if key.startswith("_db_")]:
item = {}
for dialect in dir(self):
if not dialect.startswith("_db_"):
continue
cls = getattr(self, dialect)
d = cls.__dict__
try:
cls = cls(self)
obj = cls(self)
except TypeError:
pass
for key, val in cls.__dict__.items():
if not key.startswith("_"):
item[key] = val
ret[dialect[4:]] = item
else:
props = {
prop: getattr(obj, prop)
for prop in dir(cls)
if isinstance(getattr(cls, prop), property)
}
d = {**d, **props}

ret[dialect[4:]] = {k: v for k, v in d.items() if not k.startswith("_")}

return ret

def get_db_field_types(self) -> Optional[dict[str, str]]:
Expand Down

0 comments on commit 415d1ae

Please sign in to comment.