-
Notifications
You must be signed in to change notification settings - Fork 499
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
9
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 3 commits
Commits
Show all changes
9 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 eede294
Merge branch 'develop' of github.com:tortoise/tortoise-orm into feat/…
waketzheng 98fb0de
Merge branch 'develop' of github.com:tortoise/tortoise-orm into feat/…
waketzheng 17fc48b
docs: update changelog
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,167 @@ | ||
| """ | ||
| Tests for tortoise.config module - TortoiseConfig class. | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import orjson | ||
| import pytest | ||
| import yaml | ||
|
|
||
| from tortoise.backends.base.config_generator import expand_db_url | ||
| from tortoise.config import TortoiseConfig | ||
| from tortoise.exceptions import ConfigurationError | ||
|
|
||
|
|
||
| class TestTortoiseConfig: | ||
| def test_from_invalid_dict(self): | ||
| with pytest.raises( | ||
| ConfigurationError, match="TortoiseConfig must be created from a mapping" | ||
| ): | ||
| TortoiseConfig.from_dict([]) | ||
| with pytest.raises(ConfigurationError, match='Config must define "connections" section'): | ||
| TortoiseConfig.from_dict({}) | ||
| with pytest.raises(ConfigurationError, match='Config must define "apps" section'): | ||
| TortoiseConfig.from_dict({"connections": ""}) | ||
| with pytest.raises(ConfigurationError, match='Config "connections" must be a mapping'): | ||
| TortoiseConfig.from_dict({"connections": "", "apps": ""}) | ||
| with pytest.raises(ConfigurationError, match="Connection values must be mapping or string"): | ||
| TortoiseConfig.from_dict({"connections": {"default": []}, "apps": ""}) | ||
| with pytest.raises(ConfigurationError, match="DBUrlConfig.url must be a non-empty string"): | ||
| TortoiseConfig.from_dict({"connections": {"default": ""}, "apps": ""}) | ||
| with pytest.raises(ConfigurationError, match='Config "apps" must be a mapping'): | ||
| TortoiseConfig.from_dict({"connections": {"default": "db.sqlite3"}, "apps": ""}) | ||
| with pytest.raises(ConfigurationError, match="App values must be mappings"): | ||
| TortoiseConfig.from_dict( | ||
| {"connections": {"default": "db.sqlite3"}, "apps": {"auth": ""}} | ||
| ) | ||
| with pytest.raises(ConfigurationError, match='AppConfig requires "models"'): | ||
| TortoiseConfig.from_dict( | ||
| {"connections": {"default": "db.sqlite3"}, "apps": {"auth": {}}, "routers": {}} | ||
| ) | ||
| with pytest.raises( | ||
| ConfigurationError, match="AppConfig.models must be a non-empty list of strings" | ||
| ): | ||
| TortoiseConfig.from_dict( | ||
| { | ||
| "connections": {"default": "db.sqlite3"}, | ||
| "apps": {"auth": {"models": []}}, | ||
| "routers": {}, | ||
| } | ||
| ) | ||
| with pytest.raises( | ||
| ConfigurationError, match="TortoiseConfig.routers must be a list or None" | ||
| ): | ||
| TortoiseConfig.from_dict( | ||
| { | ||
| "connections": {"default": "db.sqlite3"}, | ||
| "apps": {"auth": {"models": ["models"]}}, | ||
| "routers": "", | ||
| } | ||
| ) | ||
|
|
||
| def test_from_dict(self): | ||
| simple = { | ||
| "connections": {"default": "sqlite://db.sqlite3"}, | ||
| "apps": {"app": {"models": ["app.models"]}}, | ||
| } | ||
| assert TortoiseConfig.from_dict(simple) is not None | ||
| full = { | ||
| "connections": { | ||
| "default": "sqlite://db.sqlite3", | ||
| "second": "sqlite://db2.sqlite3", | ||
| }, | ||
| "apps": { | ||
| "app1": {"models": ["app1.models"]}, | ||
| "app2": { | ||
| "models": ["app2.models"], | ||
| "default_connection": "second", | ||
| }, | ||
| }, | ||
| "routers": ["path.Router"], | ||
| "use_tz": True, | ||
| "timezone": "UTC", | ||
| } | ||
| assert TortoiseConfig.from_dict(full) is not None | ||
|
waketzheng marked this conversation as resolved.
Outdated
|
||
|
|
||
| def test_from_config_file(self, tmp_path: Path): | ||
| simple = { | ||
| "connections": {"default": "sqlite://db.sqlite3"}, | ||
| "apps": {"app": {"models": ["app.models"]}}, | ||
| } | ||
| file = tmp_path / "tortoise_conf.json" | ||
| file.write_bytes(orjson.dumps(simple)) | ||
| filename: str = file.as_posix() | ||
| assert ( | ||
| TortoiseConfig.from_config_file(file) | ||
| == TortoiseConfig.from_config_file(filename) | ||
| == TortoiseConfig.from_dict(simple) | ||
| == TortoiseConfig.resolve_args(config_file=file) | ||
| ) | ||
|
|
||
| yaml_file = file.with_suffix(".yml") | ||
| with yaml_file.open("w") as f: | ||
| yaml.safe_dump(dict(simple), f, default_flow_style=False) | ||
| yaml_file_2 = file.with_suffix(".yaml") | ||
| with yaml_file_2.open("w") as f2: | ||
| yaml.safe_dump(simple, f2, default_flow_style=False) | ||
|
waketzheng marked this conversation as resolved.
Outdated
|
||
| 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) | ||
| ) | ||
|
waketzheng marked this conversation as resolved.
|
||
|
|
||
| def test_from_db_url_and_modules(self): | ||
| simple = { | ||
| "connections": {"default": "sqlite://db.sqlite3"}, | ||
| "apps": { | ||
| "app": { | ||
| "models": ["app.models"], | ||
| "default_connection": "default", | ||
| } | ||
| }, | ||
| } | ||
| db_url = simple["connections"]["default"] | ||
| modules = {"app": simple["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).apps | ||
|
waketzheng marked this conversation as resolved.
Outdated
|
||
|
|
||
| def test_resolve_args(self, tmp_path: Path): | ||
| with pytest.raises( | ||
| ConfigurationError, | ||
| match="Must provide either 'config', 'config_file', or both 'db_url' and 'modules'", | ||
| ): | ||
| TortoiseConfig.resolve_args() | ||
| with pytest.raises( | ||
| ConfigurationError, | ||
| match="Must provide either 'config', 'config_file', or both 'db_url' and 'modules'", | ||
| ): | ||
| TortoiseConfig.resolve_args(db_url="") | ||
| with pytest.raises( | ||
| ConfigurationError, match="Cannot specify both 'config' and 'config_file'" | ||
| ): | ||
| TortoiseConfig.resolve_args(config={}, config_file="a.json") | ||
|
waketzheng marked this conversation as resolved.
Outdated
|
||
| db_url = "sqlite://db.sqlite3" | ||
| config = { | ||
| "connections": {"default": db_url}, | ||
| "apps": { | ||
| "app": { | ||
| "models": ["app.models"], | ||
| "default_connection": "default", | ||
| } | ||
| }, | ||
| } | ||
| config_file = tmp_path / "config.json" | ||
| config_file.write_bytes(orjson.dumps(config)) | ||
| typed_config = TortoiseConfig.resolve_args(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.
Uh oh!
There was an error while loading. Please reload this page.