-
Notifications
You must be signed in to change notification settings - Fork 484
feat: support pathlib.Path as argument of TortoiseConfig.from_config_file
#2184
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
Open
waketzheng
wants to merge
6
commits into
tortoise:develop
Choose a base branch
from
waketzheng:feat/support-Path-as-config-file
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aac371f
feat: support pathlib.Path as argument of `TortoiseConfig.from_config…
waketzheng aa1d478
Rollback unnecessary changes
waketzheng 60755b6
tests: add tests for TortoiseConfig
waketzheng e4c07ad
Merge remote-tracking branch 'upstream/develop' into feat/support-Pat…
waketzheng ab187e8
tests: use pytest parametrize
waketzheng a6316e1
tests: reduce duplicated
waketzheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| """ | ||
| Tests for tortoise.config module - TortoiseConfig class. | ||
| """ | ||
|
|
||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| import orjson | ||
| import pytest | ||
| import yaml | ||
|
|
||
| from tortoise.backends.base.config_generator import expand_db_url | ||
| from tortoise.config import AppConfig, DBUrlConfig, TortoiseConfig | ||
| from tortoise.exceptions import ConfigurationError | ||
|
|
||
|
|
||
| class TestTortoiseConfig: | ||
| @pytest.fixture | ||
| def db_url(self) -> str: | ||
| return "sqlite://db.sqlite3" | ||
|
|
||
| @pytest.fixture | ||
| def simple_config(self, db_url: str) -> dict: | ||
| return { | ||
| "connections": {"default": db_url}, | ||
| "apps": { | ||
| "app": { | ||
| "models": ["app.models"], | ||
| "default_connection": "default", | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "config,msg", | ||
| [ | ||
| ([], "TortoiseConfig must be created from a mapping"), | ||
| ({}, 'Config must define "connections" section'), | ||
| ({"connections": ""}, 'Config must define "apps" section'), | ||
| ({"connections": "", "apps": ""}, 'Config "connections" must be a mapping'), | ||
| ( | ||
| {"connections": {"default": []}, "apps": ""}, | ||
| "Connection values must be mapping or string", | ||
| ), | ||
| ( | ||
| {"connections": {"default": ""}, "apps": ""}, | ||
| "DBUrlConfig.url must be a non-empty string", | ||
| ), | ||
| ( | ||
| {"connections": {"default": "db.sqlite3"}, "apps": ""}, | ||
| 'Config "apps" must be a mapping', | ||
| ), | ||
| ( | ||
| {"connections": {"default": "db.sqlite3"}, "apps": {"auth": ""}}, | ||
| "App values must be mappings", | ||
| ), | ||
| ( | ||
| {"connections": {"default": "db.sqlite3"}, "apps": {"auth": {}}, "routers": {}}, | ||
| 'AppConfig requires "models"', | ||
| ), | ||
| ( | ||
| { | ||
| "connections": {"default": "db.sqlite3"}, | ||
| "apps": {"auth": {"models": []}}, | ||
| "routers": {}, | ||
| }, | ||
| "AppConfig.models must be a non-empty list of strings", | ||
| ), | ||
| ( | ||
| { | ||
| "connections": {"default": "db.sqlite3"}, | ||
| "apps": {"auth": {"models": ["models"]}}, | ||
| "routers": "", | ||
| }, | ||
| "TortoiseConfig.routers must be a list or None", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_from_invalid_dict(self, config: list | dict, msg: str): | ||
| with pytest.raises(ConfigurationError, match=msg): | ||
| TortoiseConfig.from_dict(config) # type: ignore | ||
|
|
||
| def test_from_dict(self, simple_config: dict): | ||
| assert TortoiseConfig.from_dict(simple_config) == TortoiseConfig( | ||
| connections={"default": DBUrlConfig(url="sqlite://db.sqlite3")}, | ||
| apps={ | ||
| "app": AppConfig( | ||
| models=["app.models"], default_connection="default", migrations=None | ||
| ) | ||
| }, | ||
| routers=None, | ||
| use_tz=None, | ||
| timezone=None, | ||
| ) | ||
| full = { | ||
| "connections": { | ||
| "default": "sqlite://db.sqlite3", | ||
| "second": "sqlite://db2.sqlite3", | ||
| }, | ||
| "apps": { | ||
| "app1": { | ||
| "models": ["app1.models"], | ||
| "migrations": "app1.migrations", | ||
| }, | ||
| "app2": { | ||
| "models": ["app2.models"], | ||
| "default_connection": "second", | ||
| "migrations": "app2.migrations", | ||
| }, | ||
| }, | ||
| "routers": ["path.Router"], | ||
| "use_tz": True, | ||
| "timezone": "UTC", | ||
| } | ||
| assert TortoiseConfig.from_dict(full) == TortoiseConfig( | ||
| connections={ | ||
| "default": DBUrlConfig(url="sqlite://db.sqlite3"), | ||
| "second": DBUrlConfig(url="sqlite://db2.sqlite3"), | ||
| }, | ||
| apps={ | ||
| "app1": AppConfig( | ||
| models=["app1.models"], default_connection=None, migrations="app1.migrations" | ||
| ), | ||
| "app2": AppConfig( | ||
| models=["app2.models"], | ||
| default_connection="second", | ||
| migrations="app2.migrations", | ||
| ), | ||
| }, | ||
| routers=["path.Router"], | ||
| use_tz=True, | ||
| timezone="UTC", | ||
| ) | ||
|
|
||
| def test_from_config_file(self, tmp_path: Path, simple_config: dict): | ||
| file = tmp_path / "tortoise_conf.json" | ||
| file.write_bytes(orjson.dumps(simple_config)) | ||
| filename: str = file.as_posix() | ||
| assert ( | ||
| TortoiseConfig.from_config_file(file) | ||
| == TortoiseConfig.from_config_file(filename) | ||
| == TortoiseConfig.from_dict(simple_config) | ||
| == TortoiseConfig.resolve_args(config_file=file) | ||
| ) | ||
|
|
||
| yaml_file = file.with_suffix(".yml") | ||
| with yaml_file.open("w") as f: | ||
| yaml.safe_dump(simple_config, f, default_flow_style=False) | ||
| yaml_file_2 = file.with_suffix(".yaml") | ||
| shutil.copy(yaml_file, yaml_file_2) | ||
| assert ( | ||
| TortoiseConfig.from_config_file(yaml_file) | ||
| == TortoiseConfig.from_config_file(str(yaml_file)) | ||
| == TortoiseConfig.from_config_file(yaml_file_2) | ||
| == TortoiseConfig.from_config_file(file) | ||
| == TortoiseConfig.resolve_args(config_file=yaml_file) | ||
| ) | ||
|
|
||
| def test_from_db_url_and_modules(self, simple_config: dict, db_url: str): | ||
| modules = {"app": simple_config["apps"]["app"]["models"]} | ||
| typed_config = TortoiseConfig.from_db_url_and_modules(db_url, modules) | ||
| assert typed_config == TortoiseConfig.resolve_args(db_url=db_url, modules=modules) | ||
| assert typed_config.apps == TortoiseConfig.from_dict(simple_config).apps | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "config,msg", | ||
| [ | ||
| ({}, "Must provide either 'config', 'config_file', or both 'db_url' and 'modules'"), | ||
| ( | ||
| dict(db_url=""), | ||
| "Must provide either 'config', 'config_file', or both 'db_url' and 'modules'", | ||
| ), | ||
| ( | ||
| dict(config={}, config_file="a.json"), | ||
| "Cannot specify both 'config' and 'config_file'", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_resolve_args_invalid(self, config: dict, msg: str): | ||
| with pytest.raises(ConfigurationError, match=msg): | ||
| TortoiseConfig.resolve_args(**config) | ||
|
|
||
| def test_resolve_args(self, tmp_path: Path, db_url: str, simple_config: dict): | ||
| config_file = tmp_path / "config.json" | ||
| config_file.write_bytes(orjson.dumps(simple_config)) | ||
| typed_config = TortoiseConfig.resolve_args(simple_config) | ||
| assert typed_config == TortoiseConfig.resolve_args(config_file=config_file) | ||
|
|
||
| typed_config_2 = TortoiseConfig.resolve_args(db_url=db_url, modules={"app": ["app.models"]}) | ||
| assert typed_config.apps == typed_config_2.apps | ||
| assert ( | ||
| expand_db_url(str(typed_config.connections["default"].to_config())) | ||
| == typed_config_2.connections["default"].to_config() | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can move yaml to a separate test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to verify that the JSON file and YAML file produce the same result when dumped from the same dictionary, so put them in a single function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ok, I guess it's ok to leave it as is 👍