-
Notifications
You must be signed in to change notification settings - Fork 485
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
base: develop
Are you sure you want to change the base?
Changes from 3 commits
aac371f
aa1d478
60755b6
e4c07ad
ab187e8
a6316e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| ) | ||
|
Comment on lines
+146
to
+157
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can move yaml to a separate test
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok, I guess it's ok to leave it as is 👍 |
||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a nicer way to write this test: def test_from_db_url_and_modules(self):
db_url = "sqlite://db.sqlite3"
models = ["app.models"]
simple = {
"connections": {"default": db_url},
"apps": {
"app": {
"models": models,
"default_connection": "default",
}
},
}
modules = {"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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use fixture instead. |
||
|
|
||
| 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() | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.